danilom

util converter

Sep 8th, 2011
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.37 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include "util_converter.h"
  4. #include "csv_converter.h"
  5. #include "hulke.h"
  6.  
  7.  
  8. void open_file(fp_ptr, filename, header)
  9. FILE** fp_ptr;
  10. const char* filename,
  11.                   * header;
  12. {
  13.         if(IS_NOT_NULL(*fp_ptr = fopen(filename, "w"))) {
  14.                 fprintf(*fp_ptr,"%s\n",header);
  15.         } else {
  16.                 printf("Erro ao abrir o arquivo: \"%s\"\n", filename);
  17.                 exit(1);
  18.         }
  19. }
  20.  
  21. static int split(string, new_string, delimiter)
  22. char* string,
  23.         * new_string;
  24. const char* delimiter;
  25. {
  26.  
  27.         register int i = 0;
  28.         static char* last_string;
  29.  
  30.         if(IS_NULL(string))
  31.                 if(IS_NOT_NULL(last_string))
  32.                         string = last_string;
  33.  
  34.         while(IS_NOT_NUL(*string)) {
  35.                
  36.                 while(*string != *delimiter && IS_NOT_NUL(*string))
  37.                         *new_string++ = *string++;
  38.  
  39.                 while(string[i] == delimiter[i] && IS_NOT_NUL(string[i]) && IS_NOT_NUL(delimiter[i]))
  40.                         i++;
  41.                
  42.                 if(IS_NUL(string[i]) || IS_NUL(delimiter[i])) {
  43.  
  44.                         if(IS_NUL(string[i]))
  45.                                 last_string = NULL;
  46.                         else
  47.                                 last_string = string + i;
  48.  
  49.                         *new_string = NUL;
  50.                         return TRUE;
  51.                        
  52.                 } else
  53.                
  54.                         while(i) {
  55.                                 *new_string++ = *string++;
  56.                                 i--;
  57.                         }
  58.  
  59.         }
  60.        
  61.         return FALSE;
  62.        
  63. }
  64.  
  65. #define QUESTION_SEPARATOR " - "
  66. #define TEACHER_SEPARATOR "/ "
  67. #define DISCIPLINE_SEPARATOR ""
  68.  
  69. int extract_question_teacher_discipline(string_to_extract, string_question, string_teacher, string_discipline)
  70. char* string_to_extract,
  71.         * string_question,
  72.         * string_teacher,
  73.         * string_discipline;
  74. {
  75.  
  76.         if(IS_NULL(string_to_extract)) {
  77.                 return FALSE;
  78.  
  79.         }
  80. //printf("%s -> %d\n", string_to_extract, csvc_record.length);
  81.  
  82.         if( split(string_to_extract, string_question, QUESTION_SEPARATOR) &&
  83.                 split(NULL, string_teacher, TEACHER_SEPARATOR) &&
  84.                 split(NULL, string_discipline, DISCIPLINE_SEPARATOR) )
  85.                         return TRUE;
  86.  
  87.         return FALSE;
  88.        
  89. }
  90.  
  91. int find_in_array(string_to_search, array_to_be_searched, length_of_array)
  92. char* string_to_search,
  93.         (* array_to_be_searched)[300];
  94. int length_of_array;
  95. {
  96.  
  97.         register int i=0;
  98.  
  99.         for(i = 0; i <= length_of_array; i++) {
  100.  
  101.                 if(!strcmp(string_to_search, array_to_be_searched[i])) {
  102.                         return i+1;
  103.                 }
  104.  
  105.         }
  106.  
  107.         return EOF;
  108.        
  109. }
  110.  
  111. void itoa(int_number, string_number)
  112. int int_number;
  113. char* string_number;
  114. {
  115.  
  116.         int string_length = 0,
  117.                 original_int_number = int_number,
  118.                 i = 0;
  119.        
  120.         do {
  121.                 string_length++;
  122.         } while(int_number /= 10);
  123.        
  124.         string_number[string_length] = NUL;
  125.        
  126.         do {
  127.                 string_number[--string_length] = original_int_number % 10 + '0';
  128.         } while(original_int_number /= 10);
  129.        
  130. }
Advertisement
Add Comment
Please, Sign In to add comment