-
Notifications
You must be signed in to change notification settings - Fork 1
/
io.c
2134 lines (1901 loc) · 61.7 KB
/
io.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/****************************************************************
io.c
=============================================================
Copyright 1996-2013 Tom Barbalet. All rights reserved.
Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or
sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
This software and Noble Ape are a continuing work of Tom Barbalet,
begun on 13 June 1996. No apes or cats were harmed in the writing
of this software.
****************************************************************/
/*! \file io.c
* \brief Covers the low level input and output relating to memory and files.
In addition to memory and file handling, io has typically been a place holder
for new functionality.
*/
/*NOBLEMAKE DEL=""*/
#include "noble.h"
/*NOBLEMAKE END=""*/
/*NOBLEMAKE VAR=""*/
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
/*NOBLEMAKE END=""*/
static n_int command_line_execution;
static n_int command_line_external_exit = 0;
void io_command_line_execution_set(void)
{
command_line_execution = 1;
}
n_int io_command_line_execution(void)
{
return command_line_execution;
}
/**
* This is a single value store used to identify if the console execution is used with the GUI version.
* @param value entering value.
* @return previously held value.
*/
void io_entry_execution(n_int argc, n_string * argv)
{
if (argv)
{
if ((argc == 2) && (argv[1][0] == 'c'))
{
io_command_line_execution_set();
}
}
}
n_int file_chain_write_generate_header(n_file_chain *initial)
{
n_uint * hash_data;
n_uint count = 0;
n_file_chain * local = initial;
n_file_chain * header;
if (local == 0L)
{
return SHOW_ERROR("Filechain Format Does not Exist");
}
header = local;
local = (n_file_chain*)header->next;
if (local == 0L)
{
return SHOW_ERROR("Filechain Format Empty");
}
do {
count++;
local = (n_file_chain*)local->next;
} while (local != 0L);
header->data = io_new(count*sizeof(n_uint));
header->expected_bytes = count*sizeof(n_uint);
hash_data = (n_uint*) header->data;
if (hash_data == 0L)
{
return SHOW_ERROR("Header not allocated");
}
local = (n_file_chain*)header->next;
count = 0;
do {
hash_data[count] = math_hash(local->data,local->expected_bytes);
count++;
local = (n_file_chain*)local->next;
} while (local != 0L);
return 0;
}
n_file_chain * file_chain_new(n_uint size)
{
n_file_chain * return_value = (n_file_chain*)io_new((size+1)*sizeof(n_file_chain));
if (return_value)
{
n_int loop = 0;
while (loop < (size+1))
{
return_value[loop].data = 0L;
return_value[loop].expected_bytes = 0;
if (loop == size)
{
return_value[loop].next = 0L;
}
else
{
return_value[loop].next = (void*)&return_value[loop+1];
}
loop++;
}
}
return return_value;
}
void file_chain_free(n_file_chain * value)
{
/* clean up header */
if (value->data != 0L)
{
io_free(value->data);
}
/* clean up chain */
if (value != 0L)
{
io_free(value);
}
}
n_int file_chain_write(n_string name, n_file_chain * initial)
{
FILE * write_file = 0L;
n_file_chain * local = initial;
write_file = fopen(name, "wb");
if (write_file == 0L)
{
return SHOW_ERROR("File could not be created");
}
do {
fwrite(local->data,1,local->expected_bytes, write_file);
local = (n_file_chain*)local->next;
} while (local != 0L);
fclose(write_file);
return 0;
}
n_int file_chain_read(n_string name, n_file_chain * initial)
{
FILE * read_file = 0L;
n_file_chain * local = initial;
read_file = fopen(name, "rb");
if (read_file == 0L)
{
return SHOW_ERROR("File could not be read");
}
do {
fread(local->data,1,local->expected_bytes, read_file);
local = (n_file_chain*)local->next;
} while (local != 0L);
fclose(read_file);
return 0;
}
n_int file_chain_read_header(n_string name, n_file_chain * header, n_uint expected_additional_entries)
{
FILE * read_file = 0L;
n_uint * hash_data = 0L;
n_uint expected_size;
n_uint actual_size;
n_uint loop = 0;
read_file = fopen(name,"rb");
if (read_file == 0L)
{
return SHOW_ERROR("File does not exist");
}
if (header == 0L)
{
fclose(read_file);
return SHOW_ERROR("No header presented");
}
expected_size = expected_additional_entries * sizeof(n_uint);
header->data = 0L;
header->data = io_new(expected_size);
if (header->data == 0L)
{
fclose(read_file);
return SHOW_ERROR("No hash allocated");
}
header->expected_bytes = expected_size;
hash_data = (n_uint*)header->data;
actual_size = fread(hash_data, 1, expected_additional_entries*sizeof(n_uint), read_file);
if (expected_size != actual_size)
{
io_free(hash_data);
fclose(read_file);
return SHOW_ERROR("File too short");
}
while (loop < expected_additional_entries)
{
header[loop+1].hash = hash_data[loop];
loop++;
}
fclose(read_file);
return 0;
}
n_int file_chain_read_validate(n_string name, n_file_chain *initial)
{
n_file_chain * local = initial;
n_uint largest_bytes = 0;
n_byte * general_buffer = 0L;
FILE * read_file = 0L;
n_int count = 0;
do {
if (local->expected_bytes > largest_bytes)
{
largest_bytes = local->expected_bytes;
}
local = (n_file_chain*)local->next;
} while (local != 0L);
read_file = fopen(name,"rb");
if (read_file == 0L)
{
return SHOW_ERROR("File does not exist");
}
general_buffer = io_new(largest_bytes);
if (general_buffer == 0L)
{
fclose(read_file);
return SHOW_ERROR("Validation buffer not created");
}
/* get past header */
local = initial;
if (local == 0L)
{
fclose(read_file);
return SHOW_ERROR("File chain invalid");
}
do {
n_uint actual_read = fread(general_buffer, 1, local->expected_bytes, read_file);
n_uint actual_hash;
if (actual_read != local->expected_bytes)
{
fclose(read_file);
io_free(general_buffer);
return SHOW_ERROR("File too short");
}
actual_hash = math_hash(general_buffer, local->expected_bytes);
if (local != initial)
{
if (actual_hash != local->hash)
{
n_string_block combination;
fclose(read_file);
io_free(general_buffer);
sprintf(combination, "Hash failed (# %ld, actual %ld, expected %ld, bytes %ld, sizeof %ld)",count, actual_hash, local->hash, local->expected_bytes, sizeof(n_int));
return SHOW_ERROR(combination);
}
}
local = (n_file_chain*)local->next;
count++;
} while (local != 0L);
fclose(read_file);
io_free(general_buffer);
return 0;
}
void file_chain_bin_name(n_string original, n_string bin_file)
{
sprintf(bin_file, "%s.bin",original);
}
void io_aiff_header(n_byte * header)
{
header[0] = 'F';
header[1] = 'O';
header[2] = 'R';
header[3] = 'M';
header[8] = 'A';
header[9] = 'I';
header[10] = 'F';
header[11] = 'F';
header[12] = 'C';
header[13] = 'O';
header[14] = 'M';
header[15] = 'M';
header[19] = 18;
header[21] = 1;
header[27] = 16;
header[28] = 0x40;
header[29] = 0x0e;
header[30] = 0xac;
header[31] = 0x44;
header[38] = 'S';
header[39] = 'S';
header[40] = 'N';
header[41] = 'D';
}
void io_aiff_uint(n_byte * buffer, n_uint value)
{
buffer[0] = (value & 0xff000000) >> 24;
buffer[1] = (value & 0x00ff0000) >> 16;
buffer[2] = (value & 0x0000ff00) >> 8;
buffer[3] = (value & 0x000000ff) >> 0;
}
n_uint io_aiff_uint_out(n_byte * buffer)
{
return (buffer[0] << 24) | (buffer[1] << 16) | (buffer[2] << 8) | buffer[3];
}
n_uint io_aiff_total_size(n_uint total_samples)
{
return 4 + 8 + 18 + 8 + (2 * total_samples) + 8;
}
n_uint io_aiff_sound_size(n_uint total_samples)
{
return (2 * total_samples) + 8;
}
n_int io_aiff_sample_size(n_uint total_size)
{
n_int total_samples2 = ((n_int)total_size) - 4 - 8 - 18 - 8 - 8;
if (total_samples2 < 0)
{
return SHOW_ERROR("Total AIFF samples less than zero");
}
if (total_samples2 & 1)
{
return SHOW_ERROR("Non multiple of 2 in AIFF file size");
}
return total_samples2 >> 1;
}
static n_int io_scan(n_byte * v1, n_byte * v2, n_uint start, n_uint stop)
{
n_int loop = start;
while (loop < stop)
{
if (v1[loop] != v2[loop])
{
return 1;
}
loop++;
}
return 0;
}
n_int io_aiff_header_check_length(n_byte * header)
{
n_byte comparison[54];
n_uint total_samples, sound_size;
io_aiff_header(comparison);
if (io_scan(comparison, header, 0, 4))
{
return SHOW_ERROR("AIFF fails first section");
}
if (io_scan(comparison, header, 8, 16))
{
return SHOW_ERROR("AIFF fails second section");
}
if (io_scan(comparison, header, 21, 22))
{
return SHOW_ERROR("AIFF fails third section");
}
total_samples = io_aiff_uint_out(&header[22]);
if (io_scan(comparison, header, 27, 32))
{
return SHOW_ERROR("AIFF fails fourth section");
}
if (io_scan(comparison, header, 38, 42))
{
return SHOW_ERROR("AIFF fails fifth section");
}
sound_size = io_aiff_uint_out(&header[42]);
if (io_scan(comparison, header, 46, 48))
{
return SHOW_ERROR("AIFF fails sixth section");
}
/* if (total_size != io_aiff_total_size(total_samples)) - this is not a valid comparison */
if (sound_size != io_aiff_sound_size(total_samples))
{
return SHOW_ERROR("AIFF fails sound size compare");
}
return total_samples;
}
n_int io_aiff_test(void * ptr, n_string response, n_console_output output_function)
{
FILE * test_file = fopen(response, "rb");
n_byte header[54]={0};
if (test_file == 0L)
{
(void)SHOW_ERROR("AIFF test open failed");
}
else
{
n_string_block output;
n_int samples;
fread(header, 1, 54, test_file);
samples = io_aiff_header_check_length(header);
sprintf(output, "%ld\n",samples);
output_function(output);
fclose(test_file);
}
return 0;
}
/**
* This is a historical legacy function as all platforms now use memcpy. Although in the future this may change.
* @param from pointer to copy from.
* @param to pointer to copy to.
* @param number the number of bytes to copy.
*/
void io_copy(n_byte * from, n_byte * to, n_uint number)
{
memcpy(to, from, number);
}
/**
* This is a historical legacy function as all platforms now use malloc. Although in the future this may change.
* @param bytes number of bytes to allocate.
* @return a void* pointer of the allocated bytes.
*/
void * io_new(n_uint bytes)
{
void * tmp = 0L;
if (bytes)
{
tmp = (void *) malloc(bytes);
}
return (tmp);
}
/**
* This is a historical legacy function as all platforms now use free. Although in the future this may change.
* @param ptr the void * pointer to be freed. Should really be a void ** to catch the 0L-ing.
*/
void io_free(void * ptr)
{
if (ptr != 0L)
{
free(ptr);
ptr = 0L;
}
}
/**
* This is allocates a range of memory depending on availability.
* @param memory_min the minimum possible allocated memory before returning 0L.
* @param memory_allocated the starting value for memory size and returning the actual size.
* @return a void* pointer of the allocated bytes.
*/
void * io_new_range(n_uint memory_min, n_uint *memory_allocated)
{
void * memory_buffer = 0L;
do
{
memory_buffer = (void *) malloc(*memory_allocated);
if (memory_buffer == 0L)
{
*memory_allocated = ((*memory_allocated) * 3) >> 2;
}
}
while((memory_buffer == 0L) && ((*memory_allocated)>memory_min));
return memory_buffer;
}
/**
* Moves the string to lower case for the length shown.
* @param value the string with the case to be lowered.
* @param length the number of bytes to be lowered.
* @return a void* pointer of the allocated bytes.
*/
void io_lower(n_string value, n_int length)
{
n_int loop = 0;
while (loop < length)
{
IO_LOWER_CHAR(value[loop]);
loop++;
}
}
/**
* Allocates a new file.
* @return a 4k worth of data file pointer.
*/
n_file * io_file_new(void)
{
n_file * output = io_new(sizeof(n_file));
if (output == 0L)
{
return 0L;
}
output->size = 4096;
output->data = io_new(4096);
if (output->data == 0L)
{
io_free(output);
return 0L;
}
output->location = 0;
return output;
}
/**
* Frees the file pointer
* @param file the pointer to be freed.
*/
void io_file_free(n_file * file)
{
if (file != 0L)
{
io_free(file->data);
}
io_free(file);
}
/**
* Reads a file from disk.
* @param local_file the pointer to the n_file data that will have the file stored in it.
* @param file_name the name of the file to be read.
* @return FILE_ERROR if there is a problem and FILE_OKAY if it is successful.
*/
n_int io_disk_read(n_file * local_file, n_string file_name)
{
#ifndef _WIN32
FILE * in_file = fopen(file_name,"rb");
#else
FILE * in_file = 0L;
fopen_s(&in_file,file_name,"rb");
#endif
if (in_file == 0L)
{
return SHOW_ERROR("File does not exist");
}
while (!feof(in_file))
{
n_byte local_char;
if (fread(&local_char,1,1,in_file)>0)
{
if (!feof(in_file))
{
n_int error = io_file_write(local_file, local_char);
if (error != 0)
{
return FILE_ERROR;
}
}
}
}
fclose(in_file);
return FILE_OKAY;
}
/**
* Writes a file to disk.
* @param local_file the pointer to the n_file data that is written to disk.
* @param file_name the name of the file to be written.
* @return FILE_ERROR if there is a problem and FILE_OKAY if it is successful.
*/
n_int io_disk_write(n_file * local_file, n_string file_name)
{
n_uint written_length;
FILE * out_file = 0L;
#ifndef _WIN32
out_file = fopen(file_name,"wb");
#else
fopen_s(&out_file,file_name,"wb");
#endif
if (out_file == 0L)
{
return SHOW_ERROR("Error opening file to write");
}
if (local_file->data == 0L)
{
return SHOW_ERROR("No data in file to be written");
}
written_length = fwrite(local_file->data,1,local_file->location, out_file);
fclose(out_file);
if (written_length != local_file->location)
{
return SHOW_ERROR("File did not complete write");
}
return FILE_OKAY;
}
/**
* Appends a file to disk.
* @param local_file the pointer to the n_file data that is written to disk.
* @param file_name the name of the file to be appended.
* @return FILE_ERROR if there is a problem and FILE_OKAY if it is successful.
*/
n_int io_disk_append(n_file * local_file, n_string file_name)
{
n_uint written_length;
#ifndef _WIN32
FILE * out_file = fopen(file_name,"a");
#else
FILE * out_file = 0L;
fopen_s(&out_file,file_name,"a");
#endif
written_length = fwrite(local_file->data,1,local_file->location, out_file);
fclose(out_file);
if (written_length != local_file->location)
{
return SHOW_ERROR("File did not complete write");
}
return FILE_OKAY;
}
/**
* Appends a file to disk.
* @param local_file the pointer to the n_file data that is written to disk.
* @param file_name the name of the file to be appended.
* @return FILE_ERROR if there is a problem and FILE_OKAY if it is successful.
*/
n_int io_disk_check(n_string file_name)
{
#ifndef _WIN32
FILE * local_check_file = fopen(file_name,"rb");
#else
FILE * local_check_file = 0L;
fopen_s(&local_check_file,file_name,"rb");
#endif
if (local_check_file == 0L)
return 0;
fclose(local_check_file);
return 1;
}
/**
* Adds XML open to the named string.
* @param file the pointer to the n_file data that is written.
* @param name the string that is wrapped.
* @return FILE_ERROR if there is a problem and FILE_OKAY if it is successful.
*/
n_int io_file_xml_open(n_file * file, n_string name)
{
if (io_write(file,"<", 0) == -1) return -1;
if (io_write(file,name, 0) == -1) return -1;
if (io_write(file,">", 1) == -1) return -1;
return 0;
}
/**
* Adds XML close to the named string.
* @param file the pointer to the n_file data that is written.
* @param name the string that is wrapped.
* @return FILE_ERROR if there is a problem and FILE_OKAY if it is successful.
*/
n_int io_file_xml_close(n_file * file, n_string name)
{
if (io_write(file,"</", 0) == -1) return -1;
if (io_write(file,name, 0) == -1) return -1;
if (io_write(file,">", 1) == -1) return -1;
return 0;
}
/**
* Wraps a string with XML open and close
* @param file the pointer to the n_file data that is written.
* @param name the string that is the wrapper.
* @param string the string that is wrapped.
* @return FILE_ERROR if there is a problem and FILE_OKAY if it is successful.
*/
n_int io_file_xml_string(n_file * file, n_string name, n_string string)
{
if (io_file_xml_open(file,name) == -1) return -1;
if (io_write(file,string, 0) == -1) return -1;
if (io_file_xml_close(file,name) == -1) return -1;
return 0;
}
/**
* Wraps an integer with XML open and close
* @param file the pointer to the n_file data that is written.
* @param name the string that is the wrapper.
* @param number the integer that is wrapped.
* @return FILE_ERROR if there is a problem and FILE_OKAY if it is successful.
*/
n_int io_file_xml_int(n_file * file, n_string name, n_int number)
{
if (io_file_xml_open(file,name) == -1) return -1;
if (io_writenumber(file, number, 1, 0) == -1) return -1;
if (io_file_xml_close(file,name) == -1) return -1;
return 0;
}
/**
* Reads binary data from the file pointer.
* @param fil the pointer to the n_file data that is read from.
* @param local_byte the single byte.
* @return FILE_ERROR if there is a problem and FILE_OKAY if it is successful.
*/
n_int io_read_bin(n_file * fil, n_byte * local_byte)
{
n_uint file_location = fil -> location;
if (file_location >= (fil -> size))
{
return -1;
}
*local_byte = fil -> data[file_location];
fil->location = (file_location + 1);
return 0;
}
/**
* Reads a character - white-space and comments have already been removed
* @param fil the pointer to the n_file data that is read from.
* @return CHAR_EOF if there is a problem and the byte value if it is successful.
*/
n_byte io_read(n_file * fil)
{
n_byte val = 0;
if (io_read_bin(fil, &val) == -1)
{
return(CHAR_EOF);
}
return (val);
}
/**
* Converts a tab delimited file to a series of string pointers
* @param tab_file the pointer to the n_file data that is read from.
* @param size_value the pointer to the total number.
* @param row_value the pointer to the number of columns per row.
* @return string pointers.
*/
n_string * io_tab_delimit_to_n_string_ptr(n_file * tab_file, n_int * size_value, n_int * row_value)
{
n_int string_point_location = 1;
n_int first_value = 0;
n_uint loop = 0;
n_string *string_point;
n_int local_row_value = 0;
n_byte resultant_value;
if (tab_file->location != 0)
{
while (loop < tab_file->location)
{
resultant_value = tab_file->data[loop];
if (IS_TAB(resultant_value) || IS_RETURN(resultant_value))
{
tab_file->data[loop] = 0;
first_value = 1;
if ((local_row_value == 0) && IS_RETURN(resultant_value))
{
local_row_value = string_point_location;
}
}
else
{
if (first_value)
{
string_point_location++;
first_value = 0;
}
}
loop ++;
}
}
if (size_value != 0L)
{
*size_value = string_point_location;
}
if (row_value != 0L)
{
*row_value = local_row_value;
}
if (tab_file->location == 0)
{
return 0L;
}
if ((string_point = (n_string *)io_new(string_point_location*sizeof(n_string *))) == 0L)
{
return 0L;
}
string_point_location = 0;
first_value = 0;
loop = 0;
string_point[string_point_location++] = (n_string)&(tab_file->data[0]);
while (loop < tab_file->location)
{
resultant_value = tab_file->data[loop];
if ((first_value) && (resultant_value != 0))
{
string_point[string_point_location++] = (n_string)&(tab_file->data[loop]);
first_value = 0;
}
if (resultant_value == 0)
{
first_value = 1;
}
loop ++;
}
return string_point;
}
/**
* Read a four byte value from n_file
* @param fil the pointer to the n_file data that is read from.
* @param actual_value the actual value read.
* @param final_char the final character (after the last number).
* @return number of characters read in condition of success and -1 in condition of failure.
*/
n_int io_read_byte4(n_file * fil, n_uint * actual_value, n_byte * final_char)
{
n_uint temp = 0;
n_int ten_power_place = 0;
while (1)
{
n_byte value = io_read(fil);
n_uint mod_ten;
if (!ASCII_NUMBER(value))
{
*actual_value = temp;
*final_char = value;
return ten_power_place;
}
mod_ten = value - '0';
if ((temp == 429496729) && (mod_ten > 5))
{
return -1;
}
if (temp > 429496729)
{
return -1;
}
temp = (temp * 10) + mod_ten;
ten_power_place++;
}
}
/**
* Read a number from a string.
* @param number_string the string to be read from.
* @param actual_value the actual value read.
* @param decimal_divisor the value required to provide a decimal version.
* @return number of characters read in condition of success and -1 in condition of failure.
*/
n_int io_number(n_string number_string, n_int * actual_value, n_int * decimal_divisor)
{
n_uint temp = 0;
n_int divisor = 0;
n_int ten_power_place = 0;
n_int string_point = 0;
n_byte negative = 0;
if (number_string == 0L) return -1;
if (number_string[0] == 0) return -1;
if (number_string[0] == '-')
{
negative = 1;
string_point++;
}
while (1)
{
n_byte value = number_string[string_point++];
n_uint mod_ten;
if (value == 0)
{
n_int translate;
if (negative == 1)
{
translate = 0 - temp;
}
else
{
translate = temp;
}
*actual_value = translate;
if (divisor > 0)
{
divisor--;
}
*decimal_divisor = divisor;
return ten_power_place;
}
if (value == '.')
{
if (divisor != 0)
{
return -1;
}
divisor = 1;
}
else
{
if (!ASCII_NUMBER(value))
{
return -1;
}
mod_ten = value - '0';
if (temp == 214748364)
{
if (negative == 1)
{
if (mod_ten > 8)
{
return -1;
}
}
else
{
if (mod_ten > 7)
{
return -1;