Guest User

Untitled

a guest
Dec 11th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.86 KB | None | 0 0
  1. /*******************************************************************
  2. * *
  3. * Working with utf8 only file encoding. *
  4. * This program reads file, and prints same file without words, *
  5. * that not consist of numbers and numbers ending with "-й" *
  6. * *
  7. *******************************************************************/
  8.  
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11.  
  12. #define BUF_SIZE 128
  13.  
  14. void process_output_buf(unsigned char* output_buf, int buf_size);
  15. void clear_output_buf(unsigned char* output_buf, int buf_size);
  16. unsigned char* realloc_output_buf(unsigned char* output_buf, int prev_size, int new_size);
  17.  
  18. int main(int argc, char *argv[]) {
  19. FILE *fp = NULL;
  20. unsigned char* output_buf = NULL;
  21. unsigned char file_buf[BUF_SIZE] = { 0 };
  22. int ob_i = 0;
  23. int ob_size = BUF_SIZE;
  24.  
  25. output_buf = calloc(BUF_SIZE, sizeof(unsigned char));
  26. if(output_buf == NULL) {
  27. printf("Couldn't allocate memory.\n");
  28. exit(4);
  29. }
  30.  
  31. if(argc != 2) {
  32. printf("Incorrect number of arguments.\n");
  33. exit(1);
  34. }
  35.  
  36. if((fp = fopen(argv[1], "rb") ) == NULL) {
  37. printf("Cannot open file.\n");
  38. exit(2);
  39. }
  40.  
  41. while(1) {
  42. if(fgets(file_buf, BUF_SIZE, fp)) {
  43. for(int i = 0; i < BUF_SIZE, file_buf[i] != '\0'; i++) {
  44. if(file_buf[i] == 32 || file_buf[i] == 10) {
  45. process_output_buf(output_buf, ob_size);
  46. clear_output_buf(output_buf, ob_size);
  47. ob_i = 0;
  48.  
  49. printf("%c", file_buf[i]);
  50. } else {
  51. output_buf[ob_i] = file_buf[i];
  52. ob_i++;
  53. if(ob_i >= ob_size) {
  54. output_buf = realloc_output_buf(output_buf, ob_size, ob_size + BUF_SIZE);
  55. ob_size += BUF_SIZE;
  56. }
  57. }
  58. }
  59. } else {
  60. if(ferror(fp)) {
  61. printf("Error while reading file.\n");
  62. exit(3);
  63. }
  64.  
  65. break;
  66. }
  67. }
  68.  
  69. process_output_buf(output_buf, ob_size);
  70.  
  71. fclose(fp);
  72. free(output_buf);
  73.  
  74. return 0;
  75. }
  76.  
  77. void process_output_buf(unsigned char* output_buf, int buf_size) {
  78. if(buf_size < 1) return;
  79.  
  80. if(48 <= output_buf[0] && output_buf[0] < 58) {
  81. for(int i = 1; i < buf_size, output_buf[i] != 0; i++) {
  82. if(48 <= output_buf[i] && output_buf[i] < 58) {
  83. continue;
  84. } else if(output_buf[i] == 45) { // -й
  85. if((i + 2) < buf_size) {
  86. if((output_buf[i + 1] == 208) && (output_buf[i + 2] == 185)) {
  87. printf("%s", output_buf);
  88. }
  89. }
  90. }
  91.  
  92. return;
  93. }
  94.  
  95. printf("%s", output_buf);
  96. }
  97. }
  98.  
  99. void clear_output_buf(unsigned char* output_buf, int buf_size) {
  100. for(int i = 0; i < buf_size; i++) {
  101. output_buf[i] = 0;
  102. }
  103. }
  104.  
  105. unsigned char* realloc_output_buf(unsigned char* output_buf, int prev_size, int new_size) {
  106. output_buf = realloc(output_buf, new_size * sizeof(unsigned char));
  107. if(output_buf == NULL) {
  108. printf("Couldn't allocate memory.\n");
  109. exit(4);
  110. }
  111.  
  112. for(int i = prev_size; i < new_size; i++) {
  113. output_buf[i] = 0;
  114. }
  115.  
  116. return output_buf;
  117. }
Add Comment
Please, Sign In to add comment