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
|
/* _PDCLIB_load_lines( FILE *, size_t )
This file is part of the Public Domain C Library (PDCLib).
Permission is granted to use, modify, and / or redistribute at will.
*/
#include <stdio.h>
#include <stdlib.h>
#ifndef REGTEST
char * _PDCLIB_load_lines( FILE * fh, size_t lines )
{
size_t required = 0;
long pos = ftell( fh );
char * rc = NULL;
int c;
/* Count the number of characters */
while ( lines && ( c = fgetc( fh ) ) != EOF )
{
if ( c == '\n' )
{
--lines;
}
++required;
}
if ( ! feof( fh ) )
{
if ( ( rc = malloc( required ) ) != NULL )
{
size_t i;
fseek( fh, pos, SEEK_SET );
fread( rc, 1, required, fh );
for ( i = 0; i < required; ++i )
{
if ( rc[ i ] == '\n' )
{
rc[ i ] = '\0';
}
}
}
}
return rc;
}
#endif
#ifdef TEST
#include "_PDCLIB_test.h"
int main( void )
{
#ifndef REGTEST
FILE * fh = fopen( "test_lines.txt", "w+" );
char * rc;
TESTCASE( fh != NULL );
TESTCASE( fputs( "Foo\n\nBar\n", fh ) != EOF );
rewind( fh );
rc = _PDCLIB_load_lines( fh, 3 );
fclose( fh );
remove( "test_lines.txt" );
TESTCASE( rc != NULL );
TESTCASE( strcmp( rc, "Foo" ) == 0 );
TESTCASE( strcmp( rc + 4, "" ) == 0 );
TESTCASE( strcmp( rc + 5, "Bar" ) == 0 );
#endif
return TEST_RESULTS;
}
#endif
|