Mercurial > hg > beaglert
diff include/WriteFile.h @ 157:f36313cbb55d
Added capability to WriteFile to save binary files, added example project
author | Giulio Moro <giuliomoro@yahoo.it> |
---|---|
date | Tue, 13 Oct 2015 02:01:05 +0100 |
parents | 3b7270949a97 |
children | 1e7db6610600 |
line wrap: on
line diff
--- a/include/WriteFile.h Wed Oct 07 23:38:52 2015 +0100 +++ b/include/WriteFile.h Tue Oct 13 02:01:05 2015 +0100 @@ -14,21 +14,30 @@ #include <string.h> #include <unistd.h> +typedef enum { + kBinary, + kText +} WriteFileType; + class WriteFile { private: static AuxiliaryTask writeAllFilesTask; bool echo; + int echoedLines; + int echoPeriod; char *header; char *footer; char *stringBuffer; int stringBufferLength; int bufferLength; float* buffer; - int readPointer; + int textReadPointer; + int binaryReadPointer; int writePointer; bool variableOpen; char* format; int lineLength; + WriteFileType fileType; static int sleepTimeMs; FILE *file; void writeLine(); @@ -36,7 +45,9 @@ void writeFooter(); void allocateAndCopyString(const char* source, char** destination); void print(const char* string); + void printBinary(const char* string); void setLineLength(int newLineLength); + int getOffsetFromPointer(int aPointer); std::vector<char *> formatTokens; static void sanitizeString(char* string); static void sanitizeString(char* string, int numberOfArguments); @@ -48,11 +59,36 @@ static bool staticConstructed; static void staticConstructor(); static std::vector<WriteFile *> objAddrs; + void writeOutput(bool writeAll); public: WriteFile(); + /** + * Set the type of file to write, can be either kText or kBinary. + * Binary files can be imported e.g. in Matlab: + * fid=fopen('out','r'); + * A = fread(fid, 'float'); + * */ + void setFileType(WriteFileType newFileType); void setEcho(bool newEcho); + void setEchoInterval(int newPeriod); + /** + * Set the format that you want to use for your output. + * + * Only %f is allowed (with modifiers). When in binary mode, + * the specified format is used only for echoing to console. + */ void setFormat(const char* newFormat); + /** + * Set one or more lines to be printed at the beginning of the file. + * + * This is ignored in binary mode. + */ void setHeader(const char* newHeader); + /** + * Set one or more lines to be printed at the end of the file. + * + * This is ignored in binary mode. + */ void setFooter(const char* newFooter); void log(float* array, int length); void log(float value); @@ -71,7 +107,6 @@ * and 1 being buffer empty (writing to disk is fast enough). */ float getBufferStatus(); - void writeOutput(); ~WriteFile(); static int getNumInstances(); static void writeAllHeaders();