44 const char *prefix = 0)
47 (prefix ? prefix :
""), val, (suffix ? suffix :
""));
52 const char *prefix = 0)
54 return 0 <= fprintf(
file_.
fp(),
"%s%ld%s", (prefix ? prefix :
""),
55 (
long)val, (suffix ? suffix :
""));
60 const char *prefix = 0)
62 return 0 <= fprintf(
file_.
fp(),
"%s%ld%s", (prefix ? prefix :
""),
63 (
long)val, (suffix ? suffix :
""));
68 const char *prefix = 0)
70 return 0 <= fprintf(
file_.
fp(),
"%s%ld%s", (prefix ? prefix :
""),
71 (
long)val, (suffix ? suffix :
""));
76 const char *prefix = 0)
79 (prefix ? prefix :
""), val, (suffix ? suffix :
""));
84 const char *prefix = 0)
86 return 0 <= fprintf(
file_.
fp(),
"%s%lu%s", (prefix ? prefix :
""),
87 (
unsigned long)val, (suffix ? suffix :
""));
92 const char *prefix = 0)
94 return 0 <= fprintf(
file_.
fp(),
"%s%lu%s", (prefix ? prefix :
""),
95 (
unsigned long)val, (suffix ? suffix :
""));
100 const char *prefix = 0)
102 return 0 <= fprintf(
file_.
fp(),
"%s%lu%s", (prefix ? prefix :
""),
103 (
unsigned long)val, (suffix ? suffix :
""));
108 const char *prefix = 0)
112 (suffix ? suffix :
""), (prefix ? prefix :
"")) :
114 (suffix ? suffix :
""), (prefix ? prefix :
""));
119 const char *prefix = 0)
123 (suffix ? suffix :
""), (prefix ? prefix :
"")) :
125 (suffix ? suffix :
""), (prefix ? prefix :
""));
134 return 0 <= fprintf(
file_.
fp(),
"%*.*s", (int)size, (
int)size, val);
145 return 0 <= fprintf(
file_.
fp(),
"%s%g%s", pfx, val, sfx);
147 return 0 <= fprintf(
file_.
fp(),
"%s%*.*g%s", pfx, wd, prec, val, sfx);
149 return 0 <= fprintf(
file_.
fp(),
"%s%*g%s", pfx, wd, val, sfx);
151 return 0 <= fprintf(
file_.
fp(),
"%s%.*g%s", pfx, prec, val, sfx);
154 return 0 <= fprintf(
file_.
fp(),
"%s%f%s", pfx, val, sfx);
156 return 0 <= fprintf(
file_.
fp(),
"%s%*.*f%s", pfx, wd, prec, val, sfx);
158 return 0 <= fprintf(
file_.
fp(),
"%s%*f%s", pfx, wd, val, sfx);
160 return 0 <= fprintf(
file_.
fp(),
"%s%.*f%s", pfx, prec, val, sfx);
163 return 0 <= fprintf(
file_.
fp(),
"%s%e%s", pfx, val, sfx);
165 return 0 <= fprintf(
file_.
fp(),
"%s%*.*e%s", pfx, wd, prec, val, sfx);
167 return 0 <= fprintf(
file_.
fp(),
"%s%*e%s", pfx, wd, val, sfx);
169 return 0 <= fprintf(
file_.
fp(),
"%s%.*e%s", pfx, prec, val, sfx);
182 template<
typename T >
201 const char *prefix = 0)
203 (void)prefix, (
void)suffix;
209 const char *prefix = 0)
211 (void)prefix, (
void)suffix;
217 const char *prefix = 0)
219 (void)prefix, (
void)suffix;
225 const char *prefix = 0)
227 (void)prefix, (
void)suffix;
233 const char *prefix = 0)
235 (void)prefix, (
void)suffix;
241 const char *prefix = 0)
243 (void)prefix, (
void)suffix;
249 const char *prefix = 0)
251 (void)prefix, (
void)suffix;
257 const char *prefix = 0)
259 (void)prefix, (
void)suffix;
265 const char *prefix = 0)
267 (void)prefix, (
void)suffix;
276 const char *prefix = 0)
278 (void)prefix, (
void)suffix;
301 const size_t bufSz = 512;
302 char padBuf[bufSz] = {
'\0' };
304 memset(padBuf, pad, bufSz);
307 while (ret && (bufSz < size)) {
328 else if (1 == size) {
334 const char *cbuf = (
const char *)buf;
336 size_t tNdx = size - 1;
337 while (hNdx < tNdx) {
338 tmpBuf[hNdx] = cbuf[tNdx];
339 tmpBuf[tNdx--] = cbuf[hNdx++];
390 const char *prefix = 0)
392 (void)prefix, (
void)suffix;
398 const char *prefix = 0)
400 (void)prefix, (
void)suffix;
406 const char *prefix = 0)
408 (void)prefix, (
void)suffix;
414 const char *prefix = 0)
416 (void)prefix, (
void)suffix;
422 const char *prefix = 0)
424 (void)prefix, (
void)suffix;
430 const char *prefix = 0)
432 (void)prefix, (
void)suffix;
438 const char *prefix = 0)
440 (void)prefix, (
void)suffix;
446 const char *prefix = 0)
448 (void)prefix, (
void)suffix;
454 const char *prefix = 0)
456 (void)prefix, (
void)suffix;
465 const char *prefix = 0)
467 (void)prefix, (
void)suffix;
490 const size_t bufSz = 512;
491 char padBuf[bufSz] = {
'\0' };
493 memset(padBuf, pad, bufSz);
496 while (ret && (bufSz < size)) {
524 wrap(fp, filename, mode);
537 open(filename, mode);
544 byteOrder_(ref.byteOrder_),
545 precision_(ref.precision_),
608 return 0 != feof(
fp_);
701 const size_t NumChunks = 4;
702 const size_t ChunkSize = 256;
703 const size_t FullBufSize = ChunkSize * NumChunks;
706 if (FullBufSize >= fileSize) {
714 for (
PWP_UINT chunk = 1; chunk < NumChunks; ++chunk) {
715 if (!local.
setIntPos((chunk * fileSize) / NumChunks)) {
725 for (
PWP_UINT ii = 0; ii < n; ++ii) {
726 if ((0x80 <= buf[ii]) || (0x08 >= buf[ii])) {
730 ret = ((nNonAscii / (
PWP_REAL)n) < 0.1);
748 const PWP_INT64 MIN_RECORD_SIZE = 2 * HDRFTR_VALUE_SIZE;
753 if (bytesRemaining >= MIN_RECORD_SIZE) {
759 bytesRemaining -= HDRFTR_VALUE_SIZE + blockSize;
760 if ((HDRFTR_VALUE_SIZE <= bytesRemaining) &&
763 totalRecSize = HDRFTR_VALUE_SIZE + blockSize +
796 if (0 < totalRecSize) {
798 pRecSizes->push_back(totalRecSize);
802 PWP_INT64 bytesRemaining = fileSize - totalRecSize;
805 pRecSizes->push_back(totalRecSize);
807 bytesRemaining -= totalRecSize;
809 result = (0 == bytesRemaining);
873 fpos_t pos = position;
876 return (::fseek(
fp_,position,SEEK_SET) == 0);
943 if (!std::isspace(c)) {
959 const int c = ::fgetc(
fp_);
963 if (!std::isspace(c)) {
964 tok +=
static_cast<char>(c);
983 const int c = ::fgetc(
fp_);
987 if (std::isalpha(c)) {
988 tok +=
static_cast<char>(c);
1010 while (len <= maxLen) {
1011 const int c = ::fgetc(
fp_);
1022 str +=
static_cast<char>(c);
1034 if (1 != fscanf(*
this,
"%lf", v++)) {
1047 if (1 != fscanf(*
this,
"%f", v++)) {
1060 if (1 != fscanf(*
this,
"%u", (
unsigned int*)v++)) {
1073 if (1 != fscanf(*
this,
"%c", v++)) {
1084 if (0 == maxDigits) {
1090 while (0 != maxDigits) {
1091 const int c = ::fgetc(
fp_);
1093 return !tok.empty();
1096 if (std::isdigit(c)) {
1097 tok +=
static_cast<char>(c);
1102 else if (16 == base) {
1103 if (std::isxdigit(c)) {
1104 tok +=
static_cast<char>(c);
1109 else if (8 == base) {
1110 if ((c >=
'0') && (c <=
'7')) {
1111 tok +=
static_cast<char>(c);
1116 else if (!std::isspace(c)) {
1118 tok +=
static_cast<char>(c);
1127 return tok.empty() ?
false :
1128 ((val = std::stoul(tok.c_str(), 0, int(base))),
true);
1146 if (0 == maxDigits) {
1153 const int c = ::fgetc(
fp_);
1157 if (std::isdigit(c)) {
1158 tok +=
static_cast<char>(c);
1166 return tok.empty() ?
false :
1167 ((val = std::stoul(tok.c_str(), 0, 10)),
true);
1188 switch (byteOrder) {
1369 if (doBeginRecord) {
static void swap(T &val)
swap() - (Importer-only function) Wrapper for function swapByteOrder().
virtual void getFmtFieldSingle(int &width, int &prec) const
PWP_INT64 getPos() const
Get the current file position.
virtual bool write(PWP_UINT8 val, const char *suffix=0, const char *prefix=0)
Writes a integer value with proper encoding and byte order.
static std::string & trim(std::string &s)
trim leading and trailing whitespace from s
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.
FormatType
Formatted output types for floating point values.
bool writeOrdered(const void *buf, size_t size)
bool isUnformatted() const
Determines if file was opened with pwpUnformatted mode flag.
bool wrap(FILE *fp, std::string filename=std::string(), int mode=0)
Take ownership of fp opened using pwpFileOpen(filename, mode).
PwpAsciiWriter(PwpFile &file)
bool readOptionalInt(PWP_UINT32 &v, const PWP_UINT32 defV, const PWP_UINT32 base=10, PWP_UINT32 maxDigits=0)
Reads a single PWP_UINT32 value.
virtual bool write(PWP_INT32 val, const char *suffix=0, const char *prefix=0)
Writes a integer value with proper encoding and byte order.
virtual void setFmtFieldDouble(const int width, const int prec)
virtual FormatType getFmtType() const
virtual ~PwpFileRecord()
Destructor.
The abstract PwpFileWriter class.
PwpFile & file_
The file being written.
bool setIntPos(const PWP_INT64 &pos)
Set the current file position.
bool needByteSwap() const
Determine if byte swapping is needed for this file.
FILE * fp() const
Get the FILE pointer.
virtual bool beginRecord()
Starts an unformatted record.
bool readSwap(T &val, const bool swapbyte)
Reads a single value of type T.
bool isAscii() const
Determines if file was opened with either the pwpAscii or pwpFormatted mode flag. ...
bool setPos(const sysFILEPOS &pos)
Set the current file position.
virtual void setFmtFieldDouble(const int width, const int prec)
default implementation.
PWP_ENDIANNESS setByteOrder(PWP_ENDIANNESS order)
Set the byte order used for writes.
virtual bool beginRecord()
Starts an unformatted record.
PWP_ENDIANNESS getByteOrder(bool mapBigLittle=false) const
Get the byte order used for writes.
virtual bool write(PWP_UINT16 val, const char *suffix=0, const char *prefix=0)
Writes a integer value with proper encoding and byte order.
virtual void setFmtType(FormatType type)
bool fileEof() const
Check for the end of file.
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.
int fmtPrecDouble_
Double precision format decimals.
PWP_ENUM_PRECISION
File precision values.
static PWP_ENDIANNESS getOsByteOrder()
Get the OS's native byte ordering.
FILE * fp_
The FILE pointer.
bool rewind()
Reset position to the beginning of the file.
virtual bool write(PWP_UINT64 val, const char *suffix=0, const char *prefix=0)
Writes a integer value with proper encoding and byte order.
virtual bool write(PWP_INT64 val, const char *suffix=0, const char *prefix=0)
Writes a integer value with proper encoding and byte order.
bool isOpen() const
Determines a file's status.
bool writeWdPrec(PWP_REAL val, int wd, int prec, const char *sfx, const char *pfx)
bool isEof() const
Get the end-of-file status.
static bool unlink(const char *filename)
Delete a file.
virtual bool beginRecord()
NOP default implementation.
bool readToken(std::string &tok)
Reads a whitespace delimited token (word).
bool read(void *buf, size_t size, size_t count)
Read a collection of data items.
Format "%*.*g".
bool getPos(sysFILEPOS &pos) const
Get the current file position.
virtual void getFmtFieldSingle(int &width, int &prec) const
default implementation.
virtual bool write(PWP_UINT32 val, const char *suffix=0, const char *prefix=0)
Writes a integer value with proper encoding and byte order.
PWP_ENUM_PRECISION precision_
The file's floating point precision.
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.
PwpFile(FILE *fp=0, std::string filename=std::string(), int mode=0)
Default constructor.
virtual void getFmtFieldDouble(int &width, int &prec) const
virtual bool endRecord()
Ends an unformatted record.
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.
bool checkAscii() const
Determines whether the file is Ascii by reading the contents of the file.
virtual void setFmtFieldDouble(const int width, const int prec)
virtual void getFmtFieldDouble(int &width, int &prec) const
default implementation.
virtual ~PwpAsciiWriter()
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.
virtual void setFmtFieldSingle(const int width, const int prec)
virtual bool write(PWP_INT64 val, const char *suffix=0, const char *prefix=0)
Writes a integer value with proper encoding and byte order.
bool readInt(PWP_UINT32 &v, const PWP_UINT32 base=10, PWP_UINT32 maxDigits=0)
Reads a single PWP_UINT32 value.
bool isReadable() const
Determines if file supports read operations.
const char * getName() const
Get the file's associated filename.
virtual bool write(PWP_INT16 val, const char *suffix=0, const char *prefix=0)
Writes a integer value with proper encoding and byte order.
bool isDouble() const
Determines if floating point precision is PWP_PRECISION_DOUBLE.
bool readAlphaToken(std::string &tok)
Reads a whitespace delimited token (word).
virtual bool write(PWP_INT64 val, const char *suffix=0, const char *prefix=0)
Writes a integer value with proper encoding and byte order.
bool isBinary() const
Determines if file was opened with pwpBinary mode flag.
virtual bool write(PWP_UINT64 val, const char *suffix=0, const char *prefix=0)
Writes a integer value with proper encoding and byte order.
virtual ~PwpFile()
Destructor.
virtual bool write(const char *val, PWP_INT size=-1, char pad=0)
Writes a string value.
PWU_UNFDATA & unfData()
Get the file's unformatted data buffer.
virtual bool write(PWP_UINT8 val, const char *suffix=0, const char *prefix=0)
Writes a integer value with proper encoding and byte order.
bool isSingle() const
Determines if floating point precision is PWP_PRECISION_SINGLE.
virtual bool write(PWP_INT16 val, const char *suffix=0, const char *prefix=0)
Writes a integer value with proper encoding and byte order.
PWP_ENUM_PRECISION setPrecision(PWP_ENUM_PRECISION precision)
Set the floating point precision used for writes.
virtual bool write(const char *val, PWP_INT size=-1, char=0)
Writes a string value.
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()
int fmtFieldWdDouble_
Double precision format field width.
Format "%*.*e".
virtual void setFmtType(FormatType type)
int fmtFieldWdSingle_
Single precision format field width.
bool wspaceSkip()
Reads and discards consecutive whitespace bytes.
bool checkUnformatted(bool &swapped, std::vector< PWP_INT64 > *pRecSizes=0) const
Determines unformatted type settings by reading the contents of the file.
bool getcNotEOF(int &c)
Reads a single byte value.
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.
PwpFileWriter * writer_
The file writer implementation.
PWP_ENUM_PRECISION getPrecision() const
Get the floating point precision used for writes.
std::string filename_
The file name.
virtual bool write(PWP_INT64 val, const char *suffix=0, const char *prefix=0)
Writes a integer value with proper encoding and byte order.
virtual FormatType getFmtType() const
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.
FormatType fmtType_
Formatting flags.
bool isWritable() const
Determines if file supports write operations.
virtual void getFmtFieldDouble(int &width, int &prec) const
virtual bool endRecord()
NOP default implementation.
int fmtPrecSingle_
Single precision format decimals.
PwpBinaryWriter(PwpFile &file)
virtual void setFmtFieldSingle(const int width, const int prec)
virtual bool write(PWP_INT8 val, const char *suffix=0, const char *prefix=0)
Writes a integer value with proper encoding and byte order.
bool flush()
Flush file to disk.
PWP_ENDIANNESS byteOrder_
The file's byte order.
PwpFile & operator=(const PwpFile &ref)
Assignment operator Does not copy the file position or unformatted data.
PWP_ENDIANNESS
Flags used to indicate endianness or control endian behaviors in functions.
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.
virtual bool write(PWP_INT32 val, const char *suffix=0, const char *prefix=0)
Writes a integer value with proper encoding and byte order.
void setName(std::string filename)
Set the file's associated filename.
virtual bool write(PWP_INT8 val, const char *suffix=0, const char *prefix=0)
Writes a integer value with proper encoding and byte order.
bool ungetc(const int c)
Return a byte value to the file.
PwpFileRecord(PwpFile &file, bool doBeginRecord=true)
Constructor.
bool close()
Explicitly close the file object.
virtual bool write(PWP_UINT32 val, const char *suffix=0, const char *prefix=0)
Writes a integer value with proper encoding and byte order.
static bool parseUnformattedRecord(PwpFile &theFile, PWP_INT64 &totalRecSize, bool &swap)
PWP_ENDIANNESS PwuGetOsEndianness(void)
Query the OS's native endianness.
bool getSize(PWP_UINT64 &size) const
Get the size of the file managed by this object.
virtual bool endRecord()
Ends an unformatted record.
virtual void getFmtFieldSingle(int &width, int &prec) const
bool open(std::string filename, int mode)
Opens a file with the given filename and mode.
static PWP_ENDIANNESS mapToBigLittle(PWP_ENDIANNESS byteOrder)
Map a byte ordering value to PWP_ENDIAN_BIG or PWP_ENDIAN_LITTLE based on getOsByteOrder().
Format "%*.*f".