Advertisement
Guest User

Untitled

a guest
Nov 24th, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.35 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <ctype.h>
  4. #include <string.h>
  5. #include <assert.h>
  6. #include <progbase.h>
  7. #include <progbase/console.h>
  8. #define INITIAL_CAPACITY 16
  9. const char colorsTable[16][2] = {
  10.     {'0', BG_BLACK},
  11.     {'1', BG_INTENSITY_BLACK},
  12.     {'2', BG_INTENSITY_RED},
  13.     {'3', BG_RED},
  14.     {'4', BG_GREEN},
  15.     {'5', BG_INTENSITY_GREEN},
  16.     {'6', BG_YELLOW},
  17.     {'7', BG_CYAN},
  18.     {'8', BG_BLUE},
  19.     {'9', BG_GREEN},
  20.     {'A', BG_MAGENTA},
  21.     {'B', BG_WHITE},
  22.     {'C', BG_INTENSITY_BLUE},
  23.     {'D', BG_INTENSITY_MAGENTA},
  24.     {'E', BG_INTENSITY_CYAN},
  25.     {'F', BG_INTENSITY_WHITE}};
  26.  
  27. int fileExists(const char *fileName)
  28. {
  29.     FILE *f = fopen(fileName, "rb");
  30.     if (!f)
  31.         return 0;
  32.     // false : not exists
  33.     fclose(f);
  34.     return 1; // true : exists
  35. }
  36.  
  37. int getFileSize(const char *fileName)
  38. {
  39.     FILE *f = fopen(fileName, "rb");
  40.     if (!f)
  41.         return 0;
  42.     // false : error opening file
  43.     fseek(f, 0, SEEK_END); // rewind  cursor to the end of file
  44.     int fsize = ftell(f);  // get file size in bytes
  45.  
  46.     fclose(f);
  47.     return fsize; // true : exists
  48. }
  49.  
  50. int readFileToBuffer(const char *fileName, char *buffer, int bufferLength)
  51. {
  52.     FILE *f = fopen(fileName, "rb");
  53.     if (!f)
  54.         return 0;
  55.     // false : read 0 bytes from file
  56.     int readBytes = fread(buffer, 1, bufferLength, f);
  57.     fclose(f);
  58.     return readBytes; // true : exists
  59. }
  60.  
  61. void workWithText(char *MyString)
  62. {
  63.     int i = 0, j = 0;
  64.     int lengthString = strlen(MyString);
  65.  
  66.     while (i < lengthString)
  67.     {
  68.         int saveIndexStart = i, saveIndexEnd = i + 1;
  69.         while (MyString[i] != '.' && i < lengthString)
  70.         {
  71.             i++;
  72.         }
  73.         saveIndexEnd = i;
  74.         if (saveIndexEnd - saveIndexStart < 10)
  75.         {
  76.             //Red
  77.  
  78.              Console_setCursorAttribute(BG_INTENSITY_RED);
  79.             int capacity = INITIAL_CAPACITY;
  80.             char *color = calloc(capacity, sizeof(char));
  81.             int k = 0;
  82.             for (j = saveIndexStart; j < saveIndexEnd; j++)
  83.             {
  84.                 if (k >= capacity - 1)
  85.                 {
  86.                     capacity *= 2;
  87.                     color = realloc(color, capacity * sizeof(char));
  88.                 }
  89.                 color[k] = MyString[j];
  90.                 k++;
  91.             }
  92.             color[k] = '\0';
  93.             puts(color);
  94.             free(color);
  95.             Console_setCursorAttribute(BG_DEFAULT);
  96.         }
  97.         else if (saveIndexEnd - saveIndexStart < 25)
  98.         {
  99.              Console_setCursorAttribute(BG_YELLOW);
  100.             int capacity = INITIAL_CAPACITY;
  101.             char *color = calloc(capacity, sizeof(char));
  102.             int k = 0;
  103.             for (j = saveIndexStart; j < saveIndexEnd; j++)
  104.             {
  105.                 if (k >= capacity - 1)
  106.                 {
  107.                     capacity *= 2;
  108.                     color = realloc(color, capacity * sizeof(char));
  109.                 }
  110.                 color[k] = MyString[j];
  111.                 k++;
  112.             }
  113.             color[k] = '\0';
  114.             puts(color);
  115.             free(color);
  116.             Console_setCursorAttribute(BG_DEFAULT);
  117.         }
  118.         else
  119.         {
  120.             //Green color
  121.              Console_setCursorAttribute(BG_GREEN);
  122.             int capacity = INITIAL_CAPACITY;
  123.             char *color = calloc(capacity, sizeof(char));
  124.             int k = 0;
  125.             for (j = saveIndexStart; j < saveIndexEnd; j++)
  126.             {
  127.                 if (k >= capacity - 1)
  128.                 {
  129.                     capacity *= 2;
  130.                     color = realloc(color, capacity * sizeof(char));
  131.                 }
  132.                 color[k] = MyString[j];
  133.                 k++;
  134.             }
  135.             color[k] = '\0';
  136.             puts(color);
  137.             free(color);
  138.             Console_setCursorAttribute(BG_DEFAULT);
  139.         }
  140.         i++;
  141.     }
  142. }
  143.  
  144. int main()
  145. {
  146.     char *f = "text.txt";
  147.     // char MyString[1000];
  148.     // int length;
  149.  
  150.     int length = getFileSize(f);
  151.     char *MyString = malloc(length + 1);
  152.     length = readFileToBuffer(f, MyString, length);
  153.     MyString[length] = '\0';
  154.  
  155.     assert(fileExists(f) != 0);
  156.     assert(getFileSize(f) != 0);
  157.     assert(readFileToBuffer(f, MyString, length));
  158.     workWithText(MyString);
  159.  
  160.     free(MyString);
  161.     return 0;
  162. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement