Fixing ticket #35 (Get rid of #ifdefs in our code where possible)

This commit is contained in:
Reijo Tomperi 2009-01-23 20:25:13 +00:00
parent ca56520c29
commit 8d6f41397a
11 changed files with 52 additions and 85 deletions

View File

@ -30,7 +30,7 @@
#include <cstring>
#include <stdlib.h> // <- strtoul
#include <cstdlib> // <- strtoul
//---------------------------------------------------------------------------

View File

@ -26,14 +26,8 @@
#include <string>
#include <sstream>
#include <cstring>
#include <algorithm>
#ifdef __BORLANDC__
#include <ctype.h>
#include <mem.h>
#endif
//---------------------------------------------------------------------------
CheckClass::CheckClass(const Tokenizer *tokenizer, const Settings &settings, ErrorLogger *errorLogger)
@ -101,11 +95,7 @@ struct CheckClass::VAR *CheckClass::ClassChecking_GetVarList(const Token *tok1)
// If the varname was set in one of the two if-block above, create a entry for this variable..
if (varname)
{
struct VAR *var = new VAR;
memset(var, 0, sizeof(struct VAR));
var->name = varname;
var->init = false;
var->next = varlist;
struct VAR *var = new VAR(varname, false, varlist);
varlist = var;
}
}

View File

@ -46,6 +46,13 @@ public:
private:
struct VAR
{
VAR(const char *name = 0, bool init = false, struct VAR *next = 0)
{
this->name = name;
this->init = init;
this->next = next;
}
const char *name;
bool init;
struct VAR *next;

View File

@ -21,6 +21,7 @@
//---------------------------------------------------------------------------
#include "checkheaders.h"
#include "tokenize.h"
#include "filelister.h"
#include <algorithm>
#include <list>
@ -94,7 +95,7 @@ void CheckHeaders::WarningIncludeHeader()
const char *includefile = includetok->next()->aaaa();
while (hfile < _tokenizer->getFiles()->size())
{
if (Tokenizer::SameFileName(_tokenizer->getFiles()->at(hfile).c_str(), includefile))
if (FileLister::SameFileName(_tokenizer->getFiles()->at(hfile).c_str(), includefile))
break;
++hfile;
}

View File

@ -19,23 +19,12 @@
#include "checkmemoryleak.h"
#include "errormessage.h"
#include <stdlib.h> // free
#include <algorithm>
#include <iostream>
#include <sstream>
#ifdef __BORLANDC__
#include <mem.h> // <- memset
#else
#include <string.h>
#endif
//---------------------------------------------------------------------------
CheckMemoryLeakClass::CheckMemoryLeakClass(const Tokenizer *tokenizer, const Settings &settings, ErrorLogger *errorLogger)

View File

@ -24,7 +24,7 @@
#include <list>
#include <map>
#include <sstream>
#include <stdlib.h> // <- atoi
#include <cstdlib> // <- atoi
#include <cstring>
#include <cctype>
//---------------------------------------------------------------------------

View File

@ -217,4 +217,20 @@ void FileLister::RecursiveAddFiles(std::vector<std::string> &filenames, const st
//---------------------------------------------------------------------------
bool FileLister::SameFileName(const char fname1[], const char fname2[])
{
#ifdef __linux__
return bool(strcmp(fname1, fname2) == 0);
#endif
#ifdef __GNUC__
return bool(strcasecmp(fname1, fname2) == 0);
#endif
#ifdef __BORLANDC__
return bool(stricmp(fname1, fname2) == 0);
#endif
#ifdef _MSC_VER
return bool(_stricmp(fname1, fname2) == 0);
#endif
}

View File

@ -41,6 +41,7 @@ class FileLister
public:
static void RecursiveAddFiles(std::vector<std::string> &filenames, const std::string &path, bool recursive);
static std::string simplifyPath(const char *originalPath);
static bool SameFileName(const char fname1[], const char fname2[]);
private:
static bool AcceptFile(const std::string &filename);
};

View File

@ -23,16 +23,10 @@
#include "token.h"
#include <algorithm>
#include <sstream>
#include <fstream>
#include <fstream>
#include <iostream>
#ifdef __BORLANDC__
#include <ctype>
#include <stdlib.h> // exit
#endif
#include <cstdlib>
Preprocessor::Preprocessor()
{
@ -477,8 +471,8 @@ void Preprocessor::handleIncludes(std::string &code, const std::string &filename
// std::string line;
std::string path = filename;
path.erase(1 + path.find_last_of("\\/"));
std::string::size_type pos = 0;
std::map<std::string,bool> handledFiles;
std::string::size_type pos = 0;
std::map<std::string, bool> handledFiles;
while ((pos = code.find("#include", pos)) != std::string::npos)
{
// Accept only includes that are at the start of a line
@ -497,16 +491,16 @@ void Preprocessor::handleIncludes(std::string &code, const std::string &filename
filename = getHeaderFileName(filename);
if (filename.length() == 0)
continue;
if( handledFiles.find( filename ) != handledFiles.end() )
{
// We have processed this file already once, skip
// it this time to avoid ethernal loop.
continue;
}
handledFiles[ filename ] = true;
if (handledFiles.find(filename) != handledFiles.end())
{
// We have processed this file already once, skip
// it this time to avoid ethernal loop.
continue;
}
handledFiles[ filename ] = true;
// filename contains now a file name e.g. "menu.h"
std::string processedFile;
for (std::list<std::string>::const_iterator iter = includePaths.begin(); iter != includePaths.end(); ++iter)
@ -514,7 +508,7 @@ void Preprocessor::handleIncludes(std::string &code, const std::string &filename
std::ifstream fin;
fin.open((*iter + filename).c_str());
if (fin.is_open())
{
{
filename = *iter + filename;
processedFile = Preprocessor::read(fin);
break;
@ -527,7 +521,7 @@ void Preprocessor::handleIncludes(std::string &code, const std::string &filename
std::ifstream fin(filename.c_str());
processedFile = Preprocessor::read(fin);
}
if (processedFile.length() > 0)
{
// Replace all tabs with spaces..
@ -540,10 +534,10 @@ void Preprocessor::handleIncludes(std::string &code, const std::string &filename
// Remove space characters that are after or before new line character
processedFile = removeSpaceNearNL(processedFile);
processedFile = "#file \"" + filename + "\"\n" + processedFile + "\n#endfile";
code.insert(pos, processedFile);
code.insert(pos, processedFile);
}
}
}
}
class Macro

View File

@ -22,10 +22,7 @@
#include <cstring>
#include <string>
#include <iostream>
#ifdef __BORLANDC__
#include <ctype.h> // isalpha, isdigit
#endif
#include <cctype>
Token::Token() :
_str(""),
@ -51,8 +48,8 @@ void Token::str(const char s[])
_str = s;
std::free(_cstr);
_cstr = strdup(s);
_isName = bool(_str[0] == '_' || isalpha(_str[0]));
_isNumber = bool(isdigit(_str[0]) != 0);
_isName = bool(_str[0] == '_' || std::isalpha(_str[0]));
_isNumber = bool(std::isdigit(_str[0]) != 0);
if (_str == "true" || _str == "false")
_isBoolean = true;
else

View File

@ -20,29 +20,17 @@
//---------------------------------------------------------------------------
#include "tokenize.h"
//---------------------------------------------------------------------------
#include "filelister.h"
#include <locale>
#include <fstream>
#include <string>
#include <cstring>
#include <iostream>
#include <sstream>
#include <list>
#include <algorithm>
#include <stdlib.h> // <- strtoul
#include <stdio.h>
#ifdef __BORLANDC__
#include <ctype.h>
#include <mem.h>
#endif
#include <cstdlib>
//---------------------------------------------------------------------------
@ -297,7 +285,7 @@ void Tokenizer::tokenize(std::istream &code, const char FileName[])
fileIndexes.push_back(FileIndex);
for (unsigned int i = 0; i < _files.size(); i++)
{
if (SameFileName(_files[i].c_str(), line.c_str()))
if (FileLister::SameFileName(_files[i].c_str(), line.c_str()))
{
// Use this index
foundOurfile = true;
@ -1528,22 +1516,6 @@ std::string Tokenizer::fileLine(const Token *tok) const
return ostr.str();
}
//---------------------------------------------------------------------------
bool Tokenizer::SameFileName(const char fname1[], const char fname2[])
{
#ifdef __linux__
return bool(strcmp(fname1, fname2) == 0);
#endif
#ifdef __GNUC__
return bool(strcasecmp(fname1, fname2) == 0);
#endif
#ifdef __BORLANDC__
return bool(stricmp(fname1, fname2) == 0);
#endif
#ifdef _MSC_VER
return bool(_stricmp(fname1, fname2) == 0);
#endif
}
//---------------------------------------------------------------------------