PageRenderTime 28ms CodeModel.GetById 41ms RepoModel.GetById 0ms app.codeStats 0ms

/libnaji.src/najio.c

http://github.com/najitool/najitool-libnaji-latest-code
C | 810 lines | 499 code | 253 blank | 58 comment | 124 complexity | da6c0fd60117ab2ad40042011586c0b1 MD5 | raw file
  1. /* ------- */
  2. /* najio.c */
  3. /* ------- */
  4. /* naji input/output functions */
  5. /* used by najitool and libnaji */
  6. /* this .c file is a part */
  7. /* of libnaji version 0.6.4 */
  8. /* libnaji is based on */
  9. /* the original najitool */
  10. /* both najitool and libnaji */
  11. /* are public domain and are */
  12. /* made by the same author */
  13. /* please read license.txt */
  14. /* made by NECDET COKYAZICI */
  15. #include "libnaji.h"
  16. FILE *naji_input;
  17. FILE *naji_input2;
  18. FILE *naji_output;
  19. FILE *naji_output2;
  20. FILE *naji_edit;
  21. long naji_filesize(FILE *file)
  22. {
  23. long savepos=0;
  24. long size=0;
  25. savepos = ftell(file);
  26. fseek(file, 0 , SEEK_END);
  27. size = ftell(file);
  28. fseek(file, savepos, SEEK_SET);
  29. return size;
  30. }
  31. /* you might want to make your own najin and najout functions */
  32. /* depending on what you need, for example it will probably */
  33. /* be better to change these functions for your GUI programs */
  34. void najin(char *namein)
  35. {
  36. int a;
  37. naji_input = fopen(namein, "rb");
  38. if (naji_input == NULL)
  39. {
  40. fprintf(stderr, "\n\nError, cannot open input file: %s", namein);
  41. perror(" "); fprintf(stderr, "\n\n");
  42. exit(2);
  43. }
  44. a = fgetc(naji_input);
  45. if (a == EOF)
  46. {
  47. fprintf(stderr, "\n\nError, empty file.\n\n");
  48. exit(1);
  49. }
  50. else
  51. {
  52. najinclose();
  53. naji_input = fopen(namein, "rb");
  54. if (naji_input == NULL)
  55. {
  56. fprintf(stderr, "\n\nError, cannot open input file: %s", namein);
  57. perror(" "); fprintf(stderr, "\n\n");
  58. exit(2);
  59. }
  60. }
  61. }
  62. void najintext(char *namein)
  63. {
  64. int a;
  65. naji_input = fopen(namein, "rt");
  66. if (naji_input == NULL)
  67. {
  68. fprintf(stderr, "\n\nError, cannot open input file: %s", namein);
  69. perror(" "); fprintf(stderr, "\n\n");
  70. exit(2);
  71. }
  72. a = fgetc(naji_input);
  73. if (a == EOF)
  74. {
  75. fprintf(stderr, "\n\nError, empty file.\n\n");
  76. exit(1);
  77. }
  78. else
  79. {
  80. najinclose();
  81. naji_input = fopen(namein, "rb");
  82. if (naji_input == NULL)
  83. {
  84. fprintf(stderr, "\n\nError, cannot open input file: %s", namein);
  85. perror(" "); fprintf(stderr, "\n\n");
  86. exit(2);
  87. }
  88. }
  89. }
  90. void najed(char *named)
  91. {
  92. naji_edit = fopen(named, "r+b");
  93. if (naji_edit == NULL)
  94. {
  95. fprintf(stderr, "\n\nError, cannot open edit file: %s", named);
  96. perror(" "); fprintf(stderr, "\n\n");
  97. exit(6);
  98. }
  99. }
  100. void najin2(char *namein2)
  101. {
  102. int a;
  103. naji_input2 = fopen(namein2, "rb");
  104. if (naji_input2 == NULL)
  105. {
  106. fprintf(stderr, "\n\nError, cannot open second input file: %s", namein2);
  107. perror(" "); fprintf(stderr, "\n\n");
  108. exit(2);
  109. }
  110. a = fgetc(naji_input2);
  111. if (a == EOF)
  112. {
  113. fprintf(stderr, "\n\nError, empty file.\n\n");
  114. exit(1);
  115. }
  116. else
  117. {
  118. najinclose();
  119. naji_input2 = fopen(namein2, "rb");
  120. if (naji_input2 == NULL)
  121. {
  122. fprintf(stderr, "\n\nError, cannot open second input file: %s", namein2);
  123. perror(" "); fprintf(stderr, "\n\n");
  124. exit(2);
  125. }
  126. }
  127. }
  128. void najout(char *nameout)
  129. {
  130. int choice;
  131. char buffer[1050];
  132. naji_output = fopen(nameout, "rb");
  133. if (naji_output != NULL)
  134. {
  135. fprintf(stderr, "\n\nError, output file already exists: %s\n\n", nameout);
  136. while (1)
  137. {
  138. fprintf(stderr, "[Q]uit - [R]ename - [O]verwrite ?\n\n");
  139. choice = fgetc(stdin);
  140. if (choice == 'q' || choice == 'Q')
  141. {
  142. fclose(naji_output);
  143. exit(3);
  144. }
  145. else if (choice == 'r' || choice == 'R')
  146. {
  147. fprintf(stderr, "\nType new name for output file: ");
  148. fgetc(stdin);
  149. safegets(buffer, 1024);
  150. fclose(naji_output);
  151. if (rename(nameout, buffer) != 0)
  152. {
  153. perror("\n\nError renaming output file");
  154. fclose(naji_output);
  155. exit(3);
  156. }
  157. else break;
  158. }
  159. else if (choice == 'o' || choice == 'O')
  160. {
  161. fclose(naji_output);
  162. break;
  163. }
  164. else continue;
  165. }
  166. }
  167. naji_output = fopen(nameout, "wb");
  168. if (naji_output == NULL)
  169. {
  170. fprintf(stderr, "\n\nError, cannot open output file: %s", nameout);
  171. perror(" "); fprintf(stderr, "\n\n");
  172. exit(4);
  173. }
  174. }
  175. void najout2(char *nameout2)
  176. {
  177. int choice;
  178. char buffer[1050];
  179. naji_output2 = fopen(nameout2, "rb");
  180. if (naji_output2 != NULL)
  181. {
  182. fprintf(stderr, "\n\nError, second output file already exists: %s\n\n",
  183. nameout2);
  184. while (1)
  185. {
  186. fprintf(stderr, "[Q]uit - [R]ename - [O]verwrite ?\n\n");
  187. choice = fgetc(stdin);
  188. if (choice == 'q' || choice == 'Q')
  189. {
  190. fclose(naji_output2);
  191. exit(3);
  192. }
  193. else if (choice == 'r' || choice == 'R')
  194. {
  195. fprintf(stderr, "\nType new name for second output file: ");
  196. fgetc(stdin);
  197. safegets(buffer, 1024);
  198. fclose(naji_output2);
  199. if (rename(nameout2, buffer) != 0)
  200. {
  201. perror("\n\nError renaming second output file");
  202. fclose(naji_output2);
  203. exit(3);
  204. }
  205. else break;
  206. }
  207. else if (choice == 'o' || choice == 'O')
  208. {
  209. fclose(naji_output2);
  210. break;
  211. }
  212. else continue;
  213. }
  214. }
  215. naji_output2 = fopen(nameout2, "wb");
  216. if (naji_output2 == NULL)
  217. {
  218. fprintf(stderr, "\n\nError, cannot open output file: %s", nameout2);
  219. perror(" "); fprintf(stderr, "\n\n");
  220. exit(4);
  221. }
  222. }
  223. long najinsize(void) { return naji_filesize(naji_input); }
  224. long najin2size(void) { return naji_filesize(naji_input2); }
  225. long najoutsize(void) { return naji_filesize(naji_output); }
  226. long najout2size(void) { return naji_filesize(naji_output2); }
  227. long najedsize(void) { return naji_filesize(naji_edit); }
  228. void najinclose(void) { fclose(naji_input); }
  229. void najin2close(void) { fclose(naji_input2); }
  230. void najoutclose(void) { fclose(naji_output); }
  231. void najout2close(void) { fclose(naji_output2); }
  232. void najedclose(void) { fclose(naji_edit); }
  233. void copyfile(char *namein, char *nameout)
  234. {
  235. int a=0;
  236. najin(namein);
  237. najout(nameout);
  238. loop
  239. {
  240. a = fgetc(naji_input);
  241. if (a == EOF) endloop;
  242. fputc(a, naji_output);
  243. }
  244. najinclose();
  245. najoutclose();
  246. }
  247. /* todo: allow it to copy unlimited
  248. number of files file not opening them
  249. all at the same time, open them one by one */
  250. /* append files */
  251. /*
  252. void apfiles(...)
  253. {
  254. }
  255. */
  256. /* copies while flipping the bits of each byte, for example
  257. 11110000 becomes
  258. 00001111
  259. and
  260. 01010101 becomes
  261. 10101010 etc
  262. */
  263. void flipcopy(char *namein, char *nameout)
  264. {
  265. int a=0;
  266. najin(namein);
  267. najout(nameout);
  268. loop
  269. {
  270. a = fgetc(naji_input);
  271. if (a == EOF) endloop;
  272. fputc((~a), naji_output);
  273. }
  274. najinclose();
  275. najoutclose();
  276. }
  277. /*
  278. void copyrep(unsigned long int howmany)
  279. {
  280. unsigned long int i=0;
  281. int a=0;
  282. howmany++;
  283. for (i=0; i < howmany; i++)
  284. {
  285. loop
  286. {
  287. a = fgetc(naji_input);
  288. if (a == EOF) endloop;
  289. fputc(a, naji_output);
  290. }
  291. }
  292. }
  293. */
  294. void safegets(char *buf, int size)
  295. {
  296. int a;
  297. int i=0;
  298. while(1)
  299. {
  300. a = fgetc(stdin);
  301. if (i == size)
  302. {
  303. buf[size] = '\0';
  304. if ( (a == '\n') || (a == '\r') ) return;
  305. }
  306. if (i < size)
  307. {
  308. if ( (a == '\n') || (a == '\r') )
  309. {
  310. buf[i]='\0';
  311. return;
  312. }
  313. if ( (a > 31) && (a < 127) )
  314. {
  315. buf[i]=a;
  316. i++;
  317. }
  318. }
  319. }
  320. }
  321. void nextpage(void)
  322. {
  323. printf("\n- Press ENTER for the next page...\n");
  324. getchar();
  325. }
  326. /* finds the longest line in a text file and returns the result */
  327. unsigned long longl(char *namein)
  328. {
  329. int a;
  330. unsigned long length = 0;
  331. unsigned long longest = 0;
  332. najin(namein);
  333. while (1)
  334. {
  335. a = getc(naji_input);
  336. if (a == EOF)
  337. break;
  338. if (a == '\n')
  339. {
  340. if (length > longest)
  341. longest = length;
  342. length=0;
  343. }
  344. else length++;
  345. }
  346. najinclose();
  347. return longest;
  348. }
  349. void longline(char *namein)
  350. {
  351. printf("\n\nLongest line is: %lu\n\n", longl(namein));
  352. }
  353. /* counts how many lines there are in a text file and returns the result */
  354. unsigned long howl(char *namein)
  355. {
  356. int a;
  357. unsigned long lines = 0;
  358. najin(namein);
  359. while (1)
  360. {
  361. a = getc(naji_input);
  362. if (a == EOF)
  363. break;
  364. if (a == '\n')
  365. lines++;
  366. }
  367. najinclose();
  368. return lines;
  369. }
  370. void howline(char *namein)
  371. {
  372. printf("\n\nTotal number of lines is: %lu\n\n", howl(namein));
  373. }
  374. char **naji_lines_alloc(unsigned long howmany, unsigned long howlong)
  375. {
  376. char **buffer = NULL;
  377. unsigned long i;
  378. buffer = (char **) malloc(howmany * sizeof(char *));
  379. exitnull(buffer);
  380. for (i=0; i<howmany; i++)
  381. {
  382. buffer[i] = (char *) malloc(howlong * sizeof(char) + 3);
  383. exitnull(buffer[i]);
  384. }
  385. return buffer;
  386. }
  387. void naji_lines_free(char **buffer, unsigned long howmany)
  388. {
  389. unsigned long i;
  390. for (i=0; i<howmany; i++)
  391. free(buffer[i]);
  392. free(buffer);
  393. buffer = NULL;
  394. }
  395. void naji_lines_load(char *namein, char **buffer, unsigned long howmany, unsigned long howlong)
  396. {
  397. int a;
  398. unsigned long i = 0;
  399. unsigned long c = 0;
  400. najintext(namein);
  401. while (1)
  402. {
  403. a = fgetc(naji_input);
  404. if (a == EOF)
  405. {
  406. buffer[i][c] = '\0';
  407. break;
  408. }
  409. else if (a == '\n')
  410. {
  411. buffer[i][c] = '\n';
  412. c++;
  413. buffer[i][c] = '\0';
  414. c++;
  415. i++;
  416. c = 0;
  417. if (i >= howmany)
  418. break;
  419. }
  420. else if (a == '\r')
  421. ;
  422. else
  423. {
  424. buffer[i][c] = a;
  425. c++;
  426. if (c == howlong)
  427. {
  428. buffer[i][c] = '\0';
  429. break;
  430. }
  431. }
  432. }
  433. najinclose();
  434. }
  435. void naji_lines_backwards_print(char **buffer, unsigned long howmany)
  436. {
  437. signed long backwards_howmany = 0;
  438. signed long backwards_i = 0;
  439. backwards_howmany = (signed long) howmany;
  440. backwards_howmany--;
  441. if (strlen(buffer[backwards_howmany]) > 0)
  442. if (strchr(buffer[backwards_howmany], '\n') == NULL)
  443. {
  444. printf("%s\n", buffer[backwards_howmany]);
  445. backwards_howmany--;
  446. }
  447. for (backwards_i = backwards_howmany; backwards_i >= 0; backwards_i--)
  448. printf("%s", buffer[backwards_i]);
  449. }
  450. void naji_lines_print(char **buffer, unsigned long howmany)
  451. {
  452. unsigned long i;
  453. for (i=0; i<howmany; i++)
  454. printf("%s", buffer[i]);
  455. }
  456. void lineback(char *namein)
  457. {
  458. char **buffer = NULL;
  459. unsigned long howmany;
  460. unsigned long howlong;
  461. howmany = howl(namein);
  462. howlong = longl(namein);
  463. howlong += 3;
  464. howmany ++;
  465. buffer = naji_lines_alloc(howmany, howlong);
  466. naji_lines_load(namein, buffer, howmany, howlong);
  467. naji_lines_backwards_print(buffer, howmany);
  468. naji_lines_free(buffer, howmany);
  469. }
  470. int return_random(int max)
  471. {
  472. int random_number;
  473. int limit;
  474. limit = RAND_MAX - RAND_MAX % max;
  475. do random_number = rand(); while (random_number >= limit);
  476. return random_number % max;
  477. }
  478. void shuffle_int_array(int *array, int size)
  479. {
  480. int a;
  481. int b;
  482. int c;
  483. size--;
  484. srand(time(NULL));
  485. for (a=size; a>0; a--)
  486. {
  487. b = return_random(a + 1);
  488. c = array[b];
  489. array[b] = array[a];
  490. array[a] = c;
  491. }
  492. }
  493. void naji_lines_random_print(char **buffer, int howmany)
  494. {
  495. unsigned long i = 0;
  496. int *vektor = NULL;
  497. vektor = (int *) malloc(howmany * sizeof (int));
  498. exitnull(vektor)
  499. for (i=0; i<howmany; i++)
  500. vektor[i] = i;
  501. shuffle_int_array(vektor, howmany);
  502. for (i=0; i<howmany; i++)
  503. printf("%s", buffer[vektor[i]]);
  504. free(vektor);
  505. vektor = NULL;
  506. }
  507. void rndlines(char *namein)
  508. {
  509. char **buffer = NULL;
  510. unsigned long howmany;
  511. unsigned long howlong;
  512. howmany = howl(namein);
  513. howlong = longl(namein);
  514. howlong += 3;
  515. howmany ++;
  516. buffer = naji_lines_alloc(howmany, howlong);
  517. naji_lines_load(namein, buffer, howmany, howlong);
  518. naji_lines_random_print(buffer, howmany);
  519. naji_lines_free(buffer, howmany);
  520. }
  521. void najifgets(char *buf, int size, FILE *input)
  522. {
  523. int a;
  524. int i=0;
  525. while(1)
  526. {
  527. a = fgetc(naji_input);
  528. if (a == EOF)
  529. {
  530. buf[i] = '\0';
  531. return;
  532. }
  533. if (i == size)
  534. {
  535. buf[i+1] = '\0';
  536. return;
  537. }
  538. if (a == '\n')
  539. {
  540. buf[i] = '\n';
  541. buf[i+1] = '\0';
  542. return;
  543. }
  544. else
  545. {
  546. buf[i] = a;
  547. buf[i+1] = '\0';
  548. }
  549. i++;
  550. }
  551. }