sourcepawn/compiler/scmemfil.c
author David Anderson <dvander@alliedmods.net>
Wed Feb 25 20:07:12 2009 -0500 (2009-02-25)
changeset 2607 f3b8bbe17f85
parent 1952 b7aa2b626807
permissions -rw-r--r--
Backed out changeset fa286305bd3f
ds@1952
     1
/*  Pawn compiler
ds@1952
     2
 *
ds@1952
     3
 *  Routines to maintain a "text file" in memory.
ds@1952
     4
 *
ds@1952
     5
 *  Copyright (c) ITB CompuPhase, 2003-2006
ds@1952
     6
 *
ds@1952
     7
 *  This software is provided 'as-is', without any express or implied warranty.
ds@1952
     8
 *  In no event will the authors be held liable for any damages arising from the
ds@1952
     9
 *  use of this software.
ds@1952
    10
 *
ds@1952
    11
 *  Permission is granted to anyone to use this software for any purpose,
ds@1952
    12
 *  including commercial applications, and to alter it and redistribute it
ds@1952
    13
 *  freely, subject to the following restrictions:
ds@1952
    14
 *
ds@1952
    15
 *  1. The origin of this software must not be misrepresented; you must not
ds@1952
    16
 *     claim that you wrote the original software. If you use this software in
ds@1952
    17
 *     a product, an acknowledgment in the product documentation would be
ds@1952
    18
 *     appreciated but is not required.
ds@1952
    19
 *
ds@1952
    20
 *  2. Altered source versions must be plainly marked as such, and must not be
ds@1952
    21
 *     misrepresented as being the original software.
ds@1952
    22
 *
ds@1952
    23
 *  3. This notice may not be removed or altered from any source distribution.
ds@1952
    24
 *
ds@1952
    25
 *  Version: $Id$
ds@1952
    26
 */
ds@1952
    27
ds@1952
    28
#include <assert.h>
ds@1952
    29
#include <stdio.h>
ds@1952
    30
#include <stdlib.h>
ds@1952
    31
#include <string.h>
ds@1952
    32
#include "memfile.h"
ds@1952
    33
ds@1952
    34
#if defined FORTIFY
ds@1952
    35
  #include <alloc/fortify.h>
ds@1952
    36
#endif
ds@1952
    37
typedef memfile_t MEMFILE;
ds@1952
    38
#define tMEMFILE  1
ds@1952
    39
ds@1952
    40
#include "sc.h"
ds@1952
    41
ds@1952
    42
MEMFILE *mfcreate(const char *filename)
ds@1952
    43
{
ds@1952
    44
  return memfile_creat(filename, 4096);
ds@1952
    45
}
ds@1952
    46
ds@1952
    47
void mfclose(MEMFILE *mf)
ds@1952
    48
{
ds@1952
    49
  memfile_destroy(mf);
ds@1952
    50
}
ds@1952
    51
ds@1952
    52
int mfdump(MEMFILE *mf)
ds@1952
    53
{
ds@1952
    54
  FILE *fp;
ds@1952
    55
  int okay;
ds@1952
    56
ds@1952
    57
  assert(mf!=NULL);
ds@1952
    58
  /* create the file */
ds@1952
    59
  fp=fopen(mf->name, "wb");
ds@1952
    60
  if (fp==NULL)
ds@1952
    61
    return 0;
ds@1952
    62
ds@1952
    63
  okay=1;
ds@1952
    64
  okay = okay & (fwrite(mf->base, mf->usedoffs, 1, fp)==(size_t)mf->usedoffs);
ds@1952
    65
  
ds@1952
    66
  fclose(fp);
ds@1952
    67
  return okay;
ds@1952
    68
}
ds@1952
    69
ds@1952
    70
long mflength(const MEMFILE *mf)
ds@1952
    71
{
ds@1952
    72
  return mf->usedoffs;
ds@1952
    73
}
ds@1952
    74
ds@1952
    75
long mfseek(MEMFILE *mf,long offset,int whence)
ds@1952
    76
{
ds@1952
    77
  long length;
ds@1952
    78
ds@1952
    79
  assert(mf!=NULL);
ds@1952
    80
  if (mf->usedoffs == 0)
ds@1952
    81
    return 0L;          /* early exit: not a single byte in the file */
ds@1952
    82
ds@1952
    83
  /* find the size of the memory file */
ds@1952
    84
  length=mflength(mf);
ds@1952
    85
ds@1952
    86
  /* convert the offset to an absolute position */
ds@1952
    87
  switch (whence) {
ds@1952
    88
  case SEEK_SET:
ds@1952
    89
    break;
ds@1952
    90
  case SEEK_CUR:
ds@1952
    91
    offset+=mf->offs;
ds@1952
    92
    break;
ds@1952
    93
  case SEEK_END:
ds@1952
    94
    assert(offset<=0);
ds@1952
    95
    offset+=length;
ds@1952
    96
    break;
ds@1952
    97
  } /* switch */
ds@1952
    98
ds@1952
    99
  /* clamp to the file length limit */
ds@1952
   100
  if (offset<0)
ds@1952
   101
    offset=0;
ds@1952
   102
  else if (offset>length)
ds@1952
   103
    offset=length;
ds@1952
   104
ds@1952
   105
  /* set new position and return it */
ds@1952
   106
  memfile_seek(mf, offset);
ds@1952
   107
ds@1952
   108
  return offset;
ds@1952
   109
}
ds@1952
   110
ds@1952
   111
unsigned int mfwrite(MEMFILE *mf,const unsigned char *buffer,unsigned int size)
ds@1952
   112
{
ds@1952
   113
  return (memfile_write(mf, buffer, size) ? size : 0);
ds@1952
   114
}
ds@1952
   115
ds@1952
   116
unsigned int mfread(MEMFILE *mf,unsigned char *buffer,unsigned int size)
ds@1952
   117
{
ds@1952
   118
  return memfile_read(mf, buffer, size);
ds@1952
   119
}
ds@1952
   120
ds@1952
   121
char *mfgets(MEMFILE *mf,char *string,unsigned int size)
ds@1952
   122
{
ds@1952
   123
  char *ptr;
ds@1952
   124
  unsigned int read;
ds@1952
   125
  long seek;
ds@1952
   126
ds@1952
   127
  assert(mf!=NULL);
ds@1952
   128
ds@1952
   129
  read=mfread(mf,(unsigned char *)string,size);
ds@1952
   130
  if (read==0)
ds@1952
   131
    return NULL;
ds@1952
   132
  seek=0L;
ds@1952
   133
ds@1952
   134
  /* make sure that the string is zero-terminated */
ds@1952
   135
  assert(read<=size);
ds@1952
   136
  if (read<size) {
ds@1952
   137
    string[read]='\0';
ds@1952
   138
  } else {
ds@1952
   139
    string[size-1]='\0';
ds@1952
   140
    seek=-1;            /* undo reading the character that gets overwritten */
ds@1952
   141
  } /* if */
ds@1952
   142
ds@1952
   143
  /* find the first '\n' */
ds@1952
   144
  ptr=strchr(string,'\n');
ds@1952
   145
  if (ptr!=NULL) {
ds@1952
   146
    *(ptr+1)='\0';
ds@1952
   147
    seek=(long)(ptr-string)+1-(long)read;
ds@1952
   148
  } /* if */
ds@1952
   149
ds@1952
   150
  /* undo over-read */
ds@1952
   151
  assert(seek<=0);      /* should seek backward only */
ds@1952
   152
  if (seek!=0)
ds@1952
   153
    mfseek(mf,seek,SEEK_CUR);
ds@1952
   154
ds@1952
   155
  return string;
ds@1952
   156
}
ds@1952
   157
ds@1952
   158
int mfputs(MEMFILE *mf,const char *string)
ds@1952
   159
{
ds@1952
   160
  unsigned int written,length;
ds@1952
   161
ds@1952
   162
  assert(mf!=NULL);
ds@1952
   163
ds@1952
   164
  length=strlen(string);
ds@1952
   165
  written=mfwrite(mf,(unsigned char *)string,length);
ds@1952
   166
  return written==length;
ds@1952
   167
}