BAM
Abstract Machine for Bottom-Up Evaluation with the Push Method
in_file.h
Go to the documentation of this file.
1 // ============================================================================
2 // Project: Deductive Database
3 // Filename: in_file.h
4 // Purpose: Get Input From a File
5 // Last Change: 04.08.2017
6 // Language: C++
7 // EMail: brass@informatik.uni-halle.de
8 // WWW: http://www.informatik.uni-halle.de/~brass/
9 // Address: Feldschloesschen 15, D-06120 Halle (Saale), GERMANY
10 // Copyright: (c) 1998-2017 by Stefan Brass
11 // License: See file "LICENSE" for copying conditions.
12 // Note: There is no warranty at all - this code may contain bugs.
13 // ============================================================================
14 
15 
22 //=============================================================================
23 // Include File Frame:
24 //=============================================================================
25 
26 #ifndef IN_FILE_INCLUDED
27 #define IN_FILE_INCLUDED
28 
29 //=============================================================================
30 // Used Types and Macros:
31 //=============================================================================
32 
33 #ifndef VER_INCLUDED
34 #include "../base/ver.h"
35 #endif
36 
37 #if VER_IOSTREAM
38 #include <iostream>
39 #include <fstream>
40 #endif
41 
42 #ifndef STR_INCLUDED
43 #include "../base/str.h"
44 #endif
45 
46 #ifndef CHECK_INCLUDED
47 #include "../base/check.h"
48 #endif
49 
50 #ifndef IN_INCLUDED
51 #include "in.h"
52 #endif
53 
54 //=============================================================================
55 // Constants:
56 //=============================================================================
57 
58 //-----------------------------------------------------------------------------
59 // IN_FILE_NAMESIZE: Maximal filename length.
60 //-----------------------------------------------------------------------------
61 
62 static const int IN_FILE_NAMESIZE = 128;
63 
64 //-----------------------------------------------------------------------------
65 // IN_FILE_CGI_FILENAME_CHARS: Valid characters in filenames read via web.
66 //-----------------------------------------------------------------------------
67 
68 static const str_t IN_FILE_CGI_FILENAME_CHARS =
69  "_abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
70  // Make sure that / or \ are not part of this string (for security).
71  // In addition, the program permits the extension ".txt".
72 
73 //=============================================================================
74 // Class for Input File:
75 //=============================================================================
76 
77 class in_file_c : public in_c {
78  public:
79 
80 //-----------------------------------------------------------------------------
81 // Constructor:
82 //-----------------------------------------------------------------------------
83 
84  in_file_c(str_t filename);
85 
86 //-----------------------------------------------------------------------------
87 // Destructor:
88 //-----------------------------------------------------------------------------
89 
90  ~in_file_c();
91 
92 //-----------------------------------------------------------------------------
93 // close: Close input file explicitly.
94 //-----------------------------------------------------------------------------
95 
96  bool close();
97 
98 //-----------------------------------------------------------------------------
99 // read: Read characters from input file into buffer.
100 //-----------------------------------------------------------------------------
101 
102  int read(char *buf, int size);
103 
104 //-----------------------------------------------------------------------------
105 // is_open: Is this input stream currently open?
106 //-----------------------------------------------------------------------------
107 
108  inline bool is_open()
109 #if VER_IOSTREAM
110  { return(File.is_open()); }
111 #else
112  { return(Fd != -1); }
113 #endif
114 
115 //-----------------------------------------------------------------------------
116 // filename: Name of this file.
117 //-----------------------------------------------------------------------------
118 
119  inline str_t filename()
120  { return(Filename); }
121 
122 //=============================================================================
123 // Private Class Members:
124 //=============================================================================
125 
126  private:
127 
128 //-----------------------------------------------------------------------------
129 // Fd: The file descriptor of the input file.
130 //-----------------------------------------------------------------------------
131 
132 #if VER_IOSTREAM
133  std::ifstream File;
134 #else
135  int Fd;
136 #endif
137 
138 //-----------------------------------------------------------------------------
139 // Filename: The name of the input file.
140 //-----------------------------------------------------------------------------
141 
142  char Filename[IN_FILE_NAMESIZE+1];
143 
144 //=============================================================================
145 // End of Class:
146 //=============================================================================
147 
148 };
149 
150 //-----------------------------------------------------------------------------
151 // Define pointer type:
152 //-----------------------------------------------------------------------------
153 
154 typedef in_file_c *in_file_t;
155 
156 //-----------------------------------------------------------------------------
157 // Define null pointer:
158 //-----------------------------------------------------------------------------
159 
160 #define IN_FILE_NULL (static_cast<in_file_t>(0))
161 
162 //=============================================================================
163 // End of File:
164 //=============================================================================
165 
166 #endif
167 
Definition: in.h:50
Superclass of input sources (e.g., file)
Definition: in_file.h:77
const char * str_t
Definition: str.h:41