xref: /aosp_15_r20/external/libcups/ppdc/ppdc-file.cxx (revision 5e7646d21f1134fb0638875d812ef646c12ab91e)
1*5e7646d2SAndroid Build Coastguard Worker //
2*5e7646d2SAndroid Build Coastguard Worker // File class for the CUPS PPD Compiler.
3*5e7646d2SAndroid Build Coastguard Worker //
4*5e7646d2SAndroid Build Coastguard Worker // Copyright 2007-2010 by Apple Inc.
5*5e7646d2SAndroid Build Coastguard Worker // Copyright 2002-2005 by Easy Software Products.
6*5e7646d2SAndroid Build Coastguard Worker //
7*5e7646d2SAndroid Build Coastguard Worker // Licensed under Apache License v2.0.  See the file "LICENSE" for more information.
8*5e7646d2SAndroid Build Coastguard Worker //
9*5e7646d2SAndroid Build Coastguard Worker 
10*5e7646d2SAndroid Build Coastguard Worker //
11*5e7646d2SAndroid Build Coastguard Worker // Include necessary headers...
12*5e7646d2SAndroid Build Coastguard Worker //
13*5e7646d2SAndroid Build Coastguard Worker 
14*5e7646d2SAndroid Build Coastguard Worker #include "ppdc-private.h"
15*5e7646d2SAndroid Build Coastguard Worker 
16*5e7646d2SAndroid Build Coastguard Worker 
17*5e7646d2SAndroid Build Coastguard Worker //
18*5e7646d2SAndroid Build Coastguard Worker // 'ppdcFile::ppdcFile()' - Create (open) a file.
19*5e7646d2SAndroid Build Coastguard Worker //
20*5e7646d2SAndroid Build Coastguard Worker 
ppdcFile(const char * f,cups_file_t * ffp)21*5e7646d2SAndroid Build Coastguard Worker ppdcFile::ppdcFile(const char  *f,		// I - File to open
22*5e7646d2SAndroid Build Coastguard Worker                    cups_file_t *ffp)		// I - File pointer to use
23*5e7646d2SAndroid Build Coastguard Worker {
24*5e7646d2SAndroid Build Coastguard Worker   if (ffp)
25*5e7646d2SAndroid Build Coastguard Worker   {
26*5e7646d2SAndroid Build Coastguard Worker     fp = ffp;
27*5e7646d2SAndroid Build Coastguard Worker     cupsFileRewind(fp);
28*5e7646d2SAndroid Build Coastguard Worker   }
29*5e7646d2SAndroid Build Coastguard Worker   else
30*5e7646d2SAndroid Build Coastguard Worker     fp = cupsFileOpen(f, "r");
31*5e7646d2SAndroid Build Coastguard Worker 
32*5e7646d2SAndroid Build Coastguard Worker   close_on_delete = !ffp;
33*5e7646d2SAndroid Build Coastguard Worker   filename        = f;
34*5e7646d2SAndroid Build Coastguard Worker   line            = 1;
35*5e7646d2SAndroid Build Coastguard Worker 
36*5e7646d2SAndroid Build Coastguard Worker   if (!fp)
37*5e7646d2SAndroid Build Coastguard Worker     _cupsLangPrintf(stderr, _("ppdc: Unable to open %s: %s"), f,
38*5e7646d2SAndroid Build Coastguard Worker                     strerror(errno));
39*5e7646d2SAndroid Build Coastguard Worker }
40*5e7646d2SAndroid Build Coastguard Worker 
41*5e7646d2SAndroid Build Coastguard Worker 
42*5e7646d2SAndroid Build Coastguard Worker //
43*5e7646d2SAndroid Build Coastguard Worker // 'ppdcFile::~ppdcFile()' - Delete (close) a file.
44*5e7646d2SAndroid Build Coastguard Worker //
45*5e7646d2SAndroid Build Coastguard Worker 
~ppdcFile()46*5e7646d2SAndroid Build Coastguard Worker ppdcFile::~ppdcFile()
47*5e7646d2SAndroid Build Coastguard Worker {
48*5e7646d2SAndroid Build Coastguard Worker   if (close_on_delete && fp)
49*5e7646d2SAndroid Build Coastguard Worker     cupsFileClose(fp);
50*5e7646d2SAndroid Build Coastguard Worker }
51*5e7646d2SAndroid Build Coastguard Worker 
52*5e7646d2SAndroid Build Coastguard Worker 
53*5e7646d2SAndroid Build Coastguard Worker //
54*5e7646d2SAndroid Build Coastguard Worker // 'ppdcFile::get()' - Get a character from a file.
55*5e7646d2SAndroid Build Coastguard Worker //
56*5e7646d2SAndroid Build Coastguard Worker 
57*5e7646d2SAndroid Build Coastguard Worker int
get()58*5e7646d2SAndroid Build Coastguard Worker ppdcFile::get()
59*5e7646d2SAndroid Build Coastguard Worker {
60*5e7646d2SAndroid Build Coastguard Worker   int	ch;					// Character from file
61*5e7646d2SAndroid Build Coastguard Worker 
62*5e7646d2SAndroid Build Coastguard Worker 
63*5e7646d2SAndroid Build Coastguard Worker   // Return EOF if there is no open file...
64*5e7646d2SAndroid Build Coastguard Worker   if (!fp)
65*5e7646d2SAndroid Build Coastguard Worker     return (EOF);
66*5e7646d2SAndroid Build Coastguard Worker 
67*5e7646d2SAndroid Build Coastguard Worker   // Get the character...
68*5e7646d2SAndroid Build Coastguard Worker   ch = cupsFileGetChar(fp);
69*5e7646d2SAndroid Build Coastguard Worker 
70*5e7646d2SAndroid Build Coastguard Worker   // Update the line number as needed...
71*5e7646d2SAndroid Build Coastguard Worker   if (ch == '\n')
72*5e7646d2SAndroid Build Coastguard Worker     line ++;
73*5e7646d2SAndroid Build Coastguard Worker 
74*5e7646d2SAndroid Build Coastguard Worker   // Return the character...
75*5e7646d2SAndroid Build Coastguard Worker   return (ch);
76*5e7646d2SAndroid Build Coastguard Worker }
77*5e7646d2SAndroid Build Coastguard Worker 
78*5e7646d2SAndroid Build Coastguard Worker 
79*5e7646d2SAndroid Build Coastguard Worker //
80*5e7646d2SAndroid Build Coastguard Worker // 'ppdcFile::peek()' - Look at the next character from a file.
81*5e7646d2SAndroid Build Coastguard Worker //
82*5e7646d2SAndroid Build Coastguard Worker 
83*5e7646d2SAndroid Build Coastguard Worker int					// O - Next character in file
peek()84*5e7646d2SAndroid Build Coastguard Worker ppdcFile::peek()
85*5e7646d2SAndroid Build Coastguard Worker {
86*5e7646d2SAndroid Build Coastguard Worker   // Return immediaely if there is no open file...
87*5e7646d2SAndroid Build Coastguard Worker   if (!fp)
88*5e7646d2SAndroid Build Coastguard Worker     return (EOF);
89*5e7646d2SAndroid Build Coastguard Worker 
90*5e7646d2SAndroid Build Coastguard Worker   // Otherwise return the next character without advancing...
91*5e7646d2SAndroid Build Coastguard Worker   return (cupsFilePeekChar(fp));
92*5e7646d2SAndroid Build Coastguard Worker }
93