sourcepawn/compiler/scmemfil.c
branchsourcemod-1.1.0
changeset 2283 03a67e41afbc
parent 1951 c7b4237fff50
child 2287 017a725c9d17
     1.1 --- a/sourcepawn/compiler/scmemfil.c	Fri Mar 28 06:21:34 2008 +0000
     1.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.3 @@ -1,167 +0,0 @@
     1.4 -/*  Pawn compiler
     1.5 - *
     1.6 - *  Routines to maintain a "text file" in memory.
     1.7 - *
     1.8 - *  Copyright (c) ITB CompuPhase, 2003-2006
     1.9 - *
    1.10 - *  This software is provided 'as-is', without any express or implied warranty.
    1.11 - *  In no event will the authors be held liable for any damages arising from the
    1.12 - *  use of this software.
    1.13 - *
    1.14 - *  Permission is granted to anyone to use this software for any purpose,
    1.15 - *  including commercial applications, and to alter it and redistribute it
    1.16 - *  freely, subject to the following restrictions:
    1.17 - *
    1.18 - *  1. The origin of this software must not be misrepresented; you must not
    1.19 - *     claim that you wrote the original software. If you use this software in
    1.20 - *     a product, an acknowledgment in the product documentation would be
    1.21 - *     appreciated but is not required.
    1.22 - *
    1.23 - *  2. Altered source versions must be plainly marked as such, and must not be
    1.24 - *     misrepresented as being the original software.
    1.25 - *
    1.26 - *  3. This notice may not be removed or altered from any source distribution.
    1.27 - *
    1.28 - *  Version: $Id$
    1.29 - */
    1.30 -
    1.31 -#include <assert.h>
    1.32 -#include <stdio.h>
    1.33 -#include <stdlib.h>
    1.34 -#include <string.h>
    1.35 -#include "memfile.h"
    1.36 -
    1.37 -#if defined FORTIFY
    1.38 -  #include <alloc/fortify.h>
    1.39 -#endif
    1.40 -typedef memfile_t MEMFILE;
    1.41 -#define tMEMFILE  1
    1.42 -
    1.43 -#include "sc.h"
    1.44 -
    1.45 -MEMFILE *mfcreate(const char *filename)
    1.46 -{
    1.47 -  return memfile_creat(filename, 4096);
    1.48 -}
    1.49 -
    1.50 -void mfclose(MEMFILE *mf)
    1.51 -{
    1.52 -  memfile_destroy(mf);
    1.53 -}
    1.54 -
    1.55 -int mfdump(MEMFILE *mf)
    1.56 -{
    1.57 -  FILE *fp;
    1.58 -  int okay;
    1.59 -
    1.60 -  assert(mf!=NULL);
    1.61 -  /* create the file */
    1.62 -  fp=fopen(mf->name, "wb");
    1.63 -  if (fp==NULL)
    1.64 -    return 0;
    1.65 -
    1.66 -  okay=1;
    1.67 -  okay = okay & (fwrite(mf->base, mf->usedoffs, 1, fp)==(size_t)mf->usedoffs);
    1.68 -  
    1.69 -  fclose(fp);
    1.70 -  return okay;
    1.71 -}
    1.72 -
    1.73 -long mflength(const MEMFILE *mf)
    1.74 -{
    1.75 -  return mf->usedoffs;
    1.76 -}
    1.77 -
    1.78 -long mfseek(MEMFILE *mf,long offset,int whence)
    1.79 -{
    1.80 -  long length;
    1.81 -
    1.82 -  assert(mf!=NULL);
    1.83 -  if (mf->usedoffs == 0)
    1.84 -    return 0L;          /* early exit: not a single byte in the file */
    1.85 -
    1.86 -  /* find the size of the memory file */
    1.87 -  length=mflength(mf);
    1.88 -
    1.89 -  /* convert the offset to an absolute position */
    1.90 -  switch (whence) {
    1.91 -  case SEEK_SET:
    1.92 -    break;
    1.93 -  case SEEK_CUR:
    1.94 -    offset+=mf->offs;
    1.95 -    break;
    1.96 -  case SEEK_END:
    1.97 -    assert(offset<=0);
    1.98 -    offset+=length;
    1.99 -    break;
   1.100 -  } /* switch */
   1.101 -
   1.102 -  /* clamp to the file length limit */
   1.103 -  if (offset<0)
   1.104 -    offset=0;
   1.105 -  else if (offset>length)
   1.106 -    offset=length;
   1.107 -
   1.108 -  /* set new position and return it */
   1.109 -  memfile_seek(mf, offset);
   1.110 -
   1.111 -  return offset;
   1.112 -}
   1.113 -
   1.114 -unsigned int mfwrite(MEMFILE *mf,const unsigned char *buffer,unsigned int size)
   1.115 -{
   1.116 -  return (memfile_write(mf, buffer, size) ? size : 0);
   1.117 -}
   1.118 -
   1.119 -unsigned int mfread(MEMFILE *mf,unsigned char *buffer,unsigned int size)
   1.120 -{
   1.121 -  return memfile_read(mf, buffer, size);
   1.122 -}
   1.123 -
   1.124 -char *mfgets(MEMFILE *mf,char *string,unsigned int size)
   1.125 -{
   1.126 -  char *ptr;
   1.127 -  unsigned int read;
   1.128 -  long seek;
   1.129 -
   1.130 -  assert(mf!=NULL);
   1.131 -
   1.132 -  read=mfread(mf,(unsigned char *)string,size);
   1.133 -  if (read==0)
   1.134 -    return NULL;
   1.135 -  seek=0L;
   1.136 -
   1.137 -  /* make sure that the string is zero-terminated */
   1.138 -  assert(read<=size);
   1.139 -  if (read<size) {
   1.140 -    string[read]='\0';
   1.141 -  } else {
   1.142 -    string[size-1]='\0';
   1.143 -    seek=-1;            /* undo reading the character that gets overwritten */
   1.144 -  } /* if */
   1.145 -
   1.146 -  /* find the first '\n' */
   1.147 -  ptr=strchr(string,'\n');
   1.148 -  if (ptr!=NULL) {
   1.149 -    *(ptr+1)='\0';
   1.150 -    seek=(long)(ptr-string)+1-(long)read;
   1.151 -  } /* if */
   1.152 -
   1.153 -  /* undo over-read */
   1.154 -  assert(seek<=0);      /* should seek backward only */
   1.155 -  if (seek!=0)
   1.156 -    mfseek(mf,seek,SEEK_CUR);
   1.157 -
   1.158 -  return string;
   1.159 -}
   1.160 -
   1.161 -int mfputs(MEMFILE *mf,const char *string)
   1.162 -{
   1.163 -  unsigned int written,length;
   1.164 -
   1.165 -  assert(mf!=NULL);
   1.166 -
   1.167 -  length=strlen(string);
   1.168 -  written=mfwrite(mf,(unsigned char *)string,length);
   1.169 -  return written==length;
   1.170 -}