UltraScan III
us_util.cpp
Go to the documentation of this file.
1 #include "us_util.h"
3 
4 // Get next token in a string, as defined by a separator; update for next pass
5 QString US_Util::getToken( QString& s, const QString& separator )
6 {
7  QString token;
8 
9  // Find position of 1st delimeter
10  int pos = s.indexOf( separator, 0 );
11 
12  if ( pos < 0 ) // No delimeter
13  {
14  token = s;
15  s = "";
16  return token;
17  }
18 
19  while ( pos == 0 ) // Strip leading delimiters
20  {
21  s.remove( 0, 1 ); // Remove 1st character
22  pos = s.indexOf( separator, 0 );
23 
24  if ( pos < 0 ) // No delimeter
25  {
26  token = s;
27  s = "";
28  return token;
29  }
30  }
31 
32  token = s.left( pos );
33  s.remove( 0, pos + 1 ); // Remove token plus delimeter
34  return token;
35 }
36 
37 // Generate a new GUID value and return it in QString form
38 QString US_Util::new_guid( void )
39 {
40  return QUuid::createUuid().toString().mid( 1, 36 );
41 }
42 
43 // Calculate the md5hash and size of a named file (full path)
44 QString US_Util::md5sum_file( QString filename )
45 {
46  QFile f( filename );
47 
48  // If unable to open the file, return "hash size" of "0 0"
49  if ( ! f.open( QIODevice::ReadOnly ) )
50  return "0 0";
51 
52  // Otherwise, get the full contents of the file into a QByteArray
53  QByteArray data = f.readAll();
54  f.close();
55 
56  // Get the md5 hash, convert to hex, get file size; then format "hash size"
57  QString hashandsize =
58  QString( QCryptographicHash::hash( data, QCryptographicHash::Md5 )
59  .toHex() ) + " " + QString::number( QFileInfo( filename ).size() );
60 
61  // Return the "hash size" string
62  return hashandsize;
63 }
64 
65 // Convert DateTime string to unambiguous UTC form ("yyyy-mm-dd HH:MM:SS UTC")
66 QString US_Util::toUTCDatetimeText( QString dttext, bool knownUTC )
67 {
68  QString utctext = dttext;
69  int ixLT = dttext.length() - 2; // expected last 'T'
70  int ixT = dttext.indexOf( "T" ); // index of first 'T'
71  bool haveT = ( ixT > 0 && ixT != ixLT ); // have lone 'T'
72  bool haveUTC = dttext.endsWith( "UTC" ); // have "UTC" on end
73 
74  if ( knownUTC )
75  { // If known to be UTC, just insure no single 'T' and " UTC" suffix
76 
77  if ( haveT )
78  { // If single 'T' between date and time, replace with ' '
79  utctext.replace( ixT, 1, " " );
80  }
81 
82  if ( !haveUTC )
83  { // If no " UTC" suffix, append it
84  utctext.append( " UTC" );
85  }
86  }
87 
88  else
89  { // If we don't know that it's UTC, convert when " UTC" is missing
90 
91  if ( !haveUTC )
92  { // If no " UTC", convert to UTC and append " UTC"
93  QDateTime dt;
94 
95  if ( dttext.indexOf( "-" ) == 4 )
96  { // If already in ISO form, get UTC DateTime from string in ISO form
97  dt = QDateTime::fromString( dttext, Qt::ISODate ).toUTC();
98  }
99 
100  else
101  { // If in unknown form, get UTC DateTime assuming default form
102  dt = QDateTime::fromString( dttext ).toUTC();
103  }
104 
105  // New text version in ISO form
106  utctext = dt.toString( Qt::ISODate );
107 
108  // Re-determine whether we have single 'T'
109  haveT = ( utctext.indexOf( "T" ) > 0 );
110 
111  // Append the "UTC"
112  utctext.append( " UTC" );
113  }
114 
115  if ( haveT )
116  { // If single 'T' between date and time, replace with ' '
117  utctext.replace( ixT, 1, " " );
118  }
119  }
120 
121  return utctext;
122 }
123 
124 // Ensure DateTime text is in ISO form (ISO, UTC, or unknown on input )
125 QString US_Util::toISODatetimeText( QString dttext )
126 {
127  QString isotext = dttext; // default assumes already in ISO form
128 
129  if ( dttext.contains( " UTC" ) )
130  { // If UTC form on input, convert by removing " UTC" and adding 'T'
131  isotext.replace( " UTC", "" ).replace( dttext.indexOf( " " ), 1, "T" );
132  }
133 
134  else if ( dttext.indexOf( "-" ) != 4 )
135  { // If not ISO (unknown), convert by back-and-forth from DateTime
136  isotext = QDateTime::fromString( dttext ).toString( Qt::ISODate );
137  // Otherwise, we must already have ISO form
138  }
139 
140  return isotext;
141 }
142 
143 // Convert a binary uuid to a QString
144 QString US_Util::uuid_unparse( unsigned char* uu )
145 {
146  return QString().sprintf(
147  "%02hhx%02hhx%02hhx%02hhx-%02hhx%02hhx-%02hhx%02hhx-%02hhx%02hhx-"
148  "%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx",
149  uu[ 0 ], uu[ 1 ], uu[ 2 ], uu[ 3 ], uu[ 4 ], uu[ 5 ],
150  uu[ 6 ], uu[ 7 ], uu[ 8 ], uu[ 9 ], uu[ 10 ], uu[ 11 ],
151  uu[ 12 ], uu[ 13 ], uu[ 14 ], uu[ 15 ] );
152 }
153 
154 // Convert a triple string from expanded to compressed form
155 QString US_Util::compressed_triple( const QString& ccw )
156 {
157  // For example, convert "1 / A / 290" to "1A290"
158  return ( ccw.section( "/", 0, 0 ).simplified() +
159  ccw.section( "/", 1, 1 ).simplified() +
160  ccw.section( "/", 2, 2 ).simplified() );
161 }
162 
163 // Convert a triple string from expanded to compressed form
164 QString US_Util::expanded_triple( const QString& ccw, bool spaces )
165 {
166  QString cells( "ABCDEFGHZ" );
167 
168  // Get, for example, {"4","A","280"} from "4A280"
169  int jj = 1;
170  jj = cells.contains( ccw.mid( 2, 1 ) ) ? 2 : jj;
171  jj = cells.contains( ccw.mid( 3, 1 ) ) ? 3 : jj;
172  jj = cells.contains( ccw.mid( 4, 1 ) ) ? 4 : jj;
173  QString cell = ccw.left( jj );
174  QString chan = ccw.mid ( jj, 1 );
175  QString wvln = ccw.mid ( jj + 1 );
176 
177  // Spaces value determines separator as " / " or "/"
178  QString sep = spaces ? " / " : "/";
179 
180  // Return, for example, "4 / A / 280" or "4/A/280"
181  return ( cell + sep + chan + sep + wvln );
182 }
183 
184 // A helper function to convert a character hex digit to decimal
185 unsigned char US_Util::hex2int( unsigned char c )
186 {
187  if ( c <= '9' )
188  return (unsigned char)( c - '0' );
189 
190  c &= 0x5f; // Make upper case if necessary
191  return (unsigned char)( c - 'A' + 10 );
192 }
193 
194 // Convert a QString uuid into a 16-byte binary array
195 void US_Util::uuid_parse( const QString& in, unsigned char* uu )
196 {
197  QByteArray ba = in.toUtf8();
198  unsigned char* p = (unsigned char*)ba.data();
199 
200  for ( int i = 0; i < 16; i++ )
201  {
202  while ( *p == '-' ) p++;
203 
204  char n = (char)( hex2int( *p++ ) << 4 );
205  n = (char)( (int)n | hex2int( *p++ ) );
206 
207  uu[ i ] = n;
208  }
209 }
210 
211 // Return a flag true if this is the ith time an error occurred;
212 // in truth, that a random number over the given range has hit 1.
213 bool US_Util::ithTime( int timeinc )
214 {
215  int rannum = qrand() % timeinc;
216  return ( rannum == 1 );
217 }
218 
219 // Return a flag if an XML attribute string represents true or false.
220 bool US_Util::bool_flag( const QString xmlattr )
221 {
222  return ( !xmlattr.isEmpty() && ( xmlattr == "1" || xmlattr == "T" ) );
223 }
224 
225 // Return an XML attribute string to represent true or false.
226 QString US_Util::bool_string( const bool boolval )
227 {
228  return QString( boolval ? "1" : "0" );
229 }
230