Pointwise Plugin SDK
PwpAnchoredFileValue.h
Go to the documentation of this file.
1 /****************************************************************************
2  *
3  * template class PwpAnchoredFileValue
4  * template struct PwpAnchoredFileValueWriter
5  *
6  * Proprietary software product of Pointwise, Inc.
7  * Copyright (c) 1995-2018 Pointwise, Inc.
8  * All rights reserved.
9  *
10  ***************************************************************************/
11 
12 #ifndef _PWPANCHOREDFILEVALUE_H_
13 #define _PWPANCHOREDFILEVALUE_H_
14 
15 #include "PwpFile.h"
16 #include "pwpPlatform.h"
17 
18 
27 template< typename ValType >
30 
40  static bool writeBin(bool placeholder, PwpFile &f, const ValType &v,
41  PWP_INT fldWidth, const char * suffix, const char * prefix)
42  {
43  (void)placeholder;
44  (void)fldWidth;
45  return f.write(v, suffix, prefix);
46  }
47 
48 
50 
60  static bool writeAsc(bool placeholder, PwpFile &f, const ValType &v,
61  PWP_INT fldWidth, const char * suffix, const char * prefix)
62  {
63  (void)placeholder;
64  return 0 != fprintf(f, "%s%*u%s", (prefix ? prefix : ""), (int)fldWidth,
65  (unsigned int)v, (suffix ? suffix : ""));
66  }
67 
68 
70 
80  static bool writeUnf(bool placeholder, PwpFile &f, const ValType &v,
81  PWP_INT fldWidth, const char * suffix, const char * prefix)
82  {
83  (void)fldWidth;
84  bool ret = false;
85  if (placeholder) {
86  // Writing the placeholder data value. Write normally so that the
87  // unf record byte count will properly increment.
88  ret = f.write(v, suffix, prefix);
89  }
90  else {
91  // Update anchored value. Bypass unf data handling and write value
92  // directly.
93  const void *p = PwuApplyEndianness(f.getByteOrder(), &v, sizeof(v));
94  ret = (1 == pwpFileWrite(p, sizeof(v), 1, f));
95  }
96  return ret;
97  }
98 };
99 
100 
169 template< typename ValType,
170  typename ValWriter = PwpAnchoredFileValueWriter<ValType> >
172 public:
173 
175 
184  anchored_(false),
185  v_(),
186  suffix_(0),
187  prefix_(0),
188  f_(f),
189  fldWidth_(-1) // -1 is left-aligned, packed
190  {
191  }
192 
193 
195 
208  PwpAnchoredFileValue(PwpFile &f, const ValType &initV,
209  const char * suffix = 0, const char * prefix = 0,
210  PWP_INT fldWidth = -1) :
211  anchored_(false),
212  v_(initV),
213  suffix_(suffix),
214  prefix_(prefix),
215  f_(f),
216  fldWidth_(fldWidth) // -1 is left-aligned, packed
217  {
218  }
219 
220 
222 
235  PwpAnchoredFileValue(PwpFile &f, const ValType &initV, PWP_INT fldWidth,
236  const char * suffix = 0, const char * prefix = 0) :
237  anchored_(false),
238  v_(initV),
239  suffix_(suffix),
240  prefix_(prefix),
241  f_(f),
243  {
244  }
245 
246 
248  {
249  }
250 
251 
254 
260  bool anchor(const char * suffix = 0, const char * prefix = 0,
261  PWP_INT fldWidth = 0)
262  {
263  if (0 != suffix) {
264  suffix_ = suffix;
265  }
266  if (0 != prefix) {
267  prefix_ = prefix;
268  }
269  if (0 != fldWidth) {
271  }
272  anchored_ = f_.getPos(pos_) && write();
273  return anchored_;
274  }
275 
276 
279 
285  bool anchor(PWP_INT fldWidth, const char * suffix = 0,
286  const char * prefix = 0)
287  {
288  return anchor(suffix, prefix, fldWidth);
289  }
290 
291 
294 
297  bool commit()
298  {
299  sysFILEPOS posSave;
300  return anchored_ && f_.getPos(posSave) && f_.setPos(pos_) &&
301  write() && f_.setPos(posSave);
302  }
303 
304 
307 
310  bool rollback()
311  {
312  bool ret = anchored_ && f_.setPos(pos_);
313  anchored_ = false;
314  return ret;
315  }
316 
317 
319 
322  ValType& val()
323  {
324  return v_;
325  }
326 
327 
329 
332  const ValType& val() const
333  {
334  return v_;
335  }
336 
337 
339 
342  operator const ValType&() const
343  {
344  return v_;
345  }
346 
347 
349 
352  operator ValType&()
353  {
354  return v_;
355  }
356 
357 
359 
362  const ValType& operator=(const ValType& rhs) const
363  {
364  v_ = rhs;
365  return v_;
366  }
367 
368 
370 
373  ValType& operator=(const ValType& rhs)
374  {
375  v_ = rhs;
376  return v_;
377  }
378 
379 
381 
384  const char* prefix() const
385  {
386  return prefix_ ? prefix_ : "";
387  }
388 
389 
391 
394  const char* suffix() const
395  {
396  return suffix_ ? suffix_ : "";
397  }
398 
399 
401 
404  PwpFile& f() const
405  {
406  return f_;
407  }
408 
409 
411 
414  const sysFILEPOS& pos() const
415  {
416  return pos_;
417  }
418 
419 
421 
425  {
426  return fldWidth_;
427  }
428 
429 
431 
434  bool isAnchored() const
435  {
436  return anchored_;
437  }
438 
439 
440 private:
441 
442  bool write()
443  {
444  bool ret = false;
445  if (f_.isAscii()) {
446  ret = ValWriter::writeAsc(!anchored_, f_, v_, fldWidth_, suffix_,
447  prefix_);
448  }
449  else if (f_.isBinary()) {
450  ret = ValWriter::writeBin(!anchored_, f_, v_, fldWidth_, suffix_,
451  prefix_);
452  }
453  else if (f_.isUnformatted()) {
454  ret = ValWriter::writeUnf(!anchored_, f_, v_, fldWidth_, suffix_,
455  prefix_);
456  }
457  return ret;
458  }
459 
462  {
463  return *this;
464  }
465 
466 
467 private:
468 
469  bool anchored_;
470  ValType v_;
472  const char * suffix_;
473  const char * prefix_;
476 };
477 
478 #endif // _PWPANCHOREDFILEVALUE_H_
PwpAnchoredFileValue(PwpFile &f)
Constructor.
static bool writeAsc(bool placeholder, PwpFile &f, const ValType &v, PWP_INT fldWidth, const char *suffix, const char *prefix)
Handles the writing an anchored value to an ASCII file.
PWP_ENDIANNESS getByteOrder(bool mapBigLittle=false) const
Get the byte order used for writes.
Definition: PwpFile.cxx:659
bool commit()
Writes the value to the associated file&#39;s anchored file position.
const char * prefix() const
Gets the current value prefix.
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
Handles the delayed updating of a value in a PwpFile object.
static bool writeUnf(bool placeholder, PwpFile &f, const ValType &v, PWP_INT fldWidth, const char *suffix, const char *prefix)
Handles the writing an anchored value to an fortran unformatted file.
bool anchor(PWP_INT fldWidth, const char *suffix=0, const char *prefix=0)
Anchors the value to the associated file&#39;s current file position and writes a placeholder value to th...
bool anchor(const char *suffix=0, const char *prefix=0, PWP_INT fldWidth=0)
Anchors the value to the associated file&#39;s current file position and writes a placeholder value to th...
const sysFILEPOS & pos() const
Gets the anchor position associated with the file.
PwpAnchoredFileValue(PwpFile &f, const ValType &initV, const char *suffix=0, const char *prefix=0, PWP_INT fldWidth=-1)
Constructor.
fpos_t sysFILEPOS
File position data type.
Definition: pwpPlatform.h:53
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
const char * suffix() const
Gets the current value suffix.
Cross Platform Functions.
PwpFile & f() const
Gets the file associated with the anchored value.
const void * PwuApplyEndianness(PWP_ENDIANNESS endianness, const void *buf, size_t size)
Apply specified byte order to buf containing size bytes.
Writes solver files.
Definition: PwpFile.h:111
The default ValWriter type used by PwpAnchoredFileValue<ValWriter>.
bool isAscii() const
Determines if file was opened with either the pwpAscii or pwpFormatted mode flag. ...
Definition: PwpFile.cxx:710
bool isAnchored() const
Gets the current value anchoring status.
PwpAnchoredFileValue< ValType, ValWriter > & operator=(const PwpAnchoredFileValue< ValType, ValWriter > &)
bool getPos(sysFILEPOS &pos) const
Get the current file position.
Definition: PwpFile.cxx:745
static bool writeBin(bool placeholder, PwpFile &f, const ValType &v, PWP_INT fldWidth, const char *suffix, const char *prefix)
Handles the writing an anchored value to a binary file.
long PWP_INT
integer same size as void*
Definition: apiPWP.h:155
size_t pwpFileWrite(const void *buf, size_t size, size_t count, FILE *fp)
Write an collection of data items to a file.
bool isBinary() const
Determines if file was opened with pwpBinary mode flag.
Definition: PwpFile.cxx:717
PwpAnchoredFileValue(PwpFile &f, const ValType &initV, PWP_INT fldWidth, const char *suffix=0, const char *prefix=0)
Constructor.
ValType & val()
Gets the current data value.
bool rollback()
Moves the associated file to the anchored file position and unanchors the value.
const ValType & val() const
Gets the current data value.
ValType & operator=(const ValType &rhs)
ValType assignment operator.
PWP_INT fldWidth() const
Gets the current value field width.
const ValType & operator=(const ValType &rhs) const
const ValType assignment operator.