Pointwise Plugin SDK
PwpFile.h
Go to the documentation of this file.
1 /****************************************************************************
2  *
3  * class PwpFile
4  *
5  * Proprietary software product of Pointwise, Inc.
6  * Copyright (c) 1995-2018 Pointwise, Inc.
7  * All rights reserved.
8  *
9  ***************************************************************************/
10 
11 
12 #ifndef _PWPFILE_H_
13 #define _PWPFILE_H_
14 
15 #include "apiPWPUtils.h"
16 #include "PwpFileWriter.h"
17 
18 #include <algorithm>
19 #include <cstdio>
20 #include <cctype>
21 #include <functional>
22 #include <string>
23 #include <vector>
24 
25 
111 class PwpFile : public PwpWriterInterface {
112  enum {
114  };
115 
116 public:
117 
119 
131  PwpFile(FILE *fp = 0, std::string filename = std::string(), int mode = 0);
132 
133 
135 
142  PwpFile(std::string filename, int mode);
143 
144 
146 
149  virtual ~PwpFile();
150 
151 
153 
164  bool wrap(FILE *fp, std::string filename = std::string(), int mode = 0);
165 
166 
168 
176  bool open(std::string filename, int mode);
177 
178 
181  bool isOpen() const;
182 
183 
185 
189  bool close();
190 
191 
193 
203 
204 
206 
217  PWP_ENDIANNESS getByteOrder(bool mapBigLittle = false) const;
218 
219 
221 
226 
227 
229 
233 
234 
237  bool isReadable() const;
238 
239 
242  bool isWritable() const;
243 
244 
247  bool isSingle() const;
248 
249 
252  bool isDouble() const;
253 
254 
258  bool isAscii() const;
259 
260 
263  bool isBinary() const;
264 
265 
268  bool isUnformatted() const;
269 
270 
273  bool isEof() const;
274 
275 
278  bool flush();
279 
280 
288  bool getPos(sysFILEPOS &pos) const;
289 
290 
298  bool setPos(const sysFILEPOS &pos);
299 
300 
303  bool rewind();
304 
305 
308  bool getSize(PWP_UINT64 &size) const;
309 
310 
313  static bool getSize(const char *filename, PWP_UINT64 &size);
314 
315 
317  void setName(std::string filename);
318 
319 
322  const char* getName() const;
323 
324 
326 
332  bool read(void *buf, size_t size, size_t count);
333 
334 
336 
339  bool wspaceSkip();
340 
341 
344 
348  bool readToken(std::string &tok);
349 
350 
353 
357  bool readAlphaToken(std::string &tok);
358 
359 
362 
372  bool readUntil(std::string &str, const int stopC, int maxLen = 0,
373  const bool doTrim = false);
374 
375 
379 
387  inline bool readUntilTrim(std::string &str, const int stopC, int maxLen=0) {
388  return readUntil(str, stopC, maxLen, true); }
389 
390 
392 
399  bool readAscVal(PWP_REAL *v, PWP_UINT32 cnt,
400  const PWP_UINT32 base = 10);
401 
402 
404 
411  bool readAscVal(PWP_FLOAT *v, PWP_UINT32 cnt,
412  const PWP_UINT32 base = 10);
413 
414 
416 
423  bool readAscVal(PWP_UINT32 *v, PWP_UINT32 cnt,
424  const PWP_UINT32 base = 10);
425 
426 
428 
435  bool readAscVal(char *v, PWP_UINT32 cnt,
436  const PWP_UINT32 base = 10);
437 
438 
441 
447  template< typename T >
448  bool readVal(T &v, const PWP_UINT32 base = 10) {
449  return isBinary() ? read(&v, sizeof(T), 1) :
450  readAscVal(&v, 1, base); }
451 
452 
455 
462  template< typename T >
463  bool readVal(T *v, const PWP_UINT32 cnt, const PWP_UINT32 base =10) {
464  return isBinary() ? read(v, sizeof(T), cnt) :
465  readAscVal(v, cnt, base); }
466 
467 
470 
478  bool readInt(PWP_UINT32 &v, const PWP_UINT32 base = 10,
479  PWP_UINT32 maxDigits = 0);
480 
481 
483 
489  bool readDouble(PWP_REAL &v, PWP_UINT32 maxDigits = 0);
490 
491 
494 
500  inline bool readOctal(PWP_UINT32 &v, PWP_UINT32 maxDigits = 0) {
501  return readInt(v, 8, maxDigits); }
502 
503 
506 
512  inline bool readHex(PWP_UINT32 &v, PWP_UINT32 maxDigits = 0) {
513  return readInt(v, 16, maxDigits); }
514 
515 
517 
520  inline int getc() {
521  return ::fgetc(fp_); }
522 
523 
525 
529  inline bool getcNotEOF(int &c) {
530  return EOF != (c = ::fgetc(fp_)); }
531 
532 
535 
538  inline bool ungetc(const int c) {
539  return (EOF != ::ungetc(c, fp_)); }
540 
541 
543 
547  inline bool readAlphaTokenIs(const char *tok) {
548  std::string fileTok;
549  return readAlphaToken(fileTok) && (tok == fileTok); }
550 
551 
553 
557  inline bool readTokenIs(const char *tok) {
558  std::string fileTok;
559  return readToken(fileTok) && (tok == fileTok); }
560 
561 
563 
568  inline bool wspaceSkipToChar(const int nextC) {
569  wspaceSkip();
570  return ::fgetc(fp_) == nextC; }
571 
572 
574 
578  inline bool skipToChar(const int nextC) {
579  int c;
580  while (getcNotEOF(c)) {
581  if (nextC == c) {
582  return true;
583  }
584  }
585  return false;
586  }
587 
588 
590 
594  inline bool wspaceSkipToEOF() {
595  return wspaceSkipToChar(EOF); }
596 
597 
599  inline static std::string &
600  ltrim(std::string &s)
601  {
602  s.erase(s.begin(), std::find_if(s.begin(), s.end(),
603  std::not1(std::ptr_fun<int, int>(std::isspace))));
604  return s;
605  }
606 
607 
609  inline static std::string &
610  rtrim(std::string &s)
611  {
612  s.erase(std::find_if(s.rbegin(), s.rend(),
613  std::not1(std::ptr_fun<int, int>(std::isspace))).base(), s.end());
614  return s;
615  }
616 
617 
618  // trim leading and trailing whitespace from s
619  inline static std::string &
620  trim(std::string &s)
621  {
622  return ltrim(rtrim(s));
623  }
624 
625 
628  inline FILE * fp() const {
629  return fp_; }
630 
631 
634  inline operator FILE*() const {
635  return fp_; }
636 
637 
640  inline PWU_UNFDATA& unfData() {
641  return ud_; }
642 
643 
646  inline const PWU_UNFDATA& unfData() const {
647  return ud_; }
648 
649 
651 
654  inline bool needByteSwap() const {
655  return needByteSwap(byteOrder_); }
656 
657 
659 
663  static bool unlink(const char *filename);
664 
665 
669 
670 
673 
682  static PWP_ENDIANNESS mapToBigLittle(PWP_ENDIANNESS byteOrder);
683 
684 
686 
690  static bool needByteSwap(PWP_ENDIANNESS byteOrder);
691 
692 
694 
705  template< typename T >
706  bool writeAt(sysFILEPOS pos, const T &val, const char * suffix = 0,
707  const char * prefix = 0);
708 
709 
711 
712 
713 private:
714 
715  FILE* fp_;
716  int mode_;
717  std::string filename_;
722 };
723 
724 
725 template< typename T >
726 bool
727 PwpFile::writeAt(sysFILEPOS pos, const T &val, const char * suffix,
728  const char * prefix)
729 {
730  bool ret = false;
731  sysFILEPOS curPos;
732  if (!getPos(curPos) || !setPos(pos)) {
733  ret = false; // fail
734  }
735  else if (isUnformatted()) {
736  // Can't use writer_, it will modify record stats
737  ret = (1 == pwpFileWrite(PwuApplyEndianness(getByteOrder(), &val,
738  sizeof(T)), sizeof(T), 1, fp_));
739  }
740  else {
741  ret = write(val, suffix, prefix);
742  }
743  return ret && setPos(curPos);
744 }
745 
746 
762 public:
763 
765 
773  PwpFileRecord(PwpFile &file, bool doBeginRecord = true);
774 
775 
777 
790  PwpFileRecord(PwpFile &file, PWP_UINT32 bytes, PWP_UINT32 count = 1);
791 
792 
794  virtual ~PwpFileRecord();
795 
796 
800  return file_; }
801 
802 
805  const PwpFile& file() const {
806  return file_;
807  }
808 
809 
812  bool getPos(sysFILEPOS &pos) const {
813  return file_.getPos(pos); }
814 
815 
817 
828  template< typename T >
829  bool writeAt(sysFILEPOS pos, const T &val, const char * suffix = 0,
830  const char * prefix = 0) {
831  return file_.writeAt(pos, val, suffix, prefix); }
832 
833 
835 
836 
837 private:
838 
839  PwpFileRecord & operator=(const PwpFileRecord & /*rhs*/) {
840  return *this; }
841 
842 
843 private:
844 
846 };
847 
848 #endif // _PWPFILE_H_
bool getPos(sysFILEPOS &pos) const
Get the current file position of this record&#39;s file object.
Definition: PwpFile.h:812
static std::string & trim(std::string &s)
Definition: PwpFile.h:620
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:873
PWP_ENDIANNESS getByteOrder(bool mapBigLittle=false) const
Get the byte order used for writes.
Definition: PwpFile.cxx:659
bool wrap(FILE *fp, std::string filename=std::string(), int mode=0)
Take ownership of fp opened using pwpFileOpen(filename, mode).
Definition: PwpFile.cxx:597
unsigned long long PWP_UINT64
64-bit unsigned integer
Definition: apiPWP.h:206
bool readAlphaTokenIs(const char *tok)
Read and compare an alpha token.
Definition: PwpFile.h:547
bool readOctal(PWP_UINT32 &v, PWP_UINT32 maxDigits=0)
Reads a single PWP_UINT32 octal value.
Definition: PwpFile.h:500
unsigned int PWP_UINT32
32-bit unsigned integer
Definition: apiPWP.h:194
bool readTokenIs(const char *tok)
Read and compare a token.
Definition: PwpFile.h:557
virtual ~PwpFileRecord()
Destructor.
Definition: PwpFile.cxx:1273
The abstract PwpFileWriter class.
bool setPos(const sysFILEPOS &pos)
Set the current file position.
Definition: PwpFile.cxx:752
bool isUnformatted() const
Determines if file was opened with pwpUnformatted mode flag.
Definition: PwpFile.cxx:724
PWP_ENDIANNESS setByteOrder(PWP_ENDIANNESS order)
Set the byte order used for writes.
Definition: PwpFile.cxx:650
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:829
#define PwpWriterInterface_SUBCLASS
Definition: PwpFileWriter.h:64
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:727
bool skipToChar(const int nextC)
Skip all chars up to and including nextC.
Definition: PwpFile.h:578
PWP_ENUM_PRECISION
File precision values.
Definition: apiPWP.h:718
static PWP_ENDIANNESS getOsByteOrder()
Get the OS&#39;s native byte ordering.
Definition: PwpFile.cxx:1043
FILE * fp_
The FILE pointer.
Definition: PwpFile.h:715
bool rewind()
Reset position to the beginning of the file.
Definition: PwpFile.cxx:759
bool wspaceSkipToEOF()
Skip whitespace and read until EOF.
Definition: PwpFile.h:594
bool readVal(T &v, const PWP_UINT32 base=10)
Reads a single value of type T.
Definition: PwpFile.h:448
bool isWritable() const
Determines if file suports write operations.
Definition: PwpFile.cxx:689
Manages FORTRAN unformatted file records.
Definition: PwpFile.h:761
bool isReadable() const
Determines if file suports read operations.
Definition: PwpFile.cxx:682
static bool unlink(const char *filename)
Delete a file.
Definition: PwpFile.cxx:1036
fpos_t sysFILEPOS
File position data type.
Definition: pwpPlatform.h:53
bool readToken(std::string &tok)
Reads a whitespace delimited token (word).
Definition: PwpFile.cxx:825
bool read(void *buf, size_t size, size_t count)
Read a collection of data items.
Definition: PwpFile.cxx:801
virtual bool write(PWP_INT val, const char *suffix=0, const char *prefix=0)
Writes a integer value with proper encoding and byte order.
Definition: PwpFile.cxx:1098
bool isEof() const
Get the end-of-file status.
Definition: PwpFile.cxx:731
PWP_ENUM_PRECISION precision_
The file&#39;s floating point precision.
Definition: PwpFile.h:719
bool isDouble() const
Determines if floating point precision is PWP_PRECISION_DOUBLE.
Definition: PwpFile.cxx:703
const PwpFile & file() const
Get this record&#39;s file object.
Definition: PwpFile.h:805
bool readDouble(PWP_REAL &v, PWP_UINT32 maxDigits=0)
Reads a single PWP_REAL value.
Definition: PwpFile.cxx:1006
bool needByteSwap() const
Determine if byte swapping is needed for this file.
Definition: PwpFile.h:654
float PWP_FLOAT
32-bit real
Definition: apiPWP.h:215
PwpFile(FILE *fp=0, std::string filename=std::string(), int mode=0)
Default constructor.
Definition: PwpFile.cxx:564
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:799
const char * getName() const
Get the file&#39;s associated filename.
Definition: PwpFile.cxx:794
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:902
Unformatted file data block.
Definition: apiPWPUtils.h:346
PwpFile & file_
Definition: PwpFile.h:845
const PWU_UNFDATA & unfData() const
Get the file&#39;s unformatted data buffer.
Definition: PwpFile.h:646
bool readInt(PWP_UINT32 &v, const PWP_UINT32 base=10, PWP_UINT32 maxDigits=0)
Reads a single PWP_UINT32 value.
Definition: PwpFile.cxx:954
Writes solver files.
Definition: PwpFile.h:111
bool readAlphaToken(std::string &tok)
Reads a whitespace delimited token (word).
Definition: PwpFile.cxx:849
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:387
virtual ~PwpFile()
Destructor.
Definition: PwpFile.cxx:590
bool isAscii() const
Determines if file was opened with either the pwpAscii or pwpFormatted mode flag. ...
Definition: PwpFile.cxx:710
PWU_UNFDATA & unfData()
Get the file&#39;s unformatted data buffer.
Definition: PwpFile.h:640
bool readHex(PWP_UINT32 &v, PWP_UINT32 maxDigits=0)
Reads a single PWP_UINT32 hex value.
Definition: PwpFile.h:512
bool wspaceSkipToChar(const int nextC)
Skip whitespace and read the next char.
Definition: PwpFile.h:568
bool getPos(sysFILEPOS &pos) const
Get the current file position.
Definition: PwpFile.cxx:745
bool isOpen() const
Determines a file&#39;s status.
Definition: PwpFile.cxx:629
PWP_ENUM_PRECISION setPrecision(PWP_ENUM_PRECISION precision)
Set the floating point precision used for writes.
Definition: PwpFile.cxx:666
bool isSingle() const
Determines if floating point precision is PWP_PRECISION_SINGLE.
Definition: PwpFile.cxx:696
PwpFileRecord & operator=(const PwpFileRecord &)
Definition: PwpFile.h:839
size_t pwpFileWrite(const void *buf, size_t size, size_t count, FILE *fp)
Write an collection of data items to a file.
FILE * fp() const
Get the FILE pointer.
Definition: PwpFile.h:628
bool wspaceSkip()
Reads and discards consecutive whitespace bytes.
Definition: PwpFile.cxx:808
bool isBinary() const
Determines if file was opened with pwpBinary mode flag.
Definition: PwpFile.cxx:717
bool getcNotEOF(int &c)
Reads a single byte value.
Definition: PwpFile.h:529
PwpFileWriter * writer_
The file writer implementation.
Definition: PwpFile.h:721
std::string filename_
The file name.
Definition: PwpFile.h:717
The abstract PwpWriterInterface class.
int getc()
Reads a single byte value.
Definition: PwpFile.h:520
bool flush()
Flush file to disk.
Definition: PwpFile.cxx:738
PWP_ENDIANNESS byteOrder_
The file&#39;s byte order.
Definition: PwpFile.h:718
PWP_ENDIANNESS
Flags used to indicate endianness or control endian behaviors in functions.
Definition: apiPWP.h:738
void setName(std::string filename)
Set the file&#39;s associated filename.
Definition: PwpFile.cxx:787
bool ungetc(const int c)
Return a byte value to the file.
Definition: PwpFile.h:538
int mode_
The mode.
Definition: PwpFile.h:716
PwpFileRecord(PwpFile &file, bool doBeginRecord=true)
Constructor.
Definition: PwpFile.cxx:1257
bool close()
Explicitly close the file object.
Definition: PwpFile.cxx:636
bool getSize(PWP_UINT64 &size) const
Get the size of the file managed by this object.
Definition: PwpFile.cxx:767
PWP_ENUM_PRECISION getPrecision() const
Get the floating point precision used for writes.
Definition: PwpFile.cxx:675
double PWP_REAL
64-bit real
Definition: apiPWP.h:218
bool open(std::string filename, int mode)
Opens a file with the given filename and mode.
Definition: PwpFile.cxx:622
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:1050
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:463
static std::string & rtrim(std::string &s)
trim trailing whitespace from s
Definition: PwpFile.h:610
static std::string & ltrim(std::string &s)
trim leading whitespace from s
Definition: PwpFile.h:600
Data and functions useful to PWP-API compliant plugins.
PWU_UNFDATA ud_
The file&#39;s unformatted data stats.
Definition: PwpFile.h:720