Advertisement
Guest User

Untitled

a guest
Apr 1st, 2020
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.81 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include <ctype.h>
  5.  
  6. #define NAME_LEN              512
  7. #define USER_LEN              512
  8. #define MEM_MAX               4096
  9. #define MEM_SIZE              4096
  10. #define NODE_FIRST            'a'
  11. #define NODE_LAST             'z'
  12. #define NODE_CUNT             (NODE_LAST-NODE_FIRST+1)
  13.  
  14. typedef struct node_t node_t;
  15.  
  16. typedef struct node_t{
  17.   node_t           *next[NODE_CUNT];
  18.   int               num;
  19. }node_t;
  20.  
  21. static node_t      *mem[MEM_MAX];
  22. static size_t       mem_i = -1;
  23. static size_t       mem_c = MEM_SIZE;
  24. static node_t       root;
  25.  
  26. static void         fuk         (void);
  27.  
  28. static node_t      *mem_alloc   (void);
  29.  
  30. static void         node_init   (node_t *n);
  31. static int          node_insert (node_t *n,char const *str,int agurk);
  32.  
  33. static char        *trimik      (char *str);
  34. static void         mak_nik     (char *name,char *user);
  35.  
  36. static void fuk(void){
  37.   printf("fuk\n");
  38.   exit(1);
  39. }
  40.  
  41. static node_t *mem_alloc(void){
  42.   if(mem_c >= MEM_SIZE){
  43.     mem_i++;
  44.     if(mem_i >= MEM_MAX) fuk();
  45.     mem[mem_i] = malloc(sizeof(node_t)*MEM_SIZE);
  46.     if(!mem[mem_i]) fuk();
  47.     mem_c = 0;
  48.   }
  49.   return &mem[mem_i][mem_c++];
  50. }
  51.  
  52. static void node_init(node_t *n){
  53.   memset(n->next,0x00,sizeof(n->next));
  54.   n->num = -1;
  55. }
  56.  
  57. static int node_insert(node_t *n,char const *str,int agurk){
  58.   size_t  i;
  59.   node_t *m;
  60.  
  61.   i = (size_t)str[0]-NODE_FIRST;
  62.   if(i >= NODE_CUNT) fuk();
  63.   m = n->next[i];
  64.  
  65.   if(!m){
  66.     m          = mem_alloc();
  67.     n->next[i] = m;
  68.     node_init(m);
  69.   }
  70.  
  71.   if(str[1]){
  72.     return node_insert(m,str+1,agurk);
  73.   }else{
  74.     if(!agurk && m->num >= 0) return -1;
  75.     return ++m->num;
  76.   }
  77. }
  78.  
  79. static char *trimik(char *str){
  80.   char *end = str+strlen(str)-1;
  81.   while(end >= str && isspace((unsigned char)*end)){
  82.     *end-- = 0;
  83.   }
  84.   return str;
  85. }
  86.  
  87. static void mak_nik(char *name,char *user){
  88.   char *last;
  89.   char *n;
  90.   int   num;
  91.  
  92.   last = strrchr(name,' ');
  93.   if(!last){
  94.     strcpy(user,"-");
  95.     return;
  96.   }
  97.   if(last-name+1+10 >= USER_LEN) fuk(); //idiot)
  98.   *last++ = 0;
  99.  
  100.   n = user;
  101.   for(char *c = name;*c;c++){
  102.     if(*c == ' ') continue;    
  103.     *n++ = tolower(*c);
  104.   }
  105.   *n = 0;
  106.  
  107.   if(node_insert(&root,user,0) > -1) return;
  108.  
  109.   *n++  = tolower(*last);
  110.   *n    = 0;
  111.   num   = node_insert(&root,user,1);
  112.   if(num) snprintf(n,USER_LEN-(n-user),"%d",num);
  113. }
  114.  
  115. int main(int argc,char **argv){
  116.   FILE   *f;
  117.   char    name[NAME_LEN];
  118.   char    user[USER_LEN];
  119.  
  120.   if(argc < 2) fuk();
  121.  
  122.   node_init(&root);
  123.  
  124.   f = fopen(argv[1],"r");
  125.   if(!f) fuk();
  126.  
  127.   while(fgets(name,NAME_LEN,f)){
  128.     trimik(name);
  129.     if(!*name) continue;
  130.  
  131.     printf("%-50s ",name);
  132.     mak_nik(name,user);
  133.     printf("%s\n",user);
  134.   }
  135.   if(ferror(f)) fuk();
  136.   fclose(f);
  137.  
  138.   return 0;
  139. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement