comparison core/WriteFile.cpp @ 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 45fc760622c9
comparison
equal deleted inserted replaced
156:89f28a867a09 157:f36313cbb55d
35 file = fopen(filename, "w"); 35 file = fopen(filename, "w");
36 variableOpen = false; 36 variableOpen = false;
37 lineLength = 0; 37 lineLength = 0;
38 echo = false; 38 echo = false;
39 bufferLength = 0; 39 bufferLength = 0;
40 readPointer = 0; 40 textReadPointer = 0;
41 binaryReadPointer = 0;
41 writePointer = 0; 42 writePointer = 0;
42 sleepTimeMs = 1; 43 sleepTimeMs = 1;
43 stringBufferLength = 1000; 44 stringBufferLength = 1000;
44 stringBuffer = (char*)malloc(sizeof(char) * (stringBufferLength)); 45 stringBuffer = (char*)malloc(sizeof(char) * (stringBufferLength));
45 setHeader("variable=[\n"); 46 setHeader("variable=[\n");
46 setFooter("];\n"); 47 setFooter("];\n");
47 staticConstructor(); //TODO: this line should be in the constructor, but cannot because of a bug in BeagleRT 48 staticConstructor(); //TODO: this line should be in the constructor, but cannot be because of a bug in BeagleRT
48 objAddrs.push_back(this); 49 objAddrs.push_back(this);
49 } 50 echoedLines = 0;
50 51 echoPeriod = 1;
52 }
53
54 void WriteFile::setFileType(WriteFileType newFileType){
55 fileType = newFileType;
56 }
51 void WriteFile::setEcho(bool newEcho){ 57 void WriteFile::setEcho(bool newEcho){
52 echo=newEcho; 58 echo=newEcho;
53 } 59 }
54 60 void WriteFile::setEchoInterval(int newEchoPeriod){
61 echoPeriod = newEchoPeriod;
62 if(echoPeriod != 0)
63 echo = true;
64 else
65 echo = false;
66 }
55 void WriteFile::print(const char* string){ 67 void WriteFile::print(const char* string){
56 if(echo == true){ 68 if(echo == true){
57 printf("%s", string); 69 echoedLines++;
58 } 70 if (echoedLines >= echoPeriod){
59 if(file != NULL){ 71 echoedLines = 0;
72 printf("%s", string);
73 }
74 }
75 if(file != NULL && fileType != kBinary){
60 fprintf(file, "%s", string); 76 fprintf(file, "%s", string);
61 } 77 }
62 } 78 }
63 79
64 void WriteFile::writeLine(){ 80 void WriteFile::writeLine(){
65 int stringBufferPointer = 0; 81 if(echo == true || fileType != kBinary){
66 for(unsigned int n = 0; n < formatTokens.size(); n++){ 82 int stringBufferPointer = 0;
67 int numOfCharsWritten = snprintf( &stringBuffer[stringBufferPointer], stringBufferLength - stringBufferPointer, 83 for(unsigned int n = 0; n < formatTokens.size(); n++){
68 formatTokens[n], buffer[readPointer]); 84 int numOfCharsWritten = snprintf( &stringBuffer[stringBufferPointer], stringBufferLength - stringBufferPointer,
69 stringBufferPointer += numOfCharsWritten; 85 formatTokens[n], buffer[textReadPointer]);
70 readPointer++; 86 stringBufferPointer += numOfCharsWritten;
71 if (readPointer >= bufferLength){ 87 textReadPointer++;
72 readPointer -= bufferLength; 88 if (textReadPointer >= bufferLength){
73 } 89 textReadPointer -= bufferLength;
74 } 90 }
75 print(stringBuffer); 91 }
92 print(stringBuffer);
93 }
76 } 94 }
77 95
78 void WriteFile::setLineLength(int newLineLength){ 96 void WriteFile::setLineLength(int newLineLength){
79 lineLength=newLineLength; 97 lineLength=newLineLength;
80 free(buffer); 98 free(buffer);
81 bufferLength = lineLength * 10000; // circular buffer of length 10000 lineLenghts 99 bufferLength = lineLength * 10000; // circular buffer of length 10000 lineLenghts
82 buffer = (float*)malloc(sizeof(float) * bufferLength); 100 buffer = (float*)malloc(sizeof(float) * bufferLength);
83 } 101 }
84 102
85 void WriteFile::log(float value){ 103 void WriteFile::log(float value){
86 if(format == NULL || buffer == NULL) 104 if(fileType != kBinary && (format == NULL || buffer == NULL))
87 return; 105 return;
88 buffer[writePointer] = value; 106 buffer[writePointer] = value;
89 writePointer++; 107 writePointer++;
90 if(writePointer == bufferLength){ 108 if(writePointer == bufferLength){
91 writePointer = 0; 109 writePointer = 0;
92 } 110 }
93 if(writePointer == readPointer){ 111 if((fileType == kText && writePointer == textReadPointer - 1) ||
94 fprintf(stderr, "WriteFile: pointers crossed, you should probably write less data to disk\n"); 112 (fileType == kBinary && writePointer == binaryReadPointer - 1)){
113 fprintf(stderr, "%d %d WriteFile: pointers crossed, you should probably slow down your writing to disk\n", writePointer, binaryReadPointer);
95 } 114 }
96 if(threadRunning == false){ 115 if(threadRunning == false){
97 startThread(); 116 startThread();
98 } 117 }
99 } 118 }
166 185
167 float WriteFile::getBufferStatus(){ 186 float WriteFile::getBufferStatus(){
168 return 1-getOffset()/(float)bufferLength; 187 return 1-getOffset()/(float)bufferLength;
169 } 188 }
170 189
190 int WriteFile::getOffsetFromPointer(int aReadPointer){
191 int offset = writePointer - aReadPointer;
192 if( offset < 0)
193 offset += bufferLength;
194 return offset;
195 }
171 int WriteFile::getOffset(){ 196 int WriteFile::getOffset(){
172 int offset = writePointer - readPointer; 197 if(fileType == kBinary){
173 if( offset < 0) 198 return getOffsetFromPointer(binaryReadPointer);
174 offset += bufferLength; 199 }
175 return offset; 200 else{
176 } 201 return getOffsetFromPointer(textReadPointer);
177 202 }
178 void WriteFile::writeOutput(){ 203 }
179 while( getOffset() >= lineLength ){ //if there is less than one line worth of data to write, skip over. 204
180 // So we make sure we always write full lines 205 void WriteFile::writeOutput(bool writeAll){
206 while( getOffsetFromPointer(textReadPointer) >= lineLength){ //if there is less than one line worth of data to write, skip over.
207 // So we make sure we only write full lines
181 writeLine(); 208 writeLine();
209 }
210 if(fileType == kBinary){
211 int numBinaryElementsToWriteAtOnce = 100;
212 while(getOffsetFromPointer(binaryReadPointer) > numBinaryElementsToWriteAtOnce){
213 int elementsToEndOfBuffer = bufferLength - binaryReadPointer;
214 int numberElementsToWrite = numBinaryElementsToWriteAtOnce < elementsToEndOfBuffer ?
215 numBinaryElementsToWriteAtOnce : elementsToEndOfBuffer;
216 numberElementsToWrite = fwrite(&(buffer[binaryReadPointer]), sizeof(float), numberElementsToWrite, file);
217 binaryReadPointer += numberElementsToWrite;
218 if(binaryReadPointer >= bufferLength){
219 binaryReadPointer = 0;
220 }
221 }
222 if(writeAll == true){ // flush all the buffer to the file
223 while(getOffsetFromPointer(binaryReadPointer) != 0){
224 binaryReadPointer += fwrite(&(buffer[binaryReadPointer]), sizeof(float), 1, file);
225 if(binaryReadPointer >= bufferLength){
226 binaryReadPointer = 0;
227 }
228 }
229 }
182 } 230 }
183 } 231 }
184 232
185 void WriteFile::writeAllOutputs(){ 233 void WriteFile::writeAllOutputs(){
186 for(unsigned int n = 0; n < objAddrs.size(); n++){ 234 for(unsigned int n = 0; n < objAddrs.size(); n++){
187 objAddrs[n] -> writeOutput(); 235 objAddrs[n] -> writeOutput(false);
188 } 236 }
189 } 237 }
190 238
191 void WriteFile::writeAllHeaders(){ 239 void WriteFile::writeAllHeaders(){
192 for(unsigned int n = 0; n < objAddrs.size(); n++){ 240 for(unsigned int n = 0; n < objAddrs.size(); n++){