Back to index

texmacs  1.0.7.15
mfileio.c
Go to the documentation of this file.
00001 /*  $Header: /home/cvsroot/dvipdfmx/src/mfileio.c,v 1.3 2002/10/30 02:27:11 chofchof Exp $
00002 
00003     This is dvipdfmx, an eXtended version of dvipdfm by Mark A. Wicks.
00004 
00005     Copyright (C) 2002 by Jin-Hwan Cho and Shunsaku Hirata,
00006     the dvipdfmx project team <dvipdfmx@project.ktug.or.kr>
00007     
00008     Copyright (C) 1998, 1999 by Mark A. Wicks <mwicks@kettering.edu>
00009 
00010     This program is free software; you can redistribute it and/or modify
00011     it under the terms of the GNU General Public License as published by
00012     the Free Software Foundation; either version 2 of the License, or
00013     (at your option) any later version.
00014     
00015     This program is distributed in the hope that it will be useful,
00016     but WITHOUT ANY WARRANTY; without even the implied warranty of
00017     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018     GNU General Public License for more details.
00019     
00020     You should have received a copy of the GNU General Public License
00021     along with this program; if not, write to the Free Software
00022     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
00023 */
00024 
00025 #include <stdio.h>
00026 #include <stdlib.h>
00027 #include "system.h"
00028 #include "mfileio.h"
00029 #include "error.h"
00030 
00031 #ifdef IODEBUG 
00032 static FILE *iodebug_file = NULL;
00033 static long event = 0;
00034 static void io_debug_init(void)
00035 {
00036   if (!iodebug_file) {
00037     iodebug_file = fopen ("fopen.log", "wb");
00038     fprintf (stderr, "\n*** File IO debugging started ***\n");
00039   }
00040   if (!iodebug_file) {
00041     fprintf (stderr, "\nError opening io log\n");
00042   }
00043 }
00044 #endif
00045 
00046 #ifdef IODEBUG
00047 FILE *mfopen(const char *name, const char *mode, const char *function, int line)
00048 {
00049   FILE *tmp;
00050   io_debug_init();
00051   tmp = fopen (name, mode);
00052   event += 1;
00053   fprintf(iodebug_file, "%p %07ld [fopen] %s:%d\n", tmp, event,
00054          function, line);
00055   return tmp;
00056 }
00057 int mfclose(FILE *file, const char *function, int line) 
00058 {
00059   io_debug_init();
00060   event += 1;
00061   fprintf(iodebug_file, "%p %07ld [fclose] %s:%d\n", file, event,
00062          function, line);
00063   return fclose(file);
00064 }
00065 #endif
00066 
00067 static void os_error()
00068 {
00069   ERROR ("io:  An OS command failed that should not have.\n");
00070 }
00071 
00072 void seek_absolute (FILE *file, long pos) 
00073 {
00074   if (fseek(file, pos, SEEK_SET)) {
00075     os_error();
00076   }
00077 }
00078 
00079 void seek_relative (FILE *file, long pos)
00080 {
00081   if (fseek(file, pos, SEEK_CUR)) {
00082     os_error();
00083   }
00084 }
00085 
00086 
00087 void seek_end (FILE *file) 
00088 {
00089   if (fseek(file, 0L, SEEK_END)) {
00090     os_error();
00091   }
00092 }
00093 
00094 long tell_position (FILE *file) 
00095 {
00096   long size;
00097   if ((size = ftell (file)) < 0) {
00098     os_error();
00099   }
00100   return size;
00101 }
00102 
00103 long file_size (FILE *file)
00104 {
00105   long size;
00106   /* Seek to end */
00107   seek_end (file);
00108   size = tell_position (file);
00109   rewind (file);
00110   return (size);
00111 }
00112 
00113 /* Unlike fgets, mfgets works with \r, \n, or \r\n end of lines. */
00114 char *mfgets (char *buffer, unsigned long length, FILE *file) 
00115 {
00116   int ch = 0, i = 0;
00117   while (i < length-1 && (ch = fgetc (file)) >= 0 && ch != '\n' && ch != '\r')
00118     buffer[i++] = ch;
00119   buffer[i] = 0;
00120   if (ch < 0 && i == 0)
00121     return NULL;
00122   if (ch == '\r' && (ch = fgetc (file)) >= 0 && (ch != '\n'))
00123     ungetc (ch, file);
00124   return buffer;
00125 }
00126 
00127 char work_buffer[WORK_BUFFER_SIZE];