"Fossies" - the Fresh Open Source Software Archive 
As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style:
standard) with prefixed line numbers and
code folding option.
Alternatively you can here
view or
download the uninterpreted source code file.
1 /* Copyright (C) 2000-2005 Ghostgum Software Pty Ltd. All rights reserved.
2
3 This software is provided AS-IS with no warranty, either express or
4 implied.
5
6 This software is distributed under licence and may not be copied,
7 modified or distributed except as expressly authorised under the terms
8 of the licence contained in the file LICENCE in this distribution.
9
10 For more information about licensing, please refer to
11 http://www.ghostgum.com.au/ or contact Ghostsgum Software Pty Ltd,
12 218 Gallaghers Rd, Glen Waverley VIC 3150, AUSTRALIA,
13 Fax +61 3 9886 6616.
14 */
15
16 /* $Id: cfile.c,v 1.5 2005/06/10 09:39:24 ghostgum Exp $ */
17
18 /* GFile is similar but not identical to MFC CFile, but is plain C. */
19 /* This implementation uses C file streams */
20
21
22 #ifdef __WIN32__
23 #include <windows.h>
24 #endif
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <sys/types.h>
29 #include <sys/stat.h>
30 #ifndef __WIN32__
31 #include <unistd.h>
32 #endif
33 #include "cfile.h"
34
35 /* These are the private bits */
36 struct GFile_s {
37 FILE *m_file;
38 time_t m_filetime; /* time/date of selected file */
39 FILE_POS m_length; /* length of selected file */
40 };
41
42 #ifndef ASSERT
43 #ifdef DEBUG
44 static void gfile_assert(const char *file, int len);
45 #define ASSERT(f) if (!(f)) gfile_assert(__FILE__, __LINE__)
46 #else
47 #define ASSERT(f)
48 #endif
49 #endif
50
51
52 int
53 gfile_error(GFile *gf)
54 {
55 ASSERT(gf != NULL);
56 ASSERT(gf->m_file != 0);
57 return ferror(gf->m_file);
58 }
59
60 FILE_POS gfile_get_length(GFile *gf)
61 {
62 struct stat fstatus;
63 fstat(fileno(gf->m_file), &fstatus);
64 return fstatus.st_size;
65 }
66
67 int gfile_get_datetime(GFile *gf, unsigned long *pdt_low,
68 unsigned long *pdt_high)
69 {
70 struct stat fstatus;
71 ASSERT(gf != NULL);
72 fstat(fileno(gf->m_file), &fstatus);
73 *pdt_low = fstatus.st_mtime;
74 *pdt_high = 0;
75 return 1;
76 }
77
78 int gfile_changed(GFile *gf, FILE_POS length,
79 unsigned long dt_low, unsigned long dt_high)
80 {
81 unsigned long this_dt_low, this_dt_high;
82 FILE_POS this_length = gfile_get_length(gf);
83 gfile_get_datetime(gf, &this_dt_low, &this_dt_high);
84 return ( (this_length != length) ||
85 (this_dt_low != dt_low) || (this_dt_high != dt_high));
86 }
87
88 GFile *gfile_open_handle(void *hFile, unsigned int nOpenFlags)
89 {
90 GFile *gf;
91 const char *access = "rb";
92 if ((nOpenFlags & 0xf) == gfile_modeWrite)
93 access = "wb";
94 gf = (GFile *)malloc(sizeof(GFile));
95 if (gf == NULL)
96 return NULL;
97 memset(gf, 0, sizeof(GFile));
98 gf->m_file = fdopen((long)hFile, access);
99 if (gf->m_file == NULL) {
100 free(gf);
101 gf = NULL;
102 }
103 return gf;
104 }
105
106 GFile *gfile_open(LPCTSTR lpszFileName, unsigned int nOpenFlags)
107 {
108 GFile *gf;
109 FILE *f;
110 const char *access = "rb";
111 if ((nOpenFlags & 0xf) == gfile_modeWrite)
112 access = "wb";
113
114 if (lpszFileName[0] == '\0')
115 f = stdout;
116 else
117 f = fopen(lpszFileName, access);
118 if (f == (FILE *)NULL)
119 return NULL;
120
121 gf = (GFile *)malloc(sizeof(GFile));
122 if (gf == NULL) {
123 fclose(f);
124 return NULL;
125 }
126 memset(gf, 0, sizeof(GFile));
127 gf->m_file = f;
128 return gf;
129 }
130
131 void gfile_close(GFile *gf)
132 {
133 ASSERT(gf != NULL);
134 ASSERT(gf->m_file != 0);
135 fclose(gf->m_file);
136 gf->m_file = NULL;
137 free(gf);
138 }
139
140
141 unsigned int gfile_read(GFile *gf, void *lpBuf, unsigned int nCount)
142 {
143 ASSERT(gf != NULL);
144 ASSERT(gf->m_file != 0);
145 return fread(lpBuf, 1, nCount, gf->m_file);
146 }
147
148 unsigned int gfile_write(GFile *gf, const void *lpBuf, unsigned int nCount)
149 {
150 ASSERT(gf != NULL);
151 ASSERT(gf->m_file != 0);
152 return fwrite(lpBuf, 1, nCount, gf->m_file);
153 }
154
155 /* only works with reading */
156 int gfile_seek(GFile *gf, FILE_OFFSET lOff, unsigned int nFrom)
157 {
158 int origin;
159 ASSERT(gf != NULL);
160 ASSERT(gf->m_file != 0);
161
162 switch(nFrom) {
163 default:
164 case gfile_begin:
165 origin = SEEK_SET;
166 break;
167 case gfile_current:
168 origin = SEEK_CUR;
169 break;
170 case gfile_end:
171 origin = SEEK_END;
172 break;
173 }
174 if ((origin == SEEK_SET) && (lOff == 0))
175 rewind(gf->m_file);
176 return fseek(gf->m_file, lOff, origin);
177 }
178
179 FILE_POS gfile_get_position(GFile *gf)
180 {
181 ASSERT(gf != NULL);
182 ASSERT(gf->m_file != 0);
183 return ftell(gf->m_file);
184 }
185
186 int gfile_puts(GFile *gf, const char *str)
187 {
188 return gfile_write(gf, str, strlen(str));
189 }