Advertisement
Ortodecimal

Untitled

Nov 20th, 2019
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.79 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #define SIZE 10
  5. typedef struct node{
  6. int line;/*Number of word's line*/
  7. int start;/*Begining of word and it's end*/
  8. char* string;/*hold pointer to the word*/
  9. struct node *left;
  10. struct node *right;
  11. } Tree;
  12. Tree *top = NULL;
  13.  
  14. int option(char* string);/*Check if arguement an option*/
  15.  
  16. int num_of_str(char* string);/*String to number*/
  17.  
  18.  
  19. int search_func(FILE* file, int diap1, int diap2, char* find_word);/*when we have num of last string*/
  20.  
  21. int find_me(Tree* top, char* find_word, int massiv[]);
  22.  
  23.  
  24. int replace_func(FILE* file, int diap1, int diap2, char* replace_word, char* include_word);
  25.  
  26. int replace_me(char** stroka, char* include, int dlina, int* count, int* size);
  27.  
  28.  
  29. int include_word_tree(char* word, Tree** top, int diap1, size_t count);
  30.  
  31. int clear_struct(Tree** top);
  32.  
  33.  
  34. int main(int argc,char** argv)
  35. {
  36. FILE* file;/*open file*/
  37. char* string;/*to read options*/
  38. char* find_word;/*is option is search*/
  39. char* replace_word;/*what to need remove*/
  40. char* include_word;/*what need to include*/
  41. int i = 0;/* to count value of options*/
  42. char c;
  43. int count_r = 0, count_s = 0, count_d = 0, diap1 = 1, diap2 = -1;
  44. if(argc < 3)
  45. {
  46. printf("Wrong input\nNo file or no options\n");
  47. return -1;
  48. }
  49.  
  50. file = fopen(argv[1], "r");
  51.  
  52. if (file == NULL)
  53. {
  54. printf("No file found\n");
  55. return -1;
  56. }
  57.  
  58. for (i = 2; i < argc; i++)/*Checking write input of arguements*/
  59. {
  60.  
  61. string = argv[i];
  62.  
  63. switch (option(string))
  64. {
  65. case 0:
  66. if ((count_s != 0) || (count_r != 0))
  67. {
  68. printf("Different options\n");
  69. fclose(file);
  70. return -1;
  71. }
  72. count_s++;
  73. i++;
  74.  
  75. if(i == argc)
  76. {
  77. printf("Wrong input\n");
  78. fclose(file);
  79. return -1;
  80. }
  81.  
  82. find_word = argv[i];
  83.  
  84. if(option(find_word) != -1)
  85. {
  86. printf("Wrong input\n");
  87. fclose(file);
  88. return -1;
  89. }
  90. break;
  91. case 1:
  92. if ((count_s != 0) || (count_r != 0))
  93. {
  94. printf("Different options\n");
  95. fclose(file);
  96. return -1;
  97. }
  98. count_r++;
  99. i++;
  100.  
  101. if(i == argc)
  102. {
  103. printf("Wrong arguements of options\n");
  104. fclose(file);
  105. return -1;
  106. }
  107.  
  108. replace_word = argv[i];
  109.  
  110. if(option(replace_word) != -1)
  111. {
  112. printf("Wrong input\n");
  113. fclose(file);
  114. return -1;
  115. }
  116.  
  117. i++;
  118.  
  119. if (i == argc)
  120. {
  121. printf("Wrong input\n");
  122. fclose(file);
  123. return -1;
  124. }
  125.  
  126. include_word = argv[i];
  127.  
  128. if(option(include_word) != -1)
  129. {
  130. printf("Wrong input\n");
  131. fclose(file);
  132. return -1;
  133. }
  134. break;
  135. case 2:
  136. if (count_d != 0)
  137. {
  138. printf("Wrong input\n");
  139. fclose(file);
  140. return -1;
  141. }
  142. count_d++;
  143. i++;
  144.  
  145. if (i == argc)
  146. {
  147. printf("Wrong input\n");
  148. fclose(file);
  149. return -1;
  150. }
  151.  
  152. if(option(argv[i]) != -1)
  153. {
  154. printf("Wrong input\n");
  155. fclose(file);
  156. return -1;
  157. }
  158.  
  159. diap1 = num_of_str(argv[i]);
  160.  
  161. if (diap1 < 1)
  162. {
  163. printf("Wrong input\n");
  164. fclose(file);
  165. return -1;
  166. }
  167.  
  168. i++;
  169.  
  170. if (i != argc)
  171. {
  172. if (option(argv[i]) == -1)
  173. {
  174. diap2 = num_of_str(argv[i]);
  175. if (diap2 < 1)
  176. {
  177. printf("Wrong input\n");
  178. fclose(file);
  179. return -1;
  180. }
  181. }
  182. else i--;
  183. }
  184. if ((diap1 > diap2) && (diap2 != -1))
  185. {
  186. printf("Wrong input\n");
  187. fclose(file);
  188. return -1;
  189. }
  190. break;
  191. default:
  192. fclose(file);
  193. printf("Wrong input\n");
  194. return -1;
  195. break;
  196. }
  197. }/*end of checking*/
  198.  
  199. i = 1;
  200.  
  201. while (i != diap1)
  202. {
  203. c = fgetc(file);
  204. if (c == '\n')
  205. i++;
  206. if (c == EOF)
  207. {
  208. printf("There is no line in this diapason\n");
  209. fclose(file);
  210. return -1;
  211. }
  212. }
  213.  
  214. if ((count_r == 0) && (count_s == 0))
  215. {
  216. printf("no needed options!\n");
  217. fclose(file);
  218. return -1;
  219. }
  220.  
  221.  
  222. if (count_r == 0)
  223. search_func(file, diap1, diap2, find_word);
  224. else replace_func(file, diap1, diap2, replace_word, include_word);
  225. fclose(file);
  226. return 0;
  227. }
  228.  
  229.  
  230. int search_func(FILE* file, int diap1, int diap2, char* find_word)
  231. {
  232. int i = 0, j = 0, size1, size2, count = 0;/*just a counter*/
  233. char* slovo;
  234. char** stroka = (char**)malloc(5*sizeof(char*));
  235. char c = ' ';
  236. int mas[5];
  237. int is_line = 0;
  238. while (c != EOF)
  239. {
  240. c = fgetc(file);
  241. if ((c == EOF) && (i == 0))
  242. {
  243. printf("There is no words\n");
  244. free(stroka);
  245. return c;
  246. }
  247. for(i = 0; i < 5; i++)
  248. stroka[i] = NULL;
  249. i = 0;
  250. while((((c != EOF) && (diap2 == -1)) || ((diap2 != -1) && (diap1 <= diap2) && (c != EOF))) && (i < 5))
  251. {
  252. if (c == '\n')
  253. {
  254. if (j != 0)
  255. {
  256. if (j == size1)
  257. {
  258. size1 *= 2;
  259. slovo = (char*)realloc(slovo,size1 * sizeof(char));
  260. }
  261. slovo[j] = '\0';
  262. include_word_tree(slovo, &top, diap1, count);
  263. }
  264. j = 0;
  265. diap1++;
  266. i++;
  267. }
  268. else if ((c == '.') || (c == ';') || (c == ',') ||
  269. (c == ' ') || (c == '-') || (c == ':') ||
  270. (c == '(') || (c == ')') ||(c < 0) || (c > 255))
  271. {
  272. if (j != 0)
  273. {
  274. if (j == size1)
  275. {
  276. size1 *= 2;
  277. slovo = (char*)realloc(slovo,size1 * sizeof(char));
  278. }
  279. slovo[j] = '\0';
  280. include_word_tree(slovo, &top, diap1, count);
  281. j = 0;
  282. }
  283. }
  284. else
  285. {
  286. if (j == 0)
  287. {
  288. size1 = SIZE;
  289. slovo = (char*)malloc(size1 * sizeof(char));
  290. }
  291. else if (j == size1)
  292. {
  293. size1 *= 2;
  294. slovo = (char*)realloc(slovo,size1 * sizeof(char));
  295. }
  296. slovo[j] = c;
  297. j++;
  298. }
  299.  
  300. if (c != '\n')
  301. {
  302. if (count == 0)
  303. {
  304. size2 = SIZE;
  305. stroka[diap1 % 5] = (char*)malloc(size2 * sizeof(char));
  306. }
  307. else if (count == size2)
  308. {
  309. size2 *= 2;
  310. stroka[diap1 % 5] = (char*)realloc(stroka[diap1 % 5], size2 * sizeof(char));
  311. }
  312. stroka[diap1 % 5][count] = c;
  313. count++;
  314. }
  315. else
  316. {
  317. if (count == size2)
  318. {
  319. size2 *= 2;
  320. stroka[(diap1 -1) % 5] = (char*)realloc(stroka[(diap1 - 1) % 5], size2 * sizeof(char));
  321. }
  322. if (stroka[(diap1 - 1) % 5] != NULL)
  323. stroka[(diap1 - 1) % 5][count] = '\0';
  324. count = 0;
  325. }
  326. if (i != 5)
  327. c = fgetc(file);
  328. }
  329. mas[4]=mas[3]=mas[2]=mas[1]=mas[0]=1;
  330. find_me(top,find_word,mas);
  331. for(i = 0; i < 5; i++)
  332. {
  333. if (mas[i] != 1)
  334. {
  335. printf("LINE IS %s\n\n", stroka[i]);
  336. is_line = 1;
  337. }
  338. if (stroka[i] != NULL)
  339. free(stroka[i]);
  340. }
  341. clear_struct(&top);
  342. }
  343. free(stroka);
  344. if (is_line == 0)
  345. printf("No line with this word!\n");
  346. return is_line;
  347. }
  348.  
  349.  
  350. int find_me(Tree* top, char* find_word, int massiv[])
  351. {
  352. while(top != NULL)
  353. {
  354. if (strcmp(find_word, (top -> string)) == 0)
  355. massiv[(top -> line) % 5] = 0;
  356. if (strcmp(find_word, top -> string) > 0)
  357. top = top -> right;
  358. else top = top -> left;
  359. }
  360. return 0;
  361. }
  362.  
  363.  
  364. int replace_func(FILE* file, int diap1, int diap2, char* replace_word, char* include_word)
  365. {
  366. int i = 0, j = 0, size1, size2, count = 0;/*just a counter*/
  367. char* slovo;
  368. char** stroka = (char**)malloc(5*sizeof(char*));
  369. char c = ' ';
  370. int mas[5];
  371. int is_line = 0;
  372. while (c != EOF)
  373. {
  374. c = fgetc(file);
  375. if ((c == EOF) && (i == 0))
  376. {
  377. printf("There are no words\n");
  378. free(stroka);
  379. return c;
  380. }
  381. for(i = 0; i < 5; i++)
  382. stroka[i] = NULL;
  383. i = 0;
  384. while((((c != EOF) && (diap2 == -1)) || ((diap2 != -1) && (diap1 <= diap2) && (c != EOF))) && (i < 5))
  385. {
  386. if (c == '\n')
  387. {
  388. if (j != 0)
  389. {
  390. if (j == size1)
  391. {
  392. size1 *= 2;
  393. slovo = (char*)realloc(slovo,size1 * sizeof(char));
  394. }
  395. slovo[j] = '\0';
  396. include_word_tree(slovo, &top, diap1, count);
  397. }
  398. if ((strcmp(replace_word, slovo) == 0) && (j != 0))
  399. replace_me(&(stroka[diap1 % 5]), include_word, strlen(slovo), &count, &size2);
  400. j = 0;
  401. diap1++;
  402. i++;
  403. }
  404. else if ((c == '.') || (c == ';') || (c == ',') ||
  405. (c == ' ') || (c == '-') || (c == ':') ||
  406. (c == '(') || (c == ')') || (c < 0) || (c > 255))
  407. {
  408. if (j != 0)
  409. {
  410. if (j == size1)
  411. {
  412. size1 *= 2;
  413. slovo = (char*)realloc(slovo,size1 * sizeof(char));
  414. }
  415. slovo[j] = '\0';
  416. include_word_tree(slovo, &top, diap1, count);
  417. }
  418. if ((strcmp(replace_word, slovo) == 0) && (j != 0))
  419. replace_me(&(stroka[diap1 % 5]), include_word, strlen(slovo), &count, &size2);
  420. j = 0;
  421. }
  422. else
  423. {
  424. if (j == 0)
  425. {
  426. size1 = SIZE;
  427. slovo = (char*)malloc(size1 * sizeof(char));
  428. }
  429. else if (j == size1)
  430. {
  431. size1 *= 2;
  432. slovo = (char*)realloc(slovo,size1 * sizeof(char));
  433. }
  434. slovo[j] = c;
  435. j++;
  436. }
  437.  
  438. if (c != '\n')
  439. {
  440. if (count == 0)
  441. {
  442. size2 = SIZE;
  443. stroka[diap1 % 5] = (char*)malloc(size2 * sizeof(char));
  444. }
  445. else if (count == size2)
  446. {
  447. size2 *= 2;
  448. stroka[diap1 % 5] = (char*)realloc(stroka[diap1 % 5], size2 * sizeof(char));
  449. }
  450. stroka[diap1 % 5][count] = c;
  451.  
  452. count++;
  453.  
  454. if (count == size2)
  455. {
  456. size2 *= 2;
  457. stroka[diap1 % 5] = (char*)realloc(stroka[diap1 % 5], size2 * sizeof(char));
  458. }
  459. stroka[diap1 % 5][count] = '\0';
  460. }
  461. else
  462. count = 0;
  463.  
  464. if (i != 5)
  465. c = fgetc(file);
  466. }
  467. mas[4]=mas[3]=mas[2]=mas[1]=mas[0]=1;
  468. find_me(top,replace_word,mas);
  469. for(i = 0; i < 5; i++)
  470. {
  471. if (mas[i] != 1)
  472. {
  473. printf("LINE IS %s\n\n", stroka[i]);
  474. is_line = 1;
  475. }
  476. if (stroka[i] != NULL)
  477. free(stroka[i]);
  478. }
  479. clear_struct(&top);
  480. }
  481. free(stroka);
  482. if (is_line == 0)
  483. printf("No line with this word!\n");
  484. return is_line;
  485. }
  486.  
  487. int replace_me(char** stroka, char* include, int dlina, int* count, int* size)
  488. {
  489. char c;
  490. int i;
  491. for(i = 0; i < strlen(include); i++)
  492. {
  493. if (((*count) - dlina + i) == (*size))
  494. {
  495. (*size) *= 2;
  496. (*stroka) = (char*)realloc((*stroka), (*size) * sizeof(char));
  497. }
  498. c = include[i];
  499. (*stroka)[(*count) - dlina + i] = c;
  500. }
  501. if (((*count) - dlina + i) == (*size))
  502. {
  503. (*size) *= 2;
  504. (*stroka) = (char*)realloc((*stroka), (*size) * sizeof(char));
  505. }
  506. (*stroka)[(*count) - dlina + i] = '\0';
  507. (*count) += strlen(include) - dlina;
  508. return 0;
  509. }
  510.  
  511. int include_word_tree(char* word, Tree** top, int diap1, size_t count)
  512. {
  513. if ((*top) == NULL)
  514. {
  515. *top = (Tree*)malloc(sizeof(Tree));
  516. (*top) -> line = diap1;
  517. (*top) -> string = word;
  518. (*top) -> start = count - strlen(word);
  519. (*top) -> left = NULL;
  520. (*top) -> right = NULL;
  521. }
  522. else if (strcmp(word, (*top) -> string) > 0)
  523. include_word_tree(word,&((*top) -> right),diap1,count);
  524. else include_word_tree(word,&((*top) -> left), diap1,count);
  525. return 0;
  526. }
  527.  
  528. int option(char* string)
  529. {
  530. char** massiv;
  531. int i, j;
  532. massiv = (char**)malloc(6 * sizeof(char*));
  533. massiv[0] = "-s";
  534. massiv[1] = "-r";
  535. massiv[2] = "-d";
  536. massiv[3] = "--search";
  537. massiv[4] = "--replace";
  538. massiv[5] = "--diapason";
  539. for (i = 0;i != 6; i++)
  540. {
  541. if (strcmp(string,massiv[i]) == 0)
  542. {
  543. switch (i)
  544. {
  545. case 0:
  546. case 3:
  547. j = 0;
  548. break;
  549. case 1:
  550. case 4:
  551. j = 1;
  552. break;
  553. case 2:
  554. case 5:
  555. j = 2;
  556. break;
  557. }
  558. free(massiv);
  559. return j;
  560. }
  561. }
  562. free(massiv);
  563. return -1;
  564. }
  565.  
  566. int num_of_str(char* string)
  567. {
  568. int i = 0;
  569. if (string == NULL)
  570. {
  571. printf("Where is argument?\n");
  572. return -1;
  573. }
  574.  
  575. while(*string != '\0')
  576. {
  577. if ((*string < '0') || (*string > '9'))
  578. return -1;
  579. i = i*10 + *string - '0';
  580. string++;
  581. }
  582. return i;
  583. }
  584.  
  585. int clear_struct(Tree** top)
  586. {
  587. if (*top != NULL)
  588. {
  589. clear_struct(&((*top) -> left));
  590. clear_struct(&((*top) -> right));
  591. free((*top) -> string);
  592. free(*top);
  593. *top = NULL;
  594. }
  595. return 0;
  596. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement