root/libswish3/trunk/src/libswish3/io.c

Revision 2150, 3.9 kB (checked in by karpet, 4 months ago)

ditch SWISH_META_CONNECTOR and SWISH_PROP_CONNECTOR in favor of SWISH_TOKENPOS_BUMPER; add tests showing differences in inline parsing for xml vs html

Line 
1 /*
2  * This file is part of libswish3
3  * Copyright (C) 2007 Peter Karman
4  *
5  *  libswish3 is free software; you can redistribute it and/or modify
6  *  it under the terms of the GNU General Public License as published by
7  *  the Free Software Foundation; either version 2 of the License, or
8  *  (at your option) any later version.
9  *
10  *  libswish3 is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  *  GNU General Public License for more details.
14  *
15  *  You should have received a copy of the GNU General Public License
16  *  along with libswish3; if not, write to the Free Software
17  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18 */
19
20 /* simple I/O functions */
21
22 #include <stdio.h>
23 #include <errno.h>
24 #include <err.h>
25 #include <string.h>
26
27 #include "libswish3.h"
28
29 extern int SWISH_DEBUG;
30 extern int errno;
31
32 static void no_nulls(
33     xmlChar *filename,
34     xmlChar *buffer,
35     int bytes_read
36 );
37
38 /* substitute embedded null chars with a newline so we can treat the buffer as a whole
39  * string based on similar code in swish-e ver2 file.c */
40 static void
41 no_nulls(
42     xmlChar *filename,
43     xmlChar *buffer,
44     int bytes_read
45 )
46 {
47     if (xmlStrlen(buffer) < bytes_read) {
48         int i;
49         int j = 0;
50         int i_bytes_read = (int)bytes_read;
51
52         for (i = 0; i < i_bytes_read; ++i) {
53             if (buffer[i] == '\0') {
54                 buffer[i] = '\n';
55                 j++;
56             }
57             if (buffer[i] == SWISH_TOKENPOS_BUMPER[0]) {
58                 buffer[i] = '\n';
59                 j++;
60             }
61         }
62
63         if (j) {
64             SWISH_WARN
65                 ("Substituted %d embedded null or connector character(s) in file '%s' with newline(s)",
66                  j, filename);
67         }
68     }
69
70 }
71
72 xmlChar *
73 swish_slurp_fh(
74     FILE * fh,
75     long flen
76 )
77 {
78
79     size_t bytes_read;
80     xmlChar *buffer;
81
82 /* printf("slurping %d bytes\n", flen); */
83
84     buffer = swish_xmalloc(flen + 1);
85     *buffer = '\0';
86
87     bytes_read = fread(buffer, sizeof(xmlChar), flen, fh);
88
89     if (bytes_read != flen) {
90         SWISH_CROAK("did not read expected bytes: %ld expected, %d read", flen,
91                     bytes_read);
92     }
93     buffer[bytes_read] = '\0'/* terminate the string */
94
95 /* printf("read %d bytes from stdin\n", bytes_read); */
96
97     no_nulls((xmlChar *)"filehandle", buffer, (int)bytes_read);
98
99     return buffer;
100 }
101
102 xmlChar *
103 swish_slurp_file_len(
104     xmlChar *filename,
105     long flen
106 )
107 {
108     size_t bytes_read;
109     FILE *fp;
110     xmlChar *buffer;
111
112     if (flen > SWISH_MAX_FILE_LEN) {
113         flen = SWISH_MAX_FILE_LEN;
114         SWISH_WARN("max file len %ld exceeded - cannot read %ld bytes from %s",
115                    SWISH_MAX_FILE_LEN, flen, filename);
116
117     }
118
119     buffer = swish_xmalloc(flen + 1);
120
121     if ((fp = fopen((char *)filename, "r")) == 0) {
122         SWISH_CROAK("Error reading file %s: %s", filename, strerror(errno));
123     }
124
125     bytes_read = fread(buffer, sizeof(xmlChar), flen, fp);
126
127     if (bytes_read != flen) {
128         SWISH_CROAK("did not read expected bytes: %ld expected, %d read (%s)",
129                     flen, bytes_read, strerror(errno));
130     }
131     buffer[bytes_read] = '\0'/* terminate the string */
132
133 /* close the stream */
134     if (fclose(fp))
135         SWISH_CROAK("error closing filehandle for %s: %s", filename,
136                     strerror(errno));
137
138     no_nulls(filename, buffer, (int)bytes_read);
139
140     return buffer;
141
142 }
143
144 xmlChar *
145 swish_slurp_file(
146     xmlChar *filename
147 )
148 {
149     struct stat info;
150 /* fatal error, since we can't proceed */
151     if (stat((char *)filename, &info)) {
152         SWISH_CROAK("Can't stat %s: %s\n", filename, strerror(errno));
153     }
154     return swish_slurp_file_len(filename, info.st_size);
155
156 }
157
158 boolean
159 swish_file_exists(
160     xmlChar *filename
161 )
162 {
163     struct stat info;
164     if (stat((char *)filename, &info)) {
165         return 0;
166     }
167     return 1;
168 }
Note: See TracBrowser for help on using the browser.