Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include "csv_converter.h"
- #include "hulke.h"
- #define RECORD_SEPARATOR '\n'
- #define IS_RECORD_SEPARATOR(variable) ((variable) == RECORD_SEPARATOR)
- #define IS_NOT_RECORD_SEPARATOR(variable) ((variable) != RECORD_SEPARATOR)
- #define FIELD_SEPARATOR ','
- #define IS_FIELD_SEPARATOR(variable) ((variable) == FIELD_SEPARATOR)
- #define IS_NOT_FIELD_SEPARATOR(variable) ((variable) != FIELD_SEPARATOR)
- #define TEXT_DELIMITER '"'
- #define IS_TEXT_DELIMITER(variable) ((variable) == TEXT_DELIMITER)
- #define IS_NOT_TEXT_DELIMITER(variable) ((variable) != TEXT_DELIMITER)
- #define SINGLE_CHARACTER (-2)
- #define IS_SINGLE_CHARACTER(variable) ((variable) == SINGLE_CHARACTER)
- #define IS_NOT_SINGLE_CHARACTER(variable) ((variable) != SINGLE_CHARACTER)
- #define GETC (csvc_record.last_char=getc(csvc_record.fp))
- CSVC_RECORD csvc_record = {NULL, -1, 0, 0, {}};
- FILE* csvc_open_file(file_name)
- char* file_name;
- {
- return csvc_record.fp = fopen(file_name, "r");
- }
- int csvc_close_file(void)
- {
- int returns = fclose(csvc_record.fp);
- csvc_record.fp = NULL;
- csvc_record.index = 0;
- csvc_record.length = 0;
- csvc_record.last_char = NUL;
- return returns;
- }
- static int next_char(character_ptr) //returns EOF, RECORD_SEPARATOR, FIELD_SEPARATOR or SINGLE_CHARACTER
- char* character_ptr;
- {
- if(IS_EOF(csvc_record.last_char))
- return EOF;
- register int character = 0;
- static int in_text_delimiter = FALSE;
- while(TRUE) {
- switch(character = GETC) {
- case TEXT_DELIMITER :
- if(IS_TRUE(in_text_delimiter))
- if(IS_TEXT_DELIMITER(character=GETC)) {
- *character_ptr = TEXT_DELIMITER;
- return SINGLE_CHARACTER;
- } else if(IS_FIELD_SEPARATOR(character) || IS_RECORD_SEPARATOR(character) || IS_EOF(character)) {
- *character_ptr = NUL;
- in_text_delimiter = FALSE;
- return IS_FIELD_SEPARATOR(character) ? FIELD_SEPARATOR : RECORD_SEPARATOR;
- } else {
- *character_ptr = character;
- in_text_delimiter = FALSE;
- return SINGLE_CHARACTER;
- }
- else
- in_text_delimiter = TRUE;
- break;
- case FIELD_SEPARATOR :
- case RECORD_SEPARATOR :
- if(IS_TRUE(in_text_delimiter)) {
- *character_ptr = character;
- return SINGLE_CHARACTER;
- } else {
- *character_ptr = NUL;
- return character;
- }
- break;
- case EOF :
- *character_ptr = NUL;
- in_text_delimiter = FALSE;
- return RECORD_SEPARATOR;
- break;
- default :
- *character_ptr = character;
- return SINGLE_CHARACTER;
- break;
- }
- }
- }
- static int load_field(void) //returns EOF, RECORD_SEPARATOR or FIELD_SEPARATOR
- {
- if(IS_EOF(csvc_record.last_char))
- return EOF;
- char character;
- register int signal,
- i = 0;
- while(TRUE) {
- switch(signal=next_char(&character)) {
- case SINGLE_CHARACTER :
- csvc_record.fields[csvc_record.length][i++] = character;
- break;
- case RECORD_SEPARATOR :
- case FIELD_SEPARATOR :
- csvc_record.fields[csvc_record.length++][i] = NUL;
- return signal;
- break;
- }
- }
- return EOF;
- }
- int csvc_load_record(void)
- {
- if(IS_EOF(csvc_record.last_char))
- return EOF;
- csvc_record.length = 0;
- while(IS_NOT_RECORD_SEPARATOR(load_field()));
- csvc_record.index++;
- return TRUE;
- }
- int csvc_next_record(void)
- {
- if(IS_EOF(csvc_record.last_char))
- return EOF;
- char character;
- csvc_record.length = 0;
- while(IS_NOT_RECORD_SEPARATOR(next_char(&character)));
- csvc_record.index++;
- return TRUE;
- }
- char* csvc_field_value(field_number)
- unsigned int field_number;
- {
- if(field_number > csvc_record.length)
- return NULL;
- return csvc_record.fields[field_number];
- }
- int csvc_next_field_nonempty(field_number)
- unsigned int field_number;
- {
- do {
- if(field_number > csvc_record.length)
- return EOF;
- } while(IS_NUL(*csvc_record.fields[field_number++]));
- return field_number-1;
- }
Advertisement
Add Comment
Please, Sign In to add comment