TEXT.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. // C source file with the function definitions to handle TEXT files
  2. // check_text_enc() function
  3. // function to check the encoding of a text file
  4. text_enc check_text_enc(char * text_file_path)
  5. {
  6. // load the whole file in memory
  7. byte * ptr = load_file_mem(text_file_path);
  8. if (ptr == NULL)
  9. return TEXT_ENC_UNKNOWN;
  10. // variable to return
  11. text_enc rtn = TEXT_ENC_UNKNOWN;
  12. // otherwise check the encoding of the file
  13. umax file_size = get_file_size(text_file_path);
  14. // the first encoding check that does not give
  15. // any errors is the one going to be selected
  16. // store the encoding before this one
  17. char_enc old_enc = CHAR_ARR_ENC_STATE;
  18. for (umax i = CHAR_ENC_ASCII; i < CHAR_ENC_END_INT; i++)
  19. {
  20. set_ch_arr_enc_state((char_enc) i);
  21. // iterate over the file data in ptr
  22. bool valid_enc = true;
  23. for (umax j = 0; j < file_size; /**/)
  24. {
  25. // check character size
  26. byte ch_size = chk_enc_ch(ptr + j, file_size - j);
  27. //~ printf("%llu %llu %llu\n", i, file_size, j);
  28. if (ch_size == 0) {
  29. valid_enc = false;
  30. break;
  31. }
  32. // advance to the next character
  33. j += ch_size;
  34. }
  35. // check if all encoded characters where
  36. // valid to the encoding being used
  37. if (valid_enc) {
  38. rtn = (text_enc) i;
  39. break;
  40. }
  41. }
  42. // reset char array functions to old_enc
  43. set_ch_arr_enc_state(old_enc);
  44. // free memory used
  45. free_memory(ptr);
  46. return rtn;
  47. }
  48. // check_text_inc() function
  49. // function to check if a text file is valid
  50. text_err_cd check_text_inc(char * text_file_path)
  51. {
  52. // check params
  53. if (text_file_path == NULL)
  54. return TEXT_ERR_NULL_PATH;
  55. // check if the encoding is supported
  56. if (check_text_enc(text_file_path) != TEXT_ENC_UNKNOWN)
  57. return TEXT_ERR_NO_ERR;
  58. return TEXT_ERR_UNKNOWN_ENC;
  59. }
  60. // open_text_inc() function
  61. // function to open an incomplete text structure
  62. text * open_text_inc(char * text_file_path)
  63. {
  64. // check params
  65. text_err_cd err_cd_inc = check_text_inc(text_file_path);
  66. if (err_cd_inc > TEXT_ERR_LN)
  67. goto err;
  68. // pointer to return
  69. text * fptr = allocate_memory(sizeof(text), sizeof(byte));
  70. if (fptr == NULL)
  71. goto err;
  72. // fill the structure basics
  73. fptr -> PATH = text_file_path;
  74. fptr -> DATA = load_file_mem(text_file_path);
  75. fptr -> SIZE = get_file_size(text_file_path);
  76. fptr -> ENC = check_text_enc(text_file_path);
  77. fptr -> ERR_CD_INC = err_cd_inc;
  78. // TYPE, ST and ERR_CD_TP will have to be assigned
  79. // by a specific text file type function
  80. // all good
  81. return fptr;
  82. // failure
  83. err:
  84. return NULL;
  85. }
  86. // close_text_inc() function
  87. // function to close an incomplete text structure (free memory)
  88. void close_text_inc(text * text_file)
  89. {
  90. // verify text_file is not a NULL pointer
  91. if (text_file == NULL)
  92. return;
  93. // otherwise, free memory
  94. free_memory(text_file -> DATA);
  95. free_memory(text_file);
  96. return;
  97. }
  98. // print_text_start() function
  99. // function to print the starting info of an incomplete text struct
  100. void print_text_start(text * file)
  101. {
  102. // check ptr
  103. if (file == NULL)
  104. return;
  105. // file info and error code
  106. printf("File: %s\n", file -> PATH);
  107. printf("File size (bytes): %llu\n", file -> SIZE);
  108. printf("FILE ERROR CODE (INC): %u\n", file -> ERR_CD_INC);
  109. printf("FILE ENCODING: %u\n\n", file -> ENC);
  110. // print file contents (can be awful for big text files)
  111. printf("########## Text file contents start ##########\n\n");
  112. set_ch_arr_enc_state(file -> ENC);
  113. print_chenc_arr(file -> DATA, file -> SIZE, file -> ENC);
  114. printf("\n########## Text file contents end ##########\n");
  115. // done, reset char encoding funcs
  116. set_ch_arr_enc_state(CHAR_ENC_ASCII);
  117. return;
  118. }
  119. // print_text_inc() function
  120. // function to print the information of an incomplete text structure
  121. void print_text_inc(text * file)
  122. {
  123. // check ptr
  124. if (file == NULL)
  125. return;
  126. // starting visual queue
  127. printf("\n### Text incomplete struct info start ###\n\n");
  128. // print the start of the text struct
  129. print_text_start(file);
  130. // end of structure
  131. printf("\n### Text incomplete struct info end ###\n");
  132. return;
  133. }
  134. // insert_text_chars() function
  135. // function to insert chints from a chint array into txt_f object at txt_pos
  136. bool insert_text_chars(text * txt_f, void * txt_pos, umax * chints, umax csize)
  137. {
  138. // check params
  139. if (txt_f == NULL || txt_pos == NULL || chints == NULL || csize == 0)
  140. goto err;
  141. // insert the bytes
  142. void * ptr = insert_chints_in_chenc_arr(txt_f -> DATA, txt_f -> SIZE, txt_f -> ENC, txt_pos, chints, csize);
  143. if (ptr == NULL)
  144. goto err;
  145. // if everything went nice update txt_f
  146. txt_f -> DATA = ptr;
  147. txt_f -> SIZE = txt_f -> SIZE + csize;
  148. return true;
  149. err: // error
  150. return false;
  151. }
  152. // remove_text_chars() function
  153. // function to remove characters from a txt_f object at txt_pos
  154. bool remove_text_chars(text * txt_f, void * txt_pos, umax rm_size)
  155. {
  156. // check params
  157. if (txt_f == NULL || txt_pos == NULL || rm_size == 0)
  158. goto err;
  159. // remove the characters
  160. umax result = remove_chencs_in_chenc_arr(txt_f -> DATA, txt_f -> SIZE, txt_f -> ENC, txt_pos, rm_size);
  161. if (result == 0)
  162. goto err;
  163. // update txt_f
  164. txt_f -> SIZE = txt_f -> SIZE - rm_size;
  165. // all went good
  166. return true;
  167. err: // error
  168. return false;
  169. }