2313
|
1 /* This program is free software; you can redistribute it and/or modify
|
|
2 * it under the terms of the GNU General Public License as published by
|
|
3 * the Free Software Foundation; either version 2 of the License, or
|
|
4 * (at your option) any later version.
|
|
5 *
|
|
6 * This program is distributed in the hope that it will be useful,
|
|
7 * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
8 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
9 * GNU General Public License for more details.
|
|
10 *
|
|
11 * You should have received a copy of the GNU General Public License
|
|
12 * along with this program; if not, write to the Free Software Foundation,
|
|
13 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
14 */
|
|
15
|
|
16 #include "vfs.h"
|
|
17 #include <string.h>
|
|
18 #include <stdlib.h>
|
|
19 #include <glib/gprintf.h>
|
|
20
|
|
21 /**
|
|
22 * vfs_fputc:
|
|
23 * @c: A character to write to the stream.
|
|
24 * @stream: A #VFSFile object representing the stream.
|
|
25 *
|
|
26 * Writes a character to a stream.
|
|
27 *
|
|
28 * Return value: The character on success, or EOF.
|
|
29 **/
|
|
30 gint vfs_fputc(gint c, VFSFile *stream)
|
|
31 {
|
|
32 guchar uc = (guchar) c;
|
|
33
|
|
34 if (! vfs_fwrite(&uc, 1, 1, stream)) {
|
|
35 return EOF;
|
|
36 }
|
|
37
|
|
38 return uc;
|
|
39 }
|
|
40
|
|
41 /**
|
|
42 * vfs_fgets:
|
|
43 * @s: A buffer to put the string in.
|
|
44 * @n: The amount of characters to read.
|
|
45 * @stream: A #VFSFile object representing the stream.
|
|
46 *
|
|
47 * Reads a set of characters from a stream.
|
|
48 *
|
|
49 * Return value: The string on success, or NULL.
|
|
50 **/
|
|
51 gchar *vfs_fgets(gchar *s, gint n, VFSFile *stream)
|
|
52 {
|
|
53 gint c;
|
|
54 register gchar *p;
|
|
55
|
|
56 if(n<=0) return NULL;
|
|
57
|
|
58 p = s;
|
|
59
|
|
60 while (--n) {
|
|
61 if ((c = vfs_getc(stream))== EOF) {
|
|
62 break;
|
|
63 }
|
|
64 if ((*p++ = c) == '\n') {
|
|
65 break;
|
|
66 }
|
|
67 }
|
|
68 if (p > s) {
|
|
69 *p = 0;
|
|
70 return s;
|
|
71 }
|
|
72
|
|
73 return NULL;
|
|
74 }
|
|
75
|
|
76 /**
|
|
77 * vfs_fputc:
|
|
78 * @s: A string to write to the stream.
|
|
79 * @stream: A #VFSFile object representing the stream.
|
|
80 *
|
|
81 * Writes a string to a VFS stream.
|
|
82 *
|
|
83 * Return value: The amount of bytes written.
|
|
84 **/
|
|
85 int vfs_fputs(const gchar *s, VFSFile *stream)
|
|
86 {
|
|
87 size_t n = strlen(s);
|
|
88
|
|
89 return ((vfs_fwrite(s, 1, n, stream) == n) ? n : EOF);
|
|
90 }
|
|
91
|
|
92 /**
|
|
93 * vfs_vfprintf:
|
|
94 * @stream: A #VFSFile object representing the stream.
|
|
95 * @format: A printf-style format string.
|
|
96 * @args: A va_list of args to use.
|
|
97 *
|
|
98 * Writes a formatted string to a VFS stream via a va_list of args.
|
|
99 *
|
|
100 * Return value: The amount of bytes written.
|
|
101 **/
|
|
102 int vfs_vfprintf(VFSFile *stream, gchar const *format, va_list args)
|
|
103 {
|
|
104 gchar *string;
|
|
105 gint rv = g_vasprintf(&string, format, args);
|
|
106 if (rv<0) return rv;
|
|
107 rv = vfs_fputs(string, stream);
|
|
108 free (string);
|
|
109 return rv;
|
|
110 }
|
|
111
|
|
112 /**
|
|
113 * vfs_fprintf:
|
|
114 * @stream: A #VFSFile object representing the stream.
|
|
115 * @format: A printf-style format string.
|
|
116 * @...: A list of args to use.
|
|
117 *
|
|
118 * Writes a formatted string to a VFS stream.
|
|
119 *
|
|
120 * Return value: The amount of bytes written.
|
|
121 **/
|
|
122 int vfs_fprintf(VFSFile *stream, gchar const *format, ...)
|
|
123 {
|
|
124 va_list arg;
|
|
125 gint rv;
|
|
126
|
|
127 va_start(arg, format);
|
|
128 rv = vfs_vfprintf(stream, format, arg);
|
|
129 va_end(arg);
|
|
130
|
|
131 return rv;
|
|
132 }
|
2424
|
133
|
|
134 /**
|
|
135 * vfs_file_get_contents
|
|
136 * @filename: the filename to read in
|
|
137 * @buf: pointer to pointer of buffer
|
|
138 * @sz: pointer to integer that is the size
|
|
139 **/
|
|
140 void
|
|
141 vfs_file_get_contents(const gchar *filename, gchar **buf, gsize *size)
|
|
142 {
|
|
143 VFSFile *fd;
|
|
144
|
|
145 fd = vfs_fopen(filename, "rb");
|
|
146
|
|
147 if (fd == NULL)
|
|
148 return;
|
|
149
|
|
150 vfs_fseek(fd, 0, SEEK_END);
|
|
151 *size = vfs_ftell(fd);
|
|
152
|
|
153 *buf = g_new(gchar, *size);
|
|
154
|
|
155 if (*buf == NULL)
|
|
156 return;
|
|
157
|
|
158 vfs_fseek(fd, 0, SEEK_SET);
|
|
159 vfs_fread(*buf, 1, *size, fd);
|
|
160
|
|
161 vfs_fclose(fd);
|
|
162 }
|