ABC: A System for Sequential Synthesis and Verification
 
Loading...
Searching...
No Matches
CaDiCaL::File Class Reference

#include <file.hpp>

Public Member Functions

bool piping ()
 
 ~File ()
 
int get ()
 
bool put (char ch)
 
bool endl ()
 
bool put (unsigned char ch)
 
bool put (const char *s)
 
bool put (int lit)
 
bool put (int64_t l)
 
bool put (uint64_t l)
 
const char * name () const
 
uint64_t lineno () const
 
uint64_t bytes () const
 
void connect_internal (Internal *i)
 
bool closed ()
 
void close (bool print=false)
 
void flush ()
 

Static Public Member Functions

static char * find_program (const char *prg)
 
static bool exists (const char *path)
 
static bool writable (const char *path)
 
static size_t size (const char *path)
 
static bool match (Internal *, const char *path, const int *sig)
 
static Fileread (Internal *, FILE *f, const char *name)
 
static Fileread (Internal *, const char *path)
 
static Filewrite (Internal *, FILE *, const char *name)
 
static Filewrite (Internal *, const char *path)
 

Detailed Description

Definition at line 42 of file file.hpp.

Constructor & Destructor Documentation

◆ ~File()

CaDiCaL::File::~File ( )

Definition at line 519 of file cadical_file.cpp.

519 {
520 if (file)
521 close ();
522 free (_name);
523}
void close(bool print=false)
VOID_HACK free()
Here is the call graph for this function:
Here is the caller graph for this function:

Member Function Documentation

◆ bytes()

uint64_t CaDiCaL::File::bytes ( ) const
inline

Definition at line 208 of file file.hpp.

208{ return _bytes; }
Here is the caller graph for this function:

◆ close()

void CaDiCaL::File::close ( bool print = false)

Definition at line 442 of file cadical_file.cpp.

442 {
443 CADICAL_assert (file);
444#ifndef CADICAL_QUIET
445 if (internal->opts.quiet)
446 print = false;
447 else if (internal->opts.verbose > 0)
448 print = true;
449#endif
450 if (close_file == 0) {
451 if (print)
452 MSG ("disconnecting from '%s'", name ());
453 }
454 if (close_file == 1) {
455 if (print)
456 MSG ("closing file '%s'", name ());
457 fclose (file);
458 }
459 if (close_file == 2) {
460 if (print)
461 MSG ("closing input pipe to read '%s'", name ());
462 pclose (file);
463 }
464#ifndef _WIN32
465 if (close_file == 3) {
466 if (print)
467 MSG ("closing output pipe to write '%s'", name ());
468 fclose (file);
469 waitpid (child_pid, 0, 0);
470#if defined(__APPLE__) || defined(__MACH__)
471 compressed_file_writing_mutex.unlock ();
472#endif
473 }
474#endif
475 file = 0; // mark as closed
476
477 // TODO what about error checking for 'fclose', 'pclose' or 'waitpid'?
478
479#ifndef CADICAL_QUIET
480 if (print) {
481 if (writing) {
482 uint64_t written_bytes = bytes ();
483 double written_mb = written_bytes / (double) (1 << 20);
484 MSG ("after writing %" PRIu64 " bytes %.1f MB", written_bytes,
485 written_mb);
486 if (close_file == 3) {
487 size_t actual_bytes = size (name ());
488 if (actual_bytes) {
489 double actual_mb = actual_bytes / (double) (1 << 20);
490 MSG ("deflated to %zd bytes %.1f MB", actual_bytes, actual_mb);
491 MSG ("factor %.2f (%.2f%% compression)",
492 relative (written_bytes, actual_bytes),
493 percent (actual_bytes, written_bytes));
494 } else
495 MSG ("but could not determine actual size of written file");
496 }
497 } else {
498 uint64_t read_bytes = bytes ();
499 double read_mb = read_bytes / (double) (1 << 20);
500 MSG ("after reading %" PRIu64 " bytes %.1f MB", read_bytes, read_mb);
501 if (close_file == 2) {
502 size_t actual_bytes = size (name ());
503 double actual_mb = actual_bytes / (double) (1 << 20);
504 MSG ("inflated from %zd bytes %.1f MB", actual_bytes, actual_mb);
505 MSG ("factor %.2f (%.2f%% compression)",
506 relative (read_bytes, actual_bytes),
507 percent (actual_bytes, read_bytes));
508 }
509 }
510 }
511#endif
512}
#define CADICAL_assert(ignore)
Definition global.h:14
const char * name() const
Definition file.hpp:206
uint64_t bytes() const
Definition file.hpp:208
static size_t size(const char *path)
#define MSG(...)
Definition message.hpp:49
double relative(double a, double b)
Definition util.hpp:20
double percent(double a, double b)
Definition util.hpp:21
int pclose()
Here is the call graph for this function:
Here is the caller graph for this function:

◆ closed()

bool CaDiCaL::File::closed ( )
inline

Definition at line 211 of file file.hpp.

211{ return !file; }

◆ connect_internal()

void CaDiCaL::File::connect_internal ( Internal * i)
inline

Definition at line 210 of file file.hpp.

210{ internal = i; }

◆ endl()

bool CaDiCaL::File::endl ( )
inline

Definition at line 124 of file file.hpp.

124{ return put ('\n'); }
bool put(char ch)
Definition file.hpp:116
Here is the call graph for this function:

◆ exists()

bool CaDiCaL::File::exists ( const char * path)
static

Definition at line 76 of file cadical_file.cpp.

76 {
77 struct stat buf;
78 if (stat (path, &buf))
79 return false;
80 if (access (path, R_OK))
81 return false;
82 return true;
83}
Here is the call graph for this function:
Here is the caller graph for this function:

◆ find_program()

char * CaDiCaL::File::find_program ( const char * prg)
static

Definition at line 176 of file cadical_file.cpp.

176 {
177 size_t prglen = strlen (prg);
178 const char *c = getenv ("PATH");
179 if (!c)
180 return 0;
181 size_t len = strlen (c);
182 char *e = new char[len + 1];
183 strcpy (e, c);
184 char *res = 0;
185 for (char *p = e, *q; !res && p < e + len; p = q) {
186 for (q = p; *q && *q != ':'; q++)
187 ;
188 *q++ = 0;
189 size_t pathlen = (q - p) + prglen;
190 char *path = new char[pathlen + 1];
191 snprintf (path, pathlen + 1, "%s/%s", p, prg);
192 CADICAL_assert (strlen (path) == pathlen);
193 if (exists (path))
194 res = path;
195 else
196 delete[] path;
197 }
198 delete[] e;
199 return res;
200}
static bool exists(const char *path)
Cube * p
Definition exorList.c:222
int strlen()
char * strcpy()
char * getenv()
Here is the call graph for this function:
Here is the caller graph for this function:

◆ flush()

void CaDiCaL::File::flush ( )

Definition at line 514 of file cadical_file.cpp.

514 {
515 CADICAL_assert (file);
516 fflush (file);
517}
Here is the call graph for this function:
Here is the caller graph for this function:

◆ get()

int CaDiCaL::File::get ( )
inline

Definition at line 106 of file file.hpp.

106 {
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 }
#define cadical_getc_unlocked
Definition file.hpp:23

◆ lineno()

uint64_t CaDiCaL::File::lineno ( ) const
inline

Definition at line 207 of file file.hpp.

207{ return _lineno; }

◆ match()

bool CaDiCaL::File::match ( Internal * internal,
const char * path,
const int * sig )
static

Definition at line 150 of file cadical_file.cpp.

150 {
151 CADICAL_assert (path);
152 FILE *tmp = fopen (path, "r");
153 if (!tmp) {
154 WARNING ("failed to open '%s' to check signature", path);
155 return false;
156 }
157 bool res = true;
158 for (const int *p = sig; res && (*p != EOF); p++)
159 res = (cadical_getc_unlocked (tmp) == *p);
160 fclose (tmp);
161 if (!res)
162 WARNING ("file type signature check for '%s' failed", path);
163 return res;
164}
#define WARNING(...)
Definition message.hpp:65
Here is the call graph for this function:
Here is the caller graph for this function:

◆ name()

const char * CaDiCaL::File::name ( ) const
inline

Definition at line 206 of file file.hpp.

206{ return _name; }
Here is the caller graph for this function:

◆ piping()

bool CaDiCaL::File::piping ( )

Definition at line 128 of file cadical_file.cpp.

128 {
129 struct stat stat;
130 int fd = fileno (file);
131 if (fstat (fd, &stat))
132 return true;
133 return S_ISFIFO (stat.st_mode);
134}
Here is the call graph for this function:
Here is the caller graph for this function:

◆ put() [1/6]

bool CaDiCaL::File::put ( char ch)
inline

Definition at line 116 of file file.hpp.

116 {
117 CADICAL_assert (writing);
118 if (cadical_putc_unlocked (ch, file) == EOF)
119 return false;
120 _bytes++;
121 return true;
122 }
#define cadical_putc_unlocked
Definition file.hpp:22
Here is the caller graph for this function:

◆ put() [2/6]

bool CaDiCaL::File::put ( const char * s)
inline

Definition at line 134 of file file.hpp.

134 {
135 for (const char *p = s; *p; p++)
136 if (!put (*p))
137 return false;
138 return true;
139 }
Here is the call graph for this function:

◆ put() [3/6]

bool CaDiCaL::File::put ( int lit)
inline

Definition at line 141 of file file.hpp.

141 {
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 }
int lit
Definition satVec.h:130
Here is the call graph for this function:

◆ put() [4/6]

bool CaDiCaL::File::put ( int64_t l)
inline

Definition at line 165 of file file.hpp.

165 {
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 }
Here is the call graph for this function:

◆ put() [5/6]

bool CaDiCaL::File::put ( uint64_t l)
inline

Definition at line 189 of file file.hpp.

189 {
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 }
Here is the call graph for this function:

◆ put() [6/6]

bool CaDiCaL::File::put ( unsigned char ch)
inline

Definition at line 126 of file file.hpp.

126 {
127 CADICAL_assert (writing);
128 if (cadical_putc_unlocked (ch, file) == EOF)
129 return false;
130 _bytes++;
131 return true;
132 }

◆ read() [1/2]

File * CaDiCaL::File::read ( Internal * internal,
const char * path )
static

Definition at line 385 of file cadical_file.cpp.

385 {
386 FILE *file;
387 int close_input = 2;
388 if (has_suffix (path, ".xz")) {
389 file = read_pipe (internal, "xz -c -d %s", xzsig, path);
390 if (!file)
391 goto READ_FILE;
392 } else if (has_suffix (path, ".lzma")) {
393 file = read_pipe (internal, "lzma -c -d %s", lzmasig, path);
394 if (!file)
395 goto READ_FILE;
396 } else if (has_suffix (path, ".bz2")) {
397 file = read_pipe (internal, "bzip2 -c -d %s", bz2sig, path);
398 if (!file)
399 goto READ_FILE;
400 } else if (has_suffix (path, ".gz")) {
401 file = read_pipe (internal, "gzip -c -d %s", gzsig, path);
402 if (!file)
403 goto READ_FILE;
404 } else if (has_suffix (path, ".7z")) {
405 file = read_pipe (internal, "7z x -so %s 2>/dev/null", sig7z, path);
406 if (!file)
407 goto READ_FILE;
408 } else {
409 READ_FILE:
410 file = read_file (internal, path);
411 close_input = 1;
412 }
413
414 if (!file)
415 return 0;
416
417 return new File (internal, false, close_input, 0, file, path);
418}
bool has_suffix(const char *str, const char *suffix)
Here is the call graph for this function:

◆ read() [2/2]

File * CaDiCaL::File::read ( Internal * internal,
FILE * f,
const char * name )
static

Definition at line 377 of file cadical_file.cpp.

377 {
378 return new File (internal, false, 0, 0, f, n);
379}
Here is the call graph for this function:
Here is the caller graph for this function:

◆ size()

size_t CaDiCaL::File::size ( const char * path)
static

Definition at line 166 of file cadical_file.cpp.

166 {
167 struct stat buf;
168 if (stat (path, &buf))
169 return 0;
170 return (size_t) buf.st_size;
171}
Here is the call graph for this function:
Here is the caller graph for this function:

◆ writable()

bool CaDiCaL::File::writable ( const char * path)
static

Definition at line 85 of file cadical_file.cpp.

85 {
86 int res;
87 if (!path)
88 res = 1;
89 else if (!strcmp (path, "/dev/null"))
90 res = 0;
91 else {
92 if (!*path)
93 res = 2;
94 else {
95 struct stat buf;
96 const char *p = strrchr (path, '/');
97 if (!p) {
98 if (stat (path, &buf))
99 res = ((errno == ENOENT) ? 0 : -2);
100 else if (S_ISDIR (buf.st_mode))
101 res = 3;
102 else
103 res = (access (path, W_OK) ? 4 : 0);
104 } else if (!p[1])
105 res = 5;
106 else {
107 size_t len = p - path;
108 char *dirname = new char[len + 1];
109 strncpy (dirname, path, len);
110 dirname[len] = 0;
111 if (stat (dirname, &buf))
112 res = 6;
113 else if (!S_ISDIR (buf.st_mode))
114 res = 7;
115 else if (access (dirname, W_OK))
116 res = 8;
117 else if (stat (path, &buf))
118 res = (errno == ENOENT) ? 0 : -3;
119 else
120 res = access (path, W_OK) ? 9 : 0;
121 delete[] dirname;
122 }
123 }
124 }
125 return !res;
126}
char * strrchr()
char * strncpy()
int strcmp()
Here is the call graph for this function:
Here is the caller graph for this function:

◆ write() [1/2]

File * CaDiCaL::File::write ( Internal * internal,
const char * path )
static

Definition at line 420 of file cadical_file.cpp.

420 {
421 FILE *file;
422 int close_output = 3, child_pid = 0;
423#ifndef _WIN32
424 if (has_suffix (path, ".xz"))
425 file = write_pipe (internal, "xz -c", path, child_pid);
426 else if (has_suffix (path, ".bz2"))
427 file = write_pipe (internal, "bzip2 -c", path, child_pid);
428 else if (has_suffix (path, ".gz"))
429 file = write_pipe (internal, "gzip -c", path, child_pid);
430 else if (has_suffix (path, ".7z"))
431 file = write_pipe (internal, "7z a -an -txz -si -so", path, child_pid);
432 else
433#endif
434 file = write_file (internal, path), close_output = 1;
435
436 if (!file)
437 return 0;
438
439 return new File (internal, true, close_output, child_pid, file, path);
440}
Here is the call graph for this function:

◆ write() [2/2]

File * CaDiCaL::File::write ( Internal * internal,
FILE * f,
const char * name )
static

Definition at line 381 of file cadical_file.cpp.

381 {
382 return new File (internal, true, 0, 0, f, n);
383}
Here is the call graph for this function:
Here is the caller graph for this function:

The documentation for this class was generated from the following files: