| 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 defective platforms. */ |
| 36 |
/* */ |
| 37 |
/* fread and fwrite corrected to return count not numbytes */ |
| 38 |
/* [email protected] 20090124 */ |
| 39 |
/* */ |
| 40 |
/*************************************************************************/ |
| 41 |
|
| 42 |
#include <windows.h> |
| 43 |
#include <string.h> |
| 44 |
#include <stdarg.h> |
| 45 |
#include <stdlib.h> |
| 46 |
#include <stdio.h> |
| 47 |
|
| 48 |
#include "cst_file.h" |
| 49 |
#include "cst_alloc.h" |
| 50 |
#include "cst_error.h" |
| 51 |
|
| 52 |
cst_file cst_fopen(const char *path, int mode) |
| 53 |
{ |
| 54 |
size_t count = mbstowcs(NULL,path,0)+1; |
| 55 |
wchar_t *wpath = cst_alloc(wchar_t,count); |
| 56 |
cst_file fh; |
| 57 |
long access = GENERIC_READ|GENERIC_WRITE; |
| 58 |
long creation = OPEN_ALWAYS; |
| 59 |
|
| 60 |
if (((mode & CST_OPEN_READ) && (mode & CST_OPEN_WRITE)) |
| 61 |
|| ((mode & CST_OPEN_READ) && (mode & CST_OPEN_APPEND))) |
| 62 |
{ |
| 63 |
access = GENERIC_READ|GENERIC_WRITE; |
| 64 |
creation = OPEN_ALWAYS; |
| 65 |
} |
| 66 |
else if (mode & CST_OPEN_READ) |
| 67 |
{ |
| 68 |
access = GENERIC_READ; |
| 69 |
creation = OPEN_EXISTING; |
| 70 |
} |
| 71 |
else if (mode & CST_OPEN_WRITE) |
| 72 |
{ |
| 73 |
access = GENERIC_WRITE; |
| 74 |
creation = CREATE_ALWAYS; /* FIXME: Does this truncate? Argh! */ |
| 75 |
} |
| 76 |
else if (mode & CST_OPEN_APPEND) |
| 77 |
{ |
| 78 |
access = GENERIC_WRITE; |
| 79 |
creation = OPEN_ALWAYS; |
| 80 |
} |
| 81 |
|
| 82 |
/* Note, we are ignoring CST_FILE_BINARY entirely since there is no |
| 83 |
CRLF translation done by these APIs (we hope). This might cause |
| 84 |
problems for other Windows programs that try to read our output. */ |
| 85 |
mbstowcs(wpath,path,count); |
| 86 |
fh = CreateFile(wpath,access,FILE_SHARE_READ|FILE_SHARE_WRITE,NULL, |
| 87 |
creation,FILE_ATTRIBUTE_NORMAL,NULL); |
| 88 |
cst_free(wpath); |
| 89 |
if (fh == INVALID_HANDLE_VALUE) { |
| 90 |
long foo = GetLastError(); |
| 91 |
(void)foo; |
| 92 |
return NULL; |
| 93 |
} |
| 94 |
|
| 95 |
if (mode & CST_OPEN_APPEND) |
| 96 |
SetFilePointer(fh,0,NULL,FILE_END); |
| 97 |
|
| 98 |
return fh; |
| 99 |
} |
| 100 |
|
| 101 |
long cst_fwrite(cst_file fh, const void *buf, long size, long count) |
| 102 |
{ |
| 103 |
unsigned long rv; |
| 104 |
|
| 105 |
if (!WriteFile(fh,buf,size*count,&rv,NULL)) |
| 106 |
return -1; |
| 107 |
|
| 108 |
return rv/size; |
| 109 |
} |
| 110 |
|
| 111 |
long cst_fread(cst_file fh, void *buf, long size, long count) |
| 112 |
{ |
| 113 |
unsigned long rv; |
| 114 |
|
| 115 |
if (!ReadFile(fh,buf,size*count,&rv,NULL)) |
| 116 |
return -1; |
| 117 |
return rv/size; |
| 118 |
} |
| 119 |
|
| 120 |
int cst_fgetc(cst_file fh) |
| 121 |
{ |
| 122 |
char c; |
| 123 |
if (cst_fread(fh, &c, 1, 1) == 0) |
| 124 |
return EOF; |
| 125 |
return c; |
| 126 |
} |
| 127 |
|
| 128 |
long cst_filesize(cst_file fh) |
| 129 |
{ |
| 130 |
return GetFileSize(fh, NULL); |
| 131 |
} |
| 132 |
|
| 133 |
long cst_ftell(cst_file fh) |
| 134 |
{ |
| 135 |
return SetFilePointer(fh,0,NULL,FILE_CURRENT); |
| 136 |
} |
| 137 |
|
| 138 |
long cst_fseek(cst_file fh, long pos, int whence) |
| 139 |
{ |
| 140 |
int w = 0; |
| 141 |
|
| 142 |
if (whence == CST_SEEK_ABSOLUTE) |
| 143 |
w = FILE_BEGIN; |
| 144 |
else if (whence == CST_SEEK_RELATIVE) |
| 145 |
w = FILE_CURRENT; |
| 146 |
else if (whence == CST_SEEK_ENDREL) |
| 147 |
w = FILE_END; |
| 148 |
|
| 149 |
return SetFilePointer(fh,pos,NULL,w); |
| 150 |
} |
| 151 |
|
| 152 |
int cst_fprintf(cst_file fh, char *fmt, ...) |
| 153 |
{ |
| 154 |
va_list args; |
| 155 |
char outbuf[512]; |
| 156 |
int count; |
| 157 |
|
| 158 |
va_start(args,fmt); |
| 159 |
count = vsprintf(outbuf,fmt,args); /* You use WinCE, you lose. */ |
| 160 |
va_end(args); |
| 161 |
return cst_fwrite(fh,outbuf,1,count); |
| 162 |
} |
| 163 |
|
| 164 |
int cst_sprintf(char *outbuf, const char *fmt, ...) |
| 165 |
{ |
| 166 |
va_list args; |
| 167 |
int count; |
| 168 |
|
| 169 |
va_start(args,fmt); |
| 170 |
count = vsprintf(outbuf,fmt,args); /* You use WinCE, you lose. */ |
| 171 |
va_end(args); |
| 172 |
return count; |
| 173 |
} |
| 174 |
|
| 175 |
int cst_fclose(cst_file fh) |
| 176 |
{ |
| 177 |
return CloseHandle(fh); |
| 178 |
} |
| 179 |
|
| 180 |
cst_filemap *cst_mmap_file(const char *path) |
| 181 |
{ |
| 182 |
HANDLE ffm; |
| 183 |
cst_filemap *fmap = NULL; |
| 184 |
size_t count = mbstowcs(NULL,path,0) + 1; |
| 185 |
wchar_t *wpath = cst_alloc(wchar_t,count); |
| 186 |
|
| 187 |
mbstowcs(wpath,path,count); |
| 188 |
|
| 189 |
ffm = CreateFileForMapping(wpath,GENERIC_READ,FILE_SHARE_READ,NULL, |
| 190 |
OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL); |
| 191 |
cst_free(wpath); |
| 192 |
if (ffm == INVALID_HANDLE_VALUE) { |
| 193 |
return NULL; |
| 194 |
} else { |
| 195 |
fmap = cst_alloc(cst_filemap,1); |
| 196 |
fmap->fh = CreateFileMapping(ffm,NULL,PAGE_READONLY,0,0,NULL); |
| 197 |
fmap->mapsize = GetFileSize(fmap->fh, NULL); |
| 198 |
fmap->mem = MapViewOfFile(fmap->fh,FILE_MAP_READ,0,0,0); |
| 199 |
if (fmap->fh == NULL || fmap->mem == NULL) { |
| 200 |
cst_free(fmap); |
| 201 |
return NULL; |
| 202 |
} |
| 203 |
} |
| 204 |
|
| 205 |
return fmap; |
| 206 |
} |
| 207 |
|
| 208 |
int cst_munmap_file(cst_filemap *fmap) |
| 209 |
{ |
| 210 |
UnmapViewOfFile(fmap->mem); |
| 211 |
CloseHandle(fmap->fh); |
| 212 |
cst_free(fmap); |
| 213 |
return 0; |
| 214 |
} |
| 215 |
|
| 216 |
cst_filemap *cst_read_whole_file(const char *path) |
| 217 |
{ |
| 218 |
cst_filemap *fmap; |
| 219 |
cst_file fh; |
| 220 |
|
| 221 |
if ((fh = cst_fopen(path, CST_OPEN_READ)) == NULL) { |
| 222 |
cst_errmsg("cst_read_whole_file: Failed to open file\n"); |
| 223 |
return NULL; |
| 224 |
} |
| 225 |
|
| 226 |
fmap = cst_alloc(cst_filemap, 1); |
| 227 |
fmap->fh = fh; |
| 228 |
fmap->mapsize = GetFileSize(fmap->fh, NULL); |
| 229 |
fmap->mem = VirtualAlloc(NULL, fmap->mapsize, MEM_COMMIT|MEM_TOP_DOWN, |
| 230 |
PAGE_READWRITE); |
| 231 |
cst_fread(fmap->fh, fmap->mem, 1, fmap->mapsize); |
| 232 |
|
| 233 |
return fmap; |
| 234 |
} |
| 235 |
|
| 236 |
int cst_free_whole_file(cst_filemap *fmap) |
| 237 |
{ |
| 238 |
if (cst_fclose(fmap->fh) < 0) { |
| 239 |
cst_errmsg("cst_read_whole_file: cst_fclose() failed\n"); |
| 240 |
return -1; |
| 241 |
} |
| 242 |
VirtualFree(fmap->mem, fmap->mapsize, MEM_DECOMMIT); |
| 243 |
cst_free(fmap); |
| 244 |
return 0; |
| 245 |
} |
| 246 |
|
| 247 |
cst_filemap *cst_read_part_file(const char *path) |
| 248 |
{ |
| 249 |
cst_filemap *fmap; |
| 250 |
cst_file fh; |
| 251 |
|
| 252 |
if ((fh = cst_fopen(path, CST_OPEN_READ)) == NULL) { |
| 253 |
cst_errmsg("cst_read_part_file: Failed to open file\n"); |
| 254 |
return NULL; |
| 255 |
} |
| 256 |
|
| 257 |
fmap = cst_alloc(cst_filemap, 1); |
| 258 |
fmap->fh = fh; |
| 259 |
|
| 260 |
return fmap; |
| 261 |
} |
| 262 |
|
| 263 |
int cst_free_part_file(cst_filemap *fmap) |
| 264 |
{ |
| 265 |
if (cst_fclose(fmap->fh) < 0) { |
| 266 |
cst_errmsg("cst_read_part_file: cst_fclose() failed\n"); |
| 267 |
return -1; |
| 268 |
} |
| 269 |
cst_free(fmap); |
| 270 |
return 0; |
| 271 |
} |