From 3c12d23fd9471006cc11c3cbccadc6b915102fbf Mon Sep 17 00:00:00 2001 From: Kimmo Varis Date: Sat, 17 Jul 2010 17:38:36 +0300 Subject: [PATCH] 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. --- Makefile | 6 ++++- lib/errorlogger.cpp | 29 +++-------------------- lib/lib.pri | 2 ++ lib/path.cpp | 56 +++++++++++++++++++++++++++++++++++++++++++++ lib/path.h | 54 +++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 120 insertions(+), 27 deletions(-) create mode 100644 lib/path.cpp create mode 100644 lib/path.h diff --git a/Makefile b/Makefile index 53e96e9d5..3c52face0 100644 --- a/Makefile +++ b/Makefile @@ -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 diff --git a/lib/errorlogger.cpp b/lib/errorlogger.cpp index 694d600b6..64755a499 100644 --- a/lib/errorlogger.cpp +++ b/lib/errorlogger.cpp @@ -19,6 +19,7 @@ #include "errorlogger.h" #include "tokenize.h" #include "token.h" +#include "path.h" #include @@ -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); } diff --git a/lib/lib.pri b/lib/lib.pri index d708a68d5..b39292983 100644 --- a/lib/lib.pri +++ b/lib/lib.pri @@ -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 \ diff --git a/lib/path.cpp b/lib/path.cpp new file mode 100644 index 000000000..406393a0d --- /dev/null +++ b/lib/path.cpp @@ -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 . + */ + +#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); +} diff --git a/lib/path.h b/lib/path.h new file mode 100644 index 000000000..f38213112 --- /dev/null +++ b/lib/path.h @@ -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 . + */ + +#ifndef PATH_H_INCLUDED +#define PATH_H_INCLUDED + +#include + +/// @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