Skip to content

Commit

Permalink
Refactoring: Moved LoadTimingData() into Greenpak4Device
Browse files Browse the repository at this point in the history
azonenberg committed Jun 6, 2017
1 parent b76069b commit daa9f7d
Showing 3 changed files with 62 additions and 60 deletions.
64 changes: 4 additions & 60 deletions src/gp4tchar/main.cpp
Original file line number Diff line number Diff line change
@@ -28,8 +28,6 @@ using namespace std;

DevkitCalibration g_devkitCal;

bool LoadTimingData(string fname);

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Entry point

@@ -126,8 +124,11 @@ int main(int argc, char* argv[])
//Load timing data from disk
LogNotice("Loading timing data...\n");
string tfname = "timing.json";
if(!LoadTimingData(tfname))
if(!g_calDevice.LoadTimingData(tfname))
{
LogWarning("Couldn't load existing timing data file\n");
return 1;
}

//Measure delay through each element
/*
@@ -163,63 +164,6 @@ int main(int argc, char* argv[])
return 0;
}

bool LoadTimingData(string fname)
{
//Open the file (non-existence is a legal no-op, return success silently)
FILE* fp = fopen(fname.c_str(), "rb");
if(fp == NULL)
return true;
if(0 != fseek(fp, 0, SEEK_END))
{
LogError("Failed to seek to end of timing data file %s\n", fname.c_str());
fclose(fp);
return false;
}
size_t len = ftell(fp);
if(0 != fseek(fp, 0, SEEK_SET))
{
LogError("Failed to seek to start of timing data file %s\n", fname.c_str());
return false;
}
char* json_string = new char[len + 1];
json_string[len] = '\0';
if(len != fread(json_string, 1, len, fp))
{
LogError("Failed to read contents of timing data file %s\n", fname.c_str());
delete[] json_string;
fclose(fp);
return false;
}
fclose(fp);

//Parse the JSON
json_tokener* tok = json_tokener_new();
if(!tok)
{
LogError("Failed to create JSON tokenizer object\n");
delete[] json_string;
return false;
}
json_tokener_error err;
json_object* object = json_tokener_parse_verbose(json_string, &err);
if(NULL == object)
{
const char* desc = json_tokener_error_desc(err);
LogError("JSON parsing failed (err = %s)\n", desc);
delete[] json_string;
return false;
}

//Load stuff
if(!g_calDevice.LoadTimingData(object))
return false;

//Done
json_object_put(object);
json_tokener_free(tok);
return true;
}

void WaitForKeyPress()
{
LogNotice("Press any key to continue . . .\n");
57 changes: 57 additions & 0 deletions src/greenpak4/Greenpak4Device.cpp
Original file line number Diff line number Diff line change
@@ -1299,6 +1299,63 @@ void Greenpak4Device::SaveTimingData(string fname)
fclose(fp);
}

bool Greenpak4Device::LoadTimingData(string fname)
{
//Read it
FILE* fp = fopen(fname.c_str(), "rb");
if(fp == NULL)
return false;
if(0 != fseek(fp, 0, SEEK_END))
{
LogError("Failed to seek to end of timing data file %s\n", fname.c_str());
fclose(fp);
return false;
}
size_t len = ftell(fp);
if(0 != fseek(fp, 0, SEEK_SET))
{
LogError("Failed to seek to start of timing data file %s\n", fname.c_str());
return false;
}
char* json_string = new char[len + 1];
json_string[len] = '\0';
if(len != fread(json_string, 1, len, fp))
{
LogError("Failed to read contents of timing data file %s\n", fname.c_str());
delete[] json_string;
fclose(fp);
return false;
}
fclose(fp);

//Parse the JSON
json_tokener* tok = json_tokener_new();
if(!tok)
{
LogError("Failed to create JSON tokenizer object\n");
delete[] json_string;
return false;
}
json_tokener_error err;
json_object* object = json_tokener_parse_verbose(json_string, &err);
if(NULL == object)
{
const char* desc = json_tokener_error_desc(err);
LogError("JSON parsing failed (err = %s)\n", desc);
delete[] json_string;
return false;
}

//Load stuff
if(!LoadTimingData(object))
return false;

//Done
json_object_put(object);
json_tokener_free(tok);
return true;
}

bool Greenpak4Device::LoadTimingData(json_object* object)
{
//Make a map of description -> entity
1 change: 1 addition & 0 deletions src/greenpak4/Greenpak4Device.h
Original file line number Diff line number Diff line change
@@ -334,6 +334,7 @@ class Greenpak4Device
void PrintTimingData() const;
void SaveTimingData(std::string fname);
bool LoadTimingData(json_object* object);
bool LoadTimingData(std::string fname);

protected:

0 comments on commit daa9f7d

Please sign in to comment.