Pointwise Plugin SDK
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
PwpFile.cxx
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 #include "PwpFile.h"
10 #include "PwpFileWriter.h"
11 #include "pwpPlatform.h"
12 
13 #include <cctype>
14 #include <cstring>
15 #include <cstdio>
16 #include <string>
17 
18 
19 // readOrdered(), writeOrdered() max number of swappable buffer bytes
20 enum {
22 };
23 
24 
25 //***************************************************************************
26 //***************************************************************************
27 //***************************************************************************
28 
29 class PwpAsciiWriter : public PwpFileWriter {
30 public:
31 
33  PwpFileWriter(file)
34  {
35  }
36 
37 
38  virtual ~PwpAsciiWriter()
39  {
40  }
41 
42 
43  virtual bool write(PWP_INT64 val, const char *suffix = 0,
44  const char *prefix = 0)
45  {
46  return 0 <= fprintf(file_.fp(), "%s%" PWP_INT64_FORMAT "%s",
47  (prefix ? prefix : ""), val, (suffix ? suffix : ""));
48  }
49 
50 
51  virtual bool write(PWP_INT32 val, const char *suffix = 0,
52  const char *prefix = 0)
53  {
54  return 0 <= fprintf(file_.fp(), "%s%ld%s", (prefix ? prefix : ""),
55  (long)val, (suffix ? suffix : ""));
56  }
57 
58 
59  virtual bool write(PWP_INT16 val, const char *suffix = 0,
60  const char *prefix = 0)
61  {
62  return 0 <= fprintf(file_.fp(), "%s%ld%s", (prefix ? prefix : ""),
63  (long)val, (suffix ? suffix : ""));
64  }
65 
66 
67  virtual bool write(PWP_INT8 val, const char *suffix = 0,
68  const char *prefix = 0)
69  {
70  return 0 <= fprintf(file_.fp(), "%s%ld%s", (prefix ? prefix : ""),
71  (long)val, (suffix ? suffix : ""));
72  }
73 
74 
75  virtual bool write(PWP_UINT64 val, const char *suffix = 0,
76  const char *prefix = 0)
77  {
78  return 0 <= fprintf(file_.fp(), "%s%" PWP_UINT64_FORMAT "%s",
79  (prefix ? prefix : ""), val, (suffix ? suffix : ""));
80  }
81 
82 
83  virtual bool write(PWP_UINT32 val, const char *suffix = 0,
84  const char *prefix = 0)
85  {
86  return 0 <= fprintf(file_.fp(), "%s%lu%s", (prefix ? prefix : ""),
87  (unsigned long)val, (suffix ? suffix : ""));
88  }
89 
90 
91  virtual bool write(PWP_UINT16 val, const char *suffix = 0,
92  const char *prefix = 0)
93  {
94  return 0 <= fprintf(file_.fp(), "%s%lu%s", (prefix ? prefix : ""),
95  (unsigned long)val, (suffix ? suffix : ""));
96  }
97 
98 
99  virtual bool write(PWP_UINT8 val, const char *suffix = 0,
100  const char *prefix = 0)
101  {
102  return 0 <= fprintf(file_.fp(), "%s%lu%s", (prefix ? prefix : ""),
103  (unsigned long)val, (suffix ? suffix : ""));
104  }
105 
106 
107  virtual bool write(PWP_FLOAT val, const char *suffix = 0,
108  const char *prefix = 0)
109  {
110  return file_.isDouble() ?
112  (suffix ? suffix : ""), (prefix ? prefix : "")) :
114  (suffix ? suffix : ""), (prefix ? prefix : ""));
115  }
116 
117 
118  virtual bool write(PWP_REAL val, const char *suffix = 0,
119  const char *prefix = 0)
120  {
121  return file_.isSingle() ?
123  (suffix ? suffix : ""), (prefix ? prefix : "")) :
125  (suffix ? suffix : ""), (prefix ? prefix : ""));
126  }
127 
128 
129  virtual bool write(const char * val, PWP_INT size = -1, char /*pad*/ = 0)
130  {
131  if (-1 == size) {
132  size = (PWP_INT)strlen(val);
133  }
134  return 0 <= fprintf(file_.fp(), "%*.*s", (int)size, (int)size, val);
135  }
136 
137 
138 private:
139 
140  bool writeWdPrec(PWP_REAL val, int wd, int prec, const char *sfx,
141  const char *pfx)
142  {
143  switch (fmtType_) {
144  case FormatG:
145  return 0 <= fprintf(file_.fp(), "%s%g%s", pfx, val, sfx);
146  case FormatWdPrecG:
147  return 0 <= fprintf(file_.fp(), "%s%*.*g%s", pfx, wd, prec, val, sfx);
148  case FormatWdG:
149  return 0 <= fprintf(file_.fp(), "%s%*g%s", pfx, wd, val, sfx);
150  case FormatPrecG:
151  return 0 <= fprintf(file_.fp(), "%s%.*g%s", pfx, prec, val, sfx);
152 
153  case FormatF:
154  return 0 <= fprintf(file_.fp(), "%s%f%s", pfx, val, sfx);
155  case FormatWdPrecF:
156  return 0 <= fprintf(file_.fp(), "%s%*.*f%s", pfx, wd, prec, val, sfx);
157  case FormatWdF:
158  return 0 <= fprintf(file_.fp(), "%s%*f%s", pfx, wd, val, sfx);
159  case FormatPrecF:
160  return 0 <= fprintf(file_.fp(), "%s%.*f%s", pfx, prec, val, sfx);
161 
162  case FormatE:
163  return 0 <= fprintf(file_.fp(), "%s%e%s", pfx, val, sfx);
164  case FormatWdPrecE:
165  return 0 <= fprintf(file_.fp(), "%s%*.*e%s", pfx, wd, prec, val, sfx);
166  case FormatWdE:
167  return 0 <= fprintf(file_.fp(), "%s%*e%s", pfx, wd, val, sfx);
168  case FormatPrecE:
169  return 0 <= fprintf(file_.fp(), "%s%.*e%s", pfx, prec, val, sfx);
170  }
171  return false;
172  }
173 };
174 
175 
176 //***************************************************************************
177 //***************************************************************************
178 //***************************************************************************
179 
181 
182  template< typename T >
183  bool writeOrdered(T val) {
184  return writeOrdered(&val, sizeof(T));
185  }
186 
187 public:
188 
190  PwpFileWriter(file)
191  {
192  }
193 
194 
196  {
197  }
198 
199 
200  virtual bool write(PWP_INT64 val, const char *suffix = 0,
201  const char *prefix = 0)
202  {
203  (void)prefix, (void)suffix;
204  return writeOrdered(val);
205  }
206 
207 
208  virtual bool write(PWP_INT32 val, const char *suffix = 0,
209  const char *prefix = 0)
210  {
211  (void)prefix, (void)suffix;
212  return writeOrdered(val);
213  }
214 
215 
216  virtual bool write(PWP_INT16 val, const char *suffix = 0,
217  const char *prefix = 0)
218  {
219  (void)prefix, (void)suffix;
220  return writeOrdered(val);
221  }
222 
223 
224  virtual bool write(PWP_INT8 val, const char *suffix = 0,
225  const char *prefix = 0)
226  {
227  (void)prefix, (void)suffix;
228  return writeOrdered(val);
229  }
230 
231 
232  virtual bool write(PWP_UINT64 val, const char *suffix = 0,
233  const char *prefix = 0)
234  {
235  (void)prefix, (void)suffix;
236  return writeOrdered(val);
237  }
238 
239 
240  virtual bool write(PWP_UINT32 val, const char *suffix = 0,
241  const char *prefix = 0)
242  {
243  (void)prefix, (void)suffix;
244  return writeOrdered(val);
245  }
246 
247 
248  virtual bool write(PWP_UINT16 val, const char *suffix = 0,
249  const char *prefix = 0)
250  {
251  (void)prefix, (void)suffix;
252  return writeOrdered(val);
253  }
254 
255 
256  virtual bool write(PWP_UINT8 val, const char *suffix = 0,
257  const char *prefix = 0)
258  {
259  (void)prefix, (void)suffix;
260  return writeOrdered(val);
261  }
262 
263 
264  virtual bool write(PWP_FLOAT val, const char *suffix = 0,
265  const char *prefix = 0)
266  {
267  (void)prefix, (void)suffix;
268  if (file_.isDouble()) {
269  return writeOrdered((PWP_REAL)val);
270  }
271  return writeOrdered(val);
272  }
273 
274 
275  virtual bool write(PWP_REAL val, const char *suffix = 0,
276  const char *prefix = 0)
277  {
278  (void)prefix, (void)suffix;
279  if (file_.isSingle()) {
280  return writeOrdered((PWP_FLOAT)val);
281  }
282  return writeOrdered(val);
283  }
284 
285 
286  virtual bool write(const char * val, PWP_INT size = -1, char pad = 0)
287  {
288  bool ret = false;
289  if (0 != val) {
290  PWP_INT len = (PWP_INT)strlen(val);
291  if (-1 == size) {
292  size = len;
293  }
294  if (len >= size) {
295  // no padding needed
296  ret = (1 == pwpFileWrite(val, size, 1, file_.fp()));
297  }
298  else if (1 == pwpFileWrite(val, len, 1, file_.fp())) {
299  // str already written, now write pad chars
300  ret = true;
301  const size_t bufSz = 512;
302  char padBuf[bufSz] = { '\0' }; // compiler fills all with zeros
303  if (0 != pad) {
304  memset(padBuf, pad, bufSz);
305  }
306  size -= len; // reduce size by # chars already written
307  while (ret && (bufSz < size)) {
308  ret = (1 == pwpFileWrite(padBuf, bufSz, 1, file_.fp()));
309  size -= bufSz;
310  }
311  // write final partial buffer (size == 0 is okay)
312  ret = ret && (1 == pwpFileWrite(padBuf, size, 1, file_.fp()));
313  }
314  }
315  return ret;
316  }
317 
318 private:
319 
320  bool
321  writeOrdered(const void *buf, size_t size)
322  {
323  bool ret = false;
324  FILE *fp = file_.fp();
325  if (0 == buf) {
326  // bad
327  }
328  else if (1 == size) {
329  ret = (1 == pwpFileWrite(buf, size, 1, fp));
330  }
331  else if (!(size & 0x1) && (ORDERED_MAXSIZE >= size)) {
332  static unsigned char tmpBuf[ORDERED_MAXSIZE];
333  if (file_.needByteSwap()) {
334  const char *cbuf = (const char *)buf;
335  size_t hNdx = 0;
336  size_t tNdx = size - 1;
337  while (hNdx < tNdx) {
338  tmpBuf[hNdx] = cbuf[tNdx];
339  tmpBuf[tNdx--] = cbuf[hNdx++];
340  }
341  buf = tmpBuf; // write tmpBuf instead
342  }
343  ret = (1 == pwpFileWrite(buf, size, 1, fp));
344  }
345  return ret;
346  }
347 };
348 
349 
350 //***************************************************************************
351 //***************************************************************************
352 //***************************************************************************
353 
355 public:
356 
358  PwpFileWriter(file)
359  {
361  }
362 
363 
365  {
367  }
368 
369 
370  virtual bool beginRecord() {
371  endRecord();
373  return 0 != PwuUnfRecBegin(&file_.unfData());
374  }
375 
376 
377  virtual bool beginRecord(PWP_UINT32 bytes, PWP_UINT32 count = 1) {
378  endRecord();
380  return 0 != PwuUnfRecBeginFixed(&file_.unfData(), bytes, count);
381  }
382 
383 
384  virtual bool endRecord() {
385  return 0 != PwuUnfRecEnd(&file_.unfData());
386  }
387 
388 
389  virtual bool write(PWP_INT64 val, const char *suffix = 0,
390  const char *prefix = 0)
391  {
392  (void)prefix, (void)suffix;
393  return 0 != PwuUnfRecWriteINT64(&file_.unfData(), val);
394  }
395 
396 
397  virtual bool write(PWP_INT32 val, const char *suffix = 0,
398  const char *prefix = 0)
399  {
400  (void)prefix, (void)suffix;
401  return 0 != PwuUnfRecWriteINT32(&file_.unfData(), val);
402  }
403 
404 
405  virtual bool write(PWP_INT16 val, const char *suffix = 0,
406  const char *prefix = 0)
407  {
408  (void)prefix, (void)suffix;
409  return 0 != PwuUnfRecWriteINT16(&file_.unfData(), val);
410  }
411 
412 
413  virtual bool write(PWP_INT8 val, const char *suffix = 0,
414  const char *prefix = 0)
415  {
416  (void)prefix, (void)suffix;
417  return 0 != PwuUnfRecWriteINT8(&file_.unfData(), val);
418  }
419 
420 
421  virtual bool write(PWP_UINT64 val, const char *suffix = 0,
422  const char *prefix = 0)
423  {
424  (void)prefix, (void)suffix;
425  return 0 != PwuUnfRecWriteUINT64(&file_.unfData(), val);
426  }
427 
428 
429  virtual bool write(PWP_UINT32 val, const char *suffix = 0,
430  const char *prefix = 0)
431  {
432  (void)prefix, (void)suffix;
433  return 0 != PwuUnfRecWriteUINT32(&file_.unfData(), val);
434  }
435 
436 
437  virtual bool write(PWP_UINT16 val, const char *suffix = 0,
438  const char *prefix = 0)
439  {
440  (void)prefix, (void)suffix;
441  return 0 != PwuUnfRecWriteUINT16(&file_.unfData(), val);
442  }
443 
444 
445  virtual bool write(PWP_UINT8 val, const char *suffix = 0,
446  const char *prefix = 0)
447  {
448  (void)prefix, (void)suffix;
449  return 0 != PwuUnfRecWriteUINT8(&file_.unfData(), val);
450  }
451 
452 
453  virtual bool write(PWP_FLOAT val, const char *suffix = 0,
454  const char *prefix = 0)
455  {
456  (void)prefix, (void)suffix;
457  if (file_.isDouble()) {
458  return 0 != PwuUnfRecWriteREAL(&file_.unfData(), (PWP_REAL)val);
459  }
460  return 0 != PwuUnfRecWriteFLOAT(&file_.unfData(), val);
461  }
462 
463 
464  virtual bool write(PWP_REAL val, const char *suffix = 0,
465  const char *prefix = 0)
466  {
467  (void)prefix, (void)suffix;
468  if (file_.isSingle()) {
469  return 0 != PwuUnfRecWriteFLOAT(&file_.unfData(), (PWP_FLOAT)val);
470  }
471  return 0 != PwuUnfRecWriteREAL(&file_.unfData(), val);
472  }
473 
474 
475  virtual bool write(const char * val, PWP_INT size = -1, char pad = 0)
476  {
477  bool ret = false;
478  if (0 != val) {
479  PWP_INT len = (PWP_INT)strlen(val);
480  if (-1 == size) {
481  size = len;
482  }
483  if (len >= size) {
484  // no padding needed
485  ret = (0 != PwuUnfRecWriteBuf(&file_.unfData(), val, size));
486  }
487  else if (0 != PwuUnfRecWriteBuf(&file_.unfData(), val, len)) {
488  // str already written, now write pad chars
489  ret = true;
490  const size_t bufSz = 512;
491  char padBuf[bufSz] = { '\0' }; // compiler fills all with zeros
492  if (0 != pad) {
493  memset(padBuf, pad, bufSz);
494  }
495  size -= len; // reduce size by # chars already written
496  while (ret && (bufSz < size)) {
497  ret = (0 != PwuUnfRecWriteBuf(&file_.unfData(), padBuf,
498  bufSz));
499  size -= bufSz;
500  }
501  // write final partial buffer (size == 0 is okay)
502  ret = ret && (0 != PwuUnfRecWriteBuf(&file_.unfData(), padBuf,
503  size));
504  }
505  }
506  return ret;
507  }
508 };
509 
510 
511 //***************************************************************************
512 //***************************************************************************
513 //***************************************************************************
514 
515 PwpFile::PwpFile(FILE *fp, std::string filename, int mode) :
516  fp_(0),
517  mode_(0),
518  filename_(),
519  byteOrder_(PWP_ENDIAN_NATIVE),
520  precision_(PWP_PRECISION_DOUBLE),
521  ud_(),
522  writer_(0)
523 {
524  wrap(fp, filename, mode);
525 }
526 
527 
528 PwpFile::PwpFile(std::string filename, int mode) :
529  fp_(0),
530  mode_(0),
531  filename_(),
532  byteOrder_(PWP_ENDIAN_NATIVE),
533  precision_(PWP_PRECISION_DOUBLE),
534  ud_(),
535  writer_(0)
536 {
537  open(filename, mode);
538 }
539 
541  fp_(nullptr),
542  mode_(),
543  filename_(),
544  byteOrder_(ref.byteOrder_),
545  precision_(ref.precision_),
546  ud_(),
547  writer_(nullptr)
548 {
549  open(ref.filename_, ref.mode_);
550 }
551 
553 {
554  close();
555 }
556 
557 PwpFile&
559 {
560  open(ref.filename_, ref.mode_);
561  byteOrder_ = ref.byteOrder_;
562  precision_ = ref.precision_;
563  return *this;
564 }
565 
566 
567 bool
568 PwpFile::wrap(FILE *fp, std::string filename, int mode)
569 {
570  close();
571  fp_ = fp;
572  if (0 != fp_) {
573  filename_ = filename;
574  mode_ = mode;
575  if (isAscii()) {
576  writer_ = new PwpAsciiWriter(*this);
577  }
578  else if (isBinary()) {
579  writer_ = new PwpBinaryWriter(*this);
580  }
581  else if (isUnformatted()) {
582  writer_ = new PwpUnformattedWriter(*this);
583  }
584  if (0 == writer_) {
585  close(); // bad error
586  }
587  }
588  return 0 != fp_;
589 }
590 
591 
592 bool
593 PwpFile::open(std::string filename, int mode)
594 {
595  return wrap(pwpFileOpen(filename.c_str(), mode), filename, mode);
596 }
597 
598 
599 bool
601 {
602  return 0 != fp_;
603 }
604 
605 bool
607 {
608  return 0 != feof(fp_);
609 }
610 
611 
612 bool
614 {
615  // delete writer_ BEFORE closing file so dtor has a chance to use file.
616  delete writer_;
617  writer_ = 0;
618  bool ret = (0 == pwpFileClose(fp_));
619  fp_ = 0;
620  filename_.clear();
621  mode_ = 0;
622  return ret;
623 }
624 
625 
628 {
630  byteOrder_ = byteOrder;
631  return ret;
632 }
633 
634 
636 PwpFile::getByteOrder(bool mapBigLittle) const
637 {
638  return mapBigLittle ? mapToBigLittle(byteOrder_) : byteOrder_;
639 }
640 
641 
644 {
646  precision_ = precision;
647  return ret;
648 }
649 
650 
653 {
654  return precision_;
655 }
656 
657 
658 bool
660 {
661  return 0 != (mode_ & pwpRead);
662 }
663 
664 
665 bool
667 {
668  return 0 != (mode_ & pwpWrite);
669 }
670 
671 
672 bool
674 {
676 }
677 
678 
679 bool
681 {
683 }
684 
685 
686 bool
688 {
689  return 0 != (mode_ & (pwpAscii | pwpFormatted));
690 }
691 
692 bool
694 {
695  bool ret = false;
696  PwpFile local(*this);
697  PWP_UINT64 fileSize;
698  getSize(filename_.c_str(), fileSize);
699 
700  // Process chunks of data from file to determine ASCII-ness!
701  const size_t NumChunks = 4;
702  const size_t ChunkSize = 256;
703  const size_t FullBufSize = ChunkSize * NumChunks;
704  PWP_UINT8 buf[FullBufSize];
705  PWP_UINT n = 0;
706  if (FullBufSize >= fileSize) {
707  // Entire file fits in buf
708  n = pwpFileRead(buf, sizeof(PWP_UINT8), FullBufSize, local.fp_);
709  }
710  else {
711  // Get chunk 1 from start of file
712  n = pwpFileRead(buf, sizeof(PWP_UINT8), ChunkSize, local.fp_);
713  // Get chunks 2 ... NumChunks from file at equally spaced intervals
714  for (PWP_UINT chunk = 1; chunk < NumChunks; ++chunk) {
715  if (!local.setIntPos((chunk * fileSize) / NumChunks)) {
716  continue;
717  }
718  // Fill this chunk with chars from start of interval.
719  n += pwpFileRead(buf + n, sizeof(PWP_UINT8), ChunkSize, local.fp_);
720  }
721  }
722 
723  // Scan for number of non-ascii chars found in buf[]
724  PWP_UINT nNonAscii = 0;
725  for (PWP_UINT ii = 0; ii < n; ++ii) {
726  if ((0x80 <= buf[ii]) || (0x08 >= buf[ii])) {
727  ++nNonAscii;
728  }
729  }
730  ret = ((nNonAscii / (PWP_REAL)n) < 0.1);
731 
732  return ret;
733 }
734 
735 static bool
736 parseUnformattedRecord(PwpFile &theFile, PWP_INT64 &totalRecSize, bool &swap)
737 {
738  bool result = false;
739  PWP_UINT64 fileSize;
740  theFile.getSize(fileSize);
741 
742  totalRecSize = 0;
743  if (0 < fileSize) {
744  // It is valid for block length to be zero. So, there must be at
745  // least (header + footer) bytes remaining in file for a valid
746  // record to exist.
747  const PWP_INT64 HDRFTR_VALUE_SIZE = sizeof(PWP_INT32);
748  const PWP_INT64 MIN_RECORD_SIZE = 2 * HDRFTR_VALUE_SIZE;
749 
750  try {
751  // calc starting unconsumed file bytes
752  PWP_INT64 bytesRemaining = fileSize - theFile.getPos();
753  if (bytesRemaining >= MIN_RECORD_SIZE) {
754  // get record's data block size
755  PWP_INT32 blockSize = theFile.readSwap<PWP_INT32>(swap);
756  // verify there are enough bytes left in theFile for footer
757  // skip past data block to beginning of footer
758  // consume footer and verify the header/footer values match
759  bytesRemaining -= HDRFTR_VALUE_SIZE + blockSize;
760  if ((HDRFTR_VALUE_SIZE <= bytesRemaining) &&
761  theFile.setIntPos(theFile.getPos() + blockSize) &&
762  (blockSize == theFile.readSwap<PWP_INT32>(swap))) {
763  totalRecSize = HDRFTR_VALUE_SIZE + blockSize +
764  HDRFTR_VALUE_SIZE;
765  result = true;
766  }
767  }
768  }
769  catch (...) {
770  result = false;
771  }
772  }
773  return result;
774 }
775 
776 bool
777 PwpFile::checkUnformatted(bool &swapped, std::vector<PWP_INT64> *pRecSizes) const
778 {
779  bool result = false;
780  PwpFile local(*this);
781  if (pRecSizes) {
782  pRecSizes->clear();
783  }
784 
785  PWP_INT64 totalRecSize;
786  sysFILEPOS start;
787  local.getPos(start);
788  bool swap = true;
789 
790  if (!parseUnformattedRecord(local, totalRecSize, swap)) {
791  local.setPos(start);
792  // toggle swap bytes flag and try to read first record again
793  swap = !swap;
794  parseUnformattedRecord(local, totalRecSize, swap);
795  }
796  if (0 < totalRecSize) {
797  if (pRecSizes) {
798  pRecSizes->push_back(totalRecSize);
799  }
800  PWP_UINT64 fileSize;
801  local.getSize(fileSize);
802  PWP_INT64 bytesRemaining = fileSize - totalRecSize;
803  while (parseUnformattedRecord(local, totalRecSize, swap)) {
804  if (pRecSizes) {
805  pRecSizes->push_back(totalRecSize);
806  }
807  bytesRemaining -= totalRecSize;
808  }
809  result = (0 == bytesRemaining);
810  if (result) {
811  swapped = swap;
812  }
813  }
814 
815  local.setPos(start);
816 
817  return result;
818 }
819 
820 bool
822 {
823  return 0 != (mode_ & pwpBinary);
824 }
825 
826 
827 bool
829 {
830  return 0 != (mode_ & pwpUnformatted);
831 }
832 
833 
834 bool
836 {
837  return 0 != pwpFileEof(fp_);
838 }
839 
840 
841 bool
843 {
844  return 0 == pwpFileFlush(fp_);
845 }
846 
847 
848 bool
850 {
851  return 0 == pwpFileGetpos(fp_, &pos);
852 }
853 
854 PWP_INT64
856 {
857 #if !defined(linux)
858  fpos_t pos;
859  pwpFileGetpos(fp_, &pos);
860 #else
861  //linux fpos_t cannot be casted to long
862  PWP_INT64 pos;
863  pos = ::ftell(fp_);
864 #endif
865  return (PWP_INT64)pos;
866 }
867 
868 
869 bool
871 {
872 #if !defined(linux)
873  fpos_t pos = position;
874  return 0 == pwpFileSetpos(fp_, &pos);
875 #else
876  return (::fseek(fp_,position,SEEK_SET) == 0);
877 #endif
878 }
879 
880 bool
881 PwpFile::setPos(const sysFILEPOS &position)
882 {
883  return 0 == pwpFileSetpos(fp_, &position);
884 }
885 
886 bool
888 {
890  return 0 != fp_;
891 }
892 
893 
894 bool
896 {
897  size_t sz;
898  return (0 == pwpFileGetSize(fp_, &sz)) ?
899  ((size = (PWP_UINT64)sz), true) :
900  false;
901 }
902 
903 
904 bool
905 PwpFile::getSize(const char *filename, PWP_UINT64 &size)
906 {
907  size_t sz;
908  return (0 == pwpFilenameGetSize(filename, &sz)) ?
909  ((size = (PWP_UINT64)sz), true) :
910  false;
911 }
912 
913 
914 void
915 PwpFile::setName(std::string filename)
916 {
917  filename_ = filename;
918 }
919 
920 
921 const char *
923 {
924  return filename_.c_str();
925 }
926 
927 
928 bool
929 PwpFile::read(void *buf, size_t size, size_t count)
930 {
931  return count == pwpFileRead(buf, size, count, fp_);
932 }
933 
934 
935 bool
937 {
938  int c;
939  while (1) {
940  if (!getcNotEOF(c)) {
941  return false;
942  }
943  if (!std::isspace(c)) {
944  break;
945  }
946  }
947  // put back non-whitespace char
948  return ungetc(c);
949 }
950 
951 
952 bool
953 PwpFile::readToken(std::string &tok)
954 {
955  tok.clear();
956  if (wspaceSkip()) {
957  tok.reserve(DefReserve);
958  while (1) {
959  const int c = ::fgetc(fp_);
960  if (EOF == c) {
961  break;
962  }
963  if (!std::isspace(c)) {
964  tok += static_cast<char>(c);
965  continue;
966  }
967  // found trailing white space char, put it back and stop
968  ::ungetc(c, fp_);
969  break;
970  }
971  }
972  return !tok.empty();
973 }
974 
975 
976 bool
977 PwpFile::readAlphaToken(std::string &tok)
978 {
979  tok.clear();
980  if (wspaceSkip()) {
981  tok.reserve(DefReserve);
982  while (1) {
983  const int c = ::fgetc(fp_);
984  if (EOF == c) {
985  break;
986  }
987  if (std::isalpha(c)) {
988  tok += static_cast<char>(c);
989  continue;
990  }
991  // found non-alpha char put it back
992  ::ungetc(c, fp_);
993  break;
994  }
995  }
996  return !tok.empty();
997 }
998 
999 
1000 bool
1001 PwpFile::readUntil(std::string &str, const int stopC, int maxLen,
1002  const bool doTrim)
1003 {
1004  if (0 == maxLen) {
1005  maxLen = 2048; // sanity check
1006  }
1007  str.clear();
1008  str.reserve(DefReserve);
1009  int len = 0;
1010  while (len <= maxLen) {
1011  const int c = ::fgetc(fp_);
1012  // check this first to support stopC == EOF
1013  if (stopC == c) {
1014  if (doTrim) {
1015  trim(str);
1016  }
1017  return true;
1018  }
1019  if (EOF == c) {
1020  break;
1021  }
1022  str += static_cast<char>(c);
1023  ++len;
1024  }
1025  return false;
1026 }
1027 
1028 
1029 bool
1031 {
1032  (void)base;
1033  while (cnt--) {
1034  if (1 != fscanf(*this, "%lf", v++)) {
1035  return false;
1036  }
1037  }
1038  return true;
1039 }
1040 
1041 
1042 bool
1044 {
1045  (void)base;
1046  while (cnt--) {
1047  if (1 != fscanf(*this, "%f", v++)) {
1048  return false;
1049  }
1050  }
1051  return true;
1052 }
1053 
1054 
1055 bool
1057 {
1058  (void)base;
1059  while (cnt--) {
1060  if (1 != fscanf(*this, "%u", (unsigned int*)v++)) {
1061  return false;
1062  }
1063  }
1064  return true;
1065 }
1066 
1067 
1068 bool
1069 PwpFile::readAscVal(char *v, PWP_UINT32 cnt, const PWP_UINT32 base)
1070 {
1071  (void)base;
1072  while (cnt--) {
1073  if (1 != fscanf(*this, "%c", v++)) {
1074  return false;
1075  }
1076  }
1077  return true;
1078 }
1079 
1080 bool
1082  PWP_UINT32 maxDigits)
1083 {
1084  if (0 == maxDigits) {
1085  maxDigits = PWP_UINT32_MAX;
1086  }
1087  std::string tok;
1088  if (wspaceSkip()) {
1089  tok.reserve(DefReserve);
1090  while (0 != maxDigits) {
1091  const int c = ::fgetc(fp_);
1092  if (EOF == c) {
1093  return !tok.empty();
1094  }
1095  if (10 == base) {
1096  if (std::isdigit(c)) {
1097  tok += static_cast<char>(c);
1098  --maxDigits;
1099  continue;
1100  }
1101  }
1102  else if (16 == base) {
1103  if (std::isxdigit(c)) {
1104  tok += static_cast<char>(c);
1105  --maxDigits;
1106  continue;
1107  }
1108  }
1109  else if (8 == base) {
1110  if ((c >= '0') && (c <= '7')) {
1111  tok += static_cast<char>(c);
1112  --maxDigits;
1113  continue;
1114  }
1115  }
1116  else if (!std::isspace(c)) {
1117  // other base - this is not complete
1118  tok += static_cast<char>(c);
1119  --maxDigits;
1120  continue;
1121  }
1122  // found non-int char put it back
1123  ::ungetc(c, fp_);
1124  break;
1125  }
1126  }
1127  return tok.empty() ? false :
1128  ((val = std::stoul(tok.c_str(), 0, int(base))), true);
1129 }
1130 
1131 
1132 bool
1134  const PWP_UINT32 base, PWP_UINT32 maxDigits)
1135 {
1136  return PwpFile::readInt(v, base, maxDigits) ? true : ((v = defV), false);
1137 }
1138 
1139 
1140 bool
1142 {
1143  //char *endPtr;
1144  //val = std::strtod(toks.at(0).c_str(), &endPtr);
1145  //return ret;
1146  if (0 == maxDigits) {
1147  maxDigits = PWP_UINT32_MAX;
1148  }
1149  std::string tok;
1150  if (wspaceSkip()) {
1151  tok.reserve(DefReserve);
1152  while (1) {
1153  const int c = ::fgetc(fp_);
1154  if (EOF == c) {
1155  return false;
1156  }
1157  if (std::isdigit(c)) {
1158  tok += static_cast<char>(c);
1159  continue;
1160  }
1161  // found non-alpha char put it back
1162  ::ungetc(c, fp_);
1163  break;
1164  }
1165  }
1166  return tok.empty() ? false :
1167  ((val = std::stoul(tok.c_str(), 0, 10)), true);
1168 }
1169 
1170 bool
1171 PwpFile::unlink(const char *filename)
1172 {
1173  return 0 == pwpFileDelete(filename);
1174 }
1175 
1176 
1179 {
1181 }
1182 
1183 
1186 {
1187  PWP_ENDIANNESS ret = byteOrder;
1188  switch (byteOrder) {
1189  case PWP_ENDIAN_NATIVE:
1190  ret = getOsByteOrder();
1191  break;
1192  case PWP_ENDIAN_FOREIGN:
1194  PWP_ENDIAN_BIG);
1195  break;
1196  case PWP_ENDIAN_BIG:
1197  case PWP_ENDIAN_LITTLE:
1198  break; // already set above
1199  default:
1200  ret = PWP_ENDIAN_ERROR;
1201  break;
1202  }
1203  return ret;
1204 }
1205 
1206 
1207 bool
1209 {
1210  return getOsByteOrder() != mapToBigLittle(byteOrder);
1211 }
1212 
1213 
1214 bool
1216  return writer_ && writer_->beginRecord();
1217 }
1218 
1219 
1220 bool
1222  return writer_ && writer_->beginRecord(bytes, count);
1223 }
1224 
1225 
1226 bool
1228  return writer_ && writer_->endRecord();
1229 }
1230 
1231 
1232 bool
1233 PwpFile::write(PWP_INT64 val, const char *suffix, const char *prefix)
1234 {
1235  return writer_ && writer_->write(val, suffix, prefix);
1236 }
1237 
1238 
1239 bool
1240 PwpFile::write(PWP_INT32 val, const char *suffix, const char *prefix)
1241 {
1242  return writer_ && writer_->write(val, suffix, prefix);
1243 }
1244 
1245 
1246 bool
1247 PwpFile::write(PWP_INT16 val, const char *suffix, const char *prefix)
1248 {
1249  return writer_ && writer_->write(val, suffix, prefix);
1250 }
1251 
1252 
1253 bool
1254 PwpFile::write(PWP_INT8 val, const char *suffix, const char *prefix)
1255 {
1256  return writer_ && writer_->write(val, suffix, prefix);
1257 }
1258 
1259 
1260 bool
1261 PwpFile::write(PWP_UINT64 val, const char *suffix, const char *prefix)
1262 {
1263  return writer_ && writer_->write(val, suffix, prefix);
1264 }
1265 
1266 
1267 bool
1268 PwpFile::write(PWP_UINT32 val, const char *suffix, const char *prefix)
1269 {
1270  return writer_ && writer_->write(val, suffix, prefix);
1271 }
1272 
1273 
1274 bool
1275 PwpFile::write(PWP_UINT16 val, const char *suffix, const char *prefix)
1276 {
1277  return writer_ && writer_->write(val, suffix, prefix);
1278 }
1279 
1280 
1281 bool
1282 PwpFile::write(PWP_UINT8 val, const char *suffix, const char *prefix)
1283 {
1284  return writer_ && writer_->write(val, suffix, prefix);
1285 }
1286 
1287 
1288 bool
1289 PwpFile::write(PWP_FLOAT val, const char *suffix, const char *prefix)
1290 {
1291  return writer_ && writer_->write(val, suffix, prefix);
1292 }
1293 
1294 
1295 bool
1296 PwpFile::write(PWP_REAL val, const char *suffix, const char *prefix)
1297 {
1298  return writer_ && writer_->write(val, suffix, prefix);
1299 }
1300 
1301 
1302 void
1303 PwpFile::setFmtFieldSingle(const int width, const int prec)
1304 {
1305  if (0 != writer_) {
1306  writer_->setFmtFieldSingle(width, prec);
1307  }
1308 }
1309 
1310 
1311 void
1312 PwpFile::getFmtFieldSingle(int &width, int &prec) const
1313 {
1314  if (0 != writer_) {
1315  writer_->getFmtFieldSingle(width, prec);
1316  }
1317 }
1318 
1319 
1320 void
1321 PwpFile::setFmtFieldDouble(const int width, const int prec)
1322 {
1323  if (0 != writer_) {
1324  writer_->setFmtFieldDouble(width, prec);
1325  }
1326 }
1327 
1328 
1329 void
1330 PwpFile::getFmtFieldDouble(int &width, int &prec) const
1331 {
1332  if (0 != writer_) {
1333  writer_->getFmtFieldDouble(width, prec);
1334  }
1335 }
1336 
1337 
1338 
1339 void
1341 {
1342  if (0 != writer_) {
1343  writer_->setFmtType(type);
1344  }
1345 }
1346 
1347 
1350 {
1352 }
1353 
1354 
1355 bool
1356 PwpFile::write(const char * val, PWP_INT size, char pad)
1357 {
1358  return writer_ && writer_->write(val, size, pad);
1359 }
1360 
1361 
1362 //***************************************************************************
1363 //***************************************************************************
1364 //***************************************************************************
1365 
1366 PwpFileRecord::PwpFileRecord(PwpFile &file, bool doBeginRecord) :
1367  file_(file)
1368 {
1369  if (doBeginRecord) {
1370  beginRecord();
1371  }
1372 }
1373 
1374 
1376  file_(file)
1377 {
1378  beginRecord(bytes, count);
1379 }
1380 
1381 
1383 {
1384  if (file_.isOpen()) {
1385  endRecord();
1386  }
1387 }
1388 
1389 
1390 bool
1392  return file_.beginRecord();
1393 }
1394 
1395 
1396 bool
1398  return file_.beginRecord(bytes, count);
1399 }
1400 
1401 
1402 bool
1404  return file_.endRecord();
1405 }
1406 
1407 
1408 bool
1409 PwpFileRecord::write(PWP_INT64 val, const char *suffix,
1410  const char *prefix)
1411 {
1412  return file_.write(val, suffix, prefix);
1413 }
1414 
1415 
1416 bool
1417 PwpFileRecord::write(PWP_INT32 val, const char *suffix,
1418  const char *prefix)
1419 {
1420  return file_.write(val, suffix, prefix);
1421 }
1422 
1423 
1424 bool
1425 PwpFileRecord::write(PWP_INT16 val, const char *suffix,
1426  const char *prefix)
1427 {
1428  return file_.write(val, suffix, prefix);
1429 }
1430 
1431 
1432 bool
1433 PwpFileRecord::write(PWP_INT8 val, const char *suffix,
1434  const char *prefix)
1435 {
1436  return file_.write(val, suffix, prefix);
1437 }
1438 
1439 
1440 bool
1441 PwpFileRecord::write(PWP_UINT64 val, const char *suffix,
1442  const char *prefix)
1443 {
1444  return file_.write(val, suffix, prefix);
1445 }
1446 
1447 
1448 bool
1449 PwpFileRecord::write(PWP_UINT32 val, const char *suffix,
1450  const char *prefix)
1451 {
1452  return file_.write(val, suffix, prefix);
1453 }
1454 
1455 
1456 bool
1457 PwpFileRecord::write(PWP_UINT16 val, const char *suffix,
1458  const char *prefix)
1459 {
1460  return file_.write(val, suffix, prefix);
1461 }
1462 
1463 
1464 bool
1465 PwpFileRecord::write(PWP_UINT8 val, const char *suffix,
1466  const char *prefix)
1467 {
1468  return file_.write(val, suffix, prefix);
1469 }
1470 
1471 
1472 bool
1473 PwpFileRecord::write(PWP_FLOAT val, const char *suffix,
1474  const char *prefix)
1475 {
1476  return file_.write(val, suffix, prefix);
1477 }
1478 
1479 
1480 bool
1481 PwpFileRecord::write(PWP_REAL val, const char *suffix,
1482  const char *prefix)
1483 {
1484  return file_.write(val, suffix, prefix);
1485 }
1486 
1487 
1488 void
1489 PwpFileRecord::setFmtFieldSingle(const int width, const int prec)
1490 {
1491  file_.setFmtFieldSingle(width, prec);
1492 }
1493 
1494 
1495 void
1496 PwpFileRecord::getFmtFieldSingle(int &width, int &prec) const
1497 {
1498  file_.getFmtFieldSingle(width, prec);
1499 }
1500 
1501 
1502 void
1503 PwpFileRecord::setFmtFieldDouble(const int width, const int prec)
1504 {
1505  file_.setFmtFieldDouble(width, prec);
1506 }
1507 
1508 
1509 void
1510 PwpFileRecord::getFmtFieldDouble(int &width, int &prec) const
1511 {
1512  file_.getFmtFieldDouble(width, prec);
1513 }
1514 
1515 
1516 
1517 void
1519 {
1520  file_.setFmtType(type);
1521 }
1522 
1523 
1526 {
1527  return file_.getFmtType();
1528 }
1529 
1530 
1531 bool
1532 PwpFileRecord::write(const char * val, PWP_INT size, char pad)
1533 {
1534  return file_.write(val, size, pad);
1535 }
static void swap(T &val)
swap() - (Importer-only function) Wrapper for function swapByteOrder().
Definition: PwpFile.h:602
unsigned char PWP_UINT8
8-bit unsigned integer
Definition: apiPWP.h:170
virtual void getFmtFieldSingle(int &width, int &prec) const
Definition: PwpFile.cxx:1496
PWP_BOOL PwuUnfRecBeginFixed(PWU_UNFDATA *pUData, PWP_UINT32 bytes, PWP_UINT32 count)
Prepares a PWU_UNFDATA block for writing a new unformatted data record when the amount of data being ...
PWP_INT64 getPos() const
Get the current file position.
Definition: PwpFile.cxx:855
virtual bool write(PWP_UINT8 val, const char *suffix=0, const char *prefix=0)
Writes a integer value with proper encoding and byte order.
Definition: PwpFile.cxx:256
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
FormatType
Formatted output types for floating point values.
Format &quot;%*e&quot;.
bool writeOrdered(const void *buf, size_t size)
Definition: PwpFile.cxx:321
PWP_BOOL PwuUnfRecWriteUINT16(PWU_UNFDATA *pUData, PWP_UINT16 val)
Write a PWP_UINT16 value to the current record with endian order applied.
bool isUnformatted() const
Determines if file was opened with pwpUnformatted mode flag.
Definition: PwpFile.cxx:828
Format &quot;%.*g&quot;.
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
PwpAsciiWriter(PwpFile &file)
Definition: PwpFile.cxx:32
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
virtual bool write(PWP_INT32 val, const char *suffix=0, const char *prefix=0)
Writes a integer value with proper encoding and byte order.
Definition: PwpFile.cxx:51
unsigned int PWP_UINT32
32-bit unsigned integer
Definition: apiPWP.h:210
virtual void setFmtFieldDouble(const int width, const int prec)
Definition: PwpFile.cxx:1321
virtual FormatType getFmtType() const
Definition: PwpFile.cxx:1349
virtual bool write(PWP_UINT64 val, const char *suffix=0, const char *prefix=0)
Writes a integer value with proper encoding and byte order.
Definition: PwpFile.cxx:421
virtual ~PwpFileRecord()
Destructor.
Definition: PwpFile.cxx:1382
int pwpFileEof(FILE *fp)
Queries end-of-file status.
The abstract PwpFileWriter class.
PwpFile & file_
The file being written.
Format &quot;%*g&quot;.
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
virtual bool beginRecord()
Starts an unformatted record.
Definition: PwpFile.cxx:1215
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
virtual void setFmtFieldDouble(const int width, const int prec)
default implementation.
PWP_ENDIANNESS setByteOrder(PWP_ENDIANNESS order)
Set the byte order used for writes.
Definition: PwpFile.cxx:627
virtual bool beginRecord()
Starts an unformatted record.
Definition: PwpFile.cxx:1391
PWP_ENDIANNESS getByteOrder(bool mapBigLittle=false) const
Get the byte order used for writes.
Definition: PwpFile.cxx:636
virtual bool write(PWP_UINT16 val, const char *suffix=0, const char *prefix=0)
Writes a integer value with proper encoding and byte order.
Definition: PwpFile.cxx:91
virtual void setFmtType(FormatType type)
Definition: PwpFile.cxx:1340
PWP_BOOL PwuUnfRecWriteBuf(PWU_UNFDATA *pUData, const void *buf, size_t size)
Write a data buffer to the current record.
bool fileEof() const
Check for the end of file.
Definition: PwpFile.cxx:606
virtual bool write(PWP_REAL val, const char *suffix=0, const char *prefix=0)
Writes a floating point value with proper precision, encoding and byte order.
Definition: PwpFile.cxx:118
int pwpFileClose(FILE *fp)
Closes a file opened with pwpFileOpen().
int fmtPrecDouble_
Double precision format decimals.
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
PWP_ENDIANNESS PwuUnfFileSetEndianness(PWU_UNFDATA *pUData, PWP_ENDIANNESS endianness)
Set the output endianness.
FILE * fp_
The FILE pointer.
Definition: PwpFile.h:857
int pwpFileSetpos(FILE *fp, const sysFILEPOS *pos)
Set the current file position.
FILE * pwpFileOpen(const char *filename, int mode)
Opens a file for I/O.
bool rewind()
Reset position to the beginning of the file.
Definition: PwpFile.cxx:887
virtual bool write(PWP_UINT64 val, const char *suffix=0, const char *prefix=0)
Writes a integer value with proper encoding and byte order.
Definition: PwpFile.cxx:75
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:43
bool isOpen() const
Determines a file&#39;s status.
Definition: PwpFile.cxx:600
size_t pwpFileRead(void *buf, size_t size, size_t count, FILE *fp)
Read an collection of data items from a file.
PWP_BOOL PwuUnfFileBegin(FILE *fp, PWU_UNFDATA *pUData)
Prepares a PWU_UNFDATA block for a new unformatted file I/O session.
bool writeWdPrec(PWP_REAL val, int wd, int prec, const char *sfx, const char *pfx)
Definition: PwpFile.cxx:140
bool isEof() const
Get the end-of-file status.
Definition: PwpFile.cxx:835
virtual bool write(const char *val, PWP_INT size=-1, char pad=0)
Writes a string value.
Definition: PwpFile.cxx:475
bool writeOrdered(T val)
Definition: PwpFile.cxx:183
PWP_BOOL PwuUnfFileEnd(PWU_UNFDATA *pUData)
Finalize an unformatted file I/O session.
static bool unlink(const char *filename)
Delete a file.
Definition: PwpFile.cxx:1171
fpos_t sysFILEPOS
File position data type.
Definition: pwpPlatform.h:51
virtual bool beginRecord()
NOP default implementation.
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
Format &quot;%*.*g&quot;.
bool getPos(sysFILEPOS &pos) const
Get the current file position.
Definition: PwpFile.cxx:849
virtual void getFmtFieldSingle(int &width, int &prec) const
default implementation.
virtual bool write(PWP_INT16 val, const char *suffix=0, const char *prefix=0)
Writes a integer value with proper encoding and byte order.
Definition: PwpFile.cxx:405
virtual bool write(PWP_UINT32 val, const char *suffix=0, const char *prefix=0)
Writes a integer value with proper encoding and byte order.
Definition: PwpFile.cxx:240
PWP_ENUM_PRECISION precision_
The file&#39;s floating point precision.
Definition: PwpFile.h:861
virtual FormatType getFmtType() const
default implementation.
virtual void setFmtType(FormatType type)
default implementation.
bool readDouble(PWP_REAL &v, PWP_UINT32 maxDigits=0)
Reads a single PWP_REAL value.
Definition: PwpFile.cxx:1141
Format &quot;%g&quot;.
float PWP_FLOAT
32-bit real
Definition: apiPWP.h:261
Format &quot;%*f&quot;.
Cross Platform Functions.
PwpFile(FILE *fp=0, std::string filename=std::string(), int mode=0)
Default constructor.
Definition: PwpFile.cxx:515
virtual void getFmtFieldDouble(int &width, int &prec) const
Definition: PwpFile.cxx:1330
Format &quot;%.*f&quot;.
virtual bool endRecord()
NOP default implementation.
Definition: PwpFile.cxx:384
virtual bool endRecord()
Ends an unformatted record.
Definition: PwpFile.cxx:1227
unsigned short PWP_UINT16
16-bit unsigned integer
Definition: apiPWP.h:190
#define PWP_UINT64_FORMAT
PWP_UINT64 printf format flags.
Definition: apiPWP.h:249
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
virtual void setFmtFieldDouble(const int width, const int prec)
Definition: PwpFile.cxx:1503
virtual void getFmtFieldDouble(int &width, int &prec) const
default implementation.
PwpFile & file_
Definition: PwpFile.h:987
virtual ~PwpAsciiWriter()
Definition: PwpFile.cxx:38
virtual bool write(PWP_REAL val, const char *suffix=0, const char *prefix=0)
Writes a floating point value with proper precision, encoding and byte order.
Definition: PwpFile.cxx:275
virtual void setFmtFieldSingle(const int width, const int prec)
Definition: PwpFile.cxx:1489
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
PWP_BOOL PwuUnfRecWriteREAL(PWU_UNFDATA *pUData, PWP_REAL val)
Write a PWP_REAL value to the current record with endian order applied.
Writes solver files.
Definition: PwpFile.h:110
PWP_BOOL PwuUnfRecWriteINT32(PWU_UNFDATA *pUData, PWP_INT32 val)
Write a PWP_INT32 value to the current record with endian order applied.
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
virtual bool write(PWP_INT16 val, const char *suffix=0, const char *prefix=0)
Writes a integer value with proper encoding and byte order.
Definition: PwpFile.cxx:59
PWP_BOOL PwuUnfRecWriteUINT32(PWU_UNFDATA *pUData, PWP_UINT32 val)
Write a PWP_UINT32 value to the current record with endian order applied.
short PWP_INT16
16-bit integer
Definition: apiPWP.h:187
bool isDouble() const
Determines if floating point precision is PWP_PRECISION_DOUBLE.
Definition: PwpFile.cxx:680
PWP_BOOL PwuUnfRecEnd(PWU_UNFDATA *pUData)
Finalize the current record write.
#define PWP_INT64_FORMAT
PWP_INT64 printf format flags.
Definition: apiPWP.h:246
bool readAlphaToken(std::string &tok)
Reads a whitespace delimited token (word).
Definition: PwpFile.cxx:977
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:200
bool isBinary() const
Determines if file was opened with pwpBinary mode flag.
Definition: PwpFile.cxx:821
virtual bool write(PWP_UINT64 val, const char *suffix=0, const char *prefix=0)
Writes a integer value with proper encoding and byte order.
Definition: PwpFile.cxx:232
virtual bool write(PWP_UINT16 val, const char *suffix=0, const char *prefix=0)
Writes a integer value with proper encoding and byte order.
Definition: PwpFile.cxx:437
virtual ~PwpFile()
Destructor.
Definition: PwpFile.cxx:552
int pwpFileFlush(FILE *fp)
Flush a file to disk.
virtual bool write(const char *val, PWP_INT size=-1, char pad=0)
Writes a string value.
Definition: PwpFile.cxx:286
PWU_UNFDATA & unfData()
Get the file&#39;s unformatted data buffer.
Definition: PwpFile.h:779
signed char PWP_INT8
8-bit integer
Definition: apiPWP.h:167
virtual bool write(PWP_INT32 val, const char *suffix=0, const char *prefix=0)
Writes a integer value with proper encoding and byte order.
Definition: PwpFile.cxx:397
virtual bool write(PWP_UINT8 val, const char *suffix=0, const char *prefix=0)
Writes a integer value with proper encoding and byte order.
Definition: PwpFile.cxx:99
long long PWP_INT64
64-bit integer
Definition: apiPWP.h:240
int pwpFileGetpos(FILE *fp, sysFILEPOS *pos)
Query the current file position.
bool isSingle() const
Determines if floating point precision is PWP_PRECISION_SINGLE.
Definition: PwpFile.cxx:673
virtual bool write(PWP_INT16 val, const char *suffix=0, const char *prefix=0)
Writes a integer value with proper encoding and byte order.
Definition: PwpFile.cxx:216
PWP_ENUM_PRECISION setPrecision(PWP_ENUM_PRECISION precision)
Set the floating point precision used for writes.
Definition: PwpFile.cxx:643
virtual bool write(PWP_INT8 val, const char *suffix=0, const char *prefix=0)
Writes a integer value with proper encoding and byte order.
Definition: PwpFile.cxx:413
PWP_BOOL PwuUnfRecWriteINT16(PWU_UNFDATA *pUData, PWP_INT16 val)
Write a PWP_INT16 value to the current record with endian order applied.
virtual bool write(PWP_UINT32 val, const char *suffix=0, const char *prefix=0)
Writes a integer value with proper encoding and byte order.
Definition: PwpFile.cxx:429
Format &quot;%.*e&quot;.
virtual bool write(const char *val, PWP_INT size=-1, char=0)
Writes a string value.
Definition: PwpFile.cxx:129
virtual bool write(PWP_INT64 val, const char *suffix=0, const char *prefix=0)=0
Writes a integer value with proper encoding and byte order.
virtual ~PwpBinaryWriter()
Definition: PwpFile.cxx:195
int fmtFieldWdDouble_
Double precision format field width.
Format &quot;%*.*e&quot;.
virtual void setFmtType(FormatType type)
Definition: PwpFile.cxx:1518
int fmtFieldWdSingle_
Single precision format field width.
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
virtual bool beginRecord()
NOP default implementation.
Definition: PwpFile.cxx:370
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
virtual bool write(PWP_FLOAT val, const char *suffix=0, const char *prefix=0)
Writes a floating point value with proper precision, encoding and byte order.
Definition: PwpFile.cxx:107
PwpFileWriter * writer_
The file writer implementation.
Definition: PwpFile.h:863
void pwpFileRewind(FILE *fp)
Reset position to the beginning of the file.
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
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:1409
virtual FormatType getFmtType() const
Definition: PwpFile.cxx:1525
virtual bool write(PWP_FLOAT val, const char *suffix=0, const char *prefix=0)
Writes a floating point value with proper precision, encoding and byte order.
Definition: PwpFile.cxx:264
FormatType fmtType_
Formatting flags.
bool isWritable() const
Determines if file supports write operations.
Definition: PwpFile.cxx:666
int pwpFileDelete(const char *filename)
Delete a file.
virtual bool beginRecord(PWP_UINT32 bytes, PWP_UINT32 count=1)
NOP default implementation.
Definition: PwpFile.cxx:377
virtual void getFmtFieldDouble(int &width, int &prec) const
Definition: PwpFile.cxx:1510
virtual bool endRecord()
NOP default implementation.
int fmtPrecSingle_
Single precision format decimals.
PwpBinaryWriter(PwpFile &file)
Definition: PwpFile.cxx:189
PWP_UINT32 PWP_UINT
unsigned integer same size as void*
Definition: apiPWP.h:285
PWP_BOOL PwuUnfRecBegin(PWU_UNFDATA *pUData)
Prepares a PWU_UNFDATA block for writing a new unformatted data record.
PWP_BOOL PwuUnfRecWriteUINT64(PWU_UNFDATA *pUData, PWP_UINT64 val)
Write a PWP_UINT64 value to the current record with endian order applied.
virtual void setFmtFieldSingle(const int width, const int prec)
Definition: PwpFile.cxx:1303
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:389
virtual bool write(PWP_INT8 val, const char *suffix=0, const char *prefix=0)
Writes a integer value with proper encoding and byte order.
Definition: PwpFile.cxx:67
bool flush()
Flush file to disk.
Definition: PwpFile.cxx:842
PWP_BOOL PwuUnfRecWriteFLOAT(PWU_UNFDATA *pUData, PWP_FLOAT val)
Write a PWP_FLOAT value to the current record with endian order applied.
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
virtual void setFmtFieldSingle(const int width, const int prec)
default implementation.
virtual bool write(PWP_UINT16 val, const char *suffix=0, const char *prefix=0)
Writes a integer value with proper encoding and byte order.
Definition: PwpFile.cxx:248
virtual bool write(PWP_INT32 val, const char *suffix=0, const char *prefix=0)
Writes a integer value with proper encoding and byte order.
Definition: PwpFile.cxx:208
int pwpFilenameGetSize(const char *filename, size_t *size)
Get the file&#39;s size in bytes.
void setName(std::string filename)
Set the file&#39;s associated filename.
Definition: PwpFile.cxx:915
virtual bool write(PWP_INT8 val, const char *suffix=0, const char *prefix=0)
Writes a integer value with proper encoding and byte order.
Definition: PwpFile.cxx:224
Format &quot;%e&quot;.
bool ungetc(const int c)
Return a byte value to the file.
Definition: PwpFile.h:654
int mode_
The mode.
Definition: PwpFile.h:858
PWP_INT32 PWP_INT
integer same size as void*
Definition: apiPWP.h:282
PwpFileRecord(PwpFile &file, bool doBeginRecord=true)
Constructor.
Definition: PwpFile.cxx:1366
bool close()
Explicitly close the file object.
Definition: PwpFile.cxx:613
virtual bool write(PWP_FLOAT val, const char *suffix=0, const char *prefix=0)
Writes a floating point value with proper precision, encoding and byte order.
Definition: PwpFile.cxx:453
#define PWP_UINT32_MAX
maximum valid PWP_UINT32 value
Definition: apiPWP.h:213
virtual bool write(PWP_UINT32 val, const char *suffix=0, const char *prefix=0)
Writes a integer value with proper encoding and byte order.
Definition: PwpFile.cxx:83
static bool parseUnformattedRecord(PwpFile &theFile, PWP_INT64 &totalRecSize, bool &swap)
Definition: PwpFile.cxx:736
virtual ~PwpUnformattedWriter()
Definition: PwpFile.cxx:364
Format &quot;%f&quot;.
PWP_ENDIANNESS PwuGetOsEndianness(void)
Query the OS&#39;s native endianness.
PWP_BOOL PwuUnfRecWriteUINT8(PWU_UNFDATA *pUData, PWP_UINT8 val)
Write a PWP_UINT8 value to the current record with endian order applied.
virtual bool write(PWP_REAL val, const char *suffix=0, const char *prefix=0)
Writes a floating point value with proper precision, encoding and byte order.
Definition: PwpFile.cxx:464
PwpUnformattedWriter(PwpFile &file)
Definition: PwpFile.cxx:357
bool getSize(PWP_UINT64 &size) const
Get the size of the file managed by this object.
Definition: PwpFile.cxx:895
virtual bool endRecord()
Ends an unformatted record.
Definition: PwpFile.cxx:1403
virtual bool write(PWP_UINT8 val, const char *suffix=0, const char *prefix=0)
Writes a integer value with proper encoding and byte order.
Definition: PwpFile.cxx:445
PWP_BOOL PwuUnfRecWriteINT8(PWU_UNFDATA *pUData, PWP_INT8 val)
Write a PWP_INT8 value to the current record with endian order applied.
double PWP_REAL
64-bit real
Definition: apiPWP.h:264
virtual void getFmtFieldSingle(int &width, int &prec) const
Definition: PwpFile.cxx:1312
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
PWP_BOOL PwuUnfRecWriteINT64(PWU_UNFDATA *pUData, PWP_INT64 val)
Write a PWP_INT64 value to the current record with endian order applied.
int pwpFileGetSize(FILE *fp, size_t *size)
Get the file&#39;s size in bytes.
int PWP_INT32
32-bit integer
Definition: apiPWP.h:207
Format &quot;%*.*f&quot;.