8.CaDiCal程式碼解讀——單獨的型別相關程式碼--file.hpp-file.cpp
阿新 • • 發佈:2020-07-31
說明:
(1)file.hpp中包含了相關的輸入輸出函式;
(2)File型別內部有一個Internal指標,該指標用於銜接;
Internal * internal;
(3)可以接受讀取各種壓縮個格式的cnf輸入檔案
(4)開啟檔案函式open有兩個,關閉檔案函式close只有一個;
(5)
file.hpp程式碼解讀
1 #ifndef _file_hpp_INCLUDED 2 #define _file_hpp_INCLUDED 3 4 #include <cstdio> 5 #include <cassert> 6 |
|
file.cpp程式碼解讀
1 #include "internal.hpp" //沒有直接包含file.hpp標頭檔案,該標頭檔案在internal.hpp之中 2 3 /*------------------------------------------------------------------------*/ 4 5 // Some more low-level 'C' headers. 6 7 extern "C" { 8 #include <string.h> 9 #include <errno.h> 10 #include <sys/stat.h> 11 #include <sys/types.h> 12 #include <unistd.h> 13 #include <stdlib.h> 14 } 15 16 /*------------------------------------------------------------------------*/ 17 18 namespace CaDiCaL { 19 20 /*------------------------------------------------------------------------*/ 21 22 // Private constructor. 23 24 File::File (Internal *i, bool w, int c, FILE * f, const char * n) 25 : 26 #ifndef QUIET 27 internal (i), 28 #endif 29 #if !defined(QUIET) || !defined(NDEBUG) 30 writing (w), 31 #endif 32 close_file (c), file (f), 33 _name (n), _lineno (1), _bytes (0) 34 { 35 (void) i, (void) w; 36 assert (f), assert (n); 37 } 38 39 /*------------------------------------------------------------------------*/ 40 41 bool File::exists (const char * path) { 42 struct stat buf; 43 if (stat (path, &buf)) return false; 44 if (access (path, R_OK)) return false; 45 return true; 46 } 47 48 bool File::writable (const char * path) { 49 int res; 50 if (!path) res = 1; 51 else if (!strcmp (path, "/dev/null")) res = 0; 52 else { 53 if (!*path) res = 2; 54 else { 55 struct stat buf; 56 const char * p = strrchr (path, '/'); 57 if (!p) { 58 if (stat (path, &buf)) res = ((errno == ENOENT) ? 0 : -2); 59 else if (S_ISDIR (buf.st_mode)) res = 3; 60 else res = (access (path, W_OK) ? 4 : 0); 61 } else if (!p[1]) res = 5; 62 else { 63 size_t len = p - path; 64 char * dirname = new char[len + 1]; 65 strncpy (dirname, path, len); 66 dirname[len] = 0; 67 if (stat (dirname, &buf)) res = 6; 68 else if (!S_ISDIR (buf.st_mode)) res = 7; 69 else if (access (dirname, W_OK)) res = 8; 70 else if (stat (path, &buf)) res = (errno == ENOENT) ? 0 : -3; 71 else res = access (path, W_OK) ? 9 : 0; 72 delete [] dirname; 73 } 74 } 75 } 76 return !res; 77 } 78 79 // These are signatures for supported compressed file types. In 2018 the 80 // SAT Competition was running on StarExec and used internally 'bzip2' 81 // compressed files, but gave them uncompressed to the solver using exactly 82 // the same path (with '.bz2' suffix). Then 'CaDiCaL' tried to read that 83 // actually uncompressed file through 'bzip2', which of course failed. Now 84 // we double check and fall back to reading the file as is, if the signature 85 // does not match after issuing a warning. |
|