Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- /* using fprintf, fread, fwrite, fopen, fclose */
- #include <ctype.h>
- /* using isxdigit */
- static const char* LOG_FILE = "LogFile_ProxyBufferContents_FJ_small.html";
- static const char* OUT_FILE = "LogFile_ProxyBufferContents_FJ_small_out.html";
- /*4096 would be a better size*/
- #define BUF_SIZE 200
- /**
- * Searches for CRNL line endings
- *
- * \param buf input data
- * \param size length of buf
- * \return The position of the next CR, followed by an NL. -1 if none is found
- */
- static int find_crnl(const char* buf, int size)
- {
- for(const char* pos = buf; size > 1; ++pos, --size)
- if(pos[0] == '\r') {
- if(pos[1] == '\n')
- return pos - buf;
- else
- fprintf(stderr, "Unexpected CR line ending\n");
- }
- return -1;
- }
- /**
- * Checks whether the given string is a hex number
- *
- * \param buf input data
- * \param size length of string in buf that shall be checked
- * \return 1 if buf is a valid hex number, 0 otherwise
- */
- static int is_hex(const char* buf, int size)
- {
- if(! size)
- return 0;
- for(int i = 0; i < size; ++i)
- if(! isxdigit(buf[i]))
- return 0;
- return 1;
- }
- /**
- * Writes a line from input to output unless it is just a hex number
- *
- * \param buf input data. Is assumed to start at new line (after CRNL or at
- * beginning of file
- * \param size bytes in buf
- * \param out output buffer. Will be overwritten. Must be at least size bytes
- * large
- * \param written Number of bytes in out after function returns. Is not
- * initialized with 0. Is just incremented
- * \return Start of next line or -1 if no end could be found
- */
- static int filter_line(const char* buf, int size, char* out, int* written)
- {
- int line_end = find_crnl(buf, size);
- if(line_end < 0)
- return line_end;
- if(is_hex(buf, line_end)) {
- fprintf(stderr, "Skipping hex\n");
- return line_end + 2; /* skip */
- }
- for(int i = 0; i < line_end + 2; ++i, ++(*written))
- out[i] = buf[i]; /* copy */
- return line_end + 2;
- }
- /**
- * Repeatedly calls filter_line for buffer that may contain multiple lines
- *
- * \param buf input data. Is assumed to start at new line (after CRNL or at
- * beginning of file
- * \param size byte in buf
- * \param out output buffer. Will be overwritten. Must be at least size bytes
- * large
- * \param written Number of bytes in out after function returns. Will be
- * initialized with 0
- * \return number of bytes processed. Remainder contains no complete line
- */
- static int filter_buf(const char* buf, int size, char* out, int* written)
- {
- const char* pos = buf;
- *written = 0;
- for(;;) {
- int next_start = filter_line(pos, size, out + *written, written);
- if(next_start < 0)
- return pos - buf;
- pos += next_start;
- size -= next_start;
- }
- }
- /**
- * Calls filter_buf for file input/output and handles the remainder
- *
- * \param input file opened for reading
- * \param output file opened for writing
- * \param in_buf buffer of at least BUF_SIZE length, may contain unprocessed
- * data after return
- * \param in_pos number of unprocessed bytes in in_buf from last call
- * \return number of unprocessed bytes in in_buf or -1 on EOF
- */
- static int filter_fbuf(FILE* input, FILE* output, char* in_buf, int in_pos)
- {
- char out_buf[BUF_SIZE];
- int size = fread(in_buf + in_pos, 1, BUF_SIZE - in_pos, input);
- size += in_pos;
- if(! size)
- return -1;
- int written;
- int end_pos = filter_buf(in_buf, size, out_buf, &written);
- if(end_pos) {
- for(int i = 0; i < size - end_pos; ++i)
- in_buf[i] = in_buf[i + end_pos];
- }
- else {
- if(size == BUF_SIZE) {
- fprintf(stderr, "Line too long\n");
- fwrite(in_buf, 1, size, output);
- return 0;
- }
- else {
- /* no newline at EOF */
- filter_line(in_buf, size, out_buf, &written);
- }
- }
- fwrite(out_buf, 1, written, output);
- return size - end_pos;
- }
- /**
- * Repeatedly calls filter_fbuf until the whole input file is processed
- *
- * \param input file opened for reading
- * \param output file opened for writing
- */
- static void filter_file(FILE* input, FILE* output)
- {
- char in_buf[BUF_SIZE];
- int in_pos = 0;
- do {
- in_pos = filter_fbuf(input, output, in_buf, in_pos);
- } while(in_pos >= 0);
- }
- int main(void)
- {
- FILE* input = fopen(LOG_FILE, "r");
- if(! input)
- return 1;
- FILE* output = fopen(OUT_FILE, "w");
- if(! output)
- return 1;
- filter_file(input, output);
- fclose(output);
- fclose(input);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement