Advertisement
Guest User

Untitled

a guest
Apr 1st, 2013
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.47 KB | None | 0 0
  1. //count.h
  2. #ifndef __count__
  3. #define __count__
  4.  
  5. #include <string.h>
  6. #include <fcntl.h>
  7. #include <unistd.h>
  8. #include <stdio.h>
  9. #include <ctype.h>
  10. #include "error.h"
  11.  
  12. typedef struct
  13. {
  14.     int newline;
  15.     int word;
  16.     int byte;
  17. } info;
  18.  
  19. int len(int a);
  20.  
  21. int max(int a, int b);
  22.  
  23. info count(int descr);
  24.  
  25. #endif
  26.  
  27.  
  28. ///////////////////////////////////////////////////////////
  29. //count.c
  30. #include "count.h"
  31.  
  32. int len(int a)
  33. {
  34.     int res = 0;
  35.     while(a)
  36.     {
  37.         res++;
  38.         a/=10;
  39.     }
  40.     return res;
  41. }
  42.  
  43. int max(int a, int b)
  44. {
  45.     return ((a > b) ? a : b);
  46. }
  47.  
  48. info count(int descr)
  49. {
  50.     info kol;
  51.     kol.newline = 0;
  52.     kol.word = 0;
  53.     kol.byte = 0;
  54.  
  55.     int len = 512;
  56.     char s[512];
  57.     int n;
  58.  
  59.     errno = 0;
  60.     int flag1 = 1;
  61.     int flag2 = 1;
  62.     while(n = read(descr, s, len))
  63.     {
  64.         if(n == -1)
  65.             error("Error while reading.", errno);
  66.        
  67.         errno = 0;
  68.        
  69.         kol.byte+=n;
  70.         for(int i=0; i<n; i++)
  71.         {
  72.             if(flag1)
  73.             {
  74.                 kol.newline++;
  75.                 flag1 = 0;
  76.             }
  77.  
  78.             if(isblank(s[i]) || s[i] == '\n')
  79.                 flag2 = 1;
  80.             else
  81.             {
  82.                 if(flag2)
  83.                 {
  84.                     kol.word++;
  85.                     flag2 = 0;
  86.                 }
  87.             }
  88.             if(s[i] == '\n')
  89.                 flag1 = 1;
  90.         }
  91.     }
  92.     return kol;
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement