PageRenderTime 49ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/locadora_em_c/client.c

http://ruby-pro.googlecode.com/
C | 630 lines | 509 code | 85 blank | 36 comment | 93 complexity | 8950c6b994b03ed6f1fe4e4dcf41df11 MD5 | raw file
  1. /*
  2. * File: client.c
  3. *
  4. * Created on 5 de Maio de 2010, 16:22
  5. */
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include <regex.h>
  10. #include <time.h>
  11. #include "exceptions.h"
  12. #include "status.h"
  13. #include "client.h"
  14. #include "date.h"
  15. /*
  16. * Client module
  17. */
  18. int check_by_id_client(char *input) {
  19. int id;
  20. do {
  21. printf("Qual ID? ");
  22. read_string(input);
  23. } while (!validate_id(input));
  24. id = atoi(input);
  25. // Verificar se o ID existe
  26. if (id > 0 && client_index_exist(id)) {
  27. return id;
  28. } else {
  29. printf(ID_NOT_FOUND_ERROR, __FILE__, "cliente");
  30. return NON_EXIST;
  31. }
  32. }
  33. Client * client_malloc() {
  34. Client *client = malloc(sizeof (Client));
  35. if (!client) {
  36. printf(ALLOC_ERROR, __FILE__);
  37. exit(EXIT_FAILURE);
  38. }
  39. client_initialize(client);
  40. return client;
  41. }
  42. void client_initialize(Client * client) {
  43. client->id = NON_EXIST;
  44. strcpy(client->CPF, "0");
  45. strcpy(client->RG, "0");
  46. client->birth_date = 0;
  47. strcpy(client->name, "initialize");
  48. strcpy(client->phone, "0");
  49. }
  50. Client * search_client_by_id(int id) {
  51. FILE *file_stream = NULL;
  52. Client *client;
  53. file_stream = fopen(CLIENTS_FILEPATH, "rb");
  54. if (!file_stream) {
  55. printf(READ_OPEN_ERROR, __FILE__, CLIENTS_FILEPATH);
  56. exit(EXIT_FAILURE);
  57. }
  58. client = client_malloc();
  59. fread(client, sizeof (Client), 1, file_stream);
  60. while (!feof(file_stream)) {
  61. if (client->id == id) {
  62. fclose(file_stream);
  63. return client;
  64. }
  65. fread(client, sizeof (Client), 1, file_stream);
  66. }
  67. fclose(file_stream);
  68. client->id = NON_EXIST;
  69. return client;
  70. }
  71. Client * search_client_by_name(char *name) {
  72. FILE *file_stream = NULL;
  73. Client *client;
  74. file_stream = fopen(CLIENTS_FILEPATH, "rb");
  75. if (!file_stream) {
  76. printf(READ_OPEN_ERROR, __FILE__, CLIENTS_FILEPATH);
  77. exit(EXIT_FAILURE);
  78. }
  79. client = client_malloc();
  80. fread(client, sizeof (Client), 1, file_stream);
  81. while (!feof(file_stream)) {
  82. if (!strcasecmp(name, client->name)) {
  83. fclose(file_stream);
  84. return client;
  85. }
  86. fread(client, sizeof (Client), 1, file_stream);
  87. }
  88. // Nгo achou pelo nome exato, entгo tentaremos uma substring
  89. regex_t reg;
  90. if (regcomp(&reg, name, REG_EXTENDED | REG_NOSUB | REG_ICASE)) {
  91. fprintf(stderr, "%s: ERRO na compilacao da expressao regular.\n", __FILE__);
  92. fclose(file_stream);
  93. client->id = NON_EXIST;
  94. return client;
  95. }
  96. fseek(file_stream, 0, SEEK_SET);
  97. fread(client, sizeof (Client), 1, file_stream);
  98. while (!feof(file_stream)) {
  99. if (!(regexec(&reg, client->name, 0, (regmatch_t *) NULL, 0))) {
  100. fclose(file_stream);
  101. return client;
  102. }
  103. fread(client, sizeof (Client), 1, file_stream);
  104. }
  105. // Nada foi encontrado
  106. fclose(file_stream);
  107. client->id = NON_EXIST;
  108. return client;
  109. }
  110. int clients_file_is_empty() {
  111. FILE *file_stream = NULL;
  112. file_stream = fopen(CLIENTS_FILEPATH, "rb");
  113. if (file_stream) {
  114. fclose(file_stream);
  115. return FALSE;
  116. } else {
  117. return TRUE;
  118. }
  119. }
  120. int client_index_exist(int index) {
  121. Client *client;
  122. client = client_malloc();
  123. client = search_client_by_id(index);
  124. if (client->id == NON_EXIST) {
  125. free(client);
  126. return FALSE;
  127. }
  128. free(client);
  129. return TRUE;
  130. }
  131. int client_first_index_avaliable() {
  132. FILE *file_stream = NULL;
  133. int old_id = NON_EXIST, new_id = NON_EXIST;
  134. file_stream = fopen(CLIENTS_ID_FILEPATH, "rb+");
  135. if (file_stream) {
  136. fread(&old_id, sizeof (old_id), 1, file_stream);
  137. rewind(file_stream);
  138. new_id = old_id + 1;
  139. fwrite(&new_id, sizeof (new_id), 1, file_stream);
  140. fclose(file_stream);
  141. return old_id;
  142. } else {
  143. printf("Aviso: arquivo \"%s\" foi criado agora.\n", CLIENTS_ID_FILEPATH);
  144. /* Nгo conseguiu abrir um arquivo existente, entгo, criarб. */
  145. file_stream = fopen(CLIENTS_ID_FILEPATH, "wb+");
  146. if (file_stream) {
  147. new_id = 2;
  148. fwrite(&new_id, sizeof (new_id), 1, file_stream);
  149. fclose(file_stream);
  150. return 1;
  151. } else {
  152. printf(CREATE_FILE_ERROR, __FILE__, CLIENTS_ID_FILEPATH);
  153. exit(EXIT_FAILURE);
  154. }
  155. }
  156. }
  157. int insert_client(Client * client) {
  158. FILE *file_stream = NULL;
  159. client->id = client_first_index_avaliable();
  160. file_stream = fopen(CLIENTS_FILEPATH, "rb+");
  161. if (file_stream) {
  162. fseek(file_stream, 0, SEEK_END);
  163. if (!fwrite(client, sizeof (Client), 1, file_stream)) {
  164. printf(WRITE_FILE_ERROR, __FILE__, CLIENTS_FILEPATH);
  165. fclose(file_stream);
  166. return FALSE;
  167. }
  168. fclose(file_stream);
  169. } else {
  170. printf("Aviso: arquivo \"%s\" foi criado agora.\n", CLIENTS_FILEPATH);
  171. /* Nгo conseguiu abrir um arquivo existente, entгo, criarб. */
  172. file_stream = fopen(CLIENTS_FILEPATH, "wb+");
  173. if (file_stream) {
  174. if (!fwrite(client, sizeof (Client), 1, file_stream)) {
  175. printf(WRITE_FILE_ERROR, __FILE__, CLIENTS_FILEPATH);
  176. fclose(file_stream);
  177. return FALSE;
  178. }
  179. fclose(file_stream);
  180. } else {
  181. printf(CREATE_FILE_ERROR, __FILE__, CLIENTS_FILEPATH);
  182. return FALSE;
  183. }
  184. }
  185. return TRUE;
  186. }
  187. int update_client(Client *client) {
  188. FILE *file_stream = NULL;
  189. Client *aux;
  190. file_stream = fopen(CLIENTS_FILEPATH, "rb+");
  191. if (!file_stream) {
  192. printf(FILE_NOT_FOUND_ERROR, __FILE__, CLIENTS_FILEPATH);
  193. return FALSE;
  194. }
  195. aux = client_malloc();
  196. // Procurar o registro a ser alterado no arquivo
  197. fread(aux, sizeof (Client), 1, file_stream);
  198. while (!feof(file_stream)) {
  199. if (aux->id == client->id) {
  200. fseek(file_stream, -(sizeof (Client)), SEEK_CUR);
  201. if (!fwrite(client, sizeof (Client), 1, file_stream)) {
  202. printf(WRITE_FILE_ERROR, __FILE__, CLIENTS_FILEPATH);
  203. fclose(file_stream);
  204. free(aux);
  205. return FALSE;
  206. }
  207. fclose(file_stream);
  208. free(aux);
  209. return TRUE;
  210. }
  211. fread(aux, sizeof (Client), 1, file_stream);
  212. }
  213. // Se chegar atй aqui й porque nгo encontrou nada
  214. fclose(file_stream);
  215. free(aux);
  216. return FALSE;
  217. }
  218. int erase_client(Client *client) {
  219. FILE *file_stream = NULL, *file_stream_tmp = NULL;
  220. Client *aux;
  221. file_stream = fopen(CLIENTS_FILEPATH, "rb+");
  222. if (!file_stream) {
  223. printf(FILE_NOT_FOUND_ERROR, __FILE__, CLIENTS_FILEPATH);
  224. return FALSE;
  225. }
  226. file_stream_tmp = fopen(CLIENTS_TMP_FILEPATH, "wb");
  227. if (!file_stream_tmp) {
  228. printf(FILE_NOT_FOUND_ERROR, __FILE__, CLIENTS_TMP_FILEPATH);
  229. return FALSE;
  230. }
  231. aux = client_malloc();
  232. fread(aux, sizeof (Client), 1, file_stream);
  233. while (!feof(file_stream)) {
  234. if (aux->id != client->id) {
  235. fwrite(aux, sizeof (Client), 1, file_stream_tmp);
  236. }
  237. fread(aux, sizeof (Client), 1, file_stream);
  238. }
  239. free(aux);
  240. fclose(file_stream);
  241. fclose(file_stream_tmp);
  242. if (remove(CLIENTS_FILEPATH)) {
  243. return FALSE;
  244. }
  245. if (rename(CLIENTS_TMP_FILEPATH, CLIENTS_FILEPATH)) {
  246. return FALSE;
  247. }
  248. // Verificar se o arquivo ficou com 0 bytes
  249. file_stream = fopen(CLIENTS_FILEPATH, "rb");
  250. if (!file_stream) {
  251. printf(FILE_NOT_FOUND_ERROR, __FILE__, CLIENTS_FILEPATH);
  252. return FALSE;
  253. }
  254. fseek(file_stream, 0, SEEK_END);
  255. // Se o arquivo tiver 0 bytes, serб removido.
  256. if (!ftell(file_stream)) {
  257. remove(CLIENTS_FILEPATH);
  258. }
  259. return TRUE;
  260. }
  261. void copy_client(Client * dest, Client * src) {
  262. dest->id = src->id;
  263. strcpy(dest->name, src->name);
  264. strcpy(dest->phone, src->phone);
  265. strcpy(dest->RG, src->RG);
  266. strcpy(dest->CPF, src->CPF);
  267. dest->birth_date = src->birth_date;
  268. }
  269. int get_size_clients() {
  270. FILE *file_stream = NULL;
  271. file_stream = fopen(CLIENTS_FILEPATH, "rb");
  272. if (!file_stream) {
  273. printf(FILE_NOT_FOUND_ERROR, __FILE__, CLIENTS_FILEPATH);
  274. return FALSE;
  275. }
  276. fseek(file_stream, 0, SEEK_END);
  277. return ftell(file_stream) / sizeof (Client);
  278. }
  279. Client * client_file_to_a() {
  280. FILE * file_stream = NULL;
  281. Client *vetor;
  282. int i, size;
  283. /* Antes de tudo, precisamos testar se hб algum cliente no arquivo */
  284. if (clients_file_is_empty()) {
  285. printf(EMPTY_ERROR, __FILE__, "cliente");
  286. return FALSE;
  287. }
  288. file_stream = fopen(CLIENTS_FILEPATH, "rb");
  289. if (!file_stream) {
  290. printf(FILE_NOT_FOUND_ERROR, __FILE__, CLIENTS_FILEPATH);
  291. return FALSE;
  292. }
  293. size = get_size_clients();
  294. if (!size) {
  295. printf("%s: Nao foi possivel obter a quantidade de clientes.\n", __FILE__);
  296. return FALSE;
  297. }
  298. vetor = malloc(size * sizeof (Client));
  299. if (!vetor) {
  300. printf(ALLOC_ERROR, __FILE__);
  301. return FALSE;
  302. }
  303. for (i = 0; i < size; i++) {
  304. fread(vetor + i, sizeof (Client), 1, file_stream);
  305. }
  306. fclose(file_stream);
  307. return vetor;
  308. }
  309. Client * sort_client_by_name() {
  310. Client *aux, *vetor;
  311. int size, i, j;
  312. aux = client_malloc();
  313. vetor = client_file_to_a();
  314. size = get_size_clients();
  315. for (i = 0; i < size; i++) {
  316. for (j = i + 1; j < size; j++) {
  317. if (strcmp((vetor + i)->name, (vetor + j)->name) > 0) {
  318. copy_client(aux, vetor + j);
  319. copy_client(vetor + j, vetor + i);
  320. copy_client(vetor + i, aux);
  321. }
  322. }
  323. }
  324. free(aux);
  325. return vetor;
  326. }
  327. void puts_client(Client * client) {
  328. char *buffer;
  329. printf("ID: %d\n", client->id);
  330. printf("Nome: %s\nCPF: %s\nRG: %s\n", client->name, client->CPF, client->RG);
  331. printf("Fone: %s\n", client->phone);
  332. buffer = date_to_s(&client->birth_date);
  333. printf("Data de nascimento: %s\n\n", buffer);
  334. free(buffer);
  335. }
  336. void puts_client_short(Client * client) {
  337. printf("Cliente [%d]: %s", client->id, client->name);
  338. }
  339. void puts_client_by_id(int id) {
  340. Client *client;
  341. client = client_malloc();
  342. client = search_client_by_id(id);
  343. if (client->id == NON_EXIST) {
  344. printf(ID_NOT_FOUND_ERROR, __FILE__, "cliente");
  345. free(client);
  346. return;
  347. } else {
  348. puts_client(client);
  349. }
  350. free(client);
  351. }
  352. void puts_all_clients() {
  353. FILE *file_stream = NULL;
  354. Client *client;
  355. file_stream = fopen(CLIENTS_FILEPATH, "rb");
  356. if (!file_stream) {
  357. printf(EMPTY_ERROR, __FILE__, "cliente");
  358. return;
  359. }
  360. client = client_malloc();
  361. printf("\n=======\nLISTA DE TODOS OS CLIENTES: \n\n");
  362. fread(client, sizeof (Client), 1, file_stream);
  363. while (!feof(file_stream)) {
  364. puts_client(client);
  365. fread(client, sizeof (Client), 1, file_stream);
  366. }
  367. printf("\n=======\n");
  368. fclose(file_stream);
  369. free(client);
  370. }
  371. void form_client(Client *client, char *input) {
  372. printf("Nome: ");
  373. read_string(client->name);
  374. printf("CPF: ");
  375. read_string(client->CPF);
  376. printf("RG: ");
  377. read_string(client->RG);
  378. printf("Fone: ");
  379. read_string(client->phone);
  380. client->birth_date = form_parse_date("Data de nascimento: \n", input);
  381. }
  382. void form_client_sort() {
  383. int i, size;
  384. Client *vetor;
  385. // Antes de tudo, precisamos testar se hб algum cliente no arquivo
  386. if (clients_file_is_empty()) {
  387. printf(EMPTY_ERROR, __FILE__, "cliente");
  388. return;
  389. }
  390. vetor = sort_client_by_name();
  391. size = get_size_clients();
  392. if (!vetor) {
  393. printf("Nao foi possivel ordenar corretamente!\n");
  394. return;
  395. }
  396. printf("\n=======\nLISTA DE TODOS OS CLIENTES ORDENADOS POR NOME: \n\n");
  397. for (i = 0; i < size; i++) {
  398. puts_client(vetor + i);
  399. }
  400. printf("\n=======\n");
  401. free(vetor);
  402. }
  403. void form_client_insert(char *input) {
  404. Client *client;
  405. client = client_malloc();
  406. printf("\n=======\nINSERINDO CLIENTE: \n\n");
  407. form_client(client, input);
  408. if (insert_client(client)) {
  409. printf("Cliente inserido com sucesso.\n");
  410. } else {
  411. printf("Cliente nao foi inserido corretamente!\n");
  412. }
  413. printf("\n=======\n");
  414. free(client);
  415. }
  416. void form_client_update(char *input) {
  417. Client *client;
  418. // Antes de tudo, precisamos testar se hб algum cliente no arquivo
  419. if (clients_file_is_empty()) {
  420. printf(EMPTY_ERROR, __FILE__, "cliente");
  421. return;
  422. }
  423. printf("\n=======\nMODIFICANDO CLIENTE: \n\n");
  424. client = form_client_select(input);
  425. // Verifica se cliente й vбlido
  426. if (client->id == NON_EXIST) {
  427. free(client);
  428. return;
  429. }
  430. puts_client_by_id(client->id);
  431. //Tem certeza?
  432. if (!be_sure(input)) {
  433. printf("Abortando modificacao de cliente.\n\n");
  434. free(client);
  435. return;
  436. }
  437. form_client(client, input);
  438. // Tem certeza?
  439. if (!be_sure(input)) {
  440. printf("Abortando modificacao de cliente.\n\n");
  441. free(client);
  442. return;
  443. }
  444. // Atualizaзгo confirmada!
  445. if (update_client(client)) {
  446. printf("Cliente atualizado com sucesso.\n");
  447. } else {
  448. printf("Cliente nao foi atualizado corretamente!\n");
  449. }
  450. printf("\n=======\n");
  451. free(client);
  452. }
  453. void form_client_erase(char *input) {
  454. Client *client;
  455. // Antes de tudo, precisamos testar se hб algum cliente no arquivo
  456. if (clients_file_is_empty()) {
  457. printf(EMPTY_ERROR, __FILE__, "cliente");
  458. return;
  459. }
  460. printf("\n=======\nREMOVENDO CLIENTE: \n\n");
  461. client = form_client_select(input);
  462. // Verifica se cliente й vбlido
  463. if (client->id == NON_EXIST) {
  464. free(client);
  465. return;
  466. }
  467. puts_client_by_id(client->id);
  468. // Tem certeza?
  469. if (!be_sure(input)) {
  470. printf("Abortando remocao de cliente.\n\n");
  471. free(client);
  472. return;
  473. }
  474. // Remoзгo confirmada!
  475. if (erase_client(client)) {
  476. printf("Cliente removido com sucesso.\n");
  477. } else {
  478. printf("Cliente nao foi removido corretamente!\n");
  479. }
  480. printf("\n=======\n");
  481. free(client);
  482. }
  483. void form_client_search(char *input) {
  484. Client *client;
  485. // Antes de tudo, precisamos testar se hб algum cliente no arquivo
  486. if (clients_file_is_empty()) {
  487. printf(EMPTY_ERROR, __FILE__, "cliente");
  488. return;
  489. }
  490. printf("\n=======\nPESQUISANDO CLIENTE: \n\n");
  491. client = form_client_select(input);
  492. // Verifica se cliente й vбlido
  493. if (client->id == NON_EXIST) {
  494. free(client);
  495. return;
  496. }
  497. // Mostra o cliente
  498. puts_client_by_id(client->id);
  499. printf("\n=======\n");
  500. free(client);
  501. }
  502. Client *form_client_select(char *input) {
  503. int id;
  504. Client *client;
  505. client = client_malloc();
  506. do {
  507. printf("Digite [1] para pesquisar por ID ou [2] para pesquisar por nome: ");
  508. read_string(input);
  509. switch (*input) {
  510. case '1':
  511. // Verifica se й um ID vбlido
  512. id = check_by_id_client(input);
  513. if (id == NON_EXIST) {
  514. client->id = NON_EXIST;
  515. return client;
  516. }
  517. // Procura o cliente pelo ID
  518. client = search_client_by_id(id);
  519. *input = '1';
  520. break;
  521. case '2':
  522. // Verifica se й um nome vбlido
  523. if (!check_by_name(input)) {
  524. client->id = NON_EXIST;
  525. return client;
  526. }
  527. // Procura o cliente pelo nome
  528. client = search_client_by_name(input);
  529. *input = '2';
  530. break;
  531. default:
  532. printf("Opcao invalida!\n");
  533. }
  534. // Caso nгo ache, retorna com ID = NON_EXIST
  535. if (client->id == NON_EXIST) {
  536. if (*input == '1')
  537. printf(ID_NOT_FOUND_ERROR, __FILE__, "cliente");
  538. else if (*input == '2')
  539. printf(NAME_NOT_FOUND_ERROR, __FILE__, "cliente");
  540. client->id = NON_EXIST;
  541. return client;
  542. }
  543. } while (*input != '1' && *input != '2');
  544. return client;
  545. }