source: tags/parabix-0.39/src/xmlbuffer.c @ 4027

Last change on this file since 4027 was 31, checked in by cameron, 12 years ago

Eliminate malloc in favor of new

File size: 1.6 KB
Line 
1/*  xmlbuffer.c - Input buffering for XML entities.
2    Copyright (c) 2007, Robert D. Cameron.
3    Licensed to the public under the Open Software License 3.0.
4    Licensed to International Characters, Inc., under the Academic
5    Free License 3.0.
6
7*/
8#include <stdio.h>
9#include <stdlib.h>
10#include <string.h>
11#include <errno.h>
12#include <sys/types.h>
13#include <sys/stat.h>
14#include "xmlbuffer.h"
15
16
17XML_Buffer::XML_Buffer(char* filename, int pad_bytes) {
18  int chars_read;
19  struct stat fileinfo;
20  stat(filename, &fileinfo);
21  /* Allocate a buffer big enough to hold the file contents and sentinel,
22     plus additional space up to the next block boundary. */
23  alloc_size = fileinfo.st_size + pad_bytes;
24  ByteBuffer = new unsigned char [alloc_size];
25  FILE *infile;
26  infile = fopen(filename, "rb");
27  if (!infile) {
28      fprintf(stderr, "Error: cannot open %s for input.\n", filename);
29      exit(-1);
30  }
31  chars_read = fread(ByteBuffer, 1, fileinfo.st_size, infile);
32  if (chars_read != fileinfo.st_size) {
33      fprintf(stderr, "Error: fread returned %i characters, expecting %i.\n", 
34              chars_read, fileinfo.st_size);
35      exit(-1);
36  }
37  buffer_bytes = fileinfo.st_size;
38  current_pos = 0;
39}
40
41void XML_Buffer::InstallPadding(const unsigned char* pad_string) {
42  strcpy((char *) &ByteBuffer[buffer_bytes], (char *) pad_string);
43}
44
45int XML_Buffer::PrepareBytes(int position, int bytes_to_prepare) {
46  int bytes_left = buffer_bytes - position;
47  current_pos = position;
48  if (bytes_to_prepare > bytes_left) {
49    return bytes_left;
50  }
51  else {
52    return bytes_to_prepare;
53  }
54}
55
56unsigned char * XML_Buffer::BytePtr(int pos) {
57  return &ByteBuffer[pos];
58}
Note: See TracBrowser for help on using the repository browser.