|
INET Framework for OMNeT++/OMNEST
|
00001 // 00002 // Copyright (C) 2005 Andras Varga 00003 // 00004 // This program is free software; you can redistribute it and/or 00005 // modify it under the terms of the GNU General Public License 00006 // as published by the Free Software Foundation; either version 2 00007 // of the License, or (at your option) any later version. 00008 // 00009 // This program is distributed in the hope that it will be useful, 00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00012 // GNU General Public License for more details. 00013 // 00014 // You should have received a copy of the GNU General Public License 00015 // along with this program; if not, see <http://www.gnu.org/licenses/>. 00016 // 00017 00018 #include <fstream> 00019 #include <sstream> 00020 #include "BonnMotionFileCache.h" 00021 00022 00023 const BonnMotionFile::Line *BonnMotionFile::getLine(int nodeId) const 00024 { 00025 LineList::const_iterator it = lines.begin(); 00026 for (int i=0; i<nodeId && it!=lines.end(); i++) it++; 00027 return (it==lines.end()) ? NULL : &(*it); 00028 } 00029 00030 00031 BonnMotionFileCache *BonnMotionFileCache::inst; 00032 00033 BonnMotionFileCache *BonnMotionFileCache::getInstance() 00034 { 00035 if (!inst) 00036 inst = new BonnMotionFileCache; 00037 return inst; 00038 } 00039 00040 void BonnMotionFileCache::deleteInstance() 00041 { 00042 if (inst) 00043 { 00044 delete inst; 00045 inst = NULL; 00046 } 00047 } 00048 00049 const BonnMotionFile *BonnMotionFileCache::getFile(const char *filename) 00050 { 00051 // if found, return it from cache 00052 BMFileMap::iterator it = cache.find(std::string(filename)); 00053 if (it!=cache.end()) 00054 return &(it->second); 00055 00056 // load and store in cache 00057 BonnMotionFile& bmFile = cache[filename]; 00058 parseFile(filename, bmFile); 00059 return &bmFile; 00060 } 00061 00062 void BonnMotionFileCache::parseFile(const char *filename, BonnMotionFile& bmFile) 00063 { 00064 std::ifstream in(filename, std::ios::in); 00065 if (in.fail()) 00066 opp_error("Cannot open file '%s'",filename); 00067 00068 std::string line; 00069 while (std::getline(in, line)) 00070 { 00071 bmFile.lines.push_back(BonnMotionFile::Line()); 00072 BonnMotionFile::Line& vec = bmFile.lines.back(); 00073 00074 std::stringstream linestream(line); 00075 double d; 00076 while (linestream >> d) 00077 vec.push_back(d); 00078 } 00079 in.close(); 00080 } 00081 00082 00083