danilom

csv_converter

Sep 8th, 2011
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.22 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include "csv_converter.h"
  4. #include "hulke.h"
  5.  
  6.  
  7. #define RECORD_SEPARATOR '\n'
  8. #define IS_RECORD_SEPARATOR(variable) ((variable) == RECORD_SEPARATOR)
  9. #define IS_NOT_RECORD_SEPARATOR(variable) ((variable) != RECORD_SEPARATOR)
  10.  
  11. #define FIELD_SEPARATOR ','
  12. #define IS_FIELD_SEPARATOR(variable) ((variable) == FIELD_SEPARATOR)
  13. #define IS_NOT_FIELD_SEPARATOR(variable) ((variable) != FIELD_SEPARATOR)
  14.  
  15. #define TEXT_DELIMITER '"'
  16. #define IS_TEXT_DELIMITER(variable) ((variable) == TEXT_DELIMITER)
  17. #define IS_NOT_TEXT_DELIMITER(variable) ((variable) != TEXT_DELIMITER)
  18.  
  19. #define SINGLE_CHARACTER (-2)
  20. #define IS_SINGLE_CHARACTER(variable) ((variable) == SINGLE_CHARACTER)
  21. #define IS_NOT_SINGLE_CHARACTER(variable) ((variable) != SINGLE_CHARACTER)
  22.  
  23. #define GETC (csvc_record.last_char=getc(csvc_record.fp))
  24.  
  25.  
  26.  
  27. CSVC_RECORD csvc_record = {NULL, -1, 0, 0, {}};
  28.  
  29.  
  30. FILE* csvc_open_file(file_name)
  31. char* file_name;
  32. {
  33.  
  34.         return csvc_record.fp = fopen(file_name, "r");
  35.        
  36. }
  37.  
  38. int csvc_close_file(void)
  39. {
  40.  
  41.         int returns = fclose(csvc_record.fp);
  42.        
  43.         csvc_record.fp = NULL;
  44.         csvc_record.index = 0;
  45.         csvc_record.length = 0;
  46.         csvc_record.last_char = NUL;
  47.        
  48.         return returns;
  49.        
  50. }
  51.  
  52. static int next_char(character_ptr) //returns EOF, RECORD_SEPARATOR, FIELD_SEPARATOR or SINGLE_CHARACTER
  53. char* character_ptr;
  54. {
  55.  
  56.         if(IS_EOF(csvc_record.last_char))
  57.                 return EOF;
  58.        
  59.        
  60.         register int character = 0;
  61.         static int in_text_delimiter = FALSE;
  62.  
  63.        
  64.         while(TRUE) {
  65.                 switch(character = GETC) {
  66.  
  67.                         case TEXT_DELIMITER :
  68.  
  69.                                 if(IS_TRUE(in_text_delimiter))
  70.                                         if(IS_TEXT_DELIMITER(character=GETC)) {
  71.                                                 *character_ptr = TEXT_DELIMITER;
  72.                                                 return SINGLE_CHARACTER;
  73.                                                
  74.                                         } else if(IS_FIELD_SEPARATOR(character) || IS_RECORD_SEPARATOR(character) || IS_EOF(character)) {
  75.                                                 *character_ptr = NUL;
  76.                                                 in_text_delimiter = FALSE;
  77.                                                 return IS_FIELD_SEPARATOR(character) ? FIELD_SEPARATOR : RECORD_SEPARATOR;
  78.                                                
  79.                                         } else {
  80.                                                 *character_ptr = character;
  81.                                                 in_text_delimiter = FALSE;
  82.                                                 return SINGLE_CHARACTER;
  83.                                                
  84.                                         }
  85.                                 else
  86.                                         in_text_delimiter = TRUE;
  87.                                 break;
  88.                                
  89.                         case FIELD_SEPARATOR :
  90.                         case RECORD_SEPARATOR :
  91.  
  92.                                 if(IS_TRUE(in_text_delimiter)) {
  93.                                         *character_ptr = character;
  94.                                         return SINGLE_CHARACTER;
  95.                                        
  96.                                 } else {
  97.                                         *character_ptr = NUL;
  98.                                         return character;
  99.                                        
  100.                                 }
  101.                                 break;
  102.                                
  103.                         case EOF :
  104.  
  105.                                 *character_ptr = NUL;
  106.                                 in_text_delimiter = FALSE;
  107.                                 return RECORD_SEPARATOR;
  108.                                 break;
  109.                                
  110.                         default :
  111.  
  112.                                 *character_ptr = character;
  113.                                 return SINGLE_CHARACTER;
  114.                                 break;
  115.                                
  116.                 }
  117.         }
  118.  
  119. }
  120.  
  121. static int load_field(void) //returns EOF, RECORD_SEPARATOR or FIELD_SEPARATOR
  122. {
  123.  
  124.         if(IS_EOF(csvc_record.last_char))
  125.                 return EOF;
  126.  
  127.  
  128.         char character;
  129.         register int signal,
  130.                                  i = 0;
  131.  
  132.        
  133.         while(TRUE) {
  134.  
  135.                 switch(signal=next_char(&character)) {
  136.  
  137.                         case SINGLE_CHARACTER :
  138.                                 csvc_record.fields[csvc_record.length][i++] = character;
  139.                                 break;
  140.  
  141.                         case RECORD_SEPARATOR :
  142.                         case FIELD_SEPARATOR :
  143.                                 csvc_record.fields[csvc_record.length++][i] = NUL;
  144.                                 return signal;
  145.                                 break;
  146.  
  147.                         }
  148.         }
  149.  
  150.         return EOF;
  151.        
  152. }
  153.  
  154. int csvc_load_record(void)
  155. {
  156.  
  157.         if(IS_EOF(csvc_record.last_char))
  158.                 return EOF;
  159.  
  160.  
  161.         csvc_record.length = 0;
  162.         while(IS_NOT_RECORD_SEPARATOR(load_field()));
  163.         csvc_record.index++;
  164.  
  165.         return TRUE;
  166.  
  167. }
  168.  
  169. int csvc_next_record(void)
  170. {
  171.  
  172.         if(IS_EOF(csvc_record.last_char))
  173.                 return EOF;
  174.  
  175.         char character;
  176.  
  177.  
  178.         csvc_record.length = 0;
  179.         while(IS_NOT_RECORD_SEPARATOR(next_char(&character)));
  180.         csvc_record.index++;
  181.                
  182.         return TRUE;
  183.        
  184. }
  185.  
  186. char* csvc_field_value(field_number)
  187. unsigned int field_number;
  188. {
  189.        
  190.         if(field_number > csvc_record.length)
  191.                 return NULL;
  192.        
  193.         return csvc_record.fields[field_number];
  194.        
  195. }
  196.  
  197. int csvc_next_field_nonempty(field_number)
  198. unsigned int field_number;
  199. {
  200.        
  201.         do {
  202.                
  203.                 if(field_number > csvc_record.length)
  204.                         return EOF;
  205.                        
  206.         } while(IS_NUL(*csvc_record.fields[field_number++]));
  207.        
  208.         return field_number-1;
  209.        
  210. }
Advertisement
Add Comment
Please, Sign In to add comment