Added an option to read input from a file.
This commit is contained in:
@@ -0,0 +1,56 @@
|
|||||||
|
#include "futil.h"
|
||||||
|
|
||||||
|
int ReadAllString(const char* path, char** content_ptr, size_t* bytes_read) {
|
||||||
|
if (!path || !content_ptr || !bytes_read) return 0;
|
||||||
|
|
||||||
|
FILE *file = fopen(path, "rb"); //Always open the file in binary mode.
|
||||||
|
|
||||||
|
if (!file) {
|
||||||
|
fprintf(stderr, "Failed to open '%s'. %s.\n", path, strerror(errno));
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
char *content = NULL, *temp;
|
||||||
|
size_t used = 0, capacity = 0, read = 0; //Allows initialize stack allocated values to zero!
|
||||||
|
|
||||||
|
while(1) {
|
||||||
|
if (used + FUTIL_READ_SIZE + 1 > capacity) {
|
||||||
|
capacity = used + FUTIL_READ_SIZE + 1;
|
||||||
|
|
||||||
|
temp = realloc(content, capacity);
|
||||||
|
|
||||||
|
if (!temp) {
|
||||||
|
fprintf(stderr, "Failed to realloc space for file contents. %s.\n", strerror(errno));
|
||||||
|
free(content);
|
||||||
|
fclose(file);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
content = temp;
|
||||||
|
}
|
||||||
|
|
||||||
|
read = fread(content + used, 1, FUTIL_READ_SIZE, file);
|
||||||
|
|
||||||
|
if (read == 0) break;
|
||||||
|
|
||||||
|
used += read;
|
||||||
|
}
|
||||||
|
|
||||||
|
fclose(file);
|
||||||
|
//Shrink the buffer to fit just the contents of the buffer.
|
||||||
|
temp = realloc(content, used + 1);
|
||||||
|
|
||||||
|
if (!temp) {
|
||||||
|
fprintf(stderr, "Failed to realloc to only hold the file contents. %s.\n", strerror(errno));
|
||||||
|
free(content);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
content = temp;
|
||||||
|
content[used] = '\0';
|
||||||
|
|
||||||
|
*content_ptr = content;
|
||||||
|
*bytes_read = used;
|
||||||
|
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
@@ -0,0 +1,15 @@
|
|||||||
|
#ifndef FUTIL_H
|
||||||
|
#define FUTIL_H
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <errno.h>
|
||||||
|
|
||||||
|
#ifndef FUTIL_READ_SIZE
|
||||||
|
#define FUTIL_READ_SIZE 2097152 //Default here is 2MiB
|
||||||
|
#endif
|
||||||
|
|
||||||
|
int ReadAllString(const char*, char**, size_t*);
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -2,43 +2,13 @@
|
|||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include "tstring.h"
|
#include "tstring.h"
|
||||||
|
#include "futil.h"
|
||||||
// const unsigned char First = 0x70;
|
|
||||||
// const unsigned char Teo = 0xE0;
|
|
||||||
// const unsigned char Three = 0xF0;
|
|
||||||
// const unsigned char Four = 0xF7;
|
|
||||||
|
|
||||||
int main(int argc, char** argv) {
|
int main(int argc, char** argv) {
|
||||||
char input[1024] = { 0 };
|
char input[1024] = { 0 };
|
||||||
char glyph[8] = { 0 };
|
char glyph[8] = { 0 };
|
||||||
// char characters[8] = { 42 };
|
char* file;
|
||||||
|
size_t bytesRead;
|
||||||
// int index = 2;
|
|
||||||
// TString* string = TStringCreate();
|
|
||||||
// //0xF0 0xA0 0xBA 0x8C
|
|
||||||
// char* test = u8"\xE5\x87\x84";
|
|
||||||
// char* test2 = u8"\xE3\x81\x84";
|
|
||||||
// char* test4 = u8"\xF0\xA0\xBA\x8C";
|
|
||||||
// char* test5 = "B";
|
|
||||||
// //char* test3 = " Just some extra texts";
|
|
||||||
|
|
||||||
// TStringAppendText(test, string);
|
|
||||||
// TStringAppendText(test2, string);
|
|
||||||
// TStringAppendText(test4, string);
|
|
||||||
// //TStringAppendText(test5, string);
|
|
||||||
// //TStringAppendText(test3, string);
|
|
||||||
|
|
||||||
// //TStringGetGlyph(index, characters, string);
|
|
||||||
// TStringGetGlyph(3, glyph, string);
|
|
||||||
// //TStringPrintDetails(string);
|
|
||||||
// TStringInsertText("END", 2, string);
|
|
||||||
// TStringPrintDetails(string);
|
|
||||||
// TStringPop(glyph, string);
|
|
||||||
// printf("%s\n", glyph);
|
|
||||||
// TStringPrintDetails(string);
|
|
||||||
// TStringPop(glyph, string);
|
|
||||||
// TStringPrintDetails(string);
|
|
||||||
// printf("%s\n", glyph);
|
|
||||||
|
|
||||||
TString* string = TStringCreate();
|
TString* string = TStringCreate();
|
||||||
|
|
||||||
@@ -72,6 +42,20 @@ int main(int argc, char** argv) {
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (input[0] == 'f') {
|
||||||
|
printf("Enter file path to open: ");
|
||||||
|
fgets(input, sizeof input, stdin);
|
||||||
|
input[strlen(input) - 1] = '\0';
|
||||||
|
|
||||||
|
if (ReadAllString(input, &file, &bytesRead)) {
|
||||||
|
TStringAppendText(file, string);
|
||||||
|
printf("Read %ld byte(s)\n", bytesRead);
|
||||||
|
free(file);
|
||||||
|
}
|
||||||
|
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
if (input[0] == 'r') {
|
if (input[0] == 'r') {
|
||||||
TStringPop(glyph, string);
|
TStringPop(glyph, string);
|
||||||
printf("Popped '%s'\n", glyph);
|
printf("Popped '%s'\n", glyph);
|
||||||
|
|||||||
Reference in New Issue
Block a user