-
Notifications
You must be signed in to change notification settings - Fork 0
/
xmlfile.c
83 lines (72 loc) · 1.74 KB
/
xmlfile.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
/*
* This is a file that implements the reading of XML records and sleeptimes from disk.
*
* *** DO NOT CHANGE THIS FILE ***
*/
#include "xmlfile.h"
#include <sys/errno.h>
#include <string.h>
#include <ctype.h>
FILE* xml_read_open( char* filename )
{
FILE* file = fopen( filename, "r" );
if( file == NULL )
{
fprintf( stderr, "%s:%d Failed to open %s for reading: %s\n", __FILE__, __LINE__, filename, strerror(errno) );
}
return file;
}
static int xml_readline( FILE* file, char* buffer, int bufsz )
{
char* ret;
ret = fgets( buffer, bufsz, file );
if( ret == NULL )
{
if( !feof(file) )
{
fprintf( stderr, "%s:%d An error occurred while reading from XML file: %s\n", __FILE__, __LINE__, strerror(errno) );
return -1;
}
return 0;
}
return strlen( buffer );
}
int xml_read( FILE* file, char* buffer, int bufsz )
{
char* debug = buffer;
char* fx;
int sum = 0;
int ct;
do
{
ct = xml_readline( file, buffer, bufsz );
if( ct < 0 ) return ct;
if( ct == 0 )
{
break;
}
fx = buffer;
while( *fx != 0 && isspace(*fx) ) fx++;
if( *fx == 0 ) continue;
fx = buffer;
sum += ct;
buffer += ct;
bufsz -= ct;
while( *fx != 0 && isspace(*fx) ) fx++;
if( strncmp( fx, "</record>", 9 ) == 0 )
{
break;
}
if( strncmp( fx, "<sleep", 6 ) == 0 )
{
break;
}
}
while( ct > 0 );
fprintf( stderr, "%s:%d Read the following text: >>>>%s<<<<<\n", __FILE__, __LINE__, debug );
return sum;
}
void xml_close( FILE* file )
{
fclose( file );
}