Add Path class for path handling routines.

In this initial commit the Path class contains two methods for
converting path separators. I want to move cppcheck path handling
to direction that we internally have paths with / separator. And
convert from/to native separators when needed.
This commit is contained in:
Kimmo Varis 2010-07-17 17:38:36 +03:00
parent 6db365e6f7
commit 3c12d23fd9
5 changed files with 120 additions and 27 deletions

View File

@ -26,6 +26,7 @@ LIBOBJ = lib/checkautovariables.o \
lib/filelister_unix.o \
lib/filelister_win32.o \
lib/mathlib.o \
lib/path.o \
lib/preprocessor.o \
lib/settings.o \
lib/token.o \
@ -124,7 +125,7 @@ lib/checkunusedfunctions.o: lib/checkunusedfunctions.cpp lib/checkunusedfunction
lib/cppcheck.o: lib/cppcheck.cpp lib/cppcheck.h lib/settings.h lib/errorlogger.h lib/checkunusedfunctions.h lib/check.h lib/token.h lib/tokenize.h lib/classinfo.h lib/preprocessor.h lib/filelister.h
$(CXX) $(CXXFLAGS) -Ilib -c -o lib/cppcheck.o lib/cppcheck.cpp
lib/errorlogger.o: lib/errorlogger.cpp lib/errorlogger.h lib/settings.h lib/tokenize.h lib/classinfo.h lib/token.h
lib/errorlogger.o: lib/errorlogger.cpp lib/errorlogger.h lib/settings.h lib/tokenize.h lib/classinfo.h lib/token.h lib/path.h
$(CXX) $(CXXFLAGS) -Ilib -c -o lib/errorlogger.o lib/errorlogger.cpp
lib/executionpath.o: lib/executionpath.cpp lib/executionpath.h lib/token.h
@ -142,6 +143,9 @@ lib/filelister_win32.o: lib/filelister_win32.cpp lib/filelister.h lib/filelister
lib/mathlib.o: lib/mathlib.cpp lib/mathlib.h lib/token.h
$(CXX) $(CXXFLAGS) -Ilib -c -o lib/mathlib.o lib/mathlib.cpp
lib/path.o: lib/path.cpp lib/path.h
$(CXX) $(CXXFLAGS) -Ilib -c -o lib/path.o lib/path.cpp
lib/preprocessor.o: lib/preprocessor.cpp lib/preprocessor.h lib/errorlogger.h lib/settings.h lib/tokenize.h lib/classinfo.h lib/token.h lib/filelister.h
$(CXX) $(CXXFLAGS) -Ilib -c -o lib/preprocessor.o lib/preprocessor.cpp

View File

@ -19,6 +19,7 @@
#include "errorlogger.h"
#include "tokenize.h"
#include "token.h"
#include "path.h"
#include <sstream>
@ -245,36 +246,12 @@ std::string ErrorLogger::ErrorMessage::FileLocation::getfile() const
pos = sep;
}
#if defined(_WIN32)
{
std::string::iterator iter = f.begin();
std::string::iterator end = f.end();
while (iter != end)
{
if (*iter == '/')
*iter = '\\';
++iter;
}
}
#endif
f = Path::toNativeSeparators(f);
return f;
}
void ErrorLogger::ErrorMessage::FileLocation::setfile(const std::string &file)
{
_file = file;
std::cout << "Setting file: " << file << std::endl;
#if defined(_WIN32)
{
std::string::iterator iter = _file.begin();
std::string::iterator end = _file.end();
while (iter != end)
{
if (*iter == '\\')
*iter = '/';
++iter;
}
}
#endif
_file = Path::fromNativeSeparators(_file);
}

View File

@ -17,6 +17,7 @@ HEADERS += $$PWD/check.h \
$$PWD/filelister_unix.h \
$$PWD/filelister_win32.h \
$$PWD/mathlib.h \
$$PWD/path.h \
$$PWD/preprocessor.h \
$$PWD/settings.h \
$$PWD/token.h \
@ -38,6 +39,7 @@ SOURCES += $$PWD/checkautovariables.cpp \
$$PWD/filelister_unix.cpp \
$$PWD/filelister_win32.cpp \
$$PWD/mathlib.cpp \
$$PWD/path.cpp \
$$PWD/preprocessor.cpp \
$$PWD/settings.cpp \
$$PWD/token.cpp \

56
lib/path.cpp Normal file
View File

@ -0,0 +1,56 @@
/*
* Cppcheck - A tool for static C/C++ code analysis
* Copyright (C) 2007-2010 Daniel Marjamäki and Cppcheck team.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "path.h"
static std::string replace_chars(const std::string &str, char from, char to)
{
std::string modified(str);
size_t pos = modified.find(from);
if (pos!= std::string::npos)
{
std::string::iterator iter = modified.begin() + pos;
std::string::iterator end = modified.end();
while (iter != end)
{
if (*iter == from)
*iter = to;
++iter;
}
}
return modified;
}
std::string Path::toNativeSeparators(const std::string &path)
{
#if defined(_WIN32)
char separ = '/';
char native = '\\';
#else
char separ = '\\';
char native = '/';
#endif
return replace_chars(path, separ, native);
}
std::string Path::fromNativeSeparators(const std::string &path)
{
char nonnative = '\\';
char internal = '/';
return replace_chars(path, nonnative, internal);
}

54
lib/path.h Normal file
View File

@ -0,0 +1,54 @@
/*
* Cppcheck - A tool for static C/C++ code analysis
* Copyright (C) 2007-2010 Daniel Marjamäki and Cppcheck team.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef PATH_H_INCLUDED
#define PATH_H_INCLUDED
#include <string>
/// @addtogroup Core
/// @{
/**
* @brief Path handling routines.
* Internally cppcheck wants to store paths with / separator which is also
* native separator for Unix-derived systems. When giving path to user
* or for other functions we convert path separators back to native type.
*/
class Path
{
public:
/**
* Convert path to use native separators.
* @param path Path string to convert.
* @return converted path.
*/
static std::string toNativeSeparators(const std::string &path);
/**
* Convert path to use internal path separators.
* @param path Path string to convert.
* @return converted path.
*/
static std::string fromNativeSeparators(const std::string &path);
};
/// @}
#endif // PATH_H_INCLUDED