ABC: A System for Sequential Synthesis and Verification
 
Loading...
Searching...
No Matches
file.hpp
Go to the documentation of this file.
1#ifndef _file_hpp_INCLUDED
2#define _file_hpp_INCLUDED
3
4#include "global.h"
5
6#include <cassert>
7#include <cstdint>
8#include <cstdio>
9#include <cstdlib>
10#include <vector>
11
12#ifndef CADICAL_NDEBUG
13#include <climits>
14#endif
15
16/*------------------------------------------------------------------------*/
17#ifdef WIN32
18#define cadical_putc_unlocked putc
19#define cadical_getc_unlocked getc
20#else
21#ifndef NUNLOCKED
22#define cadical_putc_unlocked putc_unlocked
23#define cadical_getc_unlocked getc_unlocked
24#else
25#define cadical_putc_unlocked putc
26#define cadical_getc_unlocked getc
27#endif
28#endif
29/*------------------------------------------------------------------------*/
30
32
33namespace CaDiCaL {
34
35// Wraps a 'C' file 'FILE' with name and supports zipped reading and writing
36// through 'popen' using external helper tools. Reading has line numbers.
37// Compression and decompression relies on external utilities, e.g., 'gzip',
38// 'bzip2', 'xz', and '7z', which should be in the 'PATH'.
39
40struct Internal;
41
42class File {
43
44 Internal *internal;
45#if !defined(CADICAL_QUIET) || !defined(CADICAL_NDEBUG)
46 bool writing;
47#endif
48
49 int close_file; // need to close file (1=fclose, 2=pclose, 3=pipe)
50 int child_pid;
51 FILE *file;
52 char *_name;
53 uint64_t _lineno;
54 uint64_t _bytes;
55
56 File (Internal *, bool, int, int, FILE *, const char *);
57
58 static FILE *open_file (Internal *, const char *path, const char *mode);
59 static FILE *read_file (Internal *, const char *path);
60 static FILE *write_file (Internal *, const char *path);
61
62 static void split_str (const char *, std::vector<char *> &);
63 static void delete_str_vector (std::vector<char *> &);
64
65 static FILE *open_pipe (Internal *, const char *fmt, const char *path,
66 const char *mode);
67 static FILE *read_pipe (Internal *, const char *fmt, const int *sig,
68 const char *path);
69#ifndef WIN32
70 static FILE *write_pipe (Internal *, const char *fmt, const char *path,
71 int &child_pid);
72#endif
73
74public:
75 static char *find_program (const char *prg); // search in 'PATH'
76 static bool exists (const char *path); // file exists?
77 static bool writable (const char *path); // can write to that file?
78 static size_t size (const char *path); // file size in bytes
79
80 bool piping (); // Is opened file a pipe?
81
82 // Does the file match the file type signature.
83 //
84 static bool match (Internal *, const char *path, const int *sig);
85
86 // Read from existing file. Assume given name.
87 //
88 static File *read (Internal *, FILE *f, const char *name);
89
90 // Open file from path name for reading (possibly through opening a pipe
91 // to a decompression utility, based on the suffix).
92 //
93 static File *read (Internal *, const char *path);
94
95 // Same for writing as for reading above.
96 //
97 static File *write (Internal *, FILE *, const char *name);
98 static File *write (Internal *, const char *path);
99
100 ~File ();
101
102 // Using the 'unlocked' versions here is way faster but
103 // not thread safe if the same file is used by different
104 // threads, which on the other hand currently is impossible.
105
106 int get () {
107 CADICAL_assert (!writing);
108 int res = cadical_getc_unlocked (file);
109 if (res == '\n')
110 _lineno++;
111 if (res != EOF)
112 _bytes++;
113 return res;
114 }
115
116 bool put (char ch) {
117 CADICAL_assert (writing);
118 if (cadical_putc_unlocked (ch, file) == EOF)
119 return false;
120 _bytes++;
121 return true;
122 }
123
124 bool endl () { return put ('\n'); }
125
126 bool put (unsigned char ch) {
127 CADICAL_assert (writing);
128 if (cadical_putc_unlocked (ch, file) == EOF)
129 return false;
130 _bytes++;
131 return true;
132 }
133
134 bool put (const char *s) {
135 for (const char *p = s; *p; p++)
136 if (!put (*p))
137 return false;
138 return true;
139 }
140
141 bool put (int lit) {
142 CADICAL_assert (writing);
143 if (!lit)
144 return put ('0');
145 else if (lit == -2147483648) {
146 CADICAL_assert (lit == INT_MIN);
147 return put ("-2147483648");
148 } else {
149 char buffer[11];
150 int i = sizeof buffer;
151 buffer[--i] = 0;
152 CADICAL_assert (lit != INT_MIN);
153 unsigned idx = abs (lit);
154 while (idx) {
155 CADICAL_assert (i > 0);
156 buffer[--i] = '0' + idx % 10;
157 idx /= 10;
158 }
159 if (lit < 0 && !put ('-'))
160 return false;
161 return put (buffer + i);
162 }
163 }
164
165 bool put (int64_t l) {
166 CADICAL_assert (writing);
167 if (!l)
168 return put ('0');
169 else if (l == INT64_MIN) {
170 CADICAL_assert (sizeof l == 8);
171 return put ("-9223372036854775808");
172 } else {
173 char buffer[21];
174 int i = sizeof buffer;
175 buffer[--i] = 0;
176 CADICAL_assert (l != INT64_MIN);
177 uint64_t k = l < 0 ? -l : l;
178 while (k) {
179 CADICAL_assert (i > 0);
180 buffer[--i] = '0' + k % 10;
181 k /= 10;
182 }
183 if (l < 0 && !put ('-'))
184 return false;
185 return put (buffer + i);
186 }
187 }
188
189 bool put (uint64_t l) {
190 CADICAL_assert (writing);
191 if (!l)
192 return put ('0');
193 else {
194 char buffer[22];
195 int i = sizeof buffer;
196 buffer[--i] = 0;
197 while (l) {
198 CADICAL_assert (i > 0);
199 buffer[--i] = '0' + l % 10;
200 l /= 10;
201 }
202 return put (buffer + i);
203 }
204 }
205
206 const char *name () const { return _name; }
207 uint64_t lineno () const { return _lineno; }
208 uint64_t bytes () const { return _bytes; }
209
210 void connect_internal (Internal *i) { internal = i; }
211 bool closed () { return !file; }
212
213 void close (bool print = false);
214 void flush ();
215};
216
217} // namespace CaDiCaL
218
220
221#endif
#define ABC_NAMESPACE_CXX_HEADER_START
#define ABC_NAMESPACE_CXX_HEADER_END
#define CADICAL_assert(ignore)
Definition global.h:14
bool put(char ch)
Definition file.hpp:116
bool closed()
Definition file.hpp:211
int get()
Definition file.hpp:106
const char * name() const
Definition file.hpp:206
static bool match(Internal *, const char *path, const int *sig)
bool put(const char *s)
Definition file.hpp:134
uint64_t bytes() const
Definition file.hpp:208
bool put(unsigned char ch)
Definition file.hpp:126
static char * find_program(const char *prg)
uint64_t lineno() const
Definition file.hpp:207
void close(bool print=false)
static bool exists(const char *path)
static File * write(Internal *, FILE *, const char *name)
void connect_internal(Internal *i)
Definition file.hpp:210
static File * read(Internal *, FILE *f, const char *name)
bool put(uint64_t l)
Definition file.hpp:189
static size_t size(const char *path)
bool put(int lit)
Definition file.hpp:141
bool endl()
Definition file.hpp:124
bool put(int64_t l)
Definition file.hpp:165
static bool writable(const char *path)
Cube * p
Definition exorList.c:222
#define cadical_getc_unlocked
Definition file.hpp:23
#define cadical_putc_unlocked
Definition file.hpp:22
int lit
Definition satVec.h:130
Definition mode.h:11