Advertisement
Guest User

Untitled

a guest
Jan 4th, 2014
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.49 KB | None | 0 0
  1. #include <unistd.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4.  
  5. #define BUF_SIZE 1024*1024
  6.  
  7. static char sumarize(char* pos, char l)
  8. {
  9.   char sum, i;
  10.   for (i = 0, sum = 0; i < l; i++) {
  11.     sum += pos[i];
  12.   }
  13.   return sum;
  14. }
  15.  
  16.  
  17. static void dict(char* pos, char posl, char* dict, char dictl)
  18. {
  19.   memset(dict, 0, dictl);
  20.   char sum, i;
  21.   for (i = 0, sum = 0; i < posl; i++) {
  22.     sum += pos[i];
  23.     dict[sum] = 1;
  24.   }
  25. }
  26.  
  27.  
  28.  
  29. static char _buf[BUF_SIZE];
  30. static int _buf_used = 0;
  31.  
  32. static void bflush()
  33. {
  34.   write(1, _buf, _buf_used);
  35.   _buf_used = 0;
  36. }
  37.  
  38. static void bwrite(char c)
  39. {
  40.   _buf[_buf_used] = c;
  41.   if(++_buf_used == BUF_SIZE)
  42.     bflush();
  43. }
  44.  
  45.  
  46. int main(int argc, char *argv[])
  47. {
  48.   char separator = ',';
  49.  
  50.   char sizes[] = {4,4,4,3,5,1,1,3,3,3,3,3,3,10,3,1,1,1,2,3,10};
  51.  
  52.   char sum = sumarize(sizes, (char)sizeof(sizes));
  53.  
  54.   char d[sum];
  55.   dict(sizes, (char)sizeof(sizes) - 1, d, sum);
  56.  
  57.   char counter = 0;
  58.  
  59.   char c;
  60.  
  61.   char buf[BUF_SIZE];
  62.   int readed, i;
  63.  
  64.   while ((readed = read(0, buf, BUF_SIZE)) > 0)
  65.     {
  66.       for(i=0; i < readed; i++)
  67.         {
  68.           c = buf[i];
  69.           if(c == '\n')
  70.             {
  71.               counter = 0;
  72.               bwrite(c);
  73.               continue;
  74.             }
  75.  
  76.           bwrite(c);
  77.  
  78.  
  79.           if(d[++counter])
  80.             {
  81.               bwrite(separator);
  82.             }
  83.         }
  84.  
  85.       if(buf[readed - 1] == EOF)
  86.         {
  87.           break;
  88.         }
  89.  
  90.     }
  91.  
  92.   bflush();
  93.  
  94.   return 0;
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement