Pointwise Plugin SDK
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
PwpFile.h
Go to the documentation of this file.
1 /****************************************************************************
2  *
3  * class PwpFile
4  *
5  * (C) 2021 Cadence Design Systems, Inc. All rights reserved worldwide.
6  *
7  ***************************************************************************/
8 
9 
10 #ifndef _PWPFILE_H_
11 #define _PWPFILE_H_
12 
13 #include "apiPWPUtils.h"
14 #include "PwpFileWriter.h"
15 
16 #include <algorithm>
17 #include <cstring>
18 #include <cstdio>
19 #include <cctype>
20 #include <functional>
21 #include <string>
22 #include <vector>
23 
24 
110 class PwpFile : public PwpWriterInterface {
111  enum {
113  };
114 
115 public:
116 
118 
130  PwpFile(FILE *fp = 0, std::string filename = std::string(), int mode = 0);
131 
132 
134 
141  PwpFile(std::string filename, int mode);
142 
145  PwpFile(const PwpFile &ref);
146 
148 
151  virtual ~PwpFile();
152 
154 
165  bool wrap(FILE *fp, std::string filename = std::string(), int mode = 0);
166 
167 
169 
177  bool open(std::string filename, int mode);
178 
179 
182  bool isOpen() const;
183 
186  bool fileEof() const;
187 
189 
193  bool close();
194 
195 
197 
207 
208 
210 
221  PWP_ENDIANNESS getByteOrder(bool mapBigLittle = false) const;
222 
223 
225 
230 
231 
233 
237 
238 
241  bool isReadable() const;
242 
243 
246  bool isWritable() const;
247 
248 
251  bool isSingle() const;
252 
253 
256  bool isDouble() const;
257 
258 
262  bool isAscii() const;
263 
267  bool checkAscii() const;
268 
272  bool checkUnformatted(bool &swapped, std::vector<PWP_INT64> *pRecSizes = 0) const;
273 
276  bool isBinary() const;
277 
278 
281  bool isUnformatted() const;
282 
283 
286  bool isEof() const;
287 
288 
291  bool flush();
292 
293 
301  bool getPos(sysFILEPOS &pos) const;
302 
310  PWP_INT64 getPos() const;
311 
312 
320  bool setIntPos(const PWP_INT64 &pos);
321 
329  bool setPos(const sysFILEPOS &pos);
330 
333  bool rewind();
334 
335 
338  bool getSize(PWP_UINT64 &size) const;
339 
340 
343  static bool getSize(const char *filename, PWP_UINT64 &size);
344 
345 
347  void setName(std::string filename);
348 
349 
352  const char* getName() const;
353 
354 
356 
362  bool read(void *buf, size_t size, size_t count);
363 
364 
366 
369  bool wspaceSkip();
370 
371 
374 
378  bool readToken(std::string &tok);
379 
380 
383 
387  bool readAlphaToken(std::string &tok);
388 
389 
392 
402  bool readUntil(std::string &str, const int stopC, int maxLen = 0,
403  const bool doTrim = false);
404 
408 
413  bool readUntil(PWP_UINT32 &v, const char stopC) {
414  const char stopTok[2]{ stopC, '\0' };
415  return readInt(v) && readTokenIs(stopTok); }
416 
420 
428  inline bool readUntilTrim(std::string &str, const int stopC, int maxLen=0) {
429  return readUntil(str, stopC, maxLen, true); }
430 
431 
433 
440  bool readAscVal(PWP_REAL *v, PWP_UINT32 cnt,
441  const PWP_UINT32 base = 10);
442 
443 
445 
452  bool readAscVal(PWP_FLOAT *v, PWP_UINT32 cnt,
453  const PWP_UINT32 base = 10);
454 
455 
457 
464  bool readAscVal(PWP_UINT32 *v, PWP_UINT32 cnt,
465  const PWP_UINT32 base = 10);
466 
468 
475  bool readAscVal(char *v, PWP_UINT32 cnt,
476  const PWP_UINT32 base = 10);
477 
478 
481 
487  template< typename T >
488  bool readVal(T &v, const PWP_UINT32 base = 10)
489  {
490  return isBinary() ? read(&v, sizeof(T), 1) : readAscVal(&v, 1, base);
491  }
492 
495 
501  template< typename T >
502  bool readSwap(T &val, const bool swapbyte)
503  {
504  const bool ret = read(&val, sizeof(T), 1);
505  if (swapbyte) {
506  swap(val);
507  }
508  return ret;
509  }
510 
513 
517  template< typename T >
518  T readSwap(const bool swapbyte)
519  {
520  T result = 0;
521  if (1 == fread(&result, sizeof(T), 1, fp_)) {
522  if (swapbyte) {
523  swap(result);
524  }
525  }
526  return result;
527  }
528 
531 
538  template< typename T >
539  bool readVal(T *v, const PWP_UINT32 cnt, const PWP_UINT32 base = 10)
540  {
541  return isBinary() ? read(v, sizeof(T), cnt) : readAscVal(v, cnt, base);
542  }
543 
544 
547 
555  bool readInt(PWP_UINT32 &v, const PWP_UINT32 base = 10,
556  PWP_UINT32 maxDigits = 0);
557 
558 
561 
571  bool readOptionalInt(PWP_UINT32 &v, const PWP_UINT32 defV,
572  const PWP_UINT32 base = 10, PWP_UINT32 maxDigits = 0);
573 
574 
576 
582  bool readDouble(PWP_REAL &v, PWP_UINT32 maxDigits = 0);
583 
586  static const void* swapByteOrder(void *buf, size_t size)
587  {
588  char *cbuf = (char*)buf;
589  size_t hNdx = 0;
590  size_t tNdx = size - 1;
591  while (hNdx < tNdx) {
592  const char tmp = cbuf[hNdx];
593  cbuf[hNdx++] = cbuf[tNdx];
594  cbuf[tNdx--] = tmp;
595  }
596  return buf;
597  }
598 
601  template<typename T>
602  static void swap(T &val)
603  {
604  swapByteOrder(&val, sizeof(T));
605  }
606 
607 
610 
616  inline bool readOctal(PWP_UINT32 &v, PWP_UINT32 maxDigits = 0) {
617  return readInt(v, 8, maxDigits); }
618 
619 
622 
628  inline bool readHex(PWP_UINT32 &v, PWP_UINT32 maxDigits = 0) {
629  return readInt(v, 16, maxDigits); }
630 
631 
633 
636  inline int getc() {
637  return ::fgetc(fp_); }
638 
639 
641 
645  inline bool getcNotEOF(int &c) {
646  return EOF != (c = ::fgetc(fp_)); }
647 
648 
651 
654  inline bool ungetc(const int c) {
655  return (EOF != ::ungetc(c, fp_)); }
656 
657 
659 
663  inline bool readAlphaTokenIs(const char *tok) {
664  std::string fileTok;
665  return readAlphaToken(fileTok) && (tok == fileTok); }
666 
667 
669 
673  inline bool readTokenIs(const char *tok) {
674  std::string fileTok;
675  return readToken(fileTok) && (tok == fileTok); }
676 
677 
679 
684  inline bool wspaceSkipToChar(const int nextC) {
685  wspaceSkip();
686  return ::fgetc(fp_) == nextC; }
687 
688 
690 
696  inline bool wspaceSkipToOneOf(const char *stopCs, char &theC) {
697  wspaceSkip();
698  theC = char(::fgetc(fp_));
699  if (nullptr == stopCs) {
700  return false;
701  }
702  const char *stopCsEnd = stopCs + strlen(stopCs);
703  return stopCsEnd != std::find(stopCs, stopCsEnd, theC); }
704 
705 
707 
711  inline bool skipToChar(const int nextC) {
712  int c;
713  while (getcNotEOF(c)) {
714  if (nextC == c) {
715  return true;
716  }
717  }
718  return false;
719  }
720 
721 
723 
727  inline bool wspaceSkipToEOF() {
728  return wspaceSkipToChar(EOF); }
729 
730 
732  inline static std::string &
733  ltrim(std::string &s)
734  {
735  // erase from begin to before first nonspace char
736  s.erase(s.begin(), std::find_if(s.begin(), s.end(),
737  [](std::string::value_type i) {
738  return 0 == std::isspace(i);
739  }));
740  return s;
741  }
742 
743 
745  inline static std::string &
746  rtrim(std::string &s)
747  {
748  // erase from after last nonspace char to before end
749  s.erase(std::find_if(s.rbegin(), s.rend(),
750  [](std::string::value_type i) {
751  return 0 == std::isspace(i);
752  }).base(), s.end());
753  return s;
754  }
755 
756 
758  inline static std::string &
759  trim(std::string &s)
760  {
761  return ltrim(rtrim(s));
762  }
763 
764 
767  inline FILE * fp() const {
768  return fp_; }
769 
770 
773  inline operator FILE*() const {
774  return fp_; }
775 
776 
779  inline PWU_UNFDATA& unfData() {
780  return ud_; }
781 
782 
785  inline const PWU_UNFDATA& unfData() const {
786  return ud_; }
787 
788 
790 
793  inline bool needByteSwap() const {
794  return needByteSwap(byteOrder_); }
795 
796 
798 
802  static bool unlink(const char *filename);
803 
804 
808 
809 
812 
821  static PWP_ENDIANNESS mapToBigLittle(PWP_ENDIANNESS byteOrder);
822 
823 
825 
829  static bool needByteSwap(PWP_ENDIANNESS byteOrder);
830 
831 
833 
844  template< typename T >
845  bool writeAt(sysFILEPOS pos, const T &val, const char * suffix = 0,
846  const char * prefix = 0);
847 
850  PwpFile & operator = (const PwpFile &ref);
851 
852 
854 
855 
856 private:
857  FILE * fp_;
858  int mode_;
859  std::string filename_;
864 };
865 
866 
867 template< typename T >
868 bool
869 PwpFile::writeAt(sysFILEPOS pos, const T &val, const char * suffix,
870  const char * prefix)
871 {
872  bool ret = false;
873  sysFILEPOS curPos;
874  if (!getPos(curPos) || !setPos(pos)) {
875  ret = false; // fail
876  }
877  else if (isUnformatted()) {
878  // Can't use writer_, it will modify record stats
879  ret = (1 == pwpFileWrite(PwuApplyEndianness(getByteOrder(), &val,
880  sizeof(T)), sizeof(T), 1, fp_));
881  }
882  else {
883  ret = write(val, suffix, prefix);
884  }
885  return ret && setPos(curPos);
886 }
887 
888 
904 public:
905 
907 
915  PwpFileRecord(PwpFile &file, bool doBeginRecord = true);
916 
917 
919 
932  PwpFileRecord(PwpFile &file, PWP_UINT32 bytes, PWP_UINT32 count = 1);
933 
934 
936  virtual ~PwpFileRecord();
937 
938 
942  return file_; }
943 
944 
947  const PwpFile& file() const {
948  return file_;
949  }
950 
951 
954  bool getPos(sysFILEPOS &pos) const {
955  return file_.getPos(pos); }
956 
957 
959 
970  template< typename T >
971  bool writeAt(sysFILEPOS pos, const T &val, const char * suffix = 0,
972  const char * prefix = 0) {
973  return file_.writeAt(pos, val, suffix, prefix); }
974 
975 
977 
978 
979 private:
980 
981  PwpFileRecord & operator=(const PwpFileRecord & /*rhs*/) {
982  return *this; }
983 
984 
985 private:
986 
988 };
989 
990 #endif // _PWPFILE_H_
static void swap(T &val)
swap() - (Importer-only function) Wrapper for function swapByteOrder().
Definition: PwpFile.h:602
PWP_INT64 getPos() const
Get the current file position.
Definition: PwpFile.cxx:855
static std::string & trim(std::string &s)
trim leading and trailing whitespace from s
Definition: PwpFile.h:759
bool readUntil(std::string &str, const int stopC, int maxLen=0, const bool doTrim=false)
Reads up to maxLen chars until the specified stop char is encountered.
Definition: PwpFile.cxx:1001
bool isUnformatted() const
Determines if file was opened with pwpUnformatted mode flag.
Definition: PwpFile.cxx:828
bool wrap(FILE *fp, std::string filename=std::string(), int mode=0)
Take ownership of fp opened using pwpFileOpen(filename, mode).
Definition: PwpFile.cxx:568
bool readOptionalInt(PWP_UINT32 &v, const PWP_UINT32 defV, const PWP_UINT32 base=10, PWP_UINT32 maxDigits=0)
Reads a single PWP_UINT32 value.
Definition: PwpFile.cxx:1133
unsigned long long PWP_UINT64
64-bit unsigned integer
Definition: apiPWP.h:243
bool readAlphaTokenIs(const char *tok)
Read and compare an alpha token.
Definition: PwpFile.h:663
bool readOctal(PWP_UINT32 &v, PWP_UINT32 maxDigits=0)
Reads a single PWP_UINT32 octal value.
Definition: PwpFile.h:616
unsigned int PWP_UINT32
32-bit unsigned integer
Definition: apiPWP.h:210
bool readTokenIs(const char *tok)
Read and compare a token.
Definition: PwpFile.h:673
virtual ~PwpFileRecord()
Destructor.
Definition: PwpFile.cxx:1382
The abstract PwpFileWriter class.
bool setIntPos(const PWP_INT64 &pos)
Set the current file position.
Definition: PwpFile.cxx:870
bool needByteSwap() const
Determine if byte swapping is needed for this file.
Definition: PwpFile.h:793
FILE * fp() const
Get the FILE pointer.
Definition: PwpFile.h:767
bool readSwap(T &val, const bool swapbyte)
Reads a single value of type T.
Definition: PwpFile.h:502
bool isAscii() const
Determines if file was opened with either the pwpAscii or pwpFormatted mode flag. ...
Definition: PwpFile.cxx:687
bool setPos(const sysFILEPOS &pos)
Set the current file position.
Definition: PwpFile.cxx:881
PWP_ENDIANNESS setByteOrder(PWP_ENDIANNESS order)
Set the byte order used for writes.
Definition: PwpFile.cxx:627
PWP_ENDIANNESS getByteOrder(bool mapBigLittle=false) const
Get the byte order used for writes.
Definition: PwpFile.cxx:636
bool writeAt(sysFILEPOS pos, const T &val, const char *suffix=0, const char *prefix=0)
Writes an endian enforced value directly at a given file position.
Definition: PwpFile.h:971
#define PwpWriterInterface_SUBCLASS
Definition: PwpFileWriter.h:58
bool writeAt(sysFILEPOS pos, const T &val, const char *suffix=0, const char *prefix=0)
Writes an endian enforced value directly at a given file position.
Definition: PwpFile.h:869
bool fileEof() const
Check for the end of file.
Definition: PwpFile.cxx:606
bool readUntil(PWP_UINT32 &v, const char stopC)
Reads a stopC terminated PWP_UINT32 value.
Definition: PwpFile.h:413
bool skipToChar(const int nextC)
Skip all chars up to and including nextC.
Definition: PwpFile.h:711
PWP_ENUM_PRECISION
File precision values.
Definition: apiPWP.h:802
static PWP_ENDIANNESS getOsByteOrder()
Get the OS&#39;s native byte ordering.
Definition: PwpFile.cxx:1178
FILE * fp_
The FILE pointer.
Definition: PwpFile.h:857
bool rewind()
Reset position to the beginning of the file.
Definition: PwpFile.cxx:887
const PwpFile & file() const
Get this record&#39;s file object.
Definition: PwpFile.h:947
bool isOpen() const
Determines a file&#39;s status.
Definition: PwpFile.cxx:600
bool wspaceSkipToEOF()
Skip whitespace and read until EOF.
Definition: PwpFile.h:727
bool readVal(T &v, const PWP_UINT32 base=10)
Reads a single value of type T.
Definition: PwpFile.h:488
bool isEof() const
Get the end-of-file status.
Definition: PwpFile.cxx:835
Manages FORTRAN unformatted file records.
Definition: PwpFile.h:903
static bool unlink(const char *filename)
Delete a file.
Definition: PwpFile.cxx:1171
fpos_t sysFILEPOS
File position data type.
Definition: pwpPlatform.h:51
bool readToken(std::string &tok)
Reads a whitespace delimited token (word).
Definition: PwpFile.cxx:953
bool read(void *buf, size_t size, size_t count)
Read a collection of data items.
Definition: PwpFile.cxx:929
bool getPos(sysFILEPOS &pos) const
Get the current file position.
Definition: PwpFile.cxx:849
PWP_ENUM_PRECISION precision_
The file&#39;s floating point precision.
Definition: PwpFile.h:861
bool readDouble(PWP_REAL &v, PWP_UINT32 maxDigits=0)
Reads a single PWP_REAL value.
Definition: PwpFile.cxx:1141
float PWP_FLOAT
32-bit real
Definition: apiPWP.h:261
PwpFile(FILE *fp=0, std::string filename=std::string(), int mode=0)
Default constructor.
Definition: PwpFile.cxx:515
const void * PwuApplyEndianness(PWP_ENDIANNESS endianness, const void *buf, size_t size)
Apply specified byte order to buf containing size bytes.
PwpFile & file()
Get this record&#39;s file object.
Definition: PwpFile.h:941
bool getPos(sysFILEPOS &pos) const
Get the current file position of this record&#39;s file object.
Definition: PwpFile.h:954
bool readAscVal(PWP_REAL *v, PWP_UINT32 cnt, const PWP_UINT32 base=10)
Reads an array of cnt PWP_REAL values from an ascii file.
Definition: PwpFile.cxx:1030
bool checkAscii() const
Determines whether the file is Ascii by reading the contents of the file.
Definition: PwpFile.cxx:693
Unformatted file data block.
Definition: apiPWPUtils.h:344
PwpFile & file_
Definition: PwpFile.h:987
virtual bool write(PWP_INT64 val, const char *suffix=0, const char *prefix=0)
Writes a integer value with proper encoding and byte order.
Definition: PwpFile.cxx:1233
bool readInt(PWP_UINT32 &v, const PWP_UINT32 base=10, PWP_UINT32 maxDigits=0)
Reads a single PWP_UINT32 value.
Definition: PwpFile.cxx:1081
Writes solver files.
Definition: PwpFile.h:110
bool isReadable() const
Determines if file supports read operations.
Definition: PwpFile.cxx:659
const char * getName() const
Get the file&#39;s associated filename.
Definition: PwpFile.cxx:922
bool isDouble() const
Determines if floating point precision is PWP_PRECISION_DOUBLE.
Definition: PwpFile.cxx:680
bool readAlphaToken(std::string &tok)
Reads a whitespace delimited token (word).
Definition: PwpFile.cxx:977
bool isBinary() const
Determines if file was opened with pwpBinary mode flag.
Definition: PwpFile.cxx:821
bool readUntilTrim(std::string &str, const int stopC, int maxLen=0)
Reads up to maxLen chars until the specified stop char is encountered.
Definition: PwpFile.h:428
virtual ~PwpFile()
Destructor.
Definition: PwpFile.cxx:552
PWU_UNFDATA & unfData()
Get the file&#39;s unformatted data buffer.
Definition: PwpFile.h:779
bool readHex(PWP_UINT32 &v, PWP_UINT32 maxDigits=0)
Reads a single PWP_UINT32 hex value.
Definition: PwpFile.h:628
bool wspaceSkipToChar(const int nextC)
Skip whitespace and read the next char.
Definition: PwpFile.h:684
long long PWP_INT64
64-bit integer
Definition: apiPWP.h:240
bool isSingle() const
Determines if floating point precision is PWP_PRECISION_SINGLE.
Definition: PwpFile.cxx:673
PWP_ENUM_PRECISION setPrecision(PWP_ENUM_PRECISION precision)
Set the floating point precision used for writes.
Definition: PwpFile.cxx:643
PwpFileRecord & operator=(const PwpFileRecord &)
Definition: PwpFile.h:981
size_t pwpFileWrite(const void *buf, size_t size, size_t count, FILE *fp)
Write an collection of data items to a file.
bool wspaceSkip()
Reads and discards consecutive whitespace bytes.
Definition: PwpFile.cxx:936
bool checkUnformatted(bool &swapped, std::vector< PWP_INT64 > *pRecSizes=0) const
Determines unformatted type settings by reading the contents of the file.
Definition: PwpFile.cxx:777
bool getcNotEOF(int &c)
Reads a single byte value.
Definition: PwpFile.h:645
PwpFileWriter * writer_
The file writer implementation.
Definition: PwpFile.h:863
PWP_ENUM_PRECISION getPrecision() const
Get the floating point precision used for writes.
Definition: PwpFile.cxx:652
std::string filename_
The file name.
Definition: PwpFile.h:859
bool isWritable() const
Determines if file supports write operations.
Definition: PwpFile.cxx:666
The abstract PwpWriterInterface class.
int getc()
Reads a single byte value.
Definition: PwpFile.h:636
bool flush()
Flush file to disk.
Definition: PwpFile.cxx:842
PWP_ENDIANNESS byteOrder_
The file&#39;s byte order.
Definition: PwpFile.h:860
PwpFile & operator=(const PwpFile &ref)
Assignment operator Does not copy the file position or unformatted data.
Definition: PwpFile.cxx:558
PWP_ENDIANNESS
Flags used to indicate endianness or control endian behaviors in functions.
Definition: apiPWP.h:822
bool wspaceSkipToOneOf(const char *stopCs, char &theC)
Skip whitespace and read the next char returning it in theC.
Definition: PwpFile.h:696
void setName(std::string filename)
Set the file&#39;s associated filename.
Definition: PwpFile.cxx:915
bool ungetc(const int c)
Return a byte value to the file.
Definition: PwpFile.h:654
int mode_
The mode.
Definition: PwpFile.h:858
PwpFileRecord(PwpFile &file, bool doBeginRecord=true)
Constructor.
Definition: PwpFile.cxx:1366
const PWU_UNFDATA & unfData() const
Get the file&#39;s unformatted data buffer.
Definition: PwpFile.h:785
bool close()
Explicitly close the file object.
Definition: PwpFile.cxx:613
T readSwap(const bool swapbyte)
Reads a single value of type T.
Definition: PwpFile.h:518
bool getSize(PWP_UINT64 &size) const
Get the size of the file managed by this object.
Definition: PwpFile.cxx:895
static const void * swapByteOrder(void *buf, size_t size)
Performs an inplace byte swapping.
Definition: PwpFile.h:586
double PWP_REAL
64-bit real
Definition: apiPWP.h:264
bool open(std::string filename, int mode)
Opens a file with the given filename and mode.
Definition: PwpFile.cxx:593
static PWP_ENDIANNESS mapToBigLittle(PWP_ENDIANNESS byteOrder)
Map a byte ordering value to PWP_ENDIAN_BIG or PWP_ENDIAN_LITTLE based on getOsByteOrder().
Definition: PwpFile.cxx:1185
bool readVal(T *v, const PWP_UINT32 cnt, const PWP_UINT32 base=10)
Reads an array of cnt values of type T.
Definition: PwpFile.h:539
static std::string & rtrim(std::string &s)
trim trailing whitespace from s
Definition: PwpFile.h:746
static std::string & ltrim(std::string &s)
trim leading whitespace from s
Definition: PwpFile.h:733
Data and functions useful to PWP-API compliant plugins.
PWU_UNFDATA ud_
The file&#39;s unformatted data stats.
Definition: PwpFile.h:862