Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <ctype.h>
- #include <string.h>
- #include <assert.h>
- #include <progbase.h>
- #include <progbase/console.h>
- #define INITIAL_CAPACITY 16
- const char colorsTable[16][2] = {
- {'0', BG_BLACK},
- {'1', BG_INTENSITY_BLACK},
- {'2', BG_INTENSITY_RED},
- {'3', BG_RED},
- {'4', BG_GREEN},
- {'5', BG_INTENSITY_GREEN},
- {'6', BG_YELLOW},
- {'7', BG_CYAN},
- {'8', BG_BLUE},
- {'9', BG_GREEN},
- {'A', BG_MAGENTA},
- {'B', BG_WHITE},
- {'C', BG_INTENSITY_BLUE},
- {'D', BG_INTENSITY_MAGENTA},
- {'E', BG_INTENSITY_CYAN},
- {'F', BG_INTENSITY_WHITE}};
- int fileExists(const char *fileName)
- {
- FILE *f = fopen(fileName, "rb");
- if (!f)
- return 0;
- // false : not exists
- fclose(f);
- return 1; // true : exists
- }
- int getFileSize(const char *fileName)
- {
- FILE *f = fopen(fileName, "rb");
- if (!f)
- return 0;
- // false : error opening file
- fseek(f, 0, SEEK_END); // rewind cursor to the end of file
- int fsize = ftell(f); // get file size in bytes
- fclose(f);
- return fsize; // true : exists
- }
- int readFileToBuffer(const char *fileName, char *buffer, int bufferLength)
- {
- FILE *f = fopen(fileName, "rb");
- if (!f)
- return 0;
- // false : read 0 bytes from file
- int readBytes = fread(buffer, 1, bufferLength, f);
- fclose(f);
- return readBytes; // true : exists
- }
- void workWithText(char *MyString)
- {
- int i = 0, j = 0;
- int lengthString = strlen(MyString);
- while (i < lengthString)
- {
- int saveIndexStart = i, saveIndexEnd = i + 1;
- while (MyString[i] != '.' && i < lengthString)
- {
- i++;
- }
- saveIndexEnd = i;
- if (saveIndexEnd - saveIndexStart < 10)
- {
- //Red
- Console_setCursorAttribute(BG_INTENSITY_RED);
- int capacity = INITIAL_CAPACITY;
- char *color = calloc(capacity, sizeof(char));
- int k = 0;
- for (j = saveIndexStart; j < saveIndexEnd; j++)
- {
- if (k >= capacity - 1)
- {
- capacity *= 2;
- color = realloc(color, capacity * sizeof(char));
- }
- color[k] = MyString[j];
- k++;
- }
- color[k] = '\0';
- puts(color);
- free(color);
- Console_setCursorAttribute(BG_DEFAULT);
- }
- else if (saveIndexEnd - saveIndexStart < 25)
- {
- Console_setCursorAttribute(BG_YELLOW);
- int capacity = INITIAL_CAPACITY;
- char *color = calloc(capacity, sizeof(char));
- int k = 0;
- for (j = saveIndexStart; j < saveIndexEnd; j++)
- {
- if (k >= capacity - 1)
- {
- capacity *= 2;
- color = realloc(color, capacity * sizeof(char));
- }
- color[k] = MyString[j];
- k++;
- }
- color[k] = '\0';
- puts(color);
- free(color);
- Console_setCursorAttribute(BG_DEFAULT);
- }
- else
- {
- //Green color
- Console_setCursorAttribute(BG_GREEN);
- int capacity = INITIAL_CAPACITY;
- char *color = calloc(capacity, sizeof(char));
- int k = 0;
- for (j = saveIndexStart; j < saveIndexEnd; j++)
- {
- if (k >= capacity - 1)
- {
- capacity *= 2;
- color = realloc(color, capacity * sizeof(char));
- }
- color[k] = MyString[j];
- k++;
- }
- color[k] = '\0';
- puts(color);
- free(color);
- Console_setCursorAttribute(BG_DEFAULT);
- }
- i++;
- }
- }
- int main()
- {
- char *f = "text.txt";
- // char MyString[1000];
- // int length;
- int length = getFileSize(f);
- char *MyString = malloc(length + 1);
- length = readFileToBuffer(f, MyString, length);
- MyString[length] = '\0';
- assert(fileExists(f) != 0);
- assert(getFileSize(f) != 0);
- assert(readFileToBuffer(f, MyString, length));
- workWithText(MyString);
- free(MyString);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement