summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAndrei Karas <akaras@inbox.ru>2014-05-23 21:46:19 +0300
committerAndrei Karas <akaras@inbox.ru>2014-05-23 21:46:19 +0300
commit6375e293e9f456b8a204a54ad19ae775c2bb3dbd (patch)
tree4b1f71ef2b85891d0c29a4175aeb616e09acacff /src
downloadmplint-6375e293e9f456b8a204a54ad19ae775c2bb3dbd.tar.gz
mplint-6375e293e9f456b8a204a54ad19ae775c2bb3dbd.tar.bz2
mplint-6375e293e9f456b8a204a54ad19ae775c2bb3dbd.tar.xz
mplint-6375e293e9f456b8a204a54ad19ae775c2bb3dbd.zip
Initial version.
Now it have only one rule for dump all lines in file. Nothing other is implimented.
Diffstat (limited to 'src')
-rw-r--r--src/Makefile.am20
-rw-r--r--src/lintmanager.cpp148
-rw-r--r--src/lintmanager.h61
-rw-r--r--src/localconsts.h61
-rw-r--r--src/main.cpp29
-rw-r--r--src/rulebase.cpp44
-rw-r--r--src/rulebase.h68
-rw-r--r--src/rules/dump.cpp39
-rw-r--r--src/rules/dump.h38
9 files changed, 508 insertions, 0 deletions
diff --git a/src/Makefile.am b/src/Makefile.am
new file mode 100644
index 0000000..4367755
--- /dev/null
+++ b/src/Makefile.am
@@ -0,0 +1,20 @@
+AUTOMAKE_OPTIONS = subdir-objects
+
+bin_PROGRAMS = mplint
+
+mplint_CXXFLAGS = -DPKG_DATADIR=\""$(pkgdatadir)/"\" \
+ -DLOCALEDIR=\""$(localedir)"\" \
+ -Wall -Wextra
+
+mplint_SOURCES = \
+ lintmanager.cpp \
+ lintmanager.h \
+ localconsts.h \
+ main.cpp \
+ rulebase.cpp \
+ rulebase.h \
+ rules/dump.cpp \
+ rules/dump.h
+
+# set the include path found by configure
+AM_CPPFLAGS = $(all_includes)
diff --git a/src/lintmanager.cpp b/src/lintmanager.cpp
new file mode 100644
index 0000000..312ddd6
--- /dev/null
+++ b/src/lintmanager.cpp
@@ -0,0 +1,148 @@
+/*
+ * The ManaPlus Client
+ * Copyright (C) 2014 The ManaPlus Developers
+ *
+ * This file is part of The ManaPlus Client.
+ *
+ * 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
+ * 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 "lintmanager.h"
+
+#include "rulebase.h"
+
+#include "rules/dump.h"
+
+#include <dirent.h>
+#include <fstream>
+#include <regex>
+
+#include <sys/stat.h>
+
+#include "localconsts.h"
+
+LintManager::LintManager(std::string path)
+{
+ registerRules();
+ enumFiles(path);
+}
+
+void LintManager::registerRules()
+{
+ mRules.push_back(new Dump);
+}
+
+void LintManager::enumFiles(std::string path)
+{
+ path += "/";
+ struct dirent *next_file = nullptr;
+ DIR *const dir = opendir(path.c_str());
+ struct stat s;
+ std::vector<std::string> dirs;
+
+ while ((next_file = readdir(dir)))
+ {
+ const std::string file = next_file->d_name;
+ if (file != "." && file != "..")
+ {
+ char *realPath = realpath((path + file).c_str(), nullptr);
+ stat(realPath, &s);
+ if (S_ISDIR(s.st_mode))
+ dirs.push_back(path + file);
+ else
+ processFile(realPath);
+ }
+ }
+ if (dir)
+ closedir(dir);
+
+ FOR_EACH (std::vector<std::string>::const_iterator, it, dirs)
+ enumFiles(*it);
+}
+
+void LintManager::processFile(std::string fileName)
+{
+// printf("file: %s\n", fileName.c_str());
+ mFileName = fileName;
+ selectRulesForFile();
+ if (!mSelectedRules.empty())
+ {
+ readFile();
+ applyRulesToFile();
+ }
+}
+
+void LintManager::applyRulesToFile()
+{
+ FOR_EACH (std::vector<RuleBase*>::iterator, it, mRules)
+ {
+ RuleBase *const rule = *it;
+ int line = 0;
+ FOR_EACH (std::vector<std::string>::const_iterator, itStr, mFileData)
+ {
+ const std::string &str = *itStr;
+ rule->setLine(line);
+ rule->parseLine(str);
+ line ++;
+ }
+ }
+}
+
+void LintManager::readFile()
+{
+// printf("readFile: %s\n", mFileName.c_str());
+ std::ifstream file;
+ char line[3001];
+
+ mFileData.clear();
+
+ file.open(mFileName.c_str(), std::ios::in);
+ if (!file.is_open())
+ return;
+
+ while (file.getline(line, 3000))
+ mFileData.push_back(line);
+
+ if (file.is_open())
+ file.close();
+}
+
+void LintManager::selectRulesForFile()
+{
+// printf("selectRulesForFile: %s\n", mFileName.c_str());
+ mSelectedRules.clear();
+ FOR_EACH (std::vector<RuleBase*>::iterator, it, mRules)
+ {
+ RuleBase *const rule = *it;
+ if (isMatchFile(rule))
+ {
+ rule->setFile(mFileName);
+ rule->init();
+ mSelectedRules.push_back(rule);
+ }
+ }
+}
+
+bool LintManager::isMatchFile(RuleBase *const rule)
+{
+// printf("isMatchFile: %s\n", fileName.c_str());
+ const std::set<std::string> &masks = rule->getMasks();
+ FOR_EACH (std::set<std::string>::const_iterator, it, masks)
+ {
+ std::regex exp(*it);
+ if (std::regex_match (mFileName, exp))
+ return true;
+ }
+ return false;
+}
diff --git a/src/lintmanager.h b/src/lintmanager.h
new file mode 100644
index 0000000..9d7fe1f
--- /dev/null
+++ b/src/lintmanager.h
@@ -0,0 +1,61 @@
+/*
+ * The ManaPlus Client
+ * Copyright (C) 2014 The ManaPlus Developers
+ *
+ * This file is part of The ManaPlus Client.
+ *
+ * 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
+ * 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 LINTMANAGER_H
+#define LINTMANAGER_H
+
+#include <string>
+#include <vector>
+
+#include "localconsts.h"
+
+class RuleBase;
+
+class LintManager final
+{
+ public:
+ LintManager(std::string path);
+
+ protected:
+ void enumFiles(std::string path);
+
+ void processFile(std::string fileName);
+
+ bool isMatchFile(RuleBase *const rule);
+
+ void readFile();
+
+ void registerRules();
+
+ void selectRulesForFile();
+
+ void applyRulesToFile();
+
+ private:
+ std::vector<RuleBase*> mRules;
+
+ std::vector<RuleBase*> mSelectedRules;
+
+ std::vector<std::string> mFileData;
+
+ std::string mFileName;
+};
+
+#endif // LINTMANAGER_H
diff --git a/src/localconsts.h b/src/localconsts.h
new file mode 100644
index 0000000..7967c02
--- /dev/null
+++ b/src/localconsts.h
@@ -0,0 +1,61 @@
+/*
+ * The ManaPlus Client
+ * Copyright (C) 2011-2013 The ManaPlus Developers
+ *
+ * This file is part of The ManaPlus Client.
+ *
+ * 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
+ * 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/>.
+ */
+
+#if !defined(__GXX_EXPERIMENTAL_CXX0X__)
+#undef nullptr
+#define nullptr 0
+#define final
+#define override
+#define A_DELETE(func)
+#define A_DELETE_COPY(func)
+#else
+#define GCC_VERSION (__GNUC__ * 10000 \
+ + __GNUC_MINOR__ * 100 \
+ + __GNUC_PATCHLEVEL__)
+#if GCC_VERSION < 40700
+#define final
+#define override
+//#define A_DELETE
+//#define A_DELETE_COPY
+#endif
+#undef Z_NULL
+#define Z_NULL nullptr
+#define A_DELETE(func) func = delete
+#define A_DELETE_COPY(name) name(const name &) = delete; \
+ name &operator=(const name&) = delete;
+#endif
+
+#ifdef __GNUC__
+#define A_UNUSED __attribute__ ((unused))
+#define A_WARN_UNUSED __attribute__ ((warn_unused_result))
+#else
+#define A_UNUSED
+#define A_WARN_UNUSED
+#define gnu_printf printf
+#endif
+#ifdef __clang__
+#define gnu_printf printf
+#endif
+
+#define FOR_EACH(type, iter, array) for (type iter = array.begin(), \
+ iter##_end = array.end(); iter != iter##_end; ++ iter)
+
+#define FOR_EACHP(type, iter, array) for (type iter = array->begin(), \
+ iter##_end = array->end(); iter != iter##_end; ++ iter)
diff --git a/src/main.cpp b/src/main.cpp
new file mode 100644
index 0000000..88f431b
--- /dev/null
+++ b/src/main.cpp
@@ -0,0 +1,29 @@
+/*
+ * The ManaPlus Client
+ * Copyright (C) 2014 The ManaPlus Developers
+ *
+ * This file is part of The ManaPlus Client.
+ *
+ * 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
+ * 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 "lintmanager.h"
+
+#include "localconsts.h"
+
+int main(int argc A_UNUSED, char *argv[] A_UNUSED)
+{
+ LintManager lint("..");
+ return 0;
+}
diff --git a/src/rulebase.cpp b/src/rulebase.cpp
new file mode 100644
index 0000000..dfb3f1d
--- /dev/null
+++ b/src/rulebase.cpp
@@ -0,0 +1,44 @@
+/*
+ * The ManaPlus Client
+ * Copyright (C) 2014 The ManaPlus Developers
+ *
+ * This file is part of The ManaPlus Client.
+ *
+ * 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
+ * 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 "rulebase.h"
+
+#include "localconsts.h"
+
+RuleBase::RuleBase() :
+ file(),
+ line(-1)
+{
+}
+
+void RuleBase::print(const std::string &text) const
+{
+ printf("[%s:%d]: %s\n", file.c_str(), line, text.c_str());
+}
+
+void RuleBase::printRaw(const std::string &text) const
+{
+ printf("%s\n", text.c_str());
+}
+
+void RuleBase::addMask(const std::string &mask)
+{
+ mFileMasks.insert(mask);
+}
diff --git a/src/rulebase.h b/src/rulebase.h
new file mode 100644
index 0000000..7d8b0b0
--- /dev/null
+++ b/src/rulebase.h
@@ -0,0 +1,68 @@
+/*
+ * The ManaPlus Client
+ * Copyright (C) 2014 The ManaPlus Developers
+ *
+ * This file is part of The ManaPlus Client.
+ *
+ * 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
+ * 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 RULEBASE_H
+#define RULEBASE_H
+
+#include <set>
+#include <string>
+
+#include "localconsts.h"
+
+class RuleBase
+{
+ public:
+ RuleBase();
+
+ virtual ~RuleBase()
+ { }
+
+ virtual void init()
+ { }
+
+ virtual void parseLine(const std::string &data A_UNUSED)
+ { }
+
+ virtual void parseFile(const std::string &data A_UNUSED)
+ { }
+
+ const std::set<std::string> &getMasks() const
+ { return mFileMasks; }
+
+ void setFile(const std::string &file0)
+ { file = file0; }
+
+ void setLine(const int line0)
+ { line = line0; }
+
+ protected:
+ void print(const std::string &text) const;
+
+ void printRaw(const std::string &text) const;
+
+ void addMask(const std::string &mask);
+
+ std::set<std::string> mFileMasks;
+
+ std::string file;
+ int line;
+};
+
+#endif // RULEBASE_H
diff --git a/src/rules/dump.cpp b/src/rules/dump.cpp
new file mode 100644
index 0000000..163cf54
--- /dev/null
+++ b/src/rules/dump.cpp
@@ -0,0 +1,39 @@
+/*
+ * The ManaPlus Client
+ * Copyright (C) 2014 The ManaPlus Developers
+ *
+ * This file is part of The ManaPlus Client.
+ *
+ * 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
+ * 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 "rules/dump.h"
+
+#include "localconsts.h"
+
+Dump::Dump()
+{
+ addMask("(.+)[.]cpp");
+ addMask("(.+)[.]h");
+}
+
+void Dump::init()
+{
+ printRaw("Checking file: " + file);
+}
+
+void Dump::parseLine(const std::string &data)
+{
+ print(data);
+}
diff --git a/src/rules/dump.h b/src/rules/dump.h
new file mode 100644
index 0000000..6a91ef9
--- /dev/null
+++ b/src/rules/dump.h
@@ -0,0 +1,38 @@
+/*
+ * The ManaPlus Client
+ * Copyright (C) 2014 The ManaPlus Developers
+ *
+ * This file is part of The ManaPlus Client.
+ *
+ * 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
+ * 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 RULES_DUMP_H
+#define RULES_DUMP_H
+
+#include "rulebase.h"
+
+#include "localconsts.h"
+
+class Dump : public RuleBase
+{
+ public:
+ Dump();
+
+ void init();
+
+ void parseLine(const std::string &data);
+};
+
+#endif // RULES_DUMP_H