summaryrefslogtreecommitdiff
path: root/ppdc/ppdc-file.cxx
blob: 1f1d9a84e1980c3d13b532683a8be902b3f24a42 (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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
//
// "$Id$"
//
//   File class for the CUPS PPD Compiler.
//
//   Copyright 2007-2010 by Apple Inc.
//   Copyright 2002-2005 by Easy Software Products.
//
//   These coded instructions, statements, and computer programs are the
//   property of Apple Inc. and are protected by Federal copyright
//   law.  Distribution and use rights are outlined in the file "LICENSE.txt"
//   which should have been included with this file.  If this file is
//   file is missing or damaged, see the license at "http://www.cups.org/".
//
// Contents:
//
//   ppdcFile::ppdcFile()  - Create (open) a file.
//   ppdcFile::~ppdcFile() - Delete (close) a file.
//   ppdcFile::get()       - Get a character from a file.
//   ppdcFile::peek()      - Look at the next character from a file.
//

//
// Include necessary headers...
//

#include "ppdc-private.h"


//
// 'ppdcFile::ppdcFile()' - Create (open) a file.
//

ppdcFile::ppdcFile(const char  *f,		// I - File to open
                   cups_file_t *ffp)		// I - File pointer to use
{
  if (ffp)
  {
    fp = ffp;
    cupsFileRewind(fp);
  }
  else
    fp = cupsFileOpen(f, "r");

  close_on_delete = !ffp;
  filename        = f;
  line            = 1;

  if (!fp)
    _cupsLangPrintf(stderr, _("ppdc: Unable to open %s: %s"), f,
                    strerror(errno));
}


//
// 'ppdcFile::~ppdcFile()' - Delete (close) a file.
//

ppdcFile::~ppdcFile()
{
  if (close_on_delete && fp)
    cupsFileClose(fp);
}


//
// 'ppdcFile::get()' - Get a character from a file.
//

int
ppdcFile::get()
{
  int	ch;					// Character from file


  // Return EOF if there is no open file...
  if (!fp)
    return (EOF);

  // Get the character...
  ch = cupsFileGetChar(fp);

  // Update the line number as needed...
  if (ch == '\n')
    line ++;

  // Return the character...
  return (ch);
}


//
// 'ppdcFile::peek()' - Look at the next character from a file.
//

int					// O - Next character in file
ppdcFile::peek()
{
  // Return immediaely if there is no open file...
  if (!fp)
    return (EOF);

  // Otherwise return the next character without advancing...
  return (cupsFilePeekChar(fp));
}


//
// End of "$Id$".
//