rohit35

user file

Mar 16th, 2018
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.34 KB | None | 0 0
  1. #ifndef _MY_USERS
  2. #define _MY_USERS 1
  3. #include <string.h>
  4. int compare_name(node_t *str,char *str2);
  5. int compare_pass(node_t *str,char *str2);
  6. int compare_id(node_t *str,char *str2);
  7. char *int_char(int src);
  8. char *unique_id(int id_num,list_t * users);
  9. char *word(char *source);
  10. typedef struct user_t
  11. {
  12. void *name;
  13. void *id;
  14. void *password;
  15. void *pointer;
  16. } user_t;
  17. user_t *create_users()
  18. {
  19.  
  20. return create_list();
  21. }
  22. int user_insert(list_t *users, user_t *new_user)
  23. {
  24. return list_insert(users, (void *)new_user);
  25. }
  26. int user_create(list_t *users, char *name,char *password)
  27. {
  28. if(strlen(name) !=0 && strlen(password)!=0)
  29. {
  30. user_t *new_user = NULL;
  31. int rc;
  32. new_user = (user_t *)calloc(1, sizeof(user_t));
  33. if (new_user == NULL) return 0;
  34. new_user->name = word(name);
  35. new_user->password = word(password);
  36. char *res_id=unique_id(users->size+1,users);
  37. new_user->id = res_id;
  38. new_user->pointer=NULL;
  39. rc=users_check(users,name,res_id,password);
  40. if(rc)
  41. {
  42. rc = user_insert(users, new_user);
  43. }
  44. return rc;
  45. }
  46. else
  47. {
  48. return 0;
  49. }
  50. }
  51. char *unique_id(int id_num,list_t * users)
  52. {
  53. char *id =int_char(id_num);
  54. node_t *li = NULL;
  55. user_t *user = NULL;
  56. for (li = users->head ; li != NULL ; li = li->next)
  57. {
  58. user = (user_t *)li->data;
  59. if(compare_id(li,id))
  60. return unique_id(char_to_int(id)+1,users);
  61. }
  62. return id;
  63. }
  64. char * word(char *source)
  65. {
  66. int idx;
  67. char *array = (char*)malloc(512*sizeof(char));
  68. for(idx=0; source[idx] !='\0'; idx++)
  69. {
  70. array[idx] = source[idx];
  71. }
  72. array[idx]='\0';
  73. return array;
  74. }
  75. int users_check(list_t *users,char *name,char *id,char *password)
  76. {
  77. node_t *idx = NULL;
  78. user_t *check = NULL;
  79. if(users == NULL) return 1;
  80. int flag=1;
  81. for(idx=users->head; idx!=NULL ; idx=idx->next)
  82. {
  83. check = (user_t*)idx->data;
  84. if(compare_name(idx,name))
  85. {
  86. flag=2;
  87. }
  88. if(compare_pass(idx,password))
  89. {
  90. flag=3;
  91. }
  92. if(compare_id(idx,id))
  93. {
  94. flag=4;
  95. }
  96. }
  97. if(flag==2)
  98. {
  99. printf("\t \t \tPlease enter a new name\n");
  100. printf("\t \t \t");
  101. puts(name);
  102. printf(" Already exists\n");
  103. return 0;
  104. }
  105. else if(flag==3)
  106. {
  107. printf("\t \t \tPlease enter a new password\n");
  108. puts(password);
  109. printf("\t \t \tAlready exists\n");
  110. return 0;
  111. }
  112. else if(flag==4)
  113. {
  114. printf("\t \t \tPlease enter a new id number\n");
  115. puts(id);
  116. printf("\t \t \t Already exists\n");
  117. return 0;
  118. }
  119. else
  120. {
  121. return 1;
  122. }
  123. }
  124. void user_search(list_t *users,char *name,char *password)
  125. {
  126. system("cls");
  127. node_t *idx = NULL;
  128. user_t *acc = NULL;
  129. if(users==NULL)
  130. {
  131. printf("\t \t \tThe list is empty\n");
  132. }
  133. else
  134. {
  135. int flag=1;
  136. for(idx=users->head; idx!=NULL ; idx=idx->next)
  137. {
  138. if(compare_name(idx,name) && compare_pass(idx,password))
  139. {
  140. flag=0;
  141. userpage((void*)idx,users);
  142. break;
  143. }
  144. }
  145. if(flag==1)
  146. {
  147. printf("\t \t \tInvalid username or password\n");
  148. }
  149. }
  150. }
  151. user_t* search_user_tweets(char *id,list_t *users)
  152. {
  153.  
  154. node_t *idx = NULL;
  155. user_t *check = NULL;
  156. void *pos = NULL;
  157. char *cur;
  158. for(idx=users->head; idx!=NULL ; idx=idx->next)
  159. {
  160. check = (user_t*)idx->data;
  161. pos = idx->data;
  162. cur =(char*)check->id;
  163. if(strcmp(cur,id) ==0)
  164. {
  165. return pos;
  166. }
  167. }
  168. return NULL;
  169. }
  170.  
  171. int compare_name(node_t *str,char *str2)
  172. {
  173. node_t *user=str;
  174. user_t *str1=NULL;
  175. str1=(user_t*)user->data;
  176. char *cur =((char*)str1->name);
  177. int i,str1_length=strlen(cur);
  178. int flag=0,str2_length=strlen(str2);
  179. if(str1_length==str2_length)
  180. {
  181. for(i=0; i<str1_length; i++)
  182. {
  183. if(cur[i]==str2[i])
  184. flag++;
  185. }
  186. }
  187. if(flag==str2_length)
  188. return 1;
  189. else
  190. return 0;
  191. }
  192. int compare_id(node_t *str,char *str2)
  193. {
  194. node_t *user=str;
  195. user_t *str1=NULL;
  196. str1=(user_t*)user->data;
  197. char *cur = ((char*)str1->id);
  198. int i,str1_length=strlen(cur);
  199. int flag=0,str2_length=strlen(str2);
  200. if(str1_length==str2_length)
  201. {
  202. for(i=0; i<str1_length; i++)
  203. {
  204. if(cur[i]==str2[i])
  205. flag++;
  206. }
  207. }
  208. if(flag==str2_length)
  209. return 1;
  210. else
  211. return 0;
  212. }
  213. int compare_pass(node_t *str,char *str2)
  214. {
  215. node_t *user=str;
  216. user_t *str1=NULL;
  217. str1=(user_t*)user->data;
  218. char *cur =((char*)str1->password);
  219. int i,str1_length=strlen(cur);
  220. int flag=0,str2_length=strlen(str2);
  221. if(str1_length==str2_length)
  222. {
  223. for(i=0; i<str1_length; i++)
  224. {
  225. if(cur[i]==str2[i])
  226. flag++;
  227. }
  228. }
  229. if(flag==str2_length)
  230. return 1;
  231. else
  232. return 0;
  233. }
  234. void users_print(list_t *users)
  235. {
  236. node_t *li = NULL;
  237. user_t *user = NULL;
  238. void *s,*p;
  239. if (users == NULL) return;
  240. printf("\t \t \tId : username : password\n");
  241. for (li = users->head ; li != NULL ; li = li->next)
  242. {
  243. user = (user_t *)li->data;
  244. p=user->name;
  245. s=user->password;
  246. printf("\t \t \t%s : %s :%s\n", ((char*)user->id),((char*)user->name),((char*)user->password));
  247. }
  248. }
  249. char *int_char(int src)
  250. {
  251. char r,*string=(char*)malloc(100*sizeof(char));
  252. int n=src,j=0;
  253. while(n!=0)
  254. {
  255. r=n%10;
  256. string[j++]=(r+48);
  257. n/=10;
  258. }
  259. string[j]='\0';
  260. strrev(string);
  261. return string;
  262. }
  263. int char_to_int(char*num)
  264. {
  265.  
  266. int length=strlen(num);
  267. int var=1;
  268. int i,c=0,temp;
  269. for(i=0; i<length; i++)
  270. {
  271. if(i==0 && num[i]==45 )
  272. {
  273. var=-1;
  274. }
  275. else
  276. {
  277. temp=(num[i]-48);
  278. c=c*10+temp;
  279. }
  280. }
  281. c=c*var;
  282. return c;
  283. }
  284. #endif
Add Comment
Please, Sign In to add comment