Skip to content

Commit

Permalink
Refactor utils.h for readability
Browse files Browse the repository at this point in the history
Give code some consistency and flip if-else statements in `int xtoi()` to enhance readability and performance.
  • Loading branch information
impinball committed Mar 4, 2014
1 parent 0607998 commit 521c727
Showing 1 changed file with 42 additions and 59 deletions.
101 changes: 42 additions & 59 deletions utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,22 +52,21 @@
using namespace std;

struct fileCache{
time_t timestamp;
time_t timestamp;
queue<string> statOutput;
};

queue<string> exec_command(string);
vector<string> make_array(string);
void string_replacer(string&,const string,string);

vector<string> make_array(string data){
vector<string> make_array(string data) {
vector<string> result;
string delimiters = " ";
string::size_type lastPos = data.find_first_not_of(delimiters, 0);
string::size_type pos = data.find_first_of(delimiters, lastPos);

while (string::npos != pos || string::npos != lastPos)
{
while (string::npos != pos || string::npos != lastPos) {
result.push_back(data.substr(lastPos, pos - lastPos));
lastPos = data.find_first_not_of(delimiters, pos);
pos = data.find_first_of(delimiters, lastPos);
Expand All @@ -88,82 +87,66 @@ vector<string> make_array(string data){
*/
void string_replacer(string &source, const string find, const string replace) {
size_t j = 0;
for ( ; (j = source.find(find,j)) != string::npos ; ) {
source.replace( j, find.length(), replace );
for (j = source.find(find, j); j != string::npos; ) {
source.replace(j, find.length(), replace);
j = j + replace.length();
}
}

int xtoi(const char* xs, unsigned int* result)
{
size_t szlen = strlen(xs);
int i, xv, fact;

if (szlen > 0)
{
// Converting more than 32bit hexadecimal value?
if (szlen>8) return 2; // exit

// Begin conversion here
*result = 0;
fact = 1;

// Run until no more character to convert
for(i=szlen-1; i>=0 ;i--)
{
if (isxdigit(*(xs+i)))
{
if (*(xs+i)>=97)
{
xv = ( *(xs+i) - 97) + 10;
int xtoi(const char* xs, unsigned int* result) {
size_t szlen = strlen(xs);
int i, xv, fact;

// Nothing to convert
if (szlen <= 0) return 1;

// Exit if converting more than 32bit hexadecimal value
if (szlen > 8) return 2; // exit

// Begin conversion here
*result = 0;
fact = 1;

// Run until no more character to convert
for(i = szlen - 1; i >= 0; i--) {

// Conversion was abnormally terminated
// by non hexadecimal digit, hence
// returning only the converted with
// an error value 4 (illegal hex character)
if (!isxdigit(*(xs+i))) return 4;

if (*(xs+i) >= 97) {
xv = (*(xs+i) - 97) + 10;
} else if (*(xs+i) >= 65) {
xv = (*(xs+i) - 65) + 10;
} else {
xv = *(xs+i) - 48;
}
*result += (xv * fact);
fact *= 16;
}
else if ( *(xs+i) >= 65)
{
xv = (*(xs+i) - 65) + 10;
}
else
{
xv = *(xs+i) - 48;
}
*result += (xv * fact);
fact *= 16;
}
else
{
// Conversion was abnormally terminated
// by non hexadecimal digit, hence
// returning only the converted with
// an error value 4 (illegal hex character)
return 4;
}
}
}

// Nothing to convert
return 1;
}

/**
Execute the given command string as a shell command.
@param command the string to be executed as a command.
*/
queue<string> exec_command(const string command)
{
queue<string> exec_command(const string command) {
cout << "--*-- " << "exec_command: " << command << "\n";
queue<string> output;
FILE *fp = popen(command.c_str(), "r" );
FILE *fp = popen(command.c_str(), "r");

char buff[1000];
string tmp_string;
while ( fgets( buff, sizeof buff, fp ) != NULL && !feof(fp) )
{
while (fgets(buff, sizeof buff, fp) != NULL && !feof(fp)) {
tmp_string.assign(buff);
tmp_string.erase(tmp_string.size()-2);
tmp_string.erase(tmp_string.size() - 2);
output.push(tmp_string);
}

pclose( fp );
pclose(fp);

return output;
}
Expand Down

0 comments on commit 521c727

Please sign in to comment.