Skip to content

Commit 37e8618

Browse files
authored
Merge pull request #701 from os-fpga/pln_developing_BLIF_checker
pln: developing BLIF_file class and checker
2 parents 826def6 + ff497d3 commit 37e8618

File tree

6 files changed

+1039
-2
lines changed

6 files changed

+1039
-2
lines changed

stars/src/file_readers/pinc_Fio.cpp

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -554,6 +554,122 @@ int64_t MMapReader::printWC(std::ostream& os) const noexcept {
554554
return numLines;
555555
}
556556

557+
static void print_byte(std::ostream& os, CStr prefix, int b) noexcept {
558+
bool printable = std::ispunct(b) or std::isalnum(b);
559+
os_printf(os, "%s decimal= %d hex= %x",
560+
prefix ? prefix : "", b, b);
561+
if (printable)
562+
os_printf(os, " char= %c", b);
563+
os << endl;
564+
os.flush();
565+
}
566+
567+
bool MMapReader::is_text_file(string& label) const noexcept {
568+
label.clear();
569+
if (!buf_ or sz_ < 4)
570+
return false;
571+
572+
const uint* sig_p = (uint*) buf_;
573+
uint sig = *sig_p;
574+
575+
// lprintf("\t ....... sig= %u hex %x\n", sig, sig);
576+
577+
if (sig == 0x464c457f) {
578+
label = "ELF";
579+
return false;
580+
}
581+
if (sig == 0x21726152) {
582+
label = "RAR";
583+
return false;
584+
}
585+
if (sig == 0x587a37fd) {
586+
label = "XZ";
587+
return false;
588+
}
589+
if (sig == 0xfd2fb528) {
590+
label = "ZST";
591+
return false;
592+
}
593+
if (sig == 0x425a6839) {
594+
label = "BZ2";
595+
return false;
596+
}
597+
if (sig == 0x1f8b0808) {
598+
label = "GZ";
599+
return false;
600+
}
601+
if (sig == 0x46445025) {
602+
label = "PDF";
603+
return false;
604+
}
605+
if (sig == 0x213c6172) {
606+
label = "AR";
607+
return false;
608+
}
609+
610+
return true;
611+
}
612+
613+
bool MMapReader::printMagic(std::ostream& os) const noexcept {
614+
assert(fsz_ == sz_);
615+
if (!fsz_ || !sz_ || !buf_ || fnm_.empty()) {
616+
os << "(empty)" << endl;
617+
return false;
618+
}
619+
if (sz_ == 1) {
620+
print_byte(os, "(single-byte)", buf_[0]);
621+
return false;
622+
}
623+
if (sz_ < 5) {
624+
os << "(tiny) size= " << sz_ << endl;
625+
char prefix[32] = {};
626+
for (uint i = 0; i < sz_; i++) {
627+
::sprintf(prefix, " byte#%u ", i);
628+
print_byte(os, prefix, buf_[i]);
629+
}
630+
return false;
631+
}
632+
if (sz_ < UINT_MAX) {
633+
os << "(normal) size= " << sz_ << endl;
634+
char prefix[32] = {};
635+
for (uint i = 0; i < sz_; i++) {
636+
::sprintf(prefix, " byte#%u ", i);
637+
print_byte(os, prefix, buf_[i]);
638+
if (i > 8)
639+
break;
640+
}
641+
string label;
642+
bool is_txt = is_text_file(label);
643+
if (is_txt) {
644+
os << "(guessed TEXT)" << endl;
645+
} else if (!label.empty()) {
646+
os << "(guessed BINARY) : " << label << endl;
647+
}
648+
os.flush();
649+
return true;
650+
}
651+
652+
os << "(huge) size= " << sz_ << endl;
653+
return false;
654+
}
655+
656+
bool MMapReader::isGoodForParsing() const noexcept {
657+
assert(fsz_ == sz_);
658+
if (!fsz_ || !sz_ || !buf_ || fnm_.empty())
659+
return false;
660+
if (sz_ < 5)
661+
return false;
662+
663+
if (sz_ < UINT_MAX) {
664+
string label;
665+
bool is_txt = is_text_file(label);
666+
return is_txt;
667+
}
668+
669+
// huge size
670+
return false;
671+
}
672+
557673
size_t MMapReader::printLines(std::ostream& os) noexcept {
558674
if (!fsz_ || !sz_ || !buf_ || fnm_.empty()) return 0;
559675
bool hasl = hasLines();

stars/src/file_readers/pinc_Fio.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,9 +251,16 @@ class MMapReader : public Fio
251251
char* skipLine(char* curL) noexcept;
252252
bool advanceLine(char*& curL) noexcept;
253253

254+
bool printMagic(std::ostream& os) const noexcept;
255+
bool isGoodForParsing() const noexcept;
256+
254257
uint64_t hashSum() const noexcept;
255258
int dprint1() const noexcept;
256259
int dprint2() const noexcept;
260+
261+
private:
262+
bool is_text_file(string& label) const noexcept;
263+
257264
}; // MMapReader
258265

259266

@@ -455,6 +462,9 @@ class XML_Reader : public MMapReader
455462

456463
bool addIncludeGuards(LineReader& lr) noexcept;
457464

465+
inline bool c_is_digit(char c) noexcept { return uint32_t(c) - '0' < 10u; }
466+
inline bool c_is_dot_digit(char c) noexcept { return uint32_t(c) - '0' < 10u or c == '.'; }
467+
458468
inline bool has_digit(CStr z) noexcept {
459469
if (!z || !z[0]) return false;
460470

@@ -465,6 +475,16 @@ inline bool has_digit(CStr z) noexcept {
465475
return false;
466476
}
467477

478+
inline bool has_digit(CStr s, size_t len) noexcept {
479+
if (!s or !len) return false;
480+
481+
for (size_t i = 0; i < len; i++) {
482+
bool is_digit = (uint32_t(s[i]) - '0' < 10u);
483+
if (is_digit) return true;
484+
}
485+
return false;
486+
}
487+
468488
inline size_t count_digits(CStr z) noexcept {
469489
if (!z || !z[0]) return 0;
470490

0 commit comments

Comments
 (0)