File: fio.cpp

package info (click to toggle)
searchandrescue 0.8.2-10
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 4,656 kB
  • ctags: 6,111
  • sloc: ansic: 89,072; cpp: 7,691; sh: 90; makefile: 80
file content (1159 lines) | stat: -rw-r--r-- 20,460 bytes parent folder | download | duplicates (9)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <limits.h>

#include "../include/xsw_ctype.h"
#include "../include/os.h"
#include "../include/string.h"
#include "../include/fio.h"

#ifdef MEMWATCH
# include "memwatch.h"
#endif


FILE *FOpen(const char *path, const char *mode);
void FClose(FILE *fp);

void FSeekNextLine(FILE *fp);
void FSeekPastSpaces(FILE *fp);
void FSeekPastChar(FILE *fp, char c);

int FSeekToParm(FILE *fp, const char *parm, char comment, char delim);
char *FSeekNextParm(FILE *fp, char *buf, char comment, char delim);

int FGetValuesI(FILE *fp, int *value, int nvalues);
int FGetValuesL(FILE *fp, long *value, int nvalues);
int FGetValuesF(FILE *fp, double *value, int nvalues);
char *FGetString(FILE *fp);
char *FGetStringLined(FILE *fp);
char *FGetStringLiteral(FILE *fp);

char *FReadNextLineAlloc(FILE *fp, char comment);
char *FReadNextLineAllocCount(
	FILE *fp, char comment, int *line_count
);


#define ISCR(c) (((c) == '\n') || ((c) == '\r'))

/*
 *      Allocate memory while reading a line from file in chunk size
 *      of this many bytes:
 */
#define FREAD_ALLOC_CHUNK_SIZE  8


#define ATOI(s)         (((s) != NULL) ? atoi(s) : 0)
#define ATOL(s)         (((s) != NULL) ? atol(s) : 0)
#define ATOF(s)         (((s) != NULL) ? atof(s) : 0.0f)
#define STRDUP(s)       (((s) != NULL) ? strdup(s) : NULL)

#define MAX(a,b)        (((a) > (b)) ? (a) : (b))
#define MIN(a,b)        (((a) < (b)) ? (a) : (b))
#define CLIP(a,l,h)     (MIN(MAX((a),(l)),(h)))
#define STRLEN(s)       (((s) != NULL) ? strlen(s) : 0)
#define STRISEMPTY(s)   (((s) != NULL) ? (*(s) == '\0') : 1)


/*
 *	OS wrapper to open file using UNIX path notation.
 *
 *	The returned FILE pointer can be used with all the ANSI C standard
 *	file IO functions.
 */
FILE *FOpen(const char *path, const char *mode) 
{
	int len;
	char *new_path, *strptr2;
	const char *strptr1;
	FILE *fp;

	if(STRISEMPTY(path) || STRISEMPTY(mode))
	    return(NULL);

	/* Get length of path and allocate new buffer */
	len = STRLEN(path);
	new_path = (char *)malloc((len + 1) * sizeof(char));
	if(new_path == NULL)
	    return(NULL);

	/* Copy path to new_path */
	strptr1 = path;
	strptr2 = new_path;
	while(*strptr1 != '\0')
	{
	    *strptr2 = *strptr1;

#ifdef __MSW__
	    if(*strptr2 == '/')
		*strptr2 = '\\';
#endif	/* __MSW__ */

	    strptr1++; strptr2++;
	}
	*strptr2 = '\0';

	/* Open file */
	fp = fopen(new_path, mode);

	/* Delete coppied path */
	free(new_path);

	return(fp);
}

/*
 *	Closes the file opened by FOpen().
 */
void FClose(FILE *fp)
{
	if(fp != NULL)
	    fclose(fp);
}

/*
 *	Seeks to next line, escape sequences will be parsed.
 */
void FSeekNextLine(FILE *fp)
{
	int c;

	if(fp == NULL)
	    return;

	do
	{
	    c = fgetc(fp);

	    /* Escape sequence? */
	    if(c == '\\')
		c = fgetc(fp);
	    /* New line? */
	    else if(ISCR(c))
		break;

	} while(c != EOF);
}

/*
 *      Seeks fp past any spaces.
 */
void FSeekPastSpaces(FILE *fp)
{
	int c;

	if(fp == NULL)
	    return;

	while(1)
	{
	    c = fgetc(fp);
	    if(c == EOF)
		break;

	    if(ISBLANK(c))
		continue;

	    fseek(fp, -1, SEEK_CUR);
	    break;
	}
}

/*
 *	Seeks fp past the first occurance of c or EOF.
 */
void FSeekPastChar(FILE *fp, char c)
{
	int i;


	if(fp == NULL)
	    return;

	do
	{
	    i = fgetc(fp);
	    if(i == c)
		break;

	} while(i != EOF);
}

/*
 *	Seeks fp to the beginning of the value of the specified
 *	parameter.
 *
 *	The delim specifies the deliminator character between the
 *	parameter and the value. The delim can be '\0' to specify
 *	"any blanks" (in the case of no deliminators between the
 *	parameter and the value).
 *
 *	Parameters are assumed to not contain spaces or escape sequences.
 *
 *	Returns 0 on successful match or non-zero on error.
 */
int FSeekToParm(FILE *fp, const char *parm, char comment, char delim)
{
	int c, parm_len;

	if(fp == NULL)
	    return(-1);

	parm_len = STRLEN(parm);
	if(parm_len <= 0)
	    return(-1);

	do
	{
	    c = fgetc(fp);
	    if(c == EOF)
		return(-1);

	    /* Seek past spaces */
	    if(ISBLANK(c))
		FSeekPastSpaces(fp);

	    /* New line? */
	    if(ISCR(c))
	        continue;

	    /* First non-blank a comment character? */
	    if(c == comment)
	    {
		FSeekNextLine(fp);
		continue;
	    }

	    /* Matches parameter? */
	    if(c == *parm)
	    {
		/* First char matches parm */
		const char *strptr = parm + 1;


		while(*strptr != '\0')
		{
		    c = fgetc(fp);
		    if(c != *strptr)
			break;

		    strptr++;
		}
		if(*strptr == '\0')
		{
		    /* Got match, seek fp past deliminator */
		    if(delim == '\0')
		    {
			FSeekPastSpaces(fp);
		    }
		    else
		    {
			FSeekPastSpaces(fp);

			/* Seek to delim or newline */
			do
			{
			    c = fgetc(fp);
			    if((c == EOF) || (c == delim))
				break;

			    if(ISCR(c))
			    {
				fseek(fp, -1, SEEK_CUR);
				break;
			    }

			} while(1);

			FSeekPastSpaces(fp);
		    }

		    return(0);
		}
		else
		{
		    /* No match, seek to next line */
		    FSeekNextLine(fp);
		}
	    }
	    else
	    {
		/* No match, seek to next line */
		FSeekNextLine(fp);
	    }

	} while(1);

	return(-1);
}

/*
 *	Fetches the next parameter found at the file position fp.
 *
 *	If buf is NULL then a newly allocated string will be returned
 *	containing the fetched parm. If buf is not NULL, then buf will
 *	be realloc()'ed and returned as a new pointer containing
 *	the fetched parm.
 *
 *	If EOF is reached by the given fp position, then NULL will
 *	be returned and the given buf will have been free()ed by this
 *	function.
 */
char *FSeekNextParm(FILE *fp, char *buf, char comment, char delim)
{
	int c, buf_pos = 0, buf_len, buf_inc = FREAD_ALLOC_CHUNK_SIZE;


	if(fp == NULL)
	{
	    free(buf);
	    return(NULL);
	}

	/* Get length of buf (less than actual allocated is okay) */
	buf_len = (buf != NULL) ? strlen(buf) : 0;

	/* Seek past spaces and comments to next parameter */
	while(1)
	{
	    FSeekPastSpaces(fp);
	    c = fgetc(fp);
	    if(c == EOF)
	    {
		free(buf);
		return(NULL);
	    }
	    else if(c == comment)
	    {
		FSeekNextLine(fp);
		continue;
	    }
	    else if(ISCR(c))
	    {
		continue;
	    }
	    else
	    {
		fseek(fp, -1, SEEK_CUR);
		break;
	    }
	}

	/* Begin fetching this parm */
	while(1)
	{
	    /* Get next char */
	    c = fgetc(fp);
	    if(c == EOF)
	    {
		break;
	    }

	    /* Blank character reached? */
	    if(ISBLANK(c))
	    {
		/* Blank char reached, seek past delimiantor and position
		 * fp at beginning of value.
		 */
		if(delim == '\0')
		{
		    FSeekPastSpaces(fp);
		}
		else
		{
		    FSeekPastSpaces(fp);

		    /* Seek to deim or newline */
		    do
		    {
			c = fgetc(fp);
			if((c == EOF) || (c == delim))
			    break;

			if(ISCR(c))
			{
			    fseek(fp, -1, SEEK_CUR);
			    break;
			}

		    } while(1);

		    FSeekPastSpaces(fp);
		}
		break;
	    }

	    /* CR reached? */
	    if(ISCR(c))
	    {
	    	fseek(fp, -1, SEEK_CUR);
		break;
	    }

	    /* Deliminator reached? */
	    if(c == delim)
	    {
		FSeekPastSpaces(fp);
		break;
	    }

	    /* Need to allocate buffer? */
	    if(buf_pos <= buf_len)
	    {
		buf_len += buf_inc;

		buf = (char *)realloc(buf, buf_len * sizeof(char));
		if(buf == NULL)
		{
		    FSeekNextLine(fp);
		    return(NULL);
		}
	    }

	    buf[buf_pos] = (char)c;
	    buf_pos++;
	}

	/* Put null terminating byte on buffer */
	if(c == EOF)
	{
	    free(buf);
	    buf = NULL;
	}
	else
	{
	    if(buf_pos <= buf_len)
	    {
		buf_len = buf_pos + 1;

		buf = (char *)realloc(buf, buf_len * sizeof(char));
		if(buf == NULL)
		    return(NULL);
	    }
	    buf[buf_pos] = '\0';
	}

	return(buf);
}

/*
 *      Loads values as ints from the file starting at the
 *      specified fp. Will not load more than nvalues.
 *
 *      The fp will be positioned at the start of the next line.
 *
 *      Returns non-zero on error. 
 */
int FGetValuesI(FILE *fp, int *value, int nvalues)
{
	int c, i, n, line_done = 0;
#define len	80
	char num_str[len];


	if(fp == NULL)
	    return(-1);

	FSeekPastSpaces(fp);

	/* Begin fetching values */
	for(i = 0; i < nvalues; i++)
	{
	    (*num_str) = '\0';

	    /* Read number */   
	    for(n = 0; n < len; n++)
	    {
		if(line_done)   
		    break;

		c = fgetc(fp);
		if((c == EOF) || ISCR(c))
		{
		    num_str[n] = '\0';
		    line_done = 1;
		    break;
		}
		/* Escape sequence? */
		else if(c == '\\')
		{
		    c = fgetc(fp);
		    if(c == EOF)
		    {
			num_str[n] = '\0';
			line_done = 1;
			break;
		    }
		    if(c != '\\')
		       c = fgetc(fp);

		    if(c == EOF)
		    {
			num_str[n] = '\0';
			line_done = 1;
			break;
		    }
		}
		/* Separator? */
		else if(ISBLANK(c) || (c == ','))
		{
		    num_str[n] = '\0';
		    FSeekPastSpaces(fp);
		    break;
		}

		num_str[n] = (char)c;
	    }
	    num_str[len - 1] = '\0';

	    value[i] = atoi(num_str);
	}
	if(!line_done)
	    FSeekNextLine(fp);
#undef len
	return(0);
}

/*
 *	Loads values as longs from the file starting at the
 *	specified fp. Will not load more than nvalues.
 *
 *	The fp will be positioned at the start of the next line.
 *
 *	Returns non-zero on error.
 */
int FGetValuesL(FILE *fp, long *value, int nvalues)
{
	int c, i, n, line_done = 0;
#define len     80
	char num_str[len];


	if(fp == NULL)
	    return(-1);

	FSeekPastSpaces(fp);

	/* Begin fetching values */
	for(i = 0; i < nvalues; i++)
	{
	    (*num_str) = '\0';

	    /* Read number */
	    for(n = 0; n < len; n++)
	    {
		if(line_done)
		    break;

		c = fgetc(fp);
		if((c == EOF) || ISCR(c))
		{
		    num_str[n] = '\0';
		    line_done = 1;
		    break;
		}
		/* Escape sequence? */
		else if(c == '\\')
		{
		    c = fgetc(fp);
		    if(c == EOF)
		    {
			num_str[n] = '\0';
			line_done = 1;
			break;
		    }
		    if(c != '\\')
		       c = fgetc(fp);

		    if(c == EOF)
		    {
			num_str[n] = '\0';
			line_done = 1;
			break;
		    }
		}
		/* Separator? */
		else if(ISBLANK(c) || (c == ','))
		{
		    num_str[n] = '\0';
		    FSeekPastSpaces(fp);
		    break;
		}

		num_str[n] = (char)c;
	    }
	    num_str[len - 1] = '\0';

	    value[i] = atol(num_str);
	}
	if(!line_done)
	    FSeekNextLine(fp);
#undef len
	return(0);
}

/*
 *      Loads values as doubles from the file starting at the
 *      specified fp. Will not load more than nvalues.
 *
 *      The fp will be positioned at the start of the next line.
 *
 *	Returns non-zero on error.
 */
int FGetValuesF(FILE *fp, double *value, int nvalues)
{
	int c, i, n, line_done = 0;
#define len	80
	char num_str[len];


	if(fp == NULL)
	    return(-1);

	FSeekPastSpaces(fp);

	/* Begin fetching values */
	for(i = 0; i < nvalues; i++)
	{
	    (*num_str) = '\0';

	    /* Read number */
	    for(n = 0; n < len; n++)
	    {
		if(line_done)
		    break;

		c = fgetc(fp);
		if((c == EOF) || ISCR(c))
		{
		    num_str[n] = '\0';
		    line_done = 1; 
		    break;   
		}
		/* Escape sequence? */
		else if(c == '\\')
		{
		    c = fgetc(fp);
		    if(c == EOF)
		    {
			num_str[n] = '\0';
			line_done = 1;
			break;
		    }
		    if(c != '\\')
		       c = fgetc(fp); 

		    if(c == EOF)
		    {
			num_str[n] = '\0';
			line_done = 1;
			break;  
		    }
		}
		/* Separator? */
		else if(ISBLANK(c) || (c == ','))
		{
		    num_str[n] = '\0';
		    FSeekPastSpaces(fp);
		    break;
		}

		num_str[n] = (char)c;
	    }
	    num_str[len - 1] = '\0';

	    value[i] = atof(num_str);
	}
	if(!line_done)
	    FSeekNextLine(fp);

#undef len
	return(0);
}

/*
 *	Returns a dynamically allocated string containing the value as a
 *	string obtained from the file specified by fp. Reads from the
 *	current position of fp to the next new line character or EOF.
 *
 *      Escape sequences will be parsed and spaces will be stripped.
 *
 *      The fp is positioned after the new line or at the EOF.
 */
char *FGetString(FILE *fp)
{
	int c, i = 0, len = 0;
	char *s = NULL, *s2;


	if(fp == NULL)
	    return(s);

	/* Begin reading string from file */

	/* Skip initial spaces */
	c = fgetc(fp);
	while((c != EOF) && ISBLANK(c))
	    c = fgetc(fp);

	if(c == EOF)
	    return(s);

	/* Read string */
	while(1)
	{
	    /* Need to increase allocation? */
	    if(i >= len)
	    {
		len = MAX(len + 128, i + 1);
		s = (char *)realloc(s, len * sizeof(char));
		if(s == NULL)
		{
		    len = i = 0;
		    break;
		}
	    }

	    /* Get pointer to current position in string */
	    s2 = s + i;

	    /* Set new character value */
	    *s2 = c;

	    /* End of file or end of the line? */
	    if((c == EOF) || ISCR(c))
	    {
		*s2 = '\0';  
		break;
	    }
	    /* Escape sequence? */
	    else if(c == '\\')
	    {
		/* Read next character after backslash */
		c = fgetc(fp);
		if((c == EOF) || (c == '\0'))
		{
		    *s2 = '\0';
		}
		else if(ISCR(c))
		{
		    /* New line (do not save this newline char) */
		    i--;
		}
		else if(c == '\\')
		{
		    /* Literal backslash */
		    *s2 = '\\';
		}
		else if(c == '0')
		{
		    /* Null */
		    *s2 = '\0';
		}
		else if(c == 'b')
		{
		    /* Bell */
		    *s2 = '\b';
		}
		else if(c == 'n')
		{
		    /* New line */
		    *s2 = '\n';
		}
		else if(c == 'r')
		{
		    /* Line return */
		    *s2 = '\r';
		}
		else if(c == 't')
		{
		    /* Tab */
		    *s2 = '\t';
		}
		else
		{
		    /* Unsupported escape sequence, store it as is */
		    *s2 = c;
		}

		/* Read next character and increment position */
		c = fgetc(fp);
		i++;
	    }
	    /* Regular character */
	    else
	    {
		/* Read next character and increment position */
		c = fgetc(fp);
		i++;
	    }
	}

	/* Cut off tailing spaces */
	if(s != NULL)
	{
	    s2 = s + i - 1;

	    while(ISBLANK(*s2) && (s2 >= s))
		*s2 = '\0';
	}

	return(s);
}


/*
 *	Works just like FGetString() except the string is loaded
 *	literally and the only escape sequence to be handled will
 *	be the two characters '\\' '\n', when those characters
 *	are encountered the character '\n' will be saved into the return
 *	string.
 *
 *	Spaces will not be striped, the fp will be positioned after the
 *	newline or EOF (whichever is encountered first).
 */
char *FGetStringLined(FILE *fp)
{
	int c, i = 0, len = 0;
	char *s = NULL, *s2;


	if(fp == NULL)
	    return(s);

	/* Begin reading string from file */

	/* Get first character */
	c = fgetc(fp);
	if(c == EOF)
	    return(s);

	/* Read string */
	while(1)
	{
	    /* Need to increase allocation? */
	    if(i >= len)
	    {
		len = MAX(len + 128, i + 1);
		s = (char *)realloc(s, len * sizeof(char));
		if(s == NULL)
		{
		    len = i = 0;
		    break;
		}
	    }

	    /* Get pointer to current position in string */
	    s2 = s + i;

	    /* Set new character value */
	    *s2 = c;

	    /* End of file or end of line? */
	    if((c == EOF) || ISCR(c))
	    {
		*s2 = '\0';
		break;
	    }
	    /* Escape sequence? */
	    else if(c == '\\')
	    {
		/* Read next character after backslash */
		c = fgetc(fp);
		if(c == EOF)
		{
		    i++;
		    continue;
		}
		else if(ISCR(c))
		{
		    /* New line, store it as is */
		    *s2 = c;
		}
		else
		{
		    /* All other escaped characters leave as is
		     * it will be set on the next loop
		     */
		    i++;
		    continue;
		}

		/* Read next character and increment position */
		c = fgetc(fp);
		i++;
	    }
	    /* Regular character */
	    else
	    {
		/* Read next character and increment position */
		c = fgetc(fp);
		i++;
	    }
	}   

	return(s);
}

/*
 *      Works just like FGetString() except the string is loaded
 *	literally and no escape sequences parsed, that would be all
 *	characters from the current given fp position to the first
 *	encountered newline ('\n') character (escaped or not).
 *
 *	Spaces will not be striped, the fp will be positioned after the
 *	newline or EOF (whichever is encountered first).
 */
char *FGetStringLiteral(FILE *fp)  
{
	int c, i = 0, len = 0;
	char *s = NULL, *s2;


	if(fp == NULL)
	    return(s);

	/* Begin reading string from file */

	/* Get first character */
	c = fgetc(fp);
	if(c == EOF)
	    return(s);

	/* Read string */
	while(1)
	{
	    /* Need to increase allocation? */
	    if(i >= len)
	    {
		len = MAX(len + 128, i + 1);
		s = (char *)realloc(s, len * sizeof(char));
		if(s == NULL)
		{
		    len = i = 0;
		    break;
		}
	    }

	    /* Get pointer to current position in string */
	    s2 = s + i;

	    /* Set new character value */
	    *s2 = c;

	    /* End of file or end of line? */
	    if((c == EOF) || ISCR(c))
	    {
		*s2 = '\0';
		break;
	    }
	    else
	    {
		/* Read next character and increment position */
		c = fgetc(fp);   
		i++;
	    }
	}

	return(s);
}


/*
 *	Returns an allocated string containing the entire
 *	line or NULL on error or EOF.
 *
 *	If comment is '\0' then the next line is read regardless
 *	if it is a comment or not.
 *
 *	Calling function must free() the returned pointer.
 */
char *FReadNextLineAlloc(FILE *fp, char comment)
{
	return(FReadNextLineAllocCount(fp, comment, NULL));
}

char *FReadNextLineAllocCount(
	FILE *fp,
	char comment,
	int *line_count
)
{
	int i, m, n;
	char *strptr;


	if(fp == NULL)
	    return(NULL);

	/* Is comment character specified? */
	if(comment != '\0')
	{
	    /* Comment character is specified */

	    /* Read past spaces, newlines, and comments */
	    i = fgetc(fp);
	    if(i == EOF)
		return(NULL);

	    while((i == ' ') || (i == '\t') || (i == '\n') || (i == '\r') ||
		  (i == comment)
	    )
	    {
		if(i == EOF)
		    return(NULL);

		/* If newline, then increment line count */
		if((i == '\n') ||
		   (i == '\r')
		)
		{
		    if(line_count != NULL)
			*line_count += 1;
		}

		/* If comment, then skip to next line */
		if(i == comment)
		{
		    i = fgetc(fp);
		    while((i != '\n') && (i != '\r'))
		    {
			if(i == EOF)
			    return(NULL);
			i = fgetc(fp);
		    }
		    if(line_count != NULL)
			*line_count += 1;
		}

		/* Get next character */
		i = fgetc(fp);
	    }

	    /* Begin adding characters to string */
	    m = 0;	/* mem size */
	    n = 1;	/* chars read */
	    strptr = NULL;

	    while((i != '\n') && (i != '\r') && (i != '\0'))
	    {
		/* Escape character? */
		if(i == '\\')
		{
		    /* Read next character */
		    i = fgetc(fp);

		    /* Skip newlines internally */
		    if((i == '\n') || (i == '\r'))
		    {
			i = fgetc(fp);

			/* Still counts as a line though! */
			if(line_count != NULL)
			    *line_count += 1;
		    }
		}

		if(i == EOF)
		    break;

		/* Allocate more memory as needed */
		if(m < n)
		{
		    /* Allocate FREAD_ALLOC_CHUNK_SIZE more bytes */
		    m += FREAD_ALLOC_CHUNK_SIZE;

		    strptr = (char *)realloc(strptr, m * sizeof(char));
		    if(strptr == NULL)
			return(NULL);
		}

		strptr[n - 1] = (char)i;

		/* Read next character from file */
		i = fgetc(fp);
		n++;	/* Increment characters read */
	    }

	    /* Add newline and null terminate */
	    m += 2;	/* 2 more chars */
	    strptr = (char *)realloc(strptr, m * sizeof(char));
	    if(strptr == NULL)
		return(NULL);
	    strptr[n - 1] = '\n';
	    strptr[n] = '\0';

	    /* Increment line count */
	    if(line_count != NULL)
		*line_count += 1;
	}
	else
	{
	    /* Comment character is not specified */

	    i = fgetc(fp);
	    if(i == EOF)
		return(NULL);

	    /* Begin adding characters to string */
	    m = 0;      /* Memory size */
	    n = 1;      /* Characters read */
	    strptr = NULL;	/* Return string */

	    while((i != '\n') && (i != '\r') && (i != '\0'))
	    {
		/* Escape character? */
		if(i == '\\')
		{
		    /* Read next character */
		    i = fgetc(fp);

		    /* Skip newlines internally */
		    if((i == '\n') || (i == '\r'))
		    {
			i = fgetc(fp);

			/* Still counts as a line though! */
			if(line_count != NULL)
			    *line_count += 1;
		    }
		}

		if(i == EOF)
		    break;

		/* Allocate more memory as needed */
		if(m < n)
		{
		    /* Allocate FREAD_ALLOC_CHUNK_SIZE more bytes */
		    m += FREAD_ALLOC_CHUNK_SIZE;

		    strptr = (char *)realloc(strptr, m * sizeof(char));
		    if(strptr == NULL)
			return(NULL);
		}
	
		strptr[n - 1] = (char)i;

		/* Read next character from file */
		i = fgetc(fp);
		n++;	/* Increment characters read */
	    }

	    /* Add newline and null terminate */
	    m += 2;	/* 2 more chars */
	    strptr = (char *)realloc(strptr, m * sizeof(char));
	    strptr[n - 1] = '\n';
	    strptr[n] = '\0';

	    /* Increment line count */
	    if(line_count != NULL)
		*line_count += 1;
	}


	return(strptr);
}