Go to the documentation of this file.
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)) {
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);
1005 const bool FlushToStopC = (0 > maxLen);
1009 else if (FlushToStopC) {
1017 while (len < maxLen) {
1029 str +=
static_cast<char>(c);
1054 if (1 != fscanf(*
this,
"%lf", v++)) {
1067 if (1 != fscanf(*
this,
"%f", v++)) {
1080 if (1 != fscanf(*
this,
"%u", (
unsigned int*)v++)) {
1093 if (1 != fscanf(*
this,
"%c", v++)) {
1104 if (0 == maxDigits) {
1110 while (0 != maxDigits) {
1111 const int c = ::fgetc(
fp_);
1113 return !tok.empty();
1116 if (std::isdigit(c)) {
1117 tok +=
static_cast<char>(c);
1122 else if (16 == base) {
1123 if (std::isxdigit(c)) {
1124 tok +=
static_cast<char>(c);
1129 else if (8 == base) {
1130 if ((c >=
'0') && (c <=
'7')) {
1131 tok +=
static_cast<char>(c);
1136 else if (!std::isspace(c)) {
1138 tok +=
static_cast<char>(c);
1147 return tok.empty() ? false :
1148 ((val = std::stoul(tok.c_str(), 0,
int(base))),
true);
1166 if (0 == maxDigits) {
1173 const int c = ::fgetc(
fp_);
1177 if (std::isdigit(c)) {
1178 tok +=
static_cast<char>(c);
1186 return tok.empty() ? false :
1187 ((val = std::stoul(tok.c_str(), 0, 10)),
true);
1208 switch (byteOrder) {
1389 if (doBeginRecord) {
PwpAsciiWriter(PwpFile &file)
bool setIntPos(const PWP_INT64 &pos)
Set the current file position.
bool setPos(const sysFILEPOS &pos)
Set the current file position.
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.
virtual bool write(const char *val, PWP_INT size=-1, char pad=0)
Writes a string 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.
@ FormatWdPrecF
Format "%*.*f".
virtual bool endRecord()
NOP default implementation.
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.
bool readToken(std::string &tok)
Reads a whitespace delimited token (word).
virtual void getFmtFieldSingle(int &width, int &prec) const
bool isEof() const
Get the end-of-file status.
virtual bool endRecord()
Ends an unformatted record.
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 ~PwpFile()
Destructor.
virtual void getFmtFieldDouble(int &width, int &prec) const
default implementation.
bool isWritable() const
Determines if file supports write operations.
virtual ~PwpBinaryWriter()
virtual void setFmtFieldSingle(const int width, const int prec)
PWP_ENDIANNESS byteOrder_
The file's byte order.
bool isOpen() const
Determines a file's status.
bool readOptionalInt(PWP_UINT32 &v, const PWP_UINT32 defV, const PWP_UINT32 base=10, PWP_UINT32 maxDigits=0)
Reads a single PWP_UINT32 value.
bool fileEof() const
Check for the end of file.
static PWP_ENDIANNESS getOsByteOrder()
Get the OS's native byte ordering.
virtual bool beginRecord()
Starts an unformatted record.
virtual bool write(PWP_UINT32 val, const char *suffix=0, const char *prefix=0)
Writes a integer value with proper encoding and byte order.
PwpFileRecord(PwpFile &file, bool doBeginRecord=true)
Constructor.
static std::string & trim(std::string &s)
trim leading and trailing whitespace from s
virtual bool write(PWP_INT64 val, const char *suffix=0, const char *prefix=0)
Writes a integer value with proper encoding and byte order.
int fmtFieldWdSingle_
Single precision format field width.
const char * getName() const
Get the file's associated filename.
virtual void getFmtFieldDouble(int &width, int &prec) const
bool read(void *buf, size_t size, size_t count)
Read a collection of data items.
virtual bool beginRecord()
NOP default implementation.
@ FormatWdPrecG
Format "%*.*g".
PWP_ENUM_PRECISION getPrecision() const
Get the floating point precision used for writes.
bool needByteSwap() const
Determine if byte swapping is needed for this file.
virtual bool write(PWP_INT64 val, const char *suffix=0, const char *prefix=0)
Writes a integer value with proper encoding and byte order.
PWP_ENDIANNESS PwuGetOsEndianness(void)
Query the OS's native endianness.
virtual void getFmtFieldDouble(int &width, int &prec) const
bool isUnformatted() const
Determines if file was opened with pwpUnformatted mode flag.
bool getPos(sysFILEPOS &pos) const
Get the current file position.
bool readSwap(T &val, const bool swapbyte)
Reads a single value of type T.
bool isReadable() const
Determines if file supports read operations.
static void swap(T &val)
swap() - (Importer-only function) Wrapper for function swapByteOrder().
virtual void setFmtFieldSingle(const int width, const int prec)
bool ungetc(const int c)
Return a byte value to the file.
bool checkUnformatted(bool &swapped, std::vector< PWP_INT64 > *pRecSizes=0) const
Determines unformatted type settings by reading the contents of the file.
virtual void setFmtType(FormatType type)
FILE * fp() const
Get the FILE pointer.
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.
bool wrap(FILE *fp, std::string filename=std::string(), int mode=0)
Take ownership of fp opened using pwpFileOpen(filename, mode).
PwpFile & file_
The file being written.
static bool parseUnformattedRecord(PwpFile &theFile, PWP_INT64 &totalRecSize, bool &swap)
bool readAlphaToken(std::string &tok)
Reads a whitespace delimited token (word).
FILE * fp_
The FILE pointer.
virtual void setFmtFieldDouble(const int width, const int prec)
virtual void setFmtFieldDouble(const int width, const int prec)
default implementation.
virtual bool write(PWP_INT8 val, const char *suffix=0, const char *prefix=0)
Writes a integer value with proper encoding and byte order.
virtual ~PwpFileRecord()
Destructor.
virtual ~PwpAsciiWriter()
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.
virtual FormatType getFmtType() const
default implementation.
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.
void setName(std::string filename)
Set the file's associated filename.
virtual bool write(PWP_UINT32 val, const char *suffix=0, const char *prefix=0)
Writes a integer value with proper encoding and byte order.
bool wspaceSkip()
Reads and discards consecutive whitespace bytes.
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 fmtFieldWdDouble_
Double precision format field width.
PwpFileWriter * writer_
The file writer implementation.
PwpFile(FILE *fp=0, std::string filename=std::string(), int mode=0)
Default constructor.
bool writeOrdered(const void *buf, size_t size)
@ FormatPrecG
Format "%.*g".
@ FormatPrecE
Format "%.*e".
@ FormatWdPrecE
Format "%*.*e".
virtual bool write(const char *val, PWP_INT size=-1, char=0)
Writes a string value.
The abstract PwpFileWriter class.
bool isSingle() const
Determines if floating point precision is PWP_PRECISION_SINGLE.
virtual void getFmtFieldSingle(int &width, int &prec) const
default implementation.
int fmtPrecSingle_
Single precision format decimals.
bool flush()
Flush file to disk.
virtual bool write(PWP_INT64 val, const char *suffix=0, const char *prefix=0)
Writes a integer value with proper encoding and byte order.
PWP_ENDIANNESS
Flags used to indicate endianness or control endian behaviors in functions.
virtual bool beginRecord()
Starts an unformatted record.
PWP_ENDIANNESS setByteOrder(PWP_ENDIANNESS order)
Set the byte order used for writes.
PwpFile & operator=(const PwpFile &ref)
Assignment operator Does not copy the file position or unformatted data.
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 getSize(PWP_UINT64 &size) const
Get the size of the file managed by this object.
PWP_ENUM_PRECISION setPrecision(PWP_ENUM_PRECISION precision)
Set the floating point precision used for writes.
bool writeWdPrec(PWP_REAL val, int wd, int prec, const char *sfx, const char *pfx)
virtual void setFmtType(FormatType type)
default implementation.
virtual FormatType getFmtType() const
virtual bool write(PWP_UINT64 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.
static PWP_ENDIANNESS mapToBigLittle(PWP_ENDIANNESS byteOrder)
Map a byte ordering value to PWP_ENDIAN_BIG or PWP_ENDIAN_LITTLE based on getOsByteOrder().
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_UINT8 val, const char *suffix=0, const char *prefix=0)
Writes a integer value with proper encoding and byte order.
virtual bool write(PWP_UINT16 val, const char *suffix=0, const char *prefix=0)
Writes a integer value with proper encoding and byte order.
PWP_INT64 getPos() const
Get the current file position.
int fmtPrecDouble_
Double precision format decimals.
virtual bool write(PWP_INT8 val, const char *suffix=0, const char *prefix=0)
Writes a integer value with proper encoding and byte order.
virtual bool endRecord()
Ends an unformatted record.
FormatType fmtType_
Formatting flags.
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 isAscii() const
Determines if file was opened with either the pwpAscii or pwpFormatted mode flag.
PWU_UNFDATA & unfData()
Get the file's unformatted data buffer.
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 bool write(PWP_UINT64 val, const char *suffix=0, const char *prefix=0)
Writes a integer value with proper encoding and byte order.
bool close()
Explicitly close the file object.
bool isBinary() const
Determines if file was opened with pwpBinary mode flag.
PWP_ENDIANNESS getByteOrder(bool mapBigLittle=false) const
Get the byte order used for writes.
bool readInt(PWP_UINT32 &v, const PWP_UINT32 base=10, PWP_UINT32 maxDigits=0)
Reads a single PWP_UINT32 value.
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 void getFmtFieldSingle(int &width, int &prec) const
static bool unlink(const char *filename)
Delete a file.
std::string filename_
The file name.
PwpBinaryWriter(PwpFile &file)
PWP_ENUM_PRECISION precision_
The file's floating point precision.
FormatType
Formatted output types for floating point values.
bool open(std::string filename, int mode)
Opens a file with the given filename and mode.
bool readDouble(PWP_REAL &v, PWP_UINT32 maxDigits=0)
Reads a single PWP_REAL value.
virtual bool write(PWP_INT16 val, const char *suffix=0, const char *prefix=0)
Writes a integer value with proper encoding and byte order.
virtual void setFmtFieldSingle(const int width, const int prec)
default implementation.
bool getcNotEOF(int &c)
Reads a single byte value.
@ FormatPrecF
Format "%.*f".
virtual void setFmtType(FormatType type)
PWP_ENUM_PRECISION
File precision values.
bool rewind()
Reset position to the beginning of the file.
virtual void setFmtFieldDouble(const int width, const int prec)
bool checkAscii() const
Determines whether the file is Ascii by reading the contents of the file.