source: branches/OptimisedGrowerTesting/src/Utils/string_related.cc @ 1441

Last change on this file since 1441 was 1242, checked in by MatthewWhiting, 11 years ago

Tickets #193 & #195 - Implementing channel flagging. Still a lot of commented-out code, plus the MW code is still present (although not used anywhere). Also making use of the new plotting classes.

File size: 4.9 KB
Line 
1// -----------------------------------------------------------------------
2// string_related.cc: General utility functions for manipulating strings
3//                    and input flags/parameters
4// -----------------------------------------------------------------------
5// Copyright (C) 2006, Matthew Whiting, ATNF
6//
7// This program is free software; you can redistribute it and/or modify it
8// under the terms of the GNU General Public License as published by the
9// Free Software Foundation; either version 2 of the License, or (at your
10// option) any later version.
11//
12// Duchamp is distributed in the hope that it will be useful, but WITHOUT
13// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15// for more details.
16//
17// You should have received a copy of the GNU General Public License
18// along with Duchamp; if not, write to the Free Software Foundation,
19// Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA
20//
21// Correspondence concerning Duchamp may be directed to:
22//    Internet email: Matthew.Whiting [at] atnf.csiro.au
23//    Postal address: Dr. Matthew Whiting
24//                    Australia Telescope National Facility, CSIRO
25//                    PO Box 76
26//                    Epping NSW 1710
27//                    AUSTRALIA
28// -----------------------------------------------------------------------
29#include <iostream>
30#include <fstream>
31#include <sstream>
32#include <iomanip>
33#include <string>
34#include <duchamp/Utils/utils.hh>
35
36
37std::string makelower( std::string s )
38{
39  // "borrowed" from Matt Howlett's 'fred'
40  std::string out = "";
41  for( size_t i=0; i<s.size(); ++i ) {
42    out += tolower(s[i]);
43  }
44  return out;
45}
46
47std::string stringize(bool b)
48{
49  /// Convert a bool variable to the textual equivalent.
50  /// \return A std::string with the english equivalent of the bool.
51
52  std::string output;
53  if(b) output="true";
54  else output="false";
55  return output;
56}
57
58bool boolify( std::string s )
59{
60  ///  Convert a std::string to a bool variable:
61  ///  "1" and "true" get converted to true;
62  ///  "0" and "false" (and anything else) get converted to false.
63  /// \return The bool equivalent of the string.
64
65  if((s=="1") || (makelower(s)=="true")) return true;
66  else if((s=="0") || (makelower(s)=="false")) return false;
67  else return false;
68}
69
70std::string readSval(std::stringstream& ss)
71{
72  std::string val;
73  ss >> val;
74  return val;
75}
76
77std::string readFilename(std::stringstream& ss)
78{
79  std::string val;
80  getline(ss,val);
81  return removeLeadingBlanks(val);
82}
83
84bool readFlag(std::stringstream& ss)
85{
86  std::string val;
87  ss >> val;
88  return boolify(val);
89}
90
91float readFval(std::stringstream& ss)
92{
93  float val;
94  ss >> val;
95  return val;
96}
97
98int readIval(std::stringstream& ss)
99{
100  int val;
101  ss >> val;
102  return val;
103}
104
105std::string removeLeadingBlanks(std::string s)
106{
107  /**
108   * All blank spaces from the start of the string to the first
109   * non-blank-space character are deleted.
110   */
111  int i=0;
112  while(isspace(s[i])){
113    i++;
114  }
115  std::string newstring="";
116  for(unsigned int j=i;j<s.size();j++) newstring += s[j];
117  return newstring;
118}
119//------------------------------------------------------------------------
120
121std::string deblank(std::string s)
122{
123  /**
124   * All blank spaces from the start of the string to the first
125   * non-blank-space character, and from the last non-blank-space
126   * character to the end are deleted.
127   */
128  int beg=0;
129  while(isspace(s[beg])){
130    beg++;
131  }
132  int end=s.size()-1;
133  while(isspace(s[end])){
134    end--;
135  }
136  std::string newstring;
137  for(int j=beg;j<=end;j++) newstring += s[j];
138  return newstring;
139}
140
141void writeStringToBinaryFile(std::ofstream &outfile, std::string str)
142{
143  size_t size=str.size();
144  outfile.write(reinterpret_cast<const char*>(&size), sizeof size);
145  outfile.write(str.c_str(), sizeof(char) * size);
146}
147
148std::string readStringFromBinaryFile(std::ifstream &infile)
149{
150  size_t size;
151  infile.read(reinterpret_cast<char*>(&size), sizeof size);
152  char *cstr = new char[size];
153  infile.read(cstr, sizeof(char) * size);
154  std::string str(cstr);
155  str = str.substr(0,size); // don't know why this is necessary - if left out, sometimes get a string returned that is too long...
156  delete cstr;
157  return str;
158}
159
160std::vector<int> selectionToIntVec(std::string &str)
161{
162    ///  Converts a comma-separated list of items & ranges to a vector
163    /// list of integers. For instance, the string 1-4,6,9-11 will be
164    /// converted to the vector [1,2,3,4,6,9,10,11]
165    /// \return An STL integer vector
166
167    std::stringstream ss1;
168    std::string tmp;
169    std::vector<int> tmplist;
170    ss1.str(str);
171    while(!ss1.eof()){
172      getline(ss1,tmp,',');
173      for(size_t i=0;i<tmp.size();i++) if(tmp[i]=='-') tmp[i]=' ';
174      int a,b;
175      std::stringstream ss2;
176      ss2.str(tmp);
177      ss2 >>a;
178      if(!ss2.eof()) ss2 >> b;
179      else b=a;
180      for(int n=a;n<=b;n++){
181        tmplist.push_back(n);
182      }     
183    }
184    return tmplist;
185
186}
187
Note: See TracBrowser for help on using the repository browser.