"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
17 /* $Id: wfile.c,v 1.1.2.9 2005/06/10 09:39:24 ghostgum Exp $ */
18
19 /* GFile is similar but not identical to MFC CFile. */
20 /* This implementation uses Windows APIs */
21
22 #define STRICT
23 #include <windows.h>
24 #include <stdio.h>
25
26 #include "cfile.h"
27
28 /* These are the private bits */
29 struct GFile_s {
30 #ifdef MEMORYFILE
31 /* Read from a memory buffer */
32 const char *m_pBase;
33 long m_nOffset;
34 long m_nLen;
35 #else
36 #ifdef _Windows
37 /* Windows API */
38 void *m_hFile;
39 int m_error;
40 int m_openflags;
41 #else
42 FILE *m_file;
43 #endif /* !_Windows */
44 #endif /* !MEMORYFILE */
45 };
46
47
48 #ifndef ASSERT
49 #ifdef DEBUG
50 static void gfile_assert(const char *file, int len);
51 #define ASSERT(f) if (!(f)) gfile_assert(__FILE__, __LINE__)
52 static void gfile_assert(const char *file, int line)
53 {
54 printf("Assert failed in file %s at line %d\n", file, line);
55 }
56 #else
57 #define ASSERT(f)
58 #endif
59 #endif
60
61 int
62 gfile_error(GFile *gf)
63 {
64 ASSERT(gf != NULL);
65 ASSERT(gf->m_hFile != INVALID_HANDLE_VALUE);
66 return gf->m_error;
67 }
68
69 FILE_POS gfile_get_length(GFile *gf)
70 {
71 BY_HANDLE_FILE_INFORMATION fi;
72 ASSERT(gf != NULL);
73 GetFileInformationByHandle((HANDLE)gf->m_hFile, &fi);
74 /* FIX */
75 return (FILE_POS)
76 (((unsigned __int64 )fi.nFileSizeHigh << 32) + fi.nFileSizeLow);
77 /*
78 return fi.nFileSizeLow;
79 */
80 }
81
82 BOOL gfile_get_datetime(GFile *gf, unsigned long *pdt_low,
83 unsigned long *pdt_high)
84 {
85 FILETIME datetime;
86 BOOL flag;
87 ASSERT(gf != NULL);
88 flag = GetFileTime((HANDLE)gf->m_hFile, NULL, NULL, &datetime);
89 *pdt_low = datetime.dwLowDateTime;
90 *pdt_high = datetime.dwHighDateTime;
91 return flag;
92 }
93
94 BOOL gfile_changed(GFile *gf, FILE_POS length, unsigned long dt_low,
95 unsigned long dt_high)
96 {
97 unsigned long this_dt_low, this_dt_high;
98 FILE_POS this_length = gfile_get_length(gf);
99 gfile_get_datetime(gf, &this_dt_low, &this_dt_high);
100 return ( (this_length != length) ||
101 (this_dt_low != dt_low) || (this_dt_high != dt_high));
102 }
103
104
105 GFile *gfile_open_handle(void *hFile, unsigned int nOpenFlags)
106 {
107 GFile *gf = (GFile *)malloc(sizeof(GFile));
108 if (gf == NULL) {
109 CloseHandle((HANDLE)hFile);
110 return NULL;
111 }
112 memset(gf, 0, sizeof(GFile));
113 gf->m_hFile = hFile;
114 gf->m_error = 0;
115 gf->m_openflags = nOpenFlags;
116 return gf;
117 }
118
119 GFile *gfile_open(LPCTSTR lpszFileName, unsigned int nOpenFlags)
120 {
121 GFile *gf;
122 DWORD dwAccess = GENERIC_READ;
123 DWORD dwShareMode = FILE_SHARE_READ | FILE_SHARE_WRITE;
124 DWORD dwCreate = OPEN_EXISTING;
125 HANDLE hFile;
126 ASSERT(nOpenFlags == GENERIC_READ);
127 if ((nOpenFlags & 0xf) == gfile_modeRead)
128 dwAccess = GENERIC_READ;
129 if ((nOpenFlags & 0xf) == gfile_modeWrite)
130 dwAccess = GENERIC_WRITE;
131 if ((nOpenFlags & 0xf0) == gfile_shareDenyWrite)
132 dwShareMode = FILE_SHARE_READ;
133 if ((nOpenFlags & 0xf0) == gfile_shareExclusive)
134 dwShareMode = 0;
135 if ((nOpenFlags & 0xf000) == gfile_modeCreate)
136 dwCreate = CREATE_ALWAYS;
137
138 if (lpszFileName[0] == '\0')
139 hFile = GetStdHandle(STD_OUTPUT_HANDLE);
140 else
141 hFile = CreateFile(lpszFileName, dwAccess,
142 dwShareMode, NULL, dwCreate, FILE_ATTRIBUTE_NORMAL,
143 NULL);
144 if (hFile == INVALID_HANDLE_VALUE)
145 return NULL;
146 gf = (GFile *)malloc(sizeof(GFile));
147 if (gf == NULL) {
148 CloseHandle(hFile);
149 return NULL;
150 }
151 memset(gf, 0, sizeof(GFile));
152 gf->m_hFile = hFile;
153 gf->m_error = 0;
154 gf->m_openflags = nOpenFlags;
155 return gf;
156 }
157
158 void gfile_close(GFile *gf)
159 {
160 ASSERT(gf != NULL);
161 ASSERT(gf->m_hFile != 0);
162 CloseHandle((HANDLE)gf->m_hFile);
163 gf->m_hFile = 0;
164 gf->m_error = 0;
165 free(gf);
166 }
167
168
169 UINT gfile_read(GFile *gf, void *lpBuf, UINT nCount)
170 {
171 DWORD nBytesRead;
172 ASSERT(gf != NULL);
173 ASSERT(gf->m_hFile != 0);
174 if (ReadFile((HANDLE)gf->m_hFile, lpBuf, nCount, &nBytesRead, NULL))
175 return nBytesRead;
176 else
177 gf->m_error = 1; /* file error */
178 return 0;
179 }
180
181 UINT gfile_write(GFile *gf, void *lpBuf, UINT nCount)
182 {
183 DWORD nBytesWritten;
184 ASSERT(gf != NULL);
185 ASSERT(gf->m_hFile != 0);
186 if (WriteFile((HANDLE)gf->m_hFile, lpBuf, nCount, &nBytesWritten, NULL))
187 return nBytesWritten;
188 else
189 gf->m_error = 1; /* file error */
190 return 0;
191 }
192
193 int gfile_seek(GFile *gf, FILE_OFFSET lOff, unsigned int nFrom)
194 {
195 DWORD dwMoveMethod;
196 LONG lHiOff = (LONG)((unsigned __int64)lOff >> 32);
197 ASSERT(gf != NULL);
198 ASSERT(gf->m_hFile != 0);
199 switch(nFrom) {
200 default:
201 case gfile_begin:
202 dwMoveMethod = FILE_BEGIN;
203 break;
204 case gfile_current:
205 dwMoveMethod = FILE_CURRENT;
206 break;
207 case gfile_end:
208 dwMoveMethod = FILE_END;
209 break;
210 }
211 /* return value on error is 0xffffffff */
212 return (SetFilePointer((HANDLE)gf->m_hFile,
213 (LONG)lOff, &lHiOff, dwMoveMethod) == 0xffffffff);
214 }
215
216 FILE_POS gfile_get_position(GFile *gf)
217 {
218 LONG lHiOff = 0;
219 LONG lLoOff;
220 ASSERT(gf != NULL);
221 ASSERT(gf->m_hFile != 0);
222 lLoOff = SetFilePointer((HANDLE)gf->m_hFile, 0, &lHiOff, FILE_CURRENT);
223 return (FILE_POS)(((unsigned __int64)lHiOff << 32) + lLoOff);
224 }
225
226 int gfile_puts(GFile *gf, const char *str)
227 {
228 return gfile_write(gf, str, (int)strlen(str));
229 }