GnuCashew ~ GnuCash Enabled Web
GCW
Core.cpp
Go to the documentation of this file.
1 #line 2 "src/Glb/Core.cpp"
2 
3 #include <sstream>
4 #include <algorithm>
5 
6 #include <Wt/WModelIndex.h>
7 #include <Wt/WDateTime.h>
8 #include <Wt/WLocalDateTime.h>
9 
10 #include "../3rd/guid.hpp"
11 #include "gcwglobal.h"
12 #include "Core.h"
13 
14 static
15 std::vector<std::string> &
16 split( const std::string & s, char delim, std::vector<std::string> & elems )
17 {
18  std::stringstream ss( s );
19  std::string item;
20  while( std::getline( ss, item, delim ) )
21  {
22  elems.push_back( item );
23  }
24  return elems;
25 }
26 
27 
28 std::vector<std::string>
30 split( const std::string & s, char delim )
31 {
32  std::vector<std::string> elems;
33  ::split( s, delim, elems );
34  return elems;
35 }
36 
37 const char* GCW::Core::trim_ws = " \t\n\r\f\v";
38 
39 // trim from end of string (right)
40 std::string &
42 rtrim( std::string & s, const char* t )
43 {
44  s.erase( s.find_last_not_of(t) + 1 );
45  return s;
46 }
47 
48 // trim from beginning of string (left)
49 std::string &
51 ltrim( std::string & s, const char* t )
52 {
53  s.erase( 0, s.find_first_not_of(t) );
54  return s;
55 }
56 
57 // trim from both ends of string (left & right)
58 std::string &
60 trim( std::string & s, const char* t )
61 {
62  return ltrim( rtrim(s, t), t );
63 }
64 
65 std::string
67 toupper( const std::string & s )
68 {
69  std::string retVal = s;
70 
71  std::transform( retVal.begin(), retVal.end(), retVal.begin(), ::toupper );
72 
73  return retVal;
74 }
75 
76 std::string
78 tolower( const std::string & s )
79 {
80  std::string retVal = s;
81 
82  std::transform( retVal.begin(), retVal.end(), retVal.begin(), ::tolower );
83 
84  return retVal;
85 }
86 
87 /*
88 ** This will iterate a single a WTreeView and fill
89 ** a vector of every node which is the .last-expanded.
90 ** node of every branch.
91 **
92 */
93 static
94 bool
95 iterate( Wt::Json::Array & _jary, Wt::WModelIndex _parent )
96 {
97 #ifdef NEVER
98  /*
99  ** If this _parent node is not expanded, then we're basically done.
100  **
101  */
102  if( !view()-> isExpanded( _parent ) )
103  return false;
104 
105  /*
106  ** This _parent node is expanded, so loop through all the
107  ** child nodes checking if any of them are expanded.
108  **
109  */
110  bool expanded = false;
111  for( int row=0; row< view()-> model()-> rowCount( _parent ); row++ )
112  expanded |= iterate( _jary, view()-> model()-> index( row, 0, _parent ) );
113 
114  /*
115  ** None of the child nodes are expanded, so record this _parent
116  ** node as the 'last' node in the tree
117  **
118  */
119  if( !expanded )
120  {
121  /*
122  ** The true root node is not associated with an actual account,
123  ** it is simply the invisibleRoot of the tree itself, and only
124  ** contains the set of first-root nodes that actually get
125  ** displayed. So, there is no User data in this one, don't record it.
126  **
127  */
128  auto accountGuid = Wt::asString( _parent.data( Wt::ItemDataRole::User ) );
129  if( accountGuid != "" )
130  _jary.push_back( accountGuid );
131 
132  } // endif( !expanded )
133 
134  /*
135  ** Something is expanded. Either we are expanded, or
136  ** one of the sub-nodes are expanded, so return that 'someone' is
137  ** expanded.
138  **
139  */
140 #endif
141 
142  return true;
143 
144 } // endvoid iterate( Wt::WModelIndex _index ) const
145 
146 Wt::Json::Object
148 toJson( Wt::WTreeView * _view )
149 {
150  Wt::Json::Object jobj;
151 
152 #ifdef NEVER
153  jobj["selected"] = Wt::WString( selectedAccount() );
154 
155  for( int col=0; col< 7; col++ )
156  jobj[ Wt::WString("cw{1}").arg( col ).toUTF8() ] = Wt::WString( view()-> columnWidth( col ).cssText() );
157 
158  Wt::Json::Array jary;
159  iterate( jary );
160  jobj["expanded"] = jary;
161 
162 #endif
163 
164  return jobj;
165 
166 }
167 
168 auto
170 hexDump( const std::string & string, int start, int end )-> std::string
171 {
172  std::stringstream rv;
173 
174  char buffer[100];
175  for( unsigned int i=0; i<string.length(); i += 16 )
176  {
177  std::string adrLine;
178  std::string hexLine;
179  std::string ascLine;
180 
181  sprintf( buffer, "%04x: ", i );
182  adrLine = std::string(buffer);
183 
184  for( int j=0; j<16; j++ )
185  {
186  if( i+j < string.length() )
187  {
188  sprintf( buffer, "%02x ", (string.at(i+j) & 0xff) );
189  hexLine += std::string(buffer);
190 
191  if( std::isprint( string.at(i+j) ) )
192  {
193  sprintf( buffer, "%c", string.at(i+j) );
194  ascLine += std::string(buffer);
195  }
196  else
197  {
198  ascLine += ".";
199  }
200  }
201  else
202  {
203  hexLine += "xx ";
204  ascLine += ".";
205  }
206 
207  } // endfor( int j=0; j<16; j++ )
208 
209  bool showline = false;
210  if( start == -1 && end == -1 )
211  showline = true;
212 
213  else
214  if( (start > -1 && i >= start)
215  && (end > -1 && i <= end)
216  )
217  showline = true;
218 
219  if( showline )
220  rv
221  << adrLine
222  << hexLine
223  << ascLine
224  << std::endl
225  ;
226 
227  } // endfor( int i=0; i<string.length(); i += 16 )
228 
229  return rv.str();
230 
231 } // endstd::string GCW::hexDump( const std::string & string )
232 
233 auto
235 newGuid()-> std::string
236 {
237  std::string g = xg::newGuid();
238 
239  std::string retVal;
240  for( auto c : g )
241  if( c != '-' )
242  retVal.push_back( c );
243 
244  return retVal;
245 
246 } // endnewGuid()-> std::string
247 
248 
249 /*
250 ** note about this date time:
251 ** WLocalDateTime will produce the local-time
252 ** WDateTime will product a GMT time
253 **
254 ** This was tested, and gnucash seems to want the GMT time-stamp
255 ** rather than the local time (makes sense really)
256 **
257 */
258 auto
260 currentDateTime()-> Wt::WDateTime
261 {
262  return
263 // Wt::WLocalDateTime::currentDateTime().toString( GCW_DATE_FORMAT ).toUTF8();
265 
266 } // endcurrentDateTime()-> std::string
267 
268 
269 /*
270 ** note about this date time:
271 ** WLocalDateTime will produce the local-time
272 ** WDateTime will product a GMT time
273 **
274 ** This was tested, and gnucash seems to want the GMT time-stamp
275 ** rather than the local time (makes sense really)
276 **
277 */
278 auto
280 currentDateTimeStorageString()-> std::string
281 {
282  return
283  currentDateTime().toString( GCW_DATE_FORMAT_STORAGE ).toUTF8();
284 
285 } // endcurrentDateTime()-> std::string
286 
287 
288 
static std::vector< std::string > & split(const std::string &s, char delim, std::vector< std::string > &elems)
Definition: Core.cpp:16
static bool iterate(Wt::Json::Array &_jary, Wt::WModelIndex _parent)
Definition: Core.cpp:95
#define GCW_DATE_FORMAT_STORAGE
Definition: gcwglobal.h:12
const char * trim_ws
Definition: Core.cpp:37
std::string toupper(const std::string &s)
Definition: Core.cpp:67
auto currentDateTime() -> Wt::WDateTime
Current Date/Time.
Definition: Core.cpp:260
std::string tolower(const std::string &s)
Definition: Core.cpp:78
std::vector< std::string > split(const std::string &value, char delim)
Definition: Core.cpp:30
std::string & trim(std::string &s, const char *t=trim_ws)
Definition: Core.cpp:60
std::string & ltrim(std::string &s, const char *t=trim_ws)
Definition: Core.cpp:51
std::string hexDump(const std::string &data, int start=-1, int end=-1)
Definition: Core.cpp:170
std::string & rtrim(std::string &s, const char *t=trim_ws)
Definition: Core.cpp:42
Wt::Json::Object toJson(Wt::WTreeView *_view)
Definition: Core.cpp:148
auto newGuid() -> std::string
Generate new GUID string value.
Definition: Core.cpp:235
auto currentDateTimeStorageString() -> std::string
Definition: Core.cpp:280
std::string asString(Status _status)
Get Status as String.
Definition: Status.cpp:7