|
| 1 | +/* |
| 2 | + * Cppcheck - A tool for static C/C++ code analysis |
| 3 | + * Copyright (C) 2007-2013 Daniel Marjamäki and Cppcheck team. |
| 4 | + * |
| 5 | + * This program is free software: you can redistribute it and/or modify |
| 6 | + * it under the terms of the GNU General Public License as published by |
| 7 | + * the Free Software Foundation, either version 3 of the License, or |
| 8 | + * (at your option) any later version. |
| 9 | + * |
| 10 | + * This program is distributed in the hope that it will be useful, |
| 11 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | + * GNU General Public License for more details. |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU General Public License |
| 16 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 | + */ |
| 18 | + |
| 19 | +#include "environment.h" |
| 20 | +#include <tinyxml2.h> |
| 21 | + |
| 22 | +Environment::Environment() : allocid(0) |
| 23 | +{ |
| 24 | + while (!ismemory(++allocid)); |
| 25 | + _alloc["malloc"] = allocid; |
| 26 | + _alloc["calloc"] = allocid; |
| 27 | + _alloc["strdup"] = allocid; |
| 28 | + _alloc["strndup"] = allocid; |
| 29 | + _dealloc["free"] = allocid; |
| 30 | + |
| 31 | + while (!isresource(++allocid)); |
| 32 | + _alloc["fopen"] = allocid; |
| 33 | + _dealloc["fclose"] = allocid; |
| 34 | +} |
| 35 | + |
| 36 | +Environment::Environment(const Environment &env) : use(env.use), ignore(env.ignore), noreturn(env.noreturn), allocid(env.allocid), _alloc(env._alloc), _dealloc(env._dealloc) |
| 37 | +{ |
| 38 | + |
| 39 | +} |
| 40 | + |
| 41 | +Environment::~Environment() { } |
| 42 | + |
| 43 | +bool Environment::load(const char path[]) |
| 44 | +{ |
| 45 | + tinyxml2::XMLDocument doc; |
| 46 | + |
| 47 | + const tinyxml2::XMLError error = doc.LoadFile(path); |
| 48 | + if (error != tinyxml2::XML_NO_ERROR) |
| 49 | + return false; |
| 50 | + |
| 51 | + const tinyxml2::XMLElement * const rootnode = doc.FirstChildElement(); |
| 52 | + if (strcmp(rootnode->Value(),"def") != 0) |
| 53 | + return false; |
| 54 | + |
| 55 | + for (const tinyxml2::XMLElement *node = rootnode->FirstChildElement(); node; node = node->NextSiblingElement()) { |
| 56 | + if (strcmp(node->Value(),"memory")==0) { |
| 57 | + while (!ismemory(++allocid)); |
| 58 | + for (const tinyxml2::XMLElement *memorynode = node->FirstChildElement(); memorynode; memorynode = memorynode->NextSiblingElement()) { |
| 59 | + if (strcmp(memorynode->Value(),"alloc")==0) |
| 60 | + _alloc[node->GetText()] = allocid; |
| 61 | + else if (strcmp(memorynode->Value(),"dealloc")==0) |
| 62 | + _dealloc[node->GetText()] = allocid; |
| 63 | + else if (strcmp(node->Value(),"use")==0) |
| 64 | + use.insert(node->GetText()); |
| 65 | + else |
| 66 | + return false; |
| 67 | + } |
| 68 | + |
| 69 | + } |
| 70 | + |
| 71 | + else if (strcmp(node->Value(),"ignore")==0) |
| 72 | + ignore.insert(node->GetText()); |
| 73 | + else if (strcmp(node->Value(),"noreturn")==0) |
| 74 | + noreturn.insert(node->GetText()); |
| 75 | + else |
| 76 | + return false; |
| 77 | + } |
| 78 | + return true; |
| 79 | +} |
0 commit comments