StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
parseXmlString.cc
1 /***************************************************************************
2  *
3  * $Id: parseXmlString.cc,v 1.8 2012/06/11 14:33:47 fisyak Exp $
4  *
5  * Author: R. Jeff Porter
6  ***************************************************************************
7  *
8  * Description: parses Xml file & returns contents between request TAGs
9  *
10  ***************************************************************************
11  *
12  * $Log: parseXmlString.cc,v $
13  * Revision 1.8 2012/06/11 14:33:47 fisyak
14  * std namespace
15  *
16  * Revision 1.7 2004/01/15 00:02:25 fisyak
17  * Replace ostringstream => StString, add option for alpha
18  *
19  * Revision 1.6 2003/09/16 22:44:18 porter
20  * got rid of all ostrstream objects; replaced with StString+string.
21  * modified rules.make and added file stdb_streams.h for standalone compilation
22  *
23  * Revision 1.5 2003/09/02 17:57:50 perev
24  * gcc 3.2 updates + WarnOff
25  *
26  * Revision 1.4 2000/01/10 20:37:55 porter
27  * expanded functionality based on planned additions or feedback from Online work.
28  * update includes:
29  * 1. basis for real transaction model with roll-back
30  * 2. limited SQL access via the manager for run-log & tagDb
31  * 3. balance obtained between enumerated & string access to databases
32  * 4. 3-levels of diagnostic output: Quiet, Normal, Verbose
33  * 5. restructured Node model for better XML support
34  *
35  * Revision 1.3 1999/12/28 21:31:42 porter
36  * added 'using std::vector' and 'using std::list' for Solaris CC5 compilation.
37  * Also fixed some warnings arising from the CC5 compiles
38  *
39  * Revision 1.2 1999/09/30 02:06:15 porter
40  * add StDbTime to better handle timestamps, modify SQL content (mysqlAccessor)
41  * allow multiple rows (StDbTable), & Added the comment sections at top of
42  * each header and src file
43  *
44  **************************************************************************/
45 #include "parseXmlString.hh"
46 #include "stdb_streams.h"
47 #include <string.h>
48 using namespace std;
50 
51 char*
52 parseXmlString::getString(char* line, char* key1, char* key2){
53 
54 
55 char* p1 = 0;
56 char* p = &line[0];
57 
58 int i1 = getIndexAfter(line, key1);
59 if(!i1) return p1;
60 int i2 = getIndexBefore(line, key2);
61 if(!i2) return p1;
62 
63 
64 int size = i2-i1+1;
65 if(i2<i1){
66  cerr << " Missing Key " << endl;
67  return p1;
68 }
69 
70 for (int i=0; i<i1-1; i++)p++;
71 
72 p1 = new char[size+1];
73 strncpy(p1,p,size);
74 p1[size]='\0';
75 
76 // cout << p1 << endl;
77  char* retVal = removeBlankEnds(p1);
78  delete [] p1;
79  return retVal;
80  // return removeBlankEnds(p1);
81 }
82 
84 
85 char*
86 parseXmlString::getStringAfter(char* line, char* key){
87 
88  int i = getIndexAfter(line,key);
89  char* tmp =0;
90 
91  if(!i)return tmp;
92 
93  char* p1 = &line[i];
94 
95  int k = strlen(line);
96  int size = k-i;
97 
98  tmp = new char[size+1];
99  strncpy(tmp,p1,size);
100 
101  tmp[size] = '\0';
102  char* retVal = removeBlankEnds(tmp);
103  delete [] tmp;
104  return retVal;
105  // return removeBlankEnds(tmp);
106 
107 }
108 
110 
111 char*
112 parseXmlString::getStringBefore(char* line, char* key){
113 
114  int i = getIndexBefore(line,key);
115 
116  char * tmp = 0;
117  if(!i)return tmp;
118  char* p1 = &line[0];
119  int size = i;
120  tmp = new char[size+1];
121  strncpy(tmp,p1,size);
122  tmp[size] = '\0';
123 
124  char* retVal = removeBlankEnds(tmp);
125  delete [] tmp;
126  return retVal;
127  // return removeBlankEnds(tmp);
128 }
129 
131 
132 int
133 parseXmlString::getIndexAfter(char* line, char* key){
134 
135  if(!line || !key)return 0;
136  char* id = strstr(line,key);
137  if(!id)return 0; //use as bool
138 
139  int i = id - line;
140  i += strlen(key)+1;
141  return i;
142 }
143 
145 
146 int
147 parseXmlString::getIndexBefore(char* line, char* key){
148 
149  if(!line || !key) return 0;
150  char* id = strstr(line,key);
151  if(!id) return 0; //use as bool
152  int i = id - line;
153  return i--;
154 
155 }
156 
158 
159 char*
160 parseXmlString::removeBlankEnds(char* line){
161 
162  char* p1 = &line[0];
163  int k = strlen(line);
164  int i,j;
165 
166  for(i=0; i<k; i++){
167  if(line[i]!=' ')break;
168  p1++;
169  }
170 
171  for(j=k; j>0; j--){
172  if(line[j]=='\0')continue;
173  if(line[j]!=' ')break;
174  }
175 
176  int size = j-i+1;
177  char* tmp = new char[size+1];
178  strncpy(tmp,p1,size);
179  tmp[size] = '\0';
180 
181  //delete [] line;
182  return tmp;
183 }
184 
185 
186 
187 
188 
189 
190