summaryrefslogtreecommitdiff
path: root/ACEXML/common/FileCharStream.cpp
blob: 1f66991c88455b0cd904066ae7bcce3867a23c6f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
// $Id$

#include "ACEXML/common/FileCharStream.h"
#include "ace/ACE.h"

ACEXML_FileCharStream::ACEXML_FileCharStream (void)
  : filename_ (0),
    infile_ (NULL)
{
}

ACEXML_FileCharStream::~ACEXML_FileCharStream (void)
{
  if (this->infile_ != NULL)
    ACE_OS::fclose (this->infile_);
  delete this->filename_;
}

int
ACEXML_FileCharStream::open (const ACEXML_Char *name)
{
  delete[] this->filename_;
  this->filename_ = 0;

  this->infile_ = ACE_OS::fopen (name, ACE_TEXT ("r"));
  if (this->infile_ == NULL)
    return -1;

  ACE_stat statbuf;
  if (ACE_OS::stat (name, &statbuf) < 0)
    return -1;

  this->size_ = statbuf.st_size;
  this->filename_ = ACE::strnew (name);
  return 0;
}

int
ACEXML_FileCharStream::available (void)
{
  long curr;
  if ((curr = ACE_OS::ftell (this->infile_)) < 0)
    return -1;
  return (this->size_ - curr);
}

int
ACEXML_FileCharStream::close (void)
{
  delete this->filename_;
  this->filename_ = 0;
  ACE_OS::fclose (this->infile_);
  this->infile_ = NULL;
  this->size_ = 0;
  return 0;
}

int
ACEXML_FileCharStream::get (ACEXML_Char& ch)
{
  ch = (ACEXML_Char) ACE_OS::fgetc (this->infile_);
  return (ACE_OS::feof(this->infile_) ? -1 : 0);
}

int
ACEXML_FileCharStream::read (ACEXML_Char *str,
                             size_t len)
{
  return ACE_OS::fread (str, len, 1, this->infile_);
}

int
ACEXML_FileCharStream::peek (void)
{
  ACEXML_Char ch = ACE_OS::fgetc (this->infile_);
  ACE_OS::ungetc (ch, this->infile_);
  return ch;
}