danilom

conversor

Sep 8th, 2011
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 10.74 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <dirent.h>
  5. #include "csv_converter.h"
  6. #include "util_converter.h"
  7. #include "hulke.h"
  8.  
  9.  
  10. #define NUMBER_OF_CHARACTERS_IN_QUESTION 500
  11. #define NUMBER_OF_CHARACTERS_IN_TEACHER 500
  12. #define NUMBER_OF_CHARACTERS_IN_DISCIPLINE 500
  13.  
  14. #define NUMBER_OF_CHARACTERS_IN_PATH 500
  15.  
  16. #define NUMBER_OF_QUESTIONS 100000
  17. #define NUMBER_OF_TEACHERS 100000
  18. #define NUMBER_OF_DISCIPLINES 100000
  19. #define NUMBER_OF_RESPONSES 200000
  20.  
  21. #define FILENAME_QUESTION "questao.csv"
  22. #define FILENAME_TEACHER "professor.csv"
  23. #define FILENAME_DISCIPLINE "disciplina.csv"
  24. #define FILENAME_RESPONSE "resposta.csv"
  25.  
  26. #define STRING_HEADER_QUESTION "id,descricao"
  27. #define STRING_HEADER_TEACHER "id,nome"
  28. #define STRING_HEADER_DISCIPLINE "id,descricao,id_professor"
  29. #define STRING_HEADER_RESPONSE "id_questionario,id_disciplina,id_questao,ip,inicio,fim,arquivo,resposta"
  30.  
  31. #define COLUMN_START_QUESTIONNAIRE 11
  32. #define COLUMN_ID_QUESTIONNAIRE 0
  33. #define COLUMN_START_DATE 1
  34. #define COLUMN_END_DATE 2
  35. #define COLUMN_IP 3
  36.  
  37. #define INT_SIZE_IN_CHAR 10
  38.  
  39.  
  40.  
  41. struct {
  42.         int length;
  43.         char* description[NUMBER_OF_QUESTIONS];
  44. } cache_file_question = {0, {}};
  45.  
  46. struct {
  47.         int length;
  48.         char* name[NUMBER_OF_TEACHERS];
  49. } cache_file_teacher = {0, {}};
  50.  
  51. struct {
  52.         int length;
  53.         char* description[NUMBER_OF_DISCIPLINES],
  54.                 * id_teacher[NUMBER_OF_DISCIPLINES];
  55. } cache_file_discipline = {0, {}};
  56.  
  57. struct {
  58.         int length,
  59.                 id_question[NUMBER_OF_RESPONSES],
  60.                 id_discipline[NUMBER_OF_RESPONSES];
  61. } cache_file_response = {0, {}, {}};
  62.  
  63. int blob;
  64.  
  65. int main(argc, argv)
  66. int argc;
  67. char** argv;
  68. {
  69.  
  70.         DIR* directory;
  71.         struct dirent* file;
  72.  
  73.         FILE* fp_question,
  74.                 * fp_teacher,
  75.                 * fp_discipline,
  76.                 * fp_response;
  77.  
  78.         int responses_column,
  79.                 number_of_file = 0;
  80.  
  81.         char string_question[NUMBER_OF_CHARACTERS_IN_QUESTION] = {},
  82.                  string_teacher[NUMBER_OF_CHARACTERS_IN_TEACHER] = {},
  83.                  string_discipline[NUMBER_OF_CHARACTERS_IN_DISCIPLINE] = {},
  84.                  path_to_file[NUMBER_OF_CHARACTERS_IN_PATH] = {};
  85.  
  86.                  
  87.         if(argc != 2) {
  88.                 printf("�� necess��rio especificar um diret��rio. Formato:\n[./]%s um/diretorio/aqui\n", *argv);
  89.                 exit(1);
  90.         } else
  91.                 directory = opendir(argv[1]);
  92.                
  93.  
  94.         open_file(&fp_question, FILENAME_QUESTION, STRING_HEADER_QUESTION);
  95.         open_file(&fp_teacher, FILENAME_TEACHER, STRING_HEADER_TEACHER);
  96.         open_file(&fp_discipline, FILENAME_DISCIPLINE, STRING_HEADER_DISCIPLINE);
  97.         open_file(&fp_response, FILENAME_RESPONSE, STRING_HEADER_RESPONSE);
  98.                  
  99.         putchar('\n');  
  100.         while(IS_NOT_NULL(file = readdir(directory))) {
  101.                
  102.                 if(file->d_type != DT_REG)
  103.                         continue;
  104.  
  105.                 printf("----------- Arquivo %d - %s -----------\n\n", ++number_of_file, file->d_name);
  106.  
  107.  
  108.                 path_to_file[i] = NUL;
  109.                 strcat(path_to_file, argv[1]);
  110.                 strcat(path_to_file, file->d_name);
  111.                
  112.                 if(IS_NULL(csvc_open_file(path_to_file))) {
  113.                         printf("Erro ao abrir o arquivo: \"%s\"\n", file->d_name);
  114.                         exit(1);
  115.                 }
  116.                
  117.                 if(csvc_next_record() && csvc_load_record()) {
  118.                        
  119.                         responses_column = COLUMN_START_QUESTIONNAIRE;
  120.  
  121.                         while(extract_question_teacher_discipline(csvc_field_value(responses_column++), string_question, string_teacher, string_discipline)) {
  122.                                
  123.                                 int id_question;
  124.  
  125.                                 if(IS_EOF(id_question=find_in_array(string_question, cache_file_question.description, cache_file_question.length))) {
  126.  
  127.                                         cache_file_question.description[cache_file_question.length] = malloc(strlen(string_question) + 1);
  128.                                         strcpy(cache_file_question.description[cache_file_question.length], string_question);
  129.  
  130.                                         cache_file_question.length++;
  131.                                        
  132.                                         fprintf(fp_question, "%d,\"%s\"\n", id_question=cache_file_question.length, string_question);
  133.  
  134.                                 } else
  135.                                         id_question++;
  136.  
  137.                                 cache_file_response.id_question[cache_file_response.length] = id_question;
  138.                                
  139.  
  140.                                 int id_teacher;
  141.  
  142.                                 if(IS_EOF(id_teacher=find_in_array(string_teacher, cache_file_teacher.name, cache_file_teacher.length))) {
  143.                                        
  144.                                         cache_file_teacher.name[cache_file_teacher.length] = malloc(strlen(string_teacher) + 1);
  145.                                         strcpy(cache_file_teacher.name[cache_file_teacher.length], string_teacher);
  146.  
  147.                                         cache_file_teacher.length++;
  148.                                        
  149.                                         fprintf(fp_teacher, "%d,\"%s\"\n", id_teacher=cache_file_teacher.length, string_teacher);
  150.  
  151.                                 } else
  152.                                         id_teacher++;
  153.  
  154.                                
  155.                                 char string_id_teacher[INT_SIZE_IN_CHAR];
  156.                                 itoa(id_teacher, string_id_teacher);
  157.  
  158.                                 int id_discipline,
  159.                                         discipline_id_teacher;
  160.  
  161.                                 if( IS_EOF(id_discipline=find_two_in_arrays(string_discipline, string_id_teacher,
  162.                                         cache_file_discipline.description, cache_file_discipline.id_teacher,
  163.                                         cache_file_discipline.length))
  164.                                 ) {
  165.  
  166.                                         cache_file_discipline.description[cache_file_discipline.length] = malloc(strlen(string_discipline) + 1);
  167.                                         strcpy(cache_file_discipline.description[cache_file_discipline.length], string_discipline);
  168.  
  169.                                         cache_file_discipline.id_teacher[cache_file_discipline.length] = malloc(strlen(string_id_teacher) + 1);
  170.                                         strcpy(cache_file_discipline.id_teacher[cache_file_discipline.length], string_id_teacher);
  171.  
  172.                                         cache_file_discipline.length++;
  173.                                        
  174.                                         fprintf(fp_discipline, "%d,\"%s\",%s\n", id_discipline=cache_file_discipline.length, string_discipline, string_id_teacher);
  175.                                        
  176.                                 } else
  177.                                         id_discipline++;
  178.  
  179.                                 cache_file_response.id_discipline[cache_file_response.length] = id_discipline;
  180.                                 cache_file_response.length++;
  181.  
  182.                         }
  183.  
  184.                         printf("1) Quest��es, professores e disciplinas extra��dos e adicionados respectivamente nos arquivos:\n\t%s, %s e %s\n", FILENAME_QUESTION, FILENAME_TEACHER, FILENAME_DISCIPLINE);
  185.                 }
  186.                
  187.  
  188.                 char* column_id_questionnaire,
  189.                         * column_ip,
  190.                         * column_start_date,
  191.                         * column_end_date;
  192.                        
  193.                 int cache_position;
  194.  
  195.                        
  196.                 while(IS_NOT_EOF(csvc_load_record())) {
  197.  
  198.                         responses_column = COLUMN_START_QUESTIONNAIRE;
  199.                         column_id_questionnaire = csvc_field_value(COLUMN_ID_QUESTIONNAIRE);
  200.                         column_ip = csvc_field_value(COLUMN_IP);
  201.                         column_start_date = csvc_field_value(COLUMN_START_DATE);
  202.                         column_end_date = csvc_field_value(COLUMN_END_DATE);
  203.  
  204.                         while(responses_column < csvc_record.length) {
  205.  
  206.                                 cache_position = responses_column - COLUMN_START_QUESTIONNAIRE;
  207.  
  208.                                 fprintf(fp_response, "\"%s\",%d,%d,\"%s\",\"%s\",\"%s\",\"%s\",\"%s\"\n", column_id_questionnaire,
  209.                                                                                                                                                                                   cache_file_response.id_discipline[cache_position],
  210.                                                                                                                                                                                   cache_file_response.id_question[cache_position],
  211.                                                                                                                                                                                   column_ip,
  212.                                                                                                                                                                                   column_start_date,
  213.                                                                                                                                                                                   column_end_date,
  214.                                                                                                                                                                                   file->d_name,
  215.                                                                                                                                                                                   csvc_field_value(responses_column)
  216.                                 );
  217.                                 responses_column++;
  218.  
  219.                         }
  220.                 }
  221.  
  222.                 cache_file_response.length = 0;
  223.                 printf("2)Respostas foram extra��das e adicionadas no arquivo: %s\n", FILENAME_RESPONSE);
  224.  
  225.                 if(IS_NOT_EOF(csvc_close_file()))
  226.                         printf("\n---Conclu��do----------------------\n\n\n");
  227.                
  228.         }
  229.  
  230.         fclose(fp_question);
  231.         fclose(fp_teacher);
  232.         fclose(fp_discipline);
  233.         fclose(fp_response);
  234.  
  235.         printf("\n--------------------------------\nConvers��o conclu��da com sucesso!\n\n");
  236.        
  237.  
  238.         return 0;
  239.        
  240. }
Advertisement
Add Comment
Please, Sign In to add comment