source: trunk/src/xmlbuffer.c @ 4

Last change on this file since 4 was 4, checked in by cameron, 11 years ago

Initial import of parabix-0.36

File size: 1.8 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 = (unsigned char *) malloc(alloc_size);
25  if (ByteBuffer == NULL) {
26      fprintf(stderr, "Error: buffer for %s of size %i cannot be created.\n", 
27              filename, alloc_size);
28      exit(-1);
29  }
30  FILE *infile;
31  infile = fopen(filename, "rb");
32  if (!infile) {
33      fprintf(stderr, "Error: cannot open %s for input.\n", filename);
34      exit(-1);
35  }
36  chars_read = fread(ByteBuffer, 1, fileinfo.st_size, infile);
37  if (chars_read != fileinfo.st_size) {
38      fprintf(stderr, "Error: fread returned %i characters, expecting %i.\n", 
39              chars_read, fileinfo.st_size);
40      exit(-1);
41  }
42  buffer_bytes = fileinfo.st_size;
43  current_pos = 0;
44}
45
46void XML_Buffer::InstallPadding(const unsigned char* pad_string) {
47  strcpy((char *) &ByteBuffer[buffer_bytes], (char *) pad_string);
48}
49
50int XML_Buffer::PrepareBytes(int bytes_to_prepare) {
51  int bytes_left = buffer_bytes - current_pos;
52  if (bytes_to_prepare > bytes_left) {
53    return bytes_left;
54  }
55  else {
56    return bytes_to_prepare;
57  }
58}
59
60unsigned char * XML_Buffer::GetBytePtr(int pos) {
61  current_pos = pos;
62  return &ByteBuffer[current_pos];
63}
Note: See TracBrowser for help on using the repository browser.