| 1 |
/* $Header: /home/cvsroot/dvipdfmx/src/mfileio.c,v 1.3 2002/10/30 02:27:11 chofchof Exp $ |
| 2 |
|
| 3 |
This is dvipdfmx, an eXtended version of dvipdfm by Mark A. Wicks. |
| 4 |
|
| 5 |
Copyright (C) 2002-2015 by Jin-Hwan Cho and Shunsaku Hirata, |
| 6 |
the dvipdfmx project team <[email protected]> |
| 7 |
|
| 8 |
Copyright (C) 1998, 1999 by Mark A. Wicks <[email protected]> |
| 9 |
|
| 10 |
This program is free software; you can redistribute it and/or modify |
| 11 |
it under the terms of the GNU General Public License as published by |
| 12 |
the Free Software Foundation; either version 2 of the License, or |
| 13 |
(at your option) any later version. |
| 14 |
|
| 15 |
This program is distributed in the hope that it will be useful, |
| 16 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 |
GNU General Public License for more details. |
| 19 |
|
| 20 |
You should have received a copy of the GNU General Public License |
| 21 |
along with this program; if not, write to the Free Software |
| 22 |
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. |
| 23 |
*/ |
| 24 |
|
| 25 |
#if HAVE_CONFIG_H |
| 26 |
#include <w2c/config.h> |
| 27 |
#endif |
| 28 |
|
| 29 |
#include "mfileio.h" |
| 30 |
|
| 31 |
#ifdef IODEBUG |
| 32 |
static FILE *iodebug_file = NULL; |
| 33 |
static long event = 0; |
| 34 |
static void io_debug_init(void) |
| 35 |
{ |
| 36 |
if (!iodebug_file) { |
| 37 |
iodebug_file = fopen ("fopen.log", "wb"); |
| 38 |
fprintf (stderr, "\n*** File IO debugging started ***\n"); |
| 39 |
} |
| 40 |
if (!iodebug_file) { |
| 41 |
fprintf (stderr, "\nError opening io log\n"); |
| 42 |
} |
| 43 |
} |
| 44 |
#endif |
| 45 |
|
| 46 |
#ifdef IODEBUG |
| 47 |
FILE *mfopen(const char *name, const char *mode, const char *function, int line) |
| 48 |
{ |
| 49 |
FILE *tmp; |
| 50 |
io_debug_init(); |
| 51 |
tmp = fopen (name, mode); |
| 52 |
event += 1; |
| 53 |
fprintf(iodebug_file, "%p %07ld [fopen] %s:%d\n", tmp, event, |
| 54 |
function, line); |
| 55 |
return tmp; |
| 56 |
} |
| 57 |
int mfclose(FILE *file, const char *function, int line) |
| 58 |
{ |
| 59 |
io_debug_init(); |
| 60 |
event += 1; |
| 61 |
fprintf(iodebug_file, "%p %07ld [fclose] %s:%d\n", file, event, |
| 62 |
function, line); |
| 63 |
return fclose(file); |
| 64 |
} |
| 65 |
#endif |
| 66 |
|
| 67 |
static void os_error(void) |
| 68 |
{ |
| 69 |
fprintf (stderr, "io: An OS command failed that should not have.\n"); |
| 70 |
exit(-1); |
| 71 |
} |
| 72 |
|
| 73 |
void seek_absolute (FILE *file, long pos) |
| 74 |
{ |
| 75 |
if (fseek(file, pos, SEEK_SET)) { |
| 76 |
os_error(); |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
void seek_relative (FILE *file, long pos) |
| 81 |
{ |
| 82 |
if (fseek(file, pos, SEEK_CUR)) { |
| 83 |
os_error(); |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
|
| 88 |
void seek_end (FILE *file) |
| 89 |
{ |
| 90 |
if (fseek(file, 0L, SEEK_END)) { |
| 91 |
os_error(); |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
long tell_position (FILE *file) |
| 96 |
{ |
| 97 |
long size; |
| 98 |
if ((size = ftell (file)) < 0) { |
| 99 |
os_error(); |
| 100 |
} |
| 101 |
return size; |
| 102 |
} |
| 103 |
|
| 104 |
long file_size (FILE *file) |
| 105 |
{ |
| 106 |
long size; |
| 107 |
/* Seek to end */ |
| 108 |
seek_end (file); |
| 109 |
size = tell_position (file); |
| 110 |
rewind (file); |
| 111 |
return (size); |
| 112 |
} |
| 113 |
|
| 114 |
/* Unlike fgets, mfgets works with \r, \n, or \r\n end of lines. */ |
| 115 |
char *mfgets (char *buffer, int length, FILE *file) |
| 116 |
{ |
| 117 |
int ch = 0, i = 0; |
| 118 |
while (i < length-1 && (ch = fgetc (file)) >= 0 && ch != '\n' && ch != '\r') |
| 119 |
buffer[i++] = ch; |
| 120 |
buffer[i] = 0; |
| 121 |
if (ch < 0 && i == 0) |
| 122 |
return NULL; |
| 123 |
if (ch == '\r' && (ch = fgetc (file)) >= 0 && (ch != '\n')) |
| 124 |
ungetc (ch, file); |
| 125 |
return buffer; |
| 126 |
} |
| 127 |
|
| 128 |
char work_buffer[WORK_BUFFER_SIZE]; |