EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Splitter.cxx
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file Splitter.cxx
1 /* -------------------------------------------------------------------------- */
2 /* Splitter.cc */
3 /* */
4 /* A simple string parser. */
5 /* */
6 /* A.Kisselev, PNPI, St.Petersburg, Russia. */
7 /* e-mail: kisselev@hermes.desy.de */
8 /* -------------------------------------------------------------------------- */
9 
10 #include <cstring>
11 #include <cstdlib>
12 
13 #include <Splitter.h>
14 
15 /* ========================================================================== */
16 /* Replaces spaces with '0' characters and stores pointers to the */
17 /* corresponding substrings; */
18 
20 {
21  int len = strlen(buffer);
22 
23  // Cut out possible '\n' character;
24  if (buffer[len-1] == '\n') buffer[len-1] = 0;
25 
26  // Comments or pathological cases; is ' ' also a pathology?;
27  if (buffer[0] == '#' || buffer[0] == '\n' || !*buffer) return 0;
28 
29  // Reset substring counter;
30  argn = 0;
31 
32  // Eventually disassemble string in substrings;
33  for(char *ptr = strtok(buffer, " "); ptr; ptr = strtok(NULL, " "))
34  {
35  if (argn == SUBSTRINGS_MAX) return -1;
36 
37  argp[argn] = ptr;
38  argn++;
39  } /*for*/
40 
41  return argn;
42 } /* Splitter::splitStringCore */
43 
44 /* -------------------------------------------------------------------------- */
45 
46 int Splitter::splitString(char *str)
47 {
48  // String is too long, sorry;
49  if (strlen(str)+1 > STRING_LEN_MAX) return -1;
50 
51  // Copy string over to a buffer variable (yes, I would want to modify it!);
52  strcpy(buffer, str);
53 
54  return splitStringCore();
55 } /* splitString */
56 
57 /* -------------------------------------------------------------------------- */
58 
60 {
61  // Is this return code correct?;
62  if (!fgets(buffer, STRING_LEN_MAX-1, ff)) return -1;
63 
64  return splitStringCore();
65 } /* splitNextString */
66 
67 /* ========================================================================== */