mirror of
https://github.com/nasa/trick.git
synced 2025-01-18 18:56:31 +00:00
remove memory leaks parseLogHeader.cpp
This commit is contained in:
parent
33b74c07c9
commit
3c7a93c3ca
@ -17,6 +17,19 @@
|
|||||||
#include "trick/units_conv.h"
|
#include "trick/units_conv.h"
|
||||||
#include "trick/map_trick_units_to_udunits.hh"
|
#include "trick/map_trick_units_to_udunits.hh"
|
||||||
|
|
||||||
|
// index labels for char arrays
|
||||||
|
enum strIndex {
|
||||||
|
LINE,
|
||||||
|
STR1,
|
||||||
|
STR2,
|
||||||
|
STR3,
|
||||||
|
STR4,
|
||||||
|
NUM_STR
|
||||||
|
};
|
||||||
|
|
||||||
|
// helper function to free memory before successful or unsuccessful return
|
||||||
|
void shutdown(FILE*, char**, char*);
|
||||||
|
|
||||||
/* A private function for parsing log header files.
|
/* A private function for parsing log header files.
|
||||||
* This is responsible for initialize the Log classes as well
|
* This is responsible for initialize the Log classes as well
|
||||||
*/
|
*/
|
||||||
@ -25,17 +38,13 @@ int LogGroup::parseLogHeaders()
|
|||||||
|
|
||||||
int nVars;
|
int nVars;
|
||||||
int lineNum;
|
int lineNum;
|
||||||
char *line;
|
char** strs = new char*[NUM_STR];
|
||||||
char *str1;
|
|
||||||
char *str2;
|
|
||||||
char *str3;
|
|
||||||
char *str4;
|
|
||||||
char *headerName;
|
char *headerName;
|
||||||
int i;
|
int i;
|
||||||
int headerSize;
|
int headerSize;
|
||||||
int len;
|
int len;
|
||||||
|
|
||||||
LogData *currLogData;
|
LogData *currLogData = 0;
|
||||||
Var *currVar;
|
Var *currVar;
|
||||||
|
|
||||||
|
|
||||||
@ -54,6 +63,7 @@ int LogGroup::parseLogHeaders()
|
|||||||
fprintf(stderr,
|
fprintf(stderr,
|
||||||
"\nERROR:\nCouldn't open file \"%s\" \n",
|
"\nERROR:\nCouldn't open file \"%s\" \n",
|
||||||
headerName);
|
headerName);
|
||||||
|
delete[] headerName;
|
||||||
return (-1);
|
return (-1);
|
||||||
}
|
}
|
||||||
// Allocate strings, we know that buffers can't be
|
// Allocate strings, we know that buffers can't be
|
||||||
@ -63,15 +73,13 @@ int LogGroup::parseLogHeaders()
|
|||||||
fseek(fp_, 0, SEEK_END);
|
fseek(fp_, 0, SEEK_END);
|
||||||
headerSize = ftell(fp_);
|
headerSize = ftell(fp_);
|
||||||
::rewind(fp_);
|
::rewind(fp_);
|
||||||
line = new char[headerSize + 1]; // Not sure about +1 :)
|
for(int i = 0; i < NUM_STR; i++){
|
||||||
str1 = new char[headerSize + 1];
|
strs[i] = new char[headerSize + 1]; // Not sure about +1 :)
|
||||||
str2 = new char[headerSize + 1];
|
}
|
||||||
str3 = new char[headerSize + 1];
|
|
||||||
str4 = new char[headerSize + 1];
|
|
||||||
|
|
||||||
// Parse rest of file
|
// Parse rest of file
|
||||||
lineNum = 0;
|
lineNum = 0;
|
||||||
while (fgets(line, headerSize, fp_) != NULL) {
|
while (fgets(strs[LINE], headerSize, fp_) != NULL) {
|
||||||
|
|
||||||
lineNum++;
|
lineNum++;
|
||||||
|
|
||||||
@ -79,10 +87,10 @@ int LogGroup::parseLogHeaders()
|
|||||||
or
|
or
|
||||||
<log_file_name> byte_order is [lit|big]_endian
|
<log_file_name> byte_order is [lit|big]_endian
|
||||||
*/
|
*/
|
||||||
if ( sscanf(line, "%s %s %s %s", str1, str2, str3, str4) == 4 ) {
|
if ( sscanf(strs[LINE], "%s %s %s %s", strs[STR1], strs[STR2], strs[STR3], strs[STR4]) == 4 ) {
|
||||||
|
|
||||||
// Byte order statement and top of a log group?
|
// Byte order statement and top of a log group?
|
||||||
if (!strcmp(str2, "byte_order")) {
|
if (!strcmp(strs[STR2], "byte_order")) {
|
||||||
|
|
||||||
LogData *ld = new LogData;
|
LogData *ld = new LogData;
|
||||||
log.push_back(ld);
|
log.push_back(ld);
|
||||||
@ -90,10 +98,10 @@ int LogGroup::parseLogHeaders()
|
|||||||
nGroups_++;
|
nGroups_++;
|
||||||
|
|
||||||
// New binary file
|
// New binary file
|
||||||
currLogData->setBinaryFileName(str1);
|
currLogData->setBinaryFileName(strs[STR1]);
|
||||||
|
|
||||||
// Set byte order
|
// Set byte order
|
||||||
if (!strcmp(str4, "big_endian")) {
|
if (!strcmp(strs[STR4], "big_endian")) {
|
||||||
currLogData->dataByteOrder = 1;
|
currLogData->dataByteOrder = 1;
|
||||||
} else {
|
} else {
|
||||||
currLogData->dataByteOrder = 0;
|
currLogData->dataByteOrder = 0;
|
||||||
@ -102,7 +110,7 @@ int LogGroup::parseLogHeaders()
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
// Check for binary file name mismatches
|
// Check for binary file name mismatches
|
||||||
if (strcmp(str1, currLogData->getBinaryFileName())) {
|
if (currLogData && strcmp(strs[STR1], currLogData->getBinaryFileName())) {
|
||||||
printf("ERROR: Parsing log header \"%s\".\n"
|
printf("ERROR: Parsing log header \"%s\".\n"
|
||||||
" Line %d. Binary file name "
|
" Line %d. Binary file name "
|
||||||
"mismatch with \"%s\".\n"
|
"mismatch with \"%s\".\n"
|
||||||
@ -110,6 +118,7 @@ int LogGroup::parseLogHeaders()
|
|||||||
"specification.\n\n",
|
"specification.\n\n",
|
||||||
headerName, lineNum,
|
headerName, lineNum,
|
||||||
currLogData->getBinaryFileName());
|
currLogData->getBinaryFileName());
|
||||||
|
shutdown(fp_, strs, headerName);
|
||||||
return (-1);
|
return (-1);
|
||||||
}
|
}
|
||||||
// New variable
|
// New variable
|
||||||
@ -121,11 +130,12 @@ int LogGroup::parseLogHeaders()
|
|||||||
currLogData->setNumVars(nVars);
|
currLogData->setNumVars(nVars);
|
||||||
|
|
||||||
// Set Type
|
// Set Type
|
||||||
if (currVar->setType(str2) < 0) {
|
if (currVar->setType(strs[STR2]) < 0) {
|
||||||
printf("ERROR: In log header \"%s\".\n"
|
printf("ERROR: In log header \"%s\".\n"
|
||||||
" Line %d. Type \"%s\" is "
|
" Line %d. Type \"%s\" is "
|
||||||
"not supported.\n",
|
"not supported.\n",
|
||||||
headerName, lineNum, str2);
|
headerName, lineNum, strs[STR2]);
|
||||||
|
shutdown(fp_, strs, headerName);
|
||||||
return (-1);
|
return (-1);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -134,43 +144,50 @@ int LogGroup::parseLogHeaders()
|
|||||||
{
|
{
|
||||||
char new_units_spec[100];
|
char new_units_spec[100];
|
||||||
new_units_spec[0] = 0;
|
new_units_spec[0] = 0;
|
||||||
if ( convert_units_spec (str3, new_units_spec) != 0 ) {
|
if ( convert_units_spec (strs[STR3], new_units_spec) != 0 ) {
|
||||||
printf (" ERROR: Attempt to convert old units-spec, \"%s\" failed.\n\n",str3);
|
printf (" ERROR: Attempt to convert old units-spec, \"%s\" failed.\n\n",strs[STR3]);
|
||||||
|
shutdown(fp_, strs, headerName);
|
||||||
|
|
||||||
return (-1);
|
return (-1);
|
||||||
}
|
}
|
||||||
delete [] str3;
|
delete [] strs[STR3];
|
||||||
len = strlen(new_units_spec);
|
len = strlen(new_units_spec);
|
||||||
str3 = new char[len + 1] ;
|
strs[STR3] = new char[len + 1] ;
|
||||||
strcpy(str3, new_units_spec);
|
strcpy(strs[STR3], new_units_spec);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Initialize Unit class
|
// Initialize Unit class
|
||||||
if ( ! strcmp(str3,"--")) {
|
if ( ! strcmp(strs[STR3],"--")) {
|
||||||
currVar->setUnit(str3);
|
currVar->setUnit(strs[STR3]);
|
||||||
} else {
|
} else {
|
||||||
currVar->setUnit(map_trick_units_to_udunits(str3));
|
currVar->setUnit(map_trick_units_to_udunits(strs[STR3]));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set Var Name
|
// Set Var Name
|
||||||
currVar->setVarName(str4) ;
|
currVar->setVarName(strs[STR4]) ;
|
||||||
if (currVar->getSize() < 0) {
|
if (currVar->getSize() < 0) {
|
||||||
printf("ERROR: In log header \"%s\".\n"
|
printf("ERROR: In log header \"%s\".\n"
|
||||||
" Line %d. Problem with var "
|
" Line %d. Problem with var "
|
||||||
"name \"%s\" . \n",
|
"name \"%s\" . \n",
|
||||||
headerName, lineNum, str4);
|
headerName, lineNum, strs[STR4]);
|
||||||
|
shutdown(fp_, strs, headerName);
|
||||||
return (-1);
|
return (-1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fclose(fp_);
|
shutdown(fp_, strs, headerName);
|
||||||
delete[]line;
|
|
||||||
delete[]str1;
|
|
||||||
delete[]str2;
|
|
||||||
delete[]str3;
|
|
||||||
delete[]str4;
|
|
||||||
delete[]headerName;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void shutdown (FILE* fp_, char** strs, char* headerName) {
|
||||||
|
fclose(fp_);
|
||||||
|
for(int i = 0; i < NUM_STR; i++) {
|
||||||
|
delete[] strs[i];
|
||||||
|
}
|
||||||
|
delete[] strs;
|
||||||
|
delete[] headerName;
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user