All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
FileTool.cpp
Go to the documentation of this file.
1 /*
2  * FileTool.cpp
3  *
4  * This file is part of the HausmiSEP project
5  *
6  * Copyright (C) 2012, 2013 Marco Alvarado (malvcr@gmail.com)
7  *
8  * This program is free software: you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation, either version 3 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program. If not, see <http://www.gnu.org/licenses/>.
20  */
21 
22 #include <HSEP/FileTool.h>
23 #include <HSEP/StringTool.h>
24 
25 #include <string.h>
26 
27 namespace HSEP {
28 
32  struct ContextST {
33  int aResult;
34  ostream& aOutputRef;
35 
36  ContextST(ostream& pOutputRef) : aOutputRef(pOutputRef) {
37  aResult = 0;
38  } // constructor
39 
40  }; // ContextST struct
41 
42  int FileTool::store (ostream& pOutputRef, StringDictionary& pDictRef) {
43 
44  int vResult = 0;
45  ContextST vContext(pOutputRef);
46 
47  pDictRef.forEach([&](string& xKeyRef, string* xValuePtr) {
48  pOutputRef << xKeyRef << "=" << *xValuePtr << endl;
49  vResult++;
50  });
51 
52  return vResult;
53 
54  } // FileToo::store
55 
56  bool FileTool::retrieve(StringDictionary& pDictRef, istream& pInputRef ) {
57 
58  bool vResult = false;
59  pDictRef.clear();
60 
61  string vLine;
62 
63  while (!pInputRef.eof()) {
64 
65  getline(pInputRef,vLine);
66 
67  StringTool::drop(vLine,"#");
68  StringTool::ltrim(vLine);
69 
70  if (vLine.size()>0) {
71 
72  size_t vEqPos = vLine.find("=");
73 
74  if (string::npos != vEqPos) {
75 
76  string vKey = vLine.substr(0,vEqPos);
77  string vValue = vLine.substr(vEqPos+1);
78 
79  pDictRef.put(vKey,vValue);
80  vResult = true;
81  }
82  }
83  }
84 
85  return vResult;
86 
87  } // FileTool::retrieve
88 
89  int FileTool::store (ostream& pOutputRef, StringArray& pArrayRef) {
90 
91  int vResult = 0;
92 
93  pArrayRef.forEach([&](string* xValuePtr) {
94  vResult++;
95  pOutputRef << *xValuePtr << endl;
96  });
97 
98  return vResult;
99 
100  } // FileTool::store
101 
102  bool FileTool::retrieve(StringArray& pArrayRef, istream& pInputRef ) {
103 
104  bool vResult = false;
105  pArrayRef.clear();
106 
107  string vLine;
108 
109  while (!pInputRef.eof()) {
110 
111  getline(pInputRef,vLine);
112 
113  StringTool::drop(vLine,"#");
114  StringTool::ltrim(vLine);
115 
116  if (vLine.size()>0) {
117  pArrayRef.push(vLine);
118  }
119  vResult = true;
120  }
121 
122  return vResult;
123 
124  } // FileTool::retrieve
125 
126 } // HSEP namespace
127 
128 
129