19 #include "file_operations.h" 28 #include "string_operations.h" 33 int readTokensFromFile(
const char* fileName, GArray** tokens,
const char* delimiters)
35 int fd = open(fileName, O_RDONLY);
38 printf(
"FATAL: can not open %s\n", fileName);
42 *tokens = tokens_new();
44 int needConverter = 1;
45 iconv_t converter = NULL;
47 Token* remainder = NULL;
50 char convertedBuffer[BUFFSIZE];
53 size_t leftFromLast = 0;
54 while ((n = read(fd, buffer + leftFromLast,
sizeof(buffer) - leftFromLast)) > 0)
56 size_t len = (size_t) n + leftFromLast;
63 converter = guessConverter(buffer, len);
69 size_t inputLeft = len;
71 char* output = convertedBuffer;
72 size_t outputLength =
sizeof(convertedBuffer);
73 iconv(converter, &input, &inputLeft, &output, &outputLength);
75 if (outputLength !=
sizeof(convertedBuffer)) {
76 chunk = convertedBuffer;
77 len =
sizeof(convertedBuffer) - outputLength;
79 leftFromLast = inputLeft;
80 for (
size_t i = 0; i < leftFromLast; i++)
86 printf(
"WARNING: cannot re-encode '%s', going binary from now on\n", fileName);
87 iconv_close(converter);
95 int addedTokens = streamTokenize(chunk, len, delimiters, tokens, &remainder);
98 printf(
"WARNING: can not complete tokenizing of '%s'\n", fileName);
103 streamTokenize(buffer, leftFromLast, delimiters, tokens, &remainder);
104 streamTokenize(NULL, 0, NULL, tokens, &remainder);
110 iconv_close(converter);
char buffer[2048]
The last thing received from the scheduler.