Debian Codegraph @

src/utils/cst_file_stdio.c

raw
1 /*************************************************************************/
2 /* */
3 /* Cepstral, LLC */
4 /* Copyright (c) 2001 */
5 /* All Rights Reserved. */
6 /* */
7 /* Permission is hereby granted, free of charge, to use and distribute */
8 /* this software and its documentation without restriction, including */
9 /* without limitation the rights to use, copy, modify, merge, publish, */
10 /* distribute, sublicense, and/or sell copies of this work, and to */
11 /* permit persons to whom this work is furnished to do so, subject to */
12 /* the following conditions: */
13 /* 1. The code must retain the above copyright notice, this list of */
14 /* conditions and the following disclaimer. */
15 /* 2. Any modifications must be clearly marked as such. */
16 /* 3. Original authors' names are not deleted. */
17 /* 4. The authors' names are not used to endorse or promote products */
18 /* derived from this software without specific prior written */
19 /* permission. */
20 /* */
21 /* CEPSTRAL, LLC AND THE CONTRIBUTORS TO THIS WORK DISCLAIM ALL */
22 /* WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED */
23 /* WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL */
24 /* CEPSTRAL, LLC NOR THE CONTRIBUTORS BE LIABLE FOR ANY SPECIAL, */
25 /* INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER */
26 /* RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION */
27 /* OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR */
28 /* IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
29 /* */
30 /*************************************************************************/
31 /* Author: David Huggins-Daines <[email protected]> */
32 /* Date: August 2001 */
33 /*************************************************************************/
34 /* */
35 /* File I/O wrappers for normal platforms. */
36 /* Added support for urls (and libcurl) Nov 2011 ([email protected]) */
37 /* */
38 /*************************************************************************/
39
40 #include <stdio.h>
41 #include <string.h>
42 #include <stdarg.h>
43 #include "cst_file.h"
44 #include "cst_string.h"
45 #include "cst_error.h"
46 #include "cst_alloc.h"
47
48 cst_file cst_fopen(const char *path, int mode)
49 {
50 char cmode[4];
51
52 if (cst_urlp(path))
53 return cst_url_open(path);
54 else if ((mode & CST_OPEN_WRITE) && (mode & CST_OPEN_READ))
55 strcpy(cmode, "r+");
56 else if ((mode & CST_OPEN_APPEND) && (mode & CST_OPEN_READ))
57 strcpy(cmode, "a+");
58 else if (mode & CST_OPEN_WRITE)
59 strcpy(cmode, "w");
60 else if (mode & CST_OPEN_APPEND)
61 strcpy(cmode, "a");
62 else if (mode & CST_OPEN_READ)
63 strcpy(cmode, "r");
64
65 /* Windows likes to make a distinction between binary and not */
66 /* While other operating systems do not -- we're going to always */
67 /* treat files as binary, because the non-binary Windows case is */
68 /* never what we want */
69 strcat(cmode, "b");
70
71 return fopen(path, cmode);
72 }
73
74 long cst_fwrite(cst_file fh, const void *buf, long size, long count)
75 {
76 return fwrite(buf, size, count, fh);
77 }
78
79 long cst_fread(cst_file fh, void *buf, long size, long count)
80 {
81 return fread(buf, size, count, fh);
82 }
83
84 long cst_filesize(cst_file fh)
85 {
86 /* FIXME: guaranteed to break on text files on Win32 */
87 long pos, epos;
88
89 pos = ftell(fh);
90 fseek(fh, 0, SEEK_END);
91 epos = ftell(fh);
92 fseek(fh, pos, SEEK_SET);
93
94 return epos;
95 }
96
97 int cst_fgetc(cst_file fh)
98 {
99 return fgetc(fh);
100 }
101
102 long cst_ftell(cst_file fh)
103 {
104 return ftell(fh);
105 }
106
107 long cst_fseek(cst_file fh, long pos, int whence)
108 {
109 int w = 0;
110
111 if (whence == CST_SEEK_ABSOLUTE)
112 w = SEEK_SET;
113 else if (whence == CST_SEEK_RELATIVE)
114 w = SEEK_CUR;
115 else if (whence == CST_SEEK_ENDREL)
116 w = SEEK_END;
117
118 fseek(fh, pos, w);
119
120 return ftell(fh);
121 }
122
123 int cst_fprintf(cst_file fh, const char *fmt, ...)
124 {
125 va_list args;
126 int rv;
127
128 va_start(args, fmt);
129 rv = vfprintf(fh, fmt, args);
130 va_end(args);
131
132 return rv;
133 }
134
135 int cst_sprintf(char *s, const char *fmt, ...)
136 {
137 va_list args;
138 int rv;
139
140 va_start(args, fmt);
141 rv = vsprintf(s, fmt, args);
142 va_end(args);
143
144 return rv;
145 }
146
147 int cst_fclose(cst_file fh)
148 {
149 return fclose(fh);
150 }