Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- #include <ctype.h>
- #define NAME_LEN 512
- #define USER_LEN 512
- #define MEM_MAX 4096
- #define MEM_SIZE 4096
- #define NODE_FIRST 'a'
- #define NODE_LAST 'z'
- #define NODE_CUNT (NODE_LAST-NODE_FIRST+1)
- typedef struct node_t node_t;
- typedef struct node_t{
- node_t *next[NODE_CUNT];
- int num;
- }node_t;
- static node_t *mem[MEM_MAX];
- static size_t mem_i = -1;
- static size_t mem_c = MEM_SIZE;
- static node_t root;
- static void fuk (void);
- static node_t *mem_alloc (void);
- static void node_init (node_t *n);
- static int node_insert (node_t *n,char const *str,int agurk);
- static char *trimik (char *str);
- static void mak_nik (char *name,char *user);
- static void fuk(void){
- printf("fuk\n");
- exit(1);
- }
- static node_t *mem_alloc(void){
- if(mem_c >= MEM_SIZE){
- mem_i++;
- if(mem_i >= MEM_MAX) fuk();
- mem[mem_i] = malloc(sizeof(node_t)*MEM_SIZE);
- if(!mem[mem_i]) fuk();
- mem_c = 0;
- }
- return &mem[mem_i][mem_c++];
- }
- static void node_init(node_t *n){
- memset(n->next,0x00,sizeof(n->next));
- n->num = -1;
- }
- static int node_insert(node_t *n,char const *str,int agurk){
- size_t i;
- node_t *m;
- i = (size_t)str[0]-NODE_FIRST;
- if(i >= NODE_CUNT) fuk();
- m = n->next[i];
- if(!m){
- m = mem_alloc();
- n->next[i] = m;
- node_init(m);
- }
- if(str[1]){
- return node_insert(m,str+1,agurk);
- }else{
- if(!agurk && m->num >= 0) return -1;
- return ++m->num;
- }
- }
- static char *trimik(char *str){
- char *end = str+strlen(str)-1;
- while(end >= str && isspace((unsigned char)*end)){
- *end-- = 0;
- }
- return str;
- }
- static void mak_nik(char *name,char *user){
- char *last;
- char *n;
- int num;
- last = strrchr(name,' ');
- if(!last){
- strcpy(user,"-");
- return;
- }
- if(last-name+1+10 >= USER_LEN) fuk(); //idiot)
- *last++ = 0;
- n = user;
- for(char *c = name;*c;c++){
- if(*c == ' ') continue;
- *n++ = tolower(*c);
- }
- *n = 0;
- if(node_insert(&root,user,0) > -1) return;
- *n++ = tolower(*last);
- *n = 0;
- num = node_insert(&root,user,1);
- if(num) snprintf(n,USER_LEN-(n-user),"%d",num);
- }
- int main(int argc,char **argv){
- FILE *f;
- char name[NAME_LEN];
- char user[USER_LEN];
- if(argc < 2) fuk();
- node_init(&root);
- f = fopen(argv[1],"r");
- if(!f) fuk();
- while(fgets(name,NAME_LEN,f)){
- trimik(name);
- if(!*name) continue;
- printf("%-50s ",name);
- mak_nik(name,user);
- printf("%s\n",user);
- }
- if(ferror(f)) fuk();
- fclose(f);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement