Advertisement
homer512

wchar_t filter hex

Apr 15th, 2014
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.59 KB | None | 0 0
  1. #include <stdio.h>
  2. /* using fopen, fclose, fprintf, perror */
  3. #include <wctype.h>
  4. /* using iswxdigit */
  5. #include <wchar.h>
  6. /* using fputws, fgetws */
  7. #include <stdlib.h>
  8. /* using getenv */
  9. #include <locale.h>
  10. /* using setlocale */
  11.  
  12. static const char* LOG_FILE = "LogFile_ProxyBufferContents_FJ_small.html";
  13. static const char* OUT_FILE = "LogFile_ProxyBufferContents_FJ_small_out.html";
  14.  
  15. #define BUF_SIZE 4096
  16.  
  17.  
  18. static int is_hex_line(const wchar_t* buf)
  19. {
  20.   for(const wchar_t* pos = buf; ; ++pos) {
  21.     switch(*pos) {
  22.     case L'\r':
  23.     case L'\n':
  24.     case L'\0':
  25.       /* return false on empty line, true otherwise */
  26.       return pos != buf;
  27.     default:
  28.       if(! iswxdigit(*pos))
  29.     return 0;
  30.       break;
  31.     }
  32.   }
  33. }
  34.  
  35. static void filter_file(FILE* input, FILE* output)
  36. {
  37.   wchar_t in_buf[BUF_SIZE];
  38.   wchar_t* buf_alias = in_buf;
  39.   while((buf_alias = fgetws(buf_alias, BUF_SIZE, input)) != NULL) {
  40.     if(! is_hex_line(buf_alias))
  41.       fputws(buf_alias, output);
  42.   }
  43. }
  44.  
  45. static void set_locale()
  46. {
  47.   const char* env = "LC_CTYPE";
  48.   const char* val = getenv(env);
  49.   if(! val) {
  50.     val = "en_US.UTF8";
  51.     fprintf(stderr, "Guessing locale as %s\n", val);
  52.   }
  53.   if(! setlocale(LC_CTYPE, val))
  54.     fprintf(stderr, "Cannot set locate defined in %s. Continue anyway\n", env);
  55. }
  56.  
  57. int main(void)
  58. {
  59.   set_locale();
  60.   FILE* input = fopen(LOG_FILE, "r");
  61.   if(! input)
  62.     return 1;
  63.   FILE* output = fopen(OUT_FILE, "w");
  64.   if(! output)
  65.     return 1;
  66.   filter_file(input, output);
  67.   if(ferror(input))
  68.     perror("Input error");
  69.   fclose(output);
  70.   fclose(input);
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement