1 /* ---------- */
2 /* najijoin.c */
3 /* ---------- */
4
5 /* naji file spliting and joining functions */
6
7 /* this .c file is a part */
8 /* of libnaji version 0.6.4 */
9
10 /* libnaji is based on */
11 /* the original najitool */
12
13 /* both najitool and libnaji */
14 /* are public domain and are */
15 /* made by the same author */
16 /* please read license.txt */
17
18 /* made by NECDET COKYAZICI */
19
20 #include "libnaji.h"
21
22
filechop(long copytil,char * namein,char * nameout,char * nameout2)23 void filechop(long copytil, char *namein, char *nameout, char *nameout2)
24 {
25 long i;
26 long fsize;
27
28 int a;
29
30 najin(namein);
31 najout(nameout);
32 najout2(nameout2);
33
34 for (i=0; i<copytil; i++)
35 {
36 a = fgetc(naji_input);
37 fputc(a, naji_output);
38 }
39
40 fsize = naji_filesize(naji_input);
41
42 for (i=copytil; i<fsize; i++)
43 {
44 a = fgetc(naji_input);
45 fputc(a, naji_output2);
46 }
47
48 najinclose();
49 najoutclose();
50 najout2close();
51 }
52
filejoin(char * namein,char * namein2,char * nameout)53 void filejoin(char *namein, char *namein2, char *nameout)
54 {
55 int a=0;
56
57 najin(namein);
58 najin2(namein2);
59 najout(nameout);
60
61 loop
62 {
63 a = fgetc(naji_input);
64 if (a == EOF) endloop;
65 fputc(a, naji_output);
66 }
67
68 loop
69 {
70 a = fgetc(naji_input2);
71 if (a == EOF) endloop;
72 fputc(a, naji_output);
73 }
74
75 najinclose();
76 najin2close();
77 najoutclose();
78 }
79
80 /* todo: allow it to merge more than two files */
81
mergline(char * namein,char * namein2,char * nameout,char * beforeline,char * afterline)82 void mergline(char *namein, char *namein2, char *nameout, char *beforeline, char *afterline)
83 {
84 int a;
85
86 najin(namein);
87 najin2(namein2);
88 najout(nameout);
89
90 loop
91 {
92
93
94 fprintf(naji_output, "%s ", beforeline);
95
96
97
98 loop
99 {
100 a = fgetc(naji_input);
101
102 if (a == '\n') endloop;
103 if (a == EOF) endloop;
104
105
106 fputc(a, naji_output);
107 }
108
109
110
111 fputc(' ', naji_output);
112
113
114 loop
115 {
116 a = fgetc(naji_input2);
117
118 if (a == '\n') endloop;
119 if (a == EOF) endloop;
120 fputc(a, naji_output);
121 }
122
123
124
125 fprintf(naji_output, " %s", afterline);
126
127 fputc('\n', naji_output);
128
129
130 if (a == EOF) endloop;
131
132 }
133
134 najinclose();
135 najin2close();
136 najoutclose();
137 }
138
139
putlines(char * namein,char * nameout,char * beforeline,char * afterline)140 void putlines(char *namein, char *nameout, char *beforeline, char *afterline)
141 {
142 int a;
143
144 najin(namein);
145 najout(nameout);
146
147 loop
148 {
149
150 fprintf(naji_output, "%s ", beforeline);
151
152
153 loop
154 {
155 a = fgetc(naji_input);
156
157 if (a == '\n') endloop;
158 if (a == EOF) endloop;
159
160
161 fputc(a, naji_output);
162 }
163
164
165 fprintf(naji_output, " %s", afterline);
166
167 fputc('\n', naji_output);
168
169 if (a == EOF) endloop;
170
171 }
172
173 najinclose();
174 najoutclose();
175 }
176
177
copyoffs(char * namein,long startpos,long endpos,char * nameout)178 void copyoffs(char *namein, long startpos, long endpos, char *nameout)
179 {
180 long filesize=0;
181 long i;
182
183 najin(namein);
184 najout(nameout);
185
186 filesize=najinsize();
187
188
189 if (startpos > endpos)
190 {
191 fprintf(stderr,
192 "Error, start position cannot be greater than end possition.\n\n");
193 exit(15);
194 }
195
196 if (startpos > filesize)
197 {
198 fprintf(stderr,
199 "Error, start position is greater than the file size.\n\n");
200 exit(16);
201 }
202
203 if (endpos > filesize)
204 {
205 fprintf(stderr,
206 "Error, end position is greater than the file size.\n\n");
207 exit(17);
208 }
209
210
211 fseek(naji_input, startpos, SEEK_SET);
212
213 for (i=startpos; i<endpos; i++)
214 fputc(fgetc(naji_input), naji_output);
215
216
217 najinclose();
218 najoutclose();
219 }
220
221
222
linesnip(int bytes,char * namein,char * nameout)223 void linesnip(int bytes, char *namein, char *nameout)
224 {
225 int i;
226 int a;
227
228 najin(namein);
229 najout(nameout);
230
231
232 while(1)
233 {
234
235 a = fgetc(naji_input);
236
237 if (a == '\n')
238 {
239
240 fputc(a, naji_output);
241
242 for (i=0; i<bytes; i++)
243 {
244 a = fgetc(naji_input);
245
246 if (a == EOF)
247 {
248 najinclose();
249 najoutclose();
250 return;
251 }
252
253 }
254
255
256 }
257 else if (a == EOF) break;
258 else fputc(a, naji_output);
259 }
260
261
262 najinclose();
263 najoutclose();
264 }
265
266
mjoin(char * namein_original_filename,char * nameout_joined_output_file)267 void mjoin(char *namein_original_filename, char *nameout_joined_output_file)
268 {
269 int a;
270 unsigned long peice;
271 char mjoin_filename_buffer[4096];
272
273 peice = 0;
274 najout(nameout_joined_output_file);
275
276
277 while (1)
278 {
279
280
281 sprintf(mjoin_filename_buffer, "%s.%lu", namein_original_filename, peice);
282 najin(mjoin_filename_buffer);
283
284
285 while (1)
286 {
287 a = fgetc(naji_input);
288
289 if (a == EOF)
290 break;
291
292 fputc(a, naji_output);
293 }
294
295
296 najinclose();
297
298 peice++;
299 }
300
301
302 }
303
304
305
306
bytsplit(char * namein,unsigned long peice_size)307 void bytsplit(char *namein, unsigned long peice_size)
308 {
309 int a;
310 char bytsplit_filename_buffer[4096];
311 unsigned long size_reached;
312 unsigned long peice;
313
314
315 size_reached = 0;
316 peice = 0;
317
318 najin(namein);
319
320
321
322 while (1)
323 {
324
325
326 sprintf(bytsplit_filename_buffer, "%s.%lu", namein, peice);
327 najout(bytsplit_filename_buffer);
328
329
330 while (1)
331 {
332
333
334 a = fgetc(naji_input);
335
336 if (a == EOF)
337 break;
338
339 fputc(a, naji_output);
340
341 size_reached++;
342
343 if (size_reached == peice_size)
344 break;
345
346
347 }
348
349
350 najoutclose();
351
352 if (a == EOF)
353 break;
354
355 peice++;
356 size_reached = 0;
357 }
358
359
360
361 }
362
363
364
365
kbsplit(char * namein,unsigned long peice_size)366 void kbsplit(char *namein, unsigned long peice_size)
367 {
368 int a;
369 unsigned long i;
370 unsigned long size_reached;
371 unsigned long peice;
372 char kbsplit_filename_buffer[4096];
373
374
375 size_reached = 0;
376 peice = 0;
377
378 najin(namein);
379
380
381 while (1)
382 {
383
384
385 sprintf(kbsplit_filename_buffer, "%s.%lu", namein, peice);
386 najout(kbsplit_filename_buffer);
387
388
389 while (1)
390 {
391
392 for (i=0; i<1024; i++)
393 {
394
395 a = fgetc(naji_input);
396
397 if (a == EOF)
398 break;
399
400 fputc(a, naji_output);
401
402 }
403
404 if (a == EOF)
405 break;
406
407 size_reached++;
408
409 if (size_reached == peice_size)
410 break;
411
412
413 }
414
415
416 najoutclose();
417
418 if (a == EOF)
419 break;
420
421 peice++;
422 size_reached = 0;
423 }
424
425
426
427 }
428
429
430
431
mbsplit(char * namein,unsigned long peice_size)432 void mbsplit(char *namein, unsigned long peice_size)
433 {
434 int a;
435 unsigned long i;
436 unsigned long size_reached;
437 unsigned long peice;
438 char mbsplit_filename_buffer[4096];
439
440 size_reached = 0;
441 peice = 0;
442
443 najin(namein);
444
445 while (1)
446 {
447
448
449 sprintf(mbsplit_filename_buffer, "%s.%lu", namein, peice);
450 najout(mbsplit_filename_buffer);
451
452
453 while (1)
454 {
455
456 for (i=0; i<1048576; i++)
457 {
458
459 a = fgetc(naji_input);
460
461 if (a == EOF)
462 break;
463
464 fputc(a, naji_output);
465
466 }
467
468 if (a == EOF)
469 break;
470
471 size_reached++;
472
473 if (size_reached == peice_size)
474 break;
475
476
477 }
478
479
480 najoutclose();
481
482 if (a == EOF)
483 break;
484
485 peice++;
486 size_reached = 0;
487 }
488
489
490
491 }
492
493
494
495
gbsplit(char * namein,unsigned long peice_size)496 void gbsplit(char *namein, unsigned long peice_size)
497 {
498 int a;
499 unsigned long i;
500 unsigned long size_reached;
501 unsigned long peice;
502 char gbsplit_filename_buffer[4096];
503
504 size_reached = 0;
505 peice = 0;
506
507 najin(namein);
508
509 while (1)
510 {
511 sprintf(gbsplit_filename_buffer, "%s.%lu", namein, peice);
512 najout(gbsplit_filename_buffer);
513
514
515 while (1)
516 {
517
518 for (i=0; i<1073741824; i++)
519 {
520
521 a = fgetc(naji_input);
522
523 if (a == EOF)
524 break;
525
526 fputc(a, naji_output);
527
528 }
529
530 if (a == EOF)
531 break;
532
533 size_reached++;
534
535 if (size_reached == peice_size)
536 break;
537
538
539 }
540
541
542 najoutclose();
543
544 if (a == EOF)
545 break;
546
547 peice++;
548 size_reached = 0;
549 }
550
551
552
553 }
554