File: wputil.h

package info (click to toggle)
vbrfix 0.24%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, buster, stretch
  • size: 2,576 kB
  • ctags: 285
  • sloc: sh: 8,089; perl: 2,029; cpp: 834; makefile: 33
file content (268 lines) | stat: -rw-r--r-- 7,104 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
/***************************************************************************
                          wputil.h  -  description
                             -------------------
    begin                : Thu Apr 17 2003
    copyright            : (C) 2003 by Eric Benson
    email                : eric_a_benson@yahoo.com
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   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 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/
// Eric Benson altered to wputilqt.h & wputilmfc.h
// to produce this version using only C++ calls not needing MFC or QT
// Based on code by william Pye
using namespace std;

#include <stdio.h>
#include <string>
#include <fstream>

inline string trim_right(const string& source, const string& t = " \t")
{
  string str = source;
  return str.erase(str.find_last_not_of(t) + 1);
}

inline string trim_left(const string& source, const string& t = " \t")
{
  string str = source;
  return str.erase(0 ,source.find_first_not_of(t));
}

inline string string_trim(const string& source, const string& t = " \t")
{
  string str = source;
  return trim_left(trim_right(str ,t), t);
}

inline string left(const string& source, unsigned int len)
{
  return source.substr(0, len);
}

inline string right(const string& source, unsigned int len)
{
  return source.substr(source.length() - len, len);
}

class wps{
  private:
  string str;
  public:
    wps(string s){
      str=s;
    }
    wps(const char *c){
      if(c!=NULL)str=c;
    }
    wps(bool b){
      if(b) str="true";else str="false";
    }
    wps operator&(wps add){
      return wps(str+add.str);
    }
    wps operator&(int i){
      char tmp[100];
      sprintf(tmp,"%d",i);
      return wps(str+tmp);
    }
    wps operator=(wps &equal){
      wps fred(equal.str);
      return fred;
    }
    wps operator=(const char *c){
      wps fred(c);
      return fred;
    }
    const char* getStr(){
      return str.c_str();
    }
    int length(){
      return str.length();
    }
    wps trim(){
      return wps(string_trim(str));
    }
    bool isTrue(){
      if(str=="true" || str=="TRUE" || str=="True" || str=="T" || str=="Y" || str=="y" || str=="t"){
        return true;
      }
      else return false;
    }
};
class wfile{
private:
  fstream fil;
  bool rmode; //rmode=true -> read rmode=false -> write
  bool opened;
public:
  wfile(){
    opened=false;
    rmode=true;
  }
  ~wfile(){
    closeFile();
  }
  void openFile(const char* name,bool read){
    if(opened)closeFile();
    rmode=read;ios_base::openmode flags;
    if(rmode) {
      flags=ios::in|ios::binary;
    } else {
      flags=ios::out|ios::binary;
    }
    fil.open(name, flags);
    if (!fil.fail()){
	opened=true;
    }
  }
  void closeFile(){
    if (opened) {
      fil.close();
    }
    opened=false;
  }
  unsigned long getLength(){
    if (rmode) {
      streampos here = fil.tellg();
      fil.seekg(0, ios::beg);
      streampos start = fil.tellg();
      fil.seekg(0, ios::end);
      streampos finish = fil.tellg();
      fil.seekg(here, ios::beg);
      return (finish - start);
    } else {
      streampos here = fil.tellp();
      fil.seekp(0, ios::beg);
      streampos start = fil.tellp();
      fil.seekp(0, ios::end);
      streampos finish = fil.tellp();
      fil.seekp(here, ios::beg);
      return (finish - start);
    }
  }
  void fWrite(const void* ptr, unsigned int size){
    if(!rmode)fil.write((char*)ptr,size);
    if (fil.fail()) abort();
  }
  void fRead(void* ptr,unsigned int size){
    if(rmode)fil.read((char*)ptr,size);
    if (fil.fail()) fil.clear();
  }
  void readAndReturn(void* ptr,unsigned long pos, unsigned int size){
    unsigned long cpos=getpos();
    setpos(pos);
    fRead((char*)ptr,size);
    setpos(cpos);
  }
  bool isopen(){
    return opened;
  }
  unsigned long getpos(){
    unsigned long pos;
    if (!opened)return 0;
    if (rmode) {
      pos = fil.tellg();
    } else {
      pos = fil.tellp();
    }
    if (fil.fail()) fil.clear();
    return pos;
  }
  void setpos(unsigned long pos){
    if (!opened)return;
    if (rmode) {
      fil.seekg(pos, ios::beg);
    } else {
      fil.seekp(pos, ios::beg);
    }
    if (fil.fail())fil.clear();
  }
  static bool copyFile(const char *input,const char *output,bool dontreplace){
    ifstream in(input, ios::in|ios::binary);
    if(in.fail())return false;
    if(dontreplace){
      // Portable test for existing file
      ifstream test(output, ios::in|ios::binary);
      if(!test.fail()){
	in.close();
	test.close();
	return false;
      }
    }
    ofstream out(output, ios::out|ios::binary|ios::trunc);
    if(out.fail())return false;
    // fast portable file copy
    return out << in.rdbuf();
  }
  static wps loadSetting(const char *file,wps setting,wps defaultValue){
    ifstream in(file, ios::in);
    if(in.fail())return defaultValue;
    while(!in.eof()){
      string line;
      getline(in,line);
      string l=string_trim(line);
      int i=l.find("=");
      if(i<1 || left(l,2)=="//" || left(l,1)=="#" || left(l,1)==";"){//commented line
      }
      else
      {
        if(string_trim(left(l,i))==setting.getStr()){
          in.close();return wps(string_trim(right(l,l.length()-i-1)));
        }
      }
    }
    in.close();
    return defaultValue;
  }
  static void saveSetting(const char* file,wps setting,wps value){
    ifstream in(file);ofstream out("tmp.tmp");
    if(in.fail()){
      ofstream tmpout(file, ios::out);
      if(!tmpout.fail()){
        tmpout <<"#[" << file << "] CONFIG FILE\n";
        tmpout.close();
	in.open(file);
      }
    }
    if(in.fail())return;
    if(out.fail()){in.close();return;}
    bool found=false;
    while(!in.eof()){
      string line;
      getline(in, line);
      string l=string_trim(line);
      int i=l.find("=");
      if(i<1 || left(l,2)=="//" || left(l,1)=="#" || left(l,1)==";"){//commented line
        out << line << "\n";
      }else{
        if(string_trim(left(l,i))==setting.getStr()){
          if(found==false){//otherwise delete the line
            found=true;out << setting.getStr() << "=" << value.getStr() << "\n";
          }
        }
        else{
          out << line << "\n";
        }
      }
    }
    if(found==false){
      out << setting.getStr() << " = " << value.getStr() << "\n";
    }
    in.close();
    out.close();
    //copy tmp.tmp to config file
    copyFile("tmp.tmp",file,false);
    remove("tmp.tmp");
  }
  static bool remove(const char* fileName){
    int status = ::remove(fileName);
    return status == 0;
  }
};