Commit a10991dc authored by Thomas Witkowski's avatar Thomas Witkowski
Browse files

Small code refactoring.

parent 713e06b9
......@@ -50,7 +50,7 @@ namespace AMDiS {
if (0 == Parameters::singlett->allParam.size()) {
if (0==Parameters::singlett->filename.size()) {
INFO(info, 1)("no parameters defined\n");
return(0);
return 0;
} else {
Parameters::singlett->read(Parameters::singlett->filename,key);
}
......@@ -67,7 +67,7 @@ namespace AMDiS {
INFO(info, 1)("initialization of parameter `%s' fails\n", key.data());
}
return(0);
return 0;
}
va_start(arg, format);
......@@ -97,7 +97,7 @@ namespace AMDiS {
INFO(info, 4)("location of initialization unknown\n");
}
va_end(arg);
return(count);
return count;
}
count++;
......@@ -163,7 +163,8 @@ namespace AMDiS {
return count;
}
int Parameters::getGlobalParameter(int flag, const std::string& key, std::string* param)
int Parameters::getGlobalParameter(int flag, const std::string& key,
std::string* param)
{
static char tempParam[255];
TEST_EXIT(param)("no parameter\n");
......@@ -240,7 +241,8 @@ namespace AMDiS {
void Parameters::qsort(int left, int right)
{
if (left >= right) return;
if (left >= right)
return;
swap(left, (left + right) / 2);
int last = left;
......@@ -259,18 +261,16 @@ namespace AMDiS {
{
FUNCNAME("Parameters::getKey()");
std::string fn, key="";
std::string fn, key = "";
char c;
int i, epos;
std::string h= " \t\r\f";
int pos = 0;
pos = s.find_first_not_of(" \t\f\r"); //skip Blank
std::string h = " \t\r\f";
int pos = s.find_first_not_of(" \t\f\r"); //skip Blank
if (pos < 0)
return key;
if (s[pos] == comment || s[pos] == '\0' || s[pos] == '\n')
if (s[pos] == comment || s[pos] == '\0' || s[pos] == '\n')
return(key);
if (s[pos] == '#') {
......@@ -280,15 +280,15 @@ namespace AMDiS {
/****************************************************************************/
pos += strlen("#include");
pos=s.find_first_not_of(" \t\f\r");
pos = s.find_first_not_of(" \t\f\r");
i = 0;
switch (c = s[pos++]) {
case '<':
c = '>';
case '\"':
h+=c;
epos=s.find_first_not_of(h,pos);
h += c;
epos = s.find_first_not_of(h, pos);
fn = s.substr(pos,epos-1);
if (s[epos] != c) {
......@@ -336,68 +336,66 @@ namespace AMDiS {
return("");
}
return(key);
return key;
}
const std::string Parameters::getPar(const std::string& key,
const std::string& si, int *nl,
const std::string& fn)
const std::string& si, int *nl,
const std::string& fn)
{
FUNCNAME("Parameters::getPar");
int i, pos,ol = *nl;
FUNCNAME("Parameters::getPar()");
int ol = *nl;
std::string inp;
std::string s=si;
std::string s = si;
std::string parameter="";
pos=s.find(':');
pos=s.find_first_not_of(" \r\f\t",pos+1);
int pos = s.find(':');
pos = s.find_first_not_of(" \r\f\t",pos+1);
i=pos;
while(i < static_cast<int>(s.length()) && s[i]!='#' && s[i]!='%' && s[i]!='\n') {
if (s[i] == '\\' && i+1 < static_cast<int>( s.length()) && s[i+1] == '\n')
{
s = s.substr(0, i);
(*nl)++;
inputFile >> inp;
s+=inp;
if (inputFile.eof())
{
ERROR("EOF reached while reading parameters of key %s\n", key.c_str());
if (ol == *nl-1) ERROR("Skipping line %d of file %s\n", *nl, fn.c_str());
else ERROR("Skipping lines %d-%d of file %s\n", ol, *nl, fn.c_str());
int i = pos;
while(i < static_cast<int>(s.length()) &&
s[i] != '#' && s[i] != '%' && s[i] != '\n') {
if (s[i] == '\\' && i+1 < static_cast<int>(s.length()) && s[i + 1] == '\n') {
s = s.substr(0, i);
(*nl)++;
inputFile >> inp;
s+=inp;
if (inputFile.eof()) {
ERROR("EOF reached while reading parameters of key %s\n", key.c_str());
if (ol == *nl-1)
ERROR("Skipping line %d of file %s\n", *nl, fn.c_str());
else
ERROR("Skipping lines %d-%d of file %s\n", ol, *nl, fn.c_str());
return("");
}
return("");
}
else
{
if (isBlankChar(static_cast<char>(s[i])))
{
parameter+= ' ';
i=pos=s.find_first_not_of(" \r\t\f", i);
if(i == static_cast<int>(std::string::npos)) {
i = parameter.length()-1;
break;
}
}
else
{
parameter+= s.substr(i++,1);
}
} else {
if (isBlankChar(static_cast<char>(s[i]))) {
parameter+= ' ';
i=pos=s.find_first_not_of(" \r\t\f", i);
if (i == static_cast<int>(std::string::npos)) {
i = parameter.length()-1;
break;
}
} else {
parameter += s.substr(i++,1);
}
}
}
i=parameter.find_last_not_of(" \r\t\f");
parameter.resize(i+1);
i = parameter.find_last_not_of(" \r\t\f");
parameter.resize(i + 1);
if (i == 0 && isBlankChar(parameter[0]))
{
ERROR("no parameter of key %s.\n", key.c_str());
if (ol == *nl) ERROR("Skipping line %d of file %s\n", *nl, fn.c_str());
else ERROR("Skipping lines %d-%d of file %s\n", ol, *nl, fn.c_str());
if (i == 0 && isBlankChar(parameter[0])) {
ERROR("no parameter of key %s.\n", key.c_str());
if (ol == *nl)
ERROR("Skipping line %d of file %s\n", *nl, fn.c_str());
else
ERROR("Skipping lines %d-%d of file %s\n", ol, *nl, fn.c_str());
return("");
}
return("");
}
return parameter;
}
......@@ -409,6 +407,7 @@ namespace AMDiS {
const std::string& fname)
{
FUNCNAME("Parameters::addParam()");
unsigned size_k, size_p;
int scmp = 0;
param newPar;
......@@ -417,11 +416,9 @@ namespace AMDiS {
size_p = parameter.size()+1;
std::vector<param>::iterator it;
for (it = allParam.begin(); it != allParam.end(); it++) {
if ((scmp = key.compare((*it).key)) >=0 )
break;
}
for (it = allParam.begin(); it != allParam.end(); it++)
if ((scmp = key.compare((*it).key)) >=0)
break;
if (it != allParam.end() && scmp == 0) {
/****************************************************************************/
......@@ -477,7 +474,6 @@ namespace AMDiS {
{
FUNCNAME("Parameters::init()");
int val;
std::ostringstream tmp_file;
std::ofstream cpp_flags;
std::ofstream call_cpp;
......@@ -486,21 +482,20 @@ namespace AMDiS {
std::string lfn = fn;
std::string::size_type fpos= lfn.find('/');
if (fpos == std::string::npos) {
if (fpos == std::string::npos)
fpos = 0;
} else {
else
++fpos;
}
lfn.insert(fpos,".#");
struct stat buf;
if ((val = lstat(lfn.c_str(), &buf)) == 0) {
if ((buf.st_mode&S_IFLNK) && (buf.st_size>0)) {
int val = lstat(lfn.c_str(), &buf);
if (val == 0)
if (buf.st_mode&S_IFLNK && buf.st_size > 0)
ERROR_EXIT("Unsaved version of init file exists\n");
}
}
initIntern();
if (0 == fn.size()) {
......@@ -517,44 +512,31 @@ namespace AMDiS {
return;
}
singlett->inputFile.close();
#ifndef CPP
ERROR("no cpp available; reading file %s without help of cpp\n",
fn.c_str());
init(p, fn);
return;
#else
time(&act_time);
if (sys_test("test -w", "/tmp")) /* you may write to /tmp */
{
file = fn.substr(fn.find_last_of("/\\"));
tmp_file << "/tmp/"<<file<< ".cpp";
if (sys_test("test -f", tmp_file.str())) /* file exists :-( */
{
tmp_file.freeze(false);
tmp_file<< "."<< static_cast<int>( act_time);
if (sys_test("test -f", tmp_file.str())) /* file exists :-( */
*(tmp_file.str()) = '\0';
}
if (sys_test("test -w", "/tmp")) { /* you may write to /tmp */
file = fn.substr(fn.find_last_of("/\\"));
tmp_file << "/tmp/"<<file<< ".cpp";
if (sys_test("test -f", tmp_file.str())) { /* file exists :-( */
tmp_file.freeze(false);
tmp_file<< "."<< static_cast<int>( act_time);
if (sys_test("test -f", tmp_file.str())) /* file exists :-( */
*(tmp_file.str()) = '\0';
}
}
if (*(tmp_file.str()) == '\0')
{
if (sys_test("test -f", tmp_file.str()+5))
{
*(tmp_file.str()) = '\0';
}
}
if (sys_test("test -f", tmp_file.str()+5))
*(tmp_file.str()) = '\0';
if (*(tmp_file.str())!='\0' && (outFile.open(tmp_file.str())))
if (*(tmp_file.str())!='\0' && (outFile.open(tmp_file.str()))) {
outFile.close();
else
{
ERROR("could not open temporary file for CPP\n");
ERROR("can not write to /tmp and files %s.cpp and\n", fn);
ERROR_EXIT("%s.cpp.%d either exist or are not writable\n",
fn, act_time);
}
} else {
ERROR("could not open temporary file for CPP\n");
ERROR("can not write to /tmp and files %s.cpp and\n", fn);
ERROR_EXIT("%s.cpp.%d either exist or are not writable\n",
fn, act_time);
}
tmp_file.freeze(false);
......@@ -569,15 +551,13 @@ namespace AMDiS {
/****************************************************************************/
val = system(call_cpp.str());
if (val)
{
MSG("val = %d\n", val);
ERROR("error during cpp call; reading file %s without help of cpp\n",
fn);
WAIT;
init(p, fn);
return;
}
if (val) {
MSG("val = %d\n", val);
ERROR("error during cpp call; reading file %s without help of cpp\n", fn);
WAIT;
init(p, fn);
return;
}
/**********************************************************************/
/* read parameters from temp file */
......@@ -594,8 +574,6 @@ namespace AMDiS {
val = system(call_cpp.str());
call_cpp.freeze(false);
tmp_file.freeze(false);
#endif
}
val = 10; // default for level of information
......@@ -640,24 +618,22 @@ namespace AMDiS {
int Parameters::binSearch(const std::string& key, int n_keys)
{
int cond, left, right, mid;
left = 0;
right = n_keys-1;
int left = 0;
int right = n_keys - 1;
while (left <= right) {
mid = (left+right)/2;
if ((cond = allParam[mid].key.compare(key)) < 0) {
int mid = (left + right) / 2;
int cond = allParam[mid].key.compare(key);
if (cond < 0) {
left = mid + 1;
} else if (cond > 0) {
right = mid - 1;
} else {
return(mid);
return mid;
}
}
return(-1);
return -1;
}
......@@ -672,7 +648,7 @@ namespace AMDiS {
initIntern();
if ((0 == key.size()) || (0 == par.size()))
if (key.size() == 0 || par.size() == 0)
return;
singlett->addParam(key, par, std::string(file), line, fname);
......@@ -700,15 +676,13 @@ namespace AMDiS {
const char *Parameters::getNextWord(std::string *s) const
{
static char Val[512];
int wb1,wb2;
static char Val[512];
wb1 = s->find_first_not_of(" ");
if (wb1 == static_cast<int>(std::string::npos)) {
return NULL;
}
int wb1 = s->find_first_not_of(" ");
if (wb1 == static_cast<int>(std::string::npos))
return NULL;
wb2 = s->find_first_of(" ");
int wb2 = s->find_first_of(" ");
if (wb2 == static_cast<int>(std::string::npos)) {
wb2 = s->length();
}
......@@ -717,12 +691,12 @@ namespace AMDiS {
Val[wb2-wb1] = '\0';
return(Val);
return Val;
}
void Parameters::save(const std::string file, int info)
{
std::ofstream fp;
std::ofstream fp;
std::vector<param>::iterator it;
initIntern();
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment