comparison 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
comparison
equal deleted inserted replaced
156:89f28a867a09 157:f36313cbb55d
12 #include <stdio.h> 12 #include <stdio.h>
13 #include <stdlib.h> 13 #include <stdlib.h>
14 #include <string.h> 14 #include <string.h>
15 #include <unistd.h> 15 #include <unistd.h>
16 16
17 typedef enum {
18 kBinary,
19 kText
20 } WriteFileType;
21
17 class WriteFile { 22 class WriteFile {
18 private: 23 private:
19 static AuxiliaryTask writeAllFilesTask; 24 static AuxiliaryTask writeAllFilesTask;
20 bool echo; 25 bool echo;
26 int echoedLines;
27 int echoPeriod;
21 char *header; 28 char *header;
22 char *footer; 29 char *footer;
23 char *stringBuffer; 30 char *stringBuffer;
24 int stringBufferLength; 31 int stringBufferLength;
25 int bufferLength; 32 int bufferLength;
26 float* buffer; 33 float* buffer;
27 int readPointer; 34 int textReadPointer;
35 int binaryReadPointer;
28 int writePointer; 36 int writePointer;
29 bool variableOpen; 37 bool variableOpen;
30 char* format; 38 char* format;
31 int lineLength; 39 int lineLength;
40 WriteFileType fileType;
32 static int sleepTimeMs; 41 static int sleepTimeMs;
33 FILE *file; 42 FILE *file;
34 void writeLine(); 43 void writeLine();
35 void writeHeader(); 44 void writeHeader();
36 void writeFooter(); 45 void writeFooter();
37 void allocateAndCopyString(const char* source, char** destination); 46 void allocateAndCopyString(const char* source, char** destination);
38 void print(const char* string); 47 void print(const char* string);
48 void printBinary(const char* string);
39 void setLineLength(int newLineLength); 49 void setLineLength(int newLineLength);
50 int getOffsetFromPointer(int aPointer);
40 std::vector<char *> formatTokens; 51 std::vector<char *> formatTokens;
41 static void sanitizeString(char* string); 52 static void sanitizeString(char* string);
42 static void sanitizeString(char* string, int numberOfArguments); 53 static void sanitizeString(char* string, int numberOfArguments);
43 static bool isThreadRunning(); 54 static bool isThreadRunning();
44 static bool auxiliaryTaskRunning; 55 static bool auxiliaryTaskRunning;
46 static bool threadIsExiting; 57 static bool threadIsExiting;
47 static bool threadRunning; 58 static bool threadRunning;
48 static bool staticConstructed; 59 static bool staticConstructed;
49 static void staticConstructor(); 60 static void staticConstructor();
50 static std::vector<WriteFile *> objAddrs; 61 static std::vector<WriteFile *> objAddrs;
62 void writeOutput(bool writeAll);
51 public: 63 public:
52 WriteFile(); 64 WriteFile();
65 /**
66 * Set the type of file to write, can be either kText or kBinary.
67 * Binary files can be imported e.g. in Matlab:
68 * fid=fopen('out','r');
69 * A = fread(fid, 'float');
70 * */
71 void setFileType(WriteFileType newFileType);
53 void setEcho(bool newEcho); 72 void setEcho(bool newEcho);
73 void setEchoInterval(int newPeriod);
74 /**
75 * Set the format that you want to use for your output.
76 *
77 * Only %f is allowed (with modifiers). When in binary mode,
78 * the specified format is used only for echoing to console.
79 */
54 void setFormat(const char* newFormat); 80 void setFormat(const char* newFormat);
81 /**
82 * Set one or more lines to be printed at the beginning of the file.
83 *
84 * This is ignored in binary mode.
85 */
55 void setHeader(const char* newHeader); 86 void setHeader(const char* newHeader);
87 /**
88 * Set one or more lines to be printed at the end of the file.
89 *
90 * This is ignored in binary mode.
91 */
56 void setFooter(const char* newFooter); 92 void setFooter(const char* newFooter);
57 void log(float* array, int length); 93 void log(float* array, int length);
58 void log(float value); 94 void log(float value);
59 void init(const char* filename); 95 void init(const char* filename);
60 96
69 * 105 *
70 * @return a value between 0 and 1, with 0 being buffer full (writing to disk not fast enough) 106 * @return a value between 0 and 1, with 0 being buffer full (writing to disk not fast enough)
71 * and 1 being buffer empty (writing to disk is fast enough). 107 * and 1 being buffer empty (writing to disk is fast enough).
72 */ 108 */
73 float getBufferStatus(); 109 float getBufferStatus();
74 void writeOutput();
75 ~WriteFile(); 110 ~WriteFile();
76 static int getNumInstances(); 111 static int getNumInstances();
77 static void writeAllHeaders(); 112 static void writeAllHeaders();
78 static void writeAllFooters(); 113 static void writeAllFooters();
79 static void writeAllOutputs(); 114 static void writeAllOutputs();