Fortran SFF API to data I/O streams in C++
helper.cc
Go to the documentation of this file.
1 
38 #define FAPIDXX_HELPER_CC_VERSION \
39  "FAPIDXX_HELPER_CC V1.3"
40 
41 #include<algorithm>
42 #include<sstream>
43 #include<cstring>
44 #include <fapidxx/helper.h>
45 #include <fapidxx/wid2container.h>
46 #include <fapidxx/error.h>
47 
48 namespace fapidxx {
49 
51  std::string stringfromfstring(char *fstring, ftnlen slen)
52  {
53  std::string cxxstring(fstring, slen);
54  return(trimws_end(cxxstring));
55  } // std::string stringfromfstring(char *fstring, ftnlen slen)
56 
57  /*----------------------------------------------------------------------*/
58 
60  void fillfstring(const std::string& s, char *fstring, ftnlen slen)
61  {
62  std::string::const_iterator is=s.begin();
63  char *op=fstring;
64  for (int i=0; i<slen; ++i)
65  {
66  if (i<int(s.size()))
67  {
68  *op=*is;
69  }
70  else
71  {
72  *op=' ';
73  }
74  ++op;
75  ++is;
76  }
77  } // void fillfstring(const std::string& s, char *fstring, ftnlen slen)
78 
79  /*----------------------------------------------------------------------*/
80 
82  std::string trimws(std::string s)
83  {
84  if (s.length()>0)
85  {
86  string::size_type ib=s.find_first_not_of(" ", 0);
87  if (ib==string::npos)
88  {
89  s="";
90  }
91  else
92  {
93  string::size_type il=s.find_last_not_of(" \r", s.length());
94  string::size_type n=il>=ib ? il-ib+1 : 0;
95  if (n==0) { ib = 0; }
96  if ((ib!=0) || (n!=s.length())) { s=s.substr(ib,n); }
97  }
98  }
99  return(s);
100  } // std::string trimws(std::string s)
101 
102  /*----------------------------------------------------------------------*/
103 
105  std::string trimws_end(std::string s)
106  {
107  if (s.length()>0)
108  {
109  string::size_type il=s.find_last_not_of(" \r", s.length());
110  string::size_type n=il>=0 ? il+1 : 0;
111  if (n!=s.length()) { s=s.substr(0,n); }
112  }
113  return(s);
114  } // std::string trimws_end(std::string s)
115 
116  /*----------------------------------------------------------------------*/
117 
118  float maketanf(const libtime::TAbsoluteTime& time)
119  {
120  libtime::TAbsoluteTime theday(time.year(), time.month(), time.day());
121  libtime::TRelativeTime inday=time-theday;
122  return(float(libtime::time2double(inday)));
123  } // float maketanf(const libtime::TAbsoluteTime& time)
124 
125  /*----------------------------------------------------------------------*/
126 
127  libtime::TAbsoluteTime SRCEdate(char *date, char* time,
128  ftnlen date_len, ftnlen time_len)
129  {
130  std::string datestring=stringfromfstring(date, date_len);
131  std::string timestring=stringfromfstring(time, time_len);
132  std::string fulldate("");
133  fulldate+=datestring.substr(0,2);
134  fulldate+="/";
135  fulldate+=datestring.substr(2,2);
136  fulldate+="/";
137  fulldate+=datestring.substr(4,2);
138  fulldate+=" ";
139  fulldate+=timestring.substr(0,2);
140  fulldate+=":";
141  fulldate+=timestring.substr(2,2);
142  fulldate+=":";
143  fulldate+=timestring.substr(4,6);
144  libtime::TAbsoluteTime srcedate(fulldate);
145  return(srcedate);
146  } // libtime::TAbsoluteTime SRCEdate
147 
148  /*----------------------------------------------------------------------*/
149 
150  sff::FREE freeblock(integer *nline, char *lines, ftnlen lines_len)
151  {
152  sff::FREE free;
153  char* pfree=lines;
154  for (int ifline=0; ifline<*nline; ++ifline)
155  {
156  free.append(stringfromfstring(pfree, lines_len));
157  pfree += lines_len;
158  }
159  return(free);
160  } // sff::FREE freeblock(integer *nline, char *lines, ftnlen lines_len)
161 
162  /*----------------------------------------------------------------------*/
163 
164  void freeblock(const ::sff::FREE& free,
165  integer *nline, char *lines, integer *lindim,
166  integer *lenmax, ftnlen lines_len)
167  {
168  int ifline=0;
169  *lenmax=0;
170  typedef sff::FREE::Tlines Tlines;
171  Tlines::const_iterator I=free.lines.begin();
172  char* pfree=lines;
173  while ((I!=free.lines.end()) && (ifline< *lindim))
174  {
175  int linelen=static_cast<int>(I->length());
176  *lenmax= *lenmax>linelen ? *lenmax : linelen;
177  fillfstring(*I, pfree, lines_len);
178  ++I;
179  ++ifline;
180  pfree += lines_len;
181  }
182  *nline=static_cast<integer>(ifline);
183  } // freeblock(const ::sff::FREE& free,
184 
185 } // namespace fapidxx
186 
187 /* ----- END OF helper.cc ----- */
Definition: error.cc:44
sff::FREE freeblock(integer *nline, char *lines, ftnlen lines_len)
create C++ FREE block from Fortran FREE lines
Definition: helper.cc:150
long int integer
Fortran integer type.
Definition: fapidsff.h:66
container for WID2 data (prototypes)
libtime::TAbsoluteTime SRCEdate(char *date, char *time, ftnlen date_len, ftnlen time_len)
create appropriate time string for SRCE line
Definition: helper.cc:127
void fillfstring(const std::string &s, char *fstring, ftnlen slen)
fill a Fortran string with a C++ string
Definition: helper.cc:60
exceptions and error handling macros (prototypes)
std::string trimws(std::string s)
remove whitespace at begin and end of string
Definition: helper.cc:82
std::string trimws_end(std::string s)
remove whitespace at end of string
Definition: helper.cc:105
long int ftnlen
Fortran string size type.
Definition: fapidsff.h:70
std::string stringfromfstring(char *fstring, ftnlen slen)
create a C++ string from a Fortran string
Definition: helper.cc:51
float maketanf(const libtime::TAbsoluteTime &time)
create the tanf value
Definition: helper.cc:118
some helper functions (prototypes)