Xmipp  v3.23.11-Nereus
Macros | Functions
String utilities
Collaboration diagram for String utilities:

Macros

#define STR_EQUAL(str1, str2)   (strcmp((str1), (str2)) == 0)
 

Functions

String removeChar (const String &str, char character)
 
String unescape (const String &str)
 
int bestPrecision (float F, int _width)
 
float textToFloat (const char *str)
 
float textToFloat (const String &str)
 
int textToInteger (const char *str)
 
size_t textToSizeT (const char *str)
 
int textToInteger (const String &str)
 
long long textToLongLong (const char *str)
 
String floatToString (float F, int _width=8, int _prec=0)
 
String integerToString (int I, int _width=0, char fill_with='0')
 
int charToInt (const char *str)
 
String stringToString (const String &str, size_t _width=0)
 
void checkAngle (const String &str)
 
void toLower (char *_str)
 
void toLower (String &_str)
 
String simplify (const String &str)
 
void trim (String &str)
 
String removeSpaces (const String &_str)
 
void removeQuotes (char **_str)
 
String findAndReplace (const String &tInput, const String &tFind, const String &tReplace)
 
String formatString (const char *format,...)
 
void formatStringFast (String &str, const char *format,...)
 
bool matchRegExp (const String &inputString, const String &pattern)
 
String WordWrap (const String &inputString, size_t lineLength)
 
String escapeForRegularExpressions (const String &str)
 
bool endsWith (const char *str1, const char *str2)
 

Tokenization

These functions allow to split a string into small pieces separated by blank spaces, giving you a pointer to the different word each time. The different elements from the string are selected using strtok, so after the application of this function to the input string, this is modified and NULL characters are introduced as delimiters of the elements. This is useful in most situations since after reading a list you might go on reading more things, but you must be aware of it. Here goes an example of doing so:

std::cout << "Whole line: " << line << std::endl;
std::cout << "First word: " << firstToken(line) << std::endl;
std::cout << "Second word: " << nextToken() << std::endl;
std::cout << "Third word: " << nextToken() << std::endl;
...

When there are no more words, both functions return a NULL pointer. Here we make a distinction between tokens (words that might be empty) and words (words that cannot be empty, if they are then an exception or an exit error is thrown).

For STL there is another way. You supply a string object and a vector of strings is returned with all the elements

int splitString (const String &input, const String &delimiter, StringVector &results, bool includeEmpties=false)
 
char * firstToken (const char *str)
 
char * firstToken (const String &str)
 
char * nextToken ()
 
String nextToken (const String &str, size_t &i)
 
char * firstWord (char *str)
 
char * firstWord (String &str)
 
char * nextWord ()
 
void tokenize (const String &str, StringVector &tokens, const String &delimiters=" \)
 
char * memtok (char **src, char **_end, const char *sep)
 
void * _memmem (const void *haystack, size_t haystack_len, const void *needle, size_t needle_len)
 

Detailed Description

Macro Definition Documentation

◆ STR_EQUAL

#define STR_EQUAL (   str1,
  str2 
)    (strcmp((str1), (str2)) == 0)

Macro to test if to string are the same

Definition at line 42 of file xmipp_strings.h.

Function Documentation

◆ _memmem()

void* _memmem ( const void *  haystack,
size_t  haystack_len,
const void *  needle,
size_t  needle_len 
)

Memory string search, taken from GNU C Library

Definition at line 553 of file xmipp_strings.cpp.

554 {
555  if (needle_len == 0)
556  /* The first occurrence of the empty string is deemed to occur at
557  the beginning of the string. */
558  return (void *) haystack;
559 
560  /* Sanity check, otherwise the loop might search through the whole
561  memory. */
562  if (haystack_len < needle_len)
563  return NULL;
564 
565  // memchr
566  const char *pNeedle=(const char *) needle;
567 #ifdef NEVERDEFINED
568  const char *const last_possible = (const char *) haystack + haystack_len - needle_len;
569  for (const char *begin = (const char *) haystack; begin <= last_possible; ++begin)
570  if (begin[0] == pNeedle[0] &&
571  !memcmp (begin+1, pNeedle + 1, needle_len - 1))
572  return (void *) begin;
573 #else
574  const char *begin=(const char *) haystack;
575  const char *current=begin;
576  size_t remaining=haystack_len-needle_len+1;
577  int firstChar=(int)pNeedle[0];
578  while (remaining>0)
579  {
580  const char *found=(const char *) memchr(current, firstChar, remaining);
581  if (found==NULL)
582  break;
583  else
584  {
585  if (memcmp(found+1, pNeedle + 1, needle_len - 1)==0)
586  return (void *)found;
587  else
588  {
589  remaining-=found-current+1;
590  current=found+1;
591  }
592  }
593  }
594 #endif
595 
596  return NULL;
597 }

◆ bestPrecision()

int bestPrecision ( float  F,
int  _width 
)

Best precision for a float number.

This function returns the best precision to be used in a "printf" format if this number is to fit in a given width. It returns -1 if the exponential format is advised.

template<typename T>
std::ostream& operator<<(std::ostream& out, const T& val)
{
int i,j;
if (val.xdim == 0)
out << "NULL matrix" << std::endl;
else
out << std::endl;
T aux = ABSnD(val);
int prec = bestPrecision(aux.max(), 10);
for (i=STARTINGY(val); i<=FINISHINGY(val); i++)
{
for (j=STARTINGX(val); j<=FINISHINGX(val); j++)
{
out << floatToString((float) val(i,j), 10, prec) << ' ';
}
out << std::endl;
}
return out;
}

Definition at line 176 of file xmipp_strings.cpp.

177 {
178  // If it is 0
179  if (F == 0)
180  return 1;
181 
182  // Otherwise
183  int exp = FLOOR(log10(ABS(F)));
184  int advised_prec;
185 
186  if (exp >= 0)
187  if (exp > _width - 3)
188  advised_prec = -1;
189  else
190  advised_prec = _width - 2;
191  else
192  {
193  advised_prec = _width + (exp - 1) - 3;
194  if (advised_prec <= 0)
195  advised_prec = -1;
196  }
197 
198  if (advised_prec < 0)
199  advised_prec = -1; // Choose exponential format
200 
201  return advised_prec;
202 }
#define FLOOR(x)
Definition: xmipp_macros.h:240
#define ABS(x)
Definition: xmipp_macros.h:142
void log10(Image< double > &op)

◆ charToInt()

int charToInt ( const char *  str)

Character to integer conversion.

Takes a character and produces a number according to its ASCII code minus 48. For instance, ASCII=48 produces number 0, ASCII=49 produces 1, ..., ASCII=57 produces 9, ASCII=58 produces 10!!, ... This is used when you have codified numbers greater than 9 in a single character.

int param_no = textToInt(token, 1602, "Error reading number parameters");

Definition at line 293 of file xmipp_strings.cpp.

294 {
295  if (str == NULL)
296  REPORT_ERROR(ERR_MEM_NULLPOINTER, "Cannot convert NULL into int");
297 
298  char readval;
299  int ok = sscanf(str, "%c", &readval);
300 
301  if (ok)
302  return readval - 48;
303  REPORT_ERROR(ERR_VALUE_INCORRECT, "Conversion to int error");
304 
305  return 0;
306 }
#define REPORT_ERROR(nerr, ErrormMsg)
Definition: xmipp_error.h:211
Null pointer passed as parameter.
Definition: xmipp_error.h:168
Incorrect value received.
Definition: xmipp_error.h:195

◆ checkAngle()

void checkAngle ( const String str)

Check angle.

If the argument is not "rot", "tilt" nor "psi" an exception is thrown

Definition at line 320 of file xmipp_strings.cpp.

321 {
322  if (str == "rot")
323  return;
324 
325  if (str == "tilt")
326  return;
327 
328  if (str == "psi")
329  return;
330 
332  static_cast< String >(
333  "checkAngle: Not recognized angle type: " + str));
334 }
#define REPORT_ERROR(nerr, ErrormMsg)
Definition: xmipp_error.h:211
Incorrect value received.
Definition: xmipp_error.h:195

◆ endsWith()

bool endsWith ( const char *  str1,
const char *  str2 
)

Ends with. Detects if str1 finishes with str2.

Definition at line 684 of file xmipp_strings.cpp.

685 {
686  int len1 = strlen(str1);
687  int len2 = strlen(str2);
688  return (len1 >= len2) && (0 == strcmp(str1 + len1 - len2, str2));
689 }

◆ escapeForRegularExpressions()

String escapeForRegularExpressions ( const String str)

Escape for regular expressions. Escape characters that could be misunderstood by regular expressions

Definition at line 677 of file xmipp_strings.cpp.

678 {
679  String aux;
680  aux=findAndReplace(str,"+","\\+");
681  return aux;
682 }
String findAndReplace(const String &tInput, const String &tFind, const String &tReplace)
std::string String
Definition: xmipp_strings.h:34

◆ findAndReplace()

String findAndReplace ( const String tInput,
const String tFind,
const String tReplace 
)

Replace. This function replaces in the string all the occurrences of tFind and replaces it with tReplace.

Definition at line 492 of file xmipp_strings.cpp.

494 {
495  size_t uFindLen = tFind.length();
496 
497  if( uFindLen == 0 )
498  return tInput;
499 
500  size_t uPos = 0;
501  size_t uReplaceLen = tReplace.length();
502  String tOut=tInput;
503  for( ;(uPos = tOut.find(tFind, uPos)) != String::npos; )
504  {
505  tOut=tOut.replace( uPos, uFindLen, tReplace );
506  uPos += uReplaceLen;
507  }
508  return tOut;
509 }
std::string String
Definition: xmipp_strings.h:34

◆ firstToken() [1/2]

char* firstToken ( const char *  str)
inline

Returns first token (char*).

char line[80];
std::cout << "First word: " << firstToken(line) << std::endl;

Definition at line 274 of file xmipp_strings.h.

275 {
276  return strtok((char*) str, " \t\n");
277 }

◆ firstToken() [2/2]

char* firstToken ( const String str)
inline

Returns first token (STL).

String line;
std::cout << "First word: " << firstToken(line) << std::endl;

Definition at line 287 of file xmipp_strings.h.

288 {
289  return strtok((char*) str.c_str(), " \t\n");
290 }

◆ firstWord() [1/2]

char* firstWord ( char *  str)

Get non empty string (char*).

This function returns the first word found in the given line disregarding the leading blanks. If no word is found then an exception or an exit error is produced. After calling this function the first blank character after the word is substituted by a NULL character (as it uses the function firstToken. Further word readings should use the function read_nextWord

Definition at line 453 of file xmipp_strings.cpp.

454 {
455  char *token;
456 
457  // Get token
458  if (str != NULL)
459  token = firstToken(str);
460  else
461  token = nextToken();
462 
463  // Check that there is something
464  if (token == NULL)
465  REPORT_ERROR(ERR_VALUE_EMPTY, "Empty token");
466 
467  return token;
468 }
Empty value.
Definition: xmipp_error.h:194
#define REPORT_ERROR(nerr, ErrormMsg)
Definition: xmipp_error.h:211
char * firstToken(const char *str)
String nextToken(const String &str, size_t &i)

◆ firstWord() [2/2]

char* firstWord ( String str)
inline

Get non empty string (STL).

Same as the previous function but for STL strings

Definition at line 335 of file xmipp_strings.h.

336 {
337  // FIXME C-style cast
338  return firstWord((char*) str.c_str());
339 }
char * firstWord(char *str)

◆ floatToString()

String floatToString ( float  F,
int  _width = 8,
int  _prec = 0 
)

Float to string conversion.

If precision==0 the precision is automatically computed in such a way that the number fits the width (the exponential format might be chosen). If precision==-1 then the exponential format is forced. If width==0 then the minimum width is used.

REPORT_ERROR(ERR_VALUE_INCORRECT, "Value not recognised " + floatToString(val));

Definition at line 204 of file xmipp_strings.cpp.

205 {
206 #if GCC_VERSION < 30300
207  char aux[15];
208  std::ostrstream outs(aux, sizeof(aux));
209 #else
210 
211  std::ostringstream outs;
212 #endif
213 
214  outs.fill(' ');
215 
216  if (_width != 0)
217  outs.width(_width);
218 
219  if (_prec == 0)
220  _prec = bestPrecision(F, _width);
221 
222  if (_prec == -1 && _width > 7)
223  {
224  outs.precision(_width - 7);
225  outs.setf(std::ios::scientific);
226  }
227  else
228  outs.precision(_prec);
229 
230 #if GCC_VERSION < 30301
231 
232  outs << F << std::ends;
233 #else
234 
235  outs << F;
236 #endif
237 
238 #if GCC_VERSION < 30300
239 
240  return String(aux);
241 #else
242 
243  String retval = outs.str();
244  int i = retval.find('\0');
245 
246  if (i != -1)
247  retval = retval.substr(0, i);
248 
249  return retval;
250 #endif
251 }
int bestPrecision(float F, int _width)
#define i
std::string String
Definition: xmipp_strings.h:34

◆ formatString()

String formatString ( const char *  format,
  ... 
)

Obtain an string from a format in the way of printf works Example: formatString("vectorHeader@%s",fn_out.c_str())

Definition at line 602 of file xmipp_strings.cpp.

603 {
604  char formatBuffer[1024];
605  va_list args;
606  va_start(args, format);
607  vsprintf (formatBuffer, format, args);
608  String result(formatBuffer);
609  va_end (args);
610 
611  return result;
612 }
std::string String
Definition: xmipp_strings.h:34

◆ formatStringFast()

void formatStringFast ( String str,
const char *  format,
  ... 
)

Obtain an string from a format in the way of printf works

Definition at line 617 of file xmipp_strings.cpp.

618 {
619  char formatBuffer[1024];
620 
621  va_list args;
622  va_start(args, format);
623  vsprintf (formatBuffer, format, args);
624  str=formatBuffer;
625  va_end (args);
626 }

◆ integerToString()

String integerToString ( int  I,
int  _width = 0,
char  fill_with = '0' 
)

Integer to string conversion.

If width==0 then writes the number with the number of digits needed. The fill_with field indicates which is the filling character for the left positions.

REPORT_ERROR(ERR_VALUE_INCORRECT, "Error reading key " + integerToString(key));

Definition at line 253 of file xmipp_strings.cpp.

254 {
255  char aux[15];
256 
257  // Check width
258  int width = _width;
259  int Iaux = ABS(I);
260 
261  if (SGN(I) < 0)
262  width--;
263 
264  if (width == 0)
265  do
266  {
267  Iaux /= 10;
268  width++;
269  }
270  while (Iaux != 0);
271 
272  // Fill the number with the fill character
273  for (int i = 0; i < width; i++)
274  aux[i] = fill_with;
275 
276  // Start filling the array
277  aux[width--] = '\0';
278  Iaux = ABS(I);
279  do
280  {
281  int digit = Iaux % 10;
282  Iaux /= 10;
283  aux[width--] = '0' + digit;
284  }
285  while (Iaux != 0);
286 
287  if (SGN(I) < 0)
288  return static_cast< String >("-") + aux;
289  else
290  return static_cast< String >(aux);
291 }
#define i
#define ABS(x)
Definition: xmipp_macros.h:142
std::string String
Definition: xmipp_strings.h:34
#define SGN(x)
Definition: xmipp_macros.h:155

◆ matchRegExp()

bool matchRegExp ( const String inputString,
const String pattern 
)

True if the inputString matches the regular expression in pattern

Definition at line 629 of file xmipp_strings.cpp.

630 {
631  // Construct regular expression
632  regex_t re;
633  if (regcomp(&re, pattern.c_str(), REG_EXTENDED|REG_NOSUB) != 0)
634  REPORT_ERROR(ERR_ARG_INCORRECT,(String)"Pattern cannot be parsed:"+pattern);
635 
636  // Check if the string matches the pattern
637  int status = regexec(&re, inputString.c_str(), (size_t) 0, NULL, 0);
638  regfree(&re);
639  if (status != 0)
640  return false;
641  return true;
642 }
#define REPORT_ERROR(nerr, ErrormMsg)
Definition: xmipp_error.h:211
Incorrect argument received.
Definition: xmipp_error.h:113
std::string String
Definition: xmipp_strings.h:34

◆ memtok()

char* memtok ( char **  src,
char **  _end,
const char *  sep 
)

Tokenizer for char arrays. Similar to strtok but does NOT modify the input array src is a pointer to the array beginning. It may be modified to trim the token _end is a pointer to the end of the token sep is an array with the valid separator

char *start;
char *str;
char *end;
start=(char *) malloc(128);
strcpy(start,"Filtrose el\tmozo\n en una zahurda lobrega, las paredes\
enhollinadas");
fprintf(stderr,"Mstart=%d",(void *) start);
char aux[64];
str = mystrtok(&start, &end," \t\r\n");
strncpy(aux,str,end-str); aux[end-str]=0;
printf(">%s< %d\n", aux,(void *) end);
str = mystrtok(&start, &end," \t\r\n");
strncpy(aux,str,end-str); aux[end-str]=0;
printf(">%s< %d\n", aux,(void *) end);
str = mystrtok(&start, &end," \t\r\n");
strncpy(aux,str,end-str); aux[end-str]=0;
printf(">%s< %d\n", aux,(void *) end);
str = mystrtok(&start, &end," \t\r\n");
strncpy(aux,str,end-str); aux[end-str]=0;
printf(">%s< %d\n", aux,(void *) end);

Definition at line 525 of file xmipp_strings.cpp.

526 {
527  char *start = *src;
528  char *end;
529 
530  /*
531  * Skip over leading delimiters.
532  */
533  start += strspn(start, sep);
534  if (*start == 0)
535  {
536  *src = start;
537  return (0);
538  }
539 
540  /*
541  * Separate off one token.
542  */
543  end = start + strcspn(start, sep);
544  *src = start;
545  *_end=end;
546  return (start);
547 }

◆ nextToken() [1/2]

char* nextToken ( )
inline

Returns next token.

This functions returns the next word of the line we have given last as parameter to firstToken.

char line[80];
...
firstToken(line);
std::cout << "Second word: " << nextToken(line) << std::endl;
stf::string line;
...
firstToken(line);
std::cout << "Second word: " << nextToken(line) << std::endl;

Definition at line 309 of file xmipp_strings.h.

310 {
311  return strtok((char*) NULL, " \t\n");
312 }

◆ nextToken() [2/2]

String nextToken ( const String str,
size_t &  i 
)

Returns next token.

It reads from position i. Returns (in i) the following position to search on. When there are no more tokens. It returns "".

Definition at line 436 of file xmipp_strings.cpp.

437 {
438  String retval;
439  if (i >= str.length())
440  return retval;
441  int j = str.find_first_not_of(" \t\n", i);
442  if (j == -1)
443  return retval;
444  int k = str.find_first_of(" \t\n", j + 1);
445  if (k == -1)
446  k = str.length();
447  retval = str.substr(j, k - j + 1);
448  i = k + 1;
449  return retval;
450 }
#define i
ql0001_ & k(htemp+1),(cvec+1),(atemp+1),(bj+1),(bl+1),(bu+1),(x+1),(clamda+1), &iout, infoqp, &zero,(w+1), &lenw,(iw+1), &leniw, &glob_grd.epsmac
#define j
std::string String
Definition: xmipp_strings.h:34

◆ nextWord()

char* nextWord ( )
inline

Get next non empty string.

This is the same as the nextToken, but an exception is thrown or an exit error produced if the word is empty

Definition at line 346 of file xmipp_strings.h.

347 {
348  return firstWord((char*) NULL);
349 }
char * firstWord(char *str)

◆ removeChar()

String removeChar ( const String str,
char  character 
)

Removes all occurrences of 'character' from the string no matter where they are

Definition at line 36 of file xmipp_strings.cpp.

37 {
38  String temp;
39 
40  for( unsigned int i = 0 ; i < str.length( ) ; i++ )
41  {
42  if ( str[ i ] != character )
43  temp += str[ i ];
44  }
45 
46  return temp;
47 }
#define i
std::string String
Definition: xmipp_strings.h:34

◆ removeQuotes()

void removeQuotes ( char **  _str)

Remove quotes.

This function removes the first character if it is a double or single quote, as well as the last character. The char pointer might be moved.

char str[10] = "\"Hello\"";
(&str);

Definition at line 363 of file xmipp_strings.cpp.

364 {
365  String retval = *_str;
366  if (retval.length() == 0)
367  return;
368  char c = retval[0];
369  if (c == '\"' || c == '\'')
370  retval = retval.substr(1, retval.length() - 1);
371  c = retval[retval.length()-1];
372  if (c == '\"' || c == '\'')
373  retval = retval.substr(0, retval.length() - 1);
374  free(*_str);
375  *_str = strdup(retval.c_str());
376 }
doublereal * c
free((char *) ob)
std::string String
Definition: xmipp_strings.h:34

◆ removeSpaces()

String removeSpaces ( const String _str)

Remove consecutive spaces.

All consecutive spaces are replaced by a single one and starting and finishing spaces are removed

Definition at line 336 of file xmipp_strings.cpp.

337 {
338  String retval;
339  int first = _str.find_first_not_of("\n \t");
340  int last = _str.find_last_not_of("\n \t");
341  bool after_blank = false;
342 
343  for (int i = first; i <= last; i++)
344  {
345  if (_str[i] == ' ' || _str[i] == '\n' || _str[i] == '\t')
346  {
347  if (!after_blank)
348  retval += _str[i];
349 
350  after_blank = true;
351  }
352  else
353  {
354  retval += _str[i];
355  after_blank = false;
356  }
357  }
358 
359  return retval;
360 }
#define i
glob_log first
std::string String
Definition: xmipp_strings.h:34

◆ simplify()

String simplify ( const String str)

Removes white spaces from the beginning and the end of the string as well as escaped characters and simplifies the rest of groups of white spaces of the string to a single white space

Definition at line 69 of file xmipp_strings.cpp.

70 {
71  String temp;
72 
73  // First, unescape string
74  String straux = unescape( str );
75 
76  // Remove spaces from the beginning
77  int pos = straux.find_first_not_of( ' ' );
78  straux.erase( 0, pos );
79 
80  // Trim the rest of spaces
81  for( unsigned int i = 0 ; i < straux.length( ) ; )
82  {
83  temp += straux[ i ];
84 
85  if ( straux[ i ] == ' ' )
86  {
87  while( straux[ i ] == ' ' )
88  {
89  i++;
90  }
91  }
92  else
93  {
94  i++;
95  }
96  }
97 
98  // Remove space left at the end of the string
99  // if needed
100  if( temp[ temp.size( ) - 1 ] == ' ' )
101  {
102  temp.resize( temp.size() - 1 );
103  }
104 
105  return temp;
106 }
#define i
std::string String
Definition: xmipp_strings.h:34
String unescape(const String &str)

◆ splitString()

int splitString ( const String input,
const String delimiter,
StringVector results,
bool  includeEmpties = false 
)

Split a STL string given some delimiter.

Returns a the number of tokens found. The tokens are in the variable results.

Definition at line 379 of file xmipp_strings.cpp.

383 {
384  results.clear();
385  size_t delimiterSize = delimiter.size();
386  if (input.size()== 0 || delimiterSize == 0)
387  return 0;
388 
389  size_t newPos, iPos = 0;
390  String emptyString;
391  while ((newPos = input.find(delimiter, iPos))!=String::npos)
392  {
393  if (newPos==iPos)
394  {
395  if (includeEmpties)
396  results.push_back(emptyString);
397  }
398  else
399  results.push_back(input.substr(iPos, newPos-iPos));
400  iPos = newPos+delimiterSize;
401  }
402  if (iPos>=input.size())
403  {
404  if (includeEmpties)
405  results.push_back(emptyString);
406  }
407  else
408  results.push_back(input.substr(iPos, String::npos));
409  return results.size();
410 }
std::string String
Definition: xmipp_strings.h:34

◆ stringToString()

String stringToString ( const String str,
size_t  _width = 0 
)

String to string with given length conversion.

The output string will have the information of the input one with the given width. If the width is smaller than the string length then the string is truncated and if it is greater the string is right padded with spaces. If width==0 then the same string is returned.

Definition at line 308 of file xmipp_strings.cpp.

309 {
310  if (_width == 0)
311  return str;
312 
313  if (_width < str.length())
314  return str.substr(0, _width);
315 
316  String aux = str;
317  return aux.append(_width - str.length(), ' ');
318 }
std::string String
Definition: xmipp_strings.h:34

◆ textToFloat() [1/2]

float textToFloat ( const char *  str)

String (char*) to float conversion.

float key = textToFloat(firstToken(line), 1602, "Error reading key");

Definition at line 124 of file xmipp_strings.cpp.

125 {
126  if (str == NULL)
127  REPORT_ERROR(ERR_MEM_NULLPOINTER, "Cannot be converted into float");
128 
129  float retval;
130  int ok = sscanf(str, "%f", &retval);
131 
132  if (ok)
133  return retval;
134  REPORT_ERROR(ERR_VALUE_INCORRECT, "Conversion to float error");
135 }
#define REPORT_ERROR(nerr, ErrormMsg)
Definition: xmipp_error.h:211
Null pointer passed as parameter.
Definition: xmipp_error.h:168
Incorrect value received.
Definition: xmipp_error.h:195

◆ textToFloat() [2/2]

float textToFloat ( const String str)
inline

String (String) to integer conversion.

Definition at line 99 of file xmipp_strings.h.

100 {
101  return textToFloat(str.c_str());
102 }
float textToFloat(const char *str)

◆ textToInteger() [1/2]

int textToInteger ( const char *  str)

String (char*) to integer conversion.

int param_no = textToInteger(nextToken(), 1602, "Error reading number parameters")

Definition at line 137 of file xmipp_strings.cpp.

138 {
139  if (str == NULL)
140  REPORT_ERROR(ERR_MEM_NULLPOINTER, "Cannot be converted into int");
141 
142  int retval;
143  int ok = sscanf(str, "%d", &retval);
144 
145  if (ok)
146  return retval;
147  REPORT_ERROR(ERR_VALUE_INCORRECT, "Conversion to int error");
148 }
#define REPORT_ERROR(nerr, ErrormMsg)
Definition: xmipp_error.h:211
Null pointer passed as parameter.
Definition: xmipp_error.h:168
Incorrect value received.
Definition: xmipp_error.h:195

◆ textToInteger() [2/2]

int textToInteger ( const String str)
inline

String (String) to integer conversion.

Definition at line 116 of file xmipp_strings.h.

117 {
118  return textToInteger(str.c_str());
119 }
int textToInteger(const char *str)

◆ textToLongLong()

long long textToLongLong ( const char *  str)

String (char*) to long long integer conversion.

long long param_no = textToLongLong(nextToken(), 1602, "Error reading number
parameters")

Definition at line 163 of file xmipp_strings.cpp.

164 {
165  if (str == NULL)
166  REPORT_ERROR(ERR_MEM_NULLPOINTER, "Cannot be converted into long long int");
167 
168  long long int retval;
169  int ok = sscanf(str, "%lld", &retval);
170 
171  if (ok)
172  return retval;
173  REPORT_ERROR(ERR_VALUE_INCORRECT, "Conversion to long long int error");
174 }
#define REPORT_ERROR(nerr, ErrormMsg)
Definition: xmipp_error.h:211
Null pointer passed as parameter.
Definition: xmipp_error.h:168
Incorrect value received.
Definition: xmipp_error.h:195

◆ textToSizeT()

size_t textToSizeT ( const char *  str)

String (String) to size_t conversion.

Definition at line 150 of file xmipp_strings.cpp.

151 {
152  if (str == NULL)
153  REPORT_ERROR(ERR_MEM_NULLPOINTER, "Cannot be converted into int");
154 
155  long unsigned int retval;
156  int ok = sscanf(str, "%lu", &retval);
157 
158  if (ok)
159  return retval;
160  REPORT_ERROR(ERR_VALUE_INCORRECT, "Conversion to size_t error");
161 }
#define REPORT_ERROR(nerr, ErrormMsg)
Definition: xmipp_error.h:211
Null pointer passed as parameter.
Definition: xmipp_error.h:168
Incorrect value received.
Definition: xmipp_error.h:195

◆ tokenize()

void tokenize ( const String str,
StringVector tokens 
)

Tokenize a string and return a list of tokens

Definition at line 471 of file xmipp_strings.cpp.

473 {
474  tokens.clear();
475  // Skip delimiters at beginning.
476  String::size_type lastPos = str.find_first_not_of(delimiters, 0);
477  // Find first "non-delimiter".
478  String::size_type pos = str.find_first_of(delimiters, lastPos);
479 
480  while (String::npos != pos || String::npos != lastPos)
481  {
482  // Found a token, add it to the vector.
483  tokens.push_back(str.substr(lastPos, pos - lastPos));
484  // Skip delimiters. Note the "not_of"
485  lastPos = str.find_first_not_of(delimiters, pos);
486  // Find next "non-delimiter"
487  pos = str.find_first_of(delimiters, lastPos);
488  }
489 }

◆ toLower() [1/2]

void toLower ( char *  _str)

To lower.

All characters between A-Z are brought to a-z. Result is rewritten on input string

Definition at line 413 of file xmipp_strings.cpp.

414 {
415  int i = 0;
416  while (_str[i] != '\0')
417  {
418  if (_str[i] >= 'A' && _str[i] <= 'Z')
419  _str[i] += 'a' -'A';
420  i++;
421  }
422 }
#define i

◆ toLower() [2/2]

void toLower ( String _str)

To lower, for STL strings.

Definition at line 424 of file xmipp_strings.cpp.

425 {
426  int i = 0;
427  while (_str[i] != '\0')
428  {
429  if (_str[i] >= 'A' && _str[i] <= 'Z')
430  _str[i] += 'a' -'A';
431  i++;
432  }
433 }
#define i

◆ trim()

void trim ( String str)

Remove trailing spaces

Definition at line 109 of file xmipp_strings.cpp.

110 {
111  String::size_type pos = str.find_last_not_of(' ');
112 
113  if (pos != String::npos)
114  {
115  str.erase(pos + 1);
116  pos = str.find_first_not_of(' ');
117  if (pos != String::npos)
118  str.erase(0, pos);
119  }
120  else
121  str.clear();
122 }

◆ unescape()

String unescape ( const String str)

Removes escaped symbols ESC+n, t, v, b, r, f, and a

Definition at line 49 of file xmipp_strings.cpp.

50 {
51  String temp;
52 
53  for( unsigned int i = 0 ; i < str.length( ) ; i++ )
54  {
55  char current_char = str[ i ];
56 
57  if( current_char != '\n' && current_char != '\t' &&
58  current_char != '\v' && current_char != '\b' &&
59  current_char != '\r' && current_char != '\f' &&
60  current_char != '\a' )
61  {
62  temp += str[ i ];
63  }
64  }
65 
66  return temp;
67 }
#define i
std::string String
Definition: xmipp_strings.h:34

◆ WordWrap()

String WordWrap ( const String inputString,
size_t  lineLength 
)

split long comments in several lines starting with #

Definition at line 645 of file xmipp_strings.cpp.

646 {
647 
648  if(inputString.size() <= lineLength)
649  return ((String)"# " + inputString + "\n");
650  std::istringstream iss(inputString);
651  std::ostringstream ss;
652  std::string line;
653 
654  line.clear();
655  do
656  {
657  std::string word;
658  iss >> word;
659 
660  if (line.length() + word.length() > lineLength)
661  {
662  ss << "# " << line << std::endl;
663  line.clear();
664  }
665  line += word + " ";
666 
667  }
668  while (iss);
669 
670  if (!line.empty())
671  {
672  ss << "# " << line << std::endl;
673  }
674  return ss.str();
675 }
std::string String
Definition: xmipp_strings.h:34