XRootD
XrdOucGatherConf Class Reference

#include <XrdOucGatherConf.hh>

+ Collaboration diagram for XrdOucGatherConf:

Public Types

enum  Level {
  full_lines = 0 ,
  trim_lines ,
  only_body ,
  trim_body
}
 

Public Member Functions

 XrdOucGatherConf (const char **&want, XrdSysError *errP=0)
 
 XrdOucGatherConf (const char *want, XrdSysError *errP=0)
 
 ~XrdOucGatherConf ()
 
void EchoLine ()
 
void EchoOrder (bool doBefore)
 
int Gather (const char *cfname, Level lvl, const char *parms=0)
 
char * GetLine ()
 
char * GetToken (char **rest=0, int lowcase=0)
 
bool hasData ()
 
const char * LastLine ()
 
void MsgE (const char *txt1, const char *txt2=0, const char *txt3=0, const char *txt4=0, const char *txt5=0, const char *txt6=0)
 
void MsgfE (const char *fmt,...)
 
void MsgfW (const char *fmt,...)
 
void MsgW (const char *txt1, const char *txt2=0, const char *txt3=0, const char *txt4=0, const char *txt5=0, const char *txt6=0)
 
void RetToken ()
 
void Tabs (int x=1)
 
bool useData (const char *data)
 

Detailed Description

Definition at line 35 of file XrdOucGatherConf.hh.

Member Enumeration Documentation

◆ Level

Gather information from a config file.

Note
You must call this method or a successful useData() before calling any Get/Ret methods.
Parameters
cfnamePath to the configuration file.
lvlIndicates how the gathered directives are to be saved: full_lines - the full directive line, including newline. trim_lines - Like full_lines but the prefix (i.e.characters the dot) are discarded. Useful only when gathering a single prefix. only_body Saves the body of each wanted directive as a space separated string blob. trim_body Like only_body but also includes the directive characters after the dot. Useful only when gathering a single prefix.
parmsOptional pointer to initial configuration parameters. These may be present for plugins.
Returns
> 0 Success, configuration data has been gathered.
= 0 Nothing was gathered.
< 0 Problem reading the config file, returned value is -errno.
Enumerator
full_lines 

Complete lines.

trim_lines 

Prefix trimmed lines.

only_body 

Only directive bodies as a string blob.

trim_body 

Prefix trimmed lines as a string blob.

Definition at line 84 of file XrdOucGatherConf.hh.

84  {full_lines = 0,
85  trim_lines,
86  only_body,
87  trim_body
88  };
@ trim_lines
Prefix trimmed lines.
@ trim_body
Prefix trimmed lines as a string blob.
@ only_body
Only directive bodies as a string blob.
@ full_lines
Complete lines.

Constructor & Destructor Documentation

◆ XrdOucGatherConf() [1/2]

XrdOucGatherConf::XrdOucGatherConf ( const char *  want,
XrdSysError errP = 0 
)

Constructor #1

Note
This object collects relevant configuration directives ready to be processed by the Get/Ret methods. All if-fi, set, and variable substitutions are performed.
Parameters
wantA space separated list of directive prefixes (i.e. end with a dot) and actual directives that should be gathered.
errPOptional pointer to an error object. When supplied, gathered lines are echoed. Additionally, error messages are issued. supplied XrdSysError object or using std::cerr using a

Definition at line 70 of file XrdOucGatherConf.cc.

71  : gcP(new XrdOucGatherConfData(errP))
72 {
73  XrdOucString wlist(want), wtoken;
74  int wlen, wPos = 0;
75 
76  while((wPos = wlist.tokenize(wtoken, wPos, ' ')) != -1)
77  {wlen = (wtoken.endswith('.') ? wtoken.length() : 0);
78  gcP->Match = new XrdOucTList(wtoken.c_str(), wlen, gcP->Match);
79  }
80 }
const char * c_str() const
bool endswith(char c)
int length() const

References XrdOucString::c_str(), XrdOucString::endswith(), XrdOucString::length(), and XrdOucGatherConfData::Match.

+ Here is the call graph for this function:

◆ XrdOucGatherConf() [2/2]

XrdOucGatherConf::XrdOucGatherConf ( const char **&  want,
XrdSysError errP = 0 
)

Constructor #2

Note
This is the same as constructor #1 but uses vector to hold the wanted directives or directive prefixes.
Parameters
wantA vector of strings of directive prefixes (i.e. end with a dot) and actual directives that should be gathered. The end of the vector is indicated by a nil pointer (e,g, const char *want[] = {"x.c", "y.", 0};
errPOptional pointer to an error object. When supplied, gathered lines are echoed. Additionally, error messages are issued. supplied XrdSysError object or using std::cerr using a

Definition at line 86 of file XrdOucGatherConf.cc.

87  : gcP(new XrdOucGatherConfData(errP))
88 {
89  int n, i = 0;
90 
91  while(want[i])
92  {if ((n = strlen(want[i])))
93  {if (*(want[i]+(n-1)) != '.') n = 0;
94  gcP->Match = new XrdOucTList(want[i], n, gcP->Match);
95  }
96  }
97 }

References XrdOucGatherConfData::Match.

◆ ~XrdOucGatherConf()

XrdOucGatherConf::~XrdOucGatherConf ( )

Definition at line 103 of file XrdOucGatherConf.cc.

104 {
105  XrdOucTList *tP;
106 
107  while((tP = gcP->Match))
108  {gcP->Match = tP->next;
109  delete tP;
110  }
111 
112  if (gcP->gBuff) free(gcP->gBuff);
113 
114  delete gcP;
115 }
XrdOucTList * next
Definition: XrdOucTList.hh:45

References XrdOucGatherConfData::gBuff, XrdOucGatherConfData::Match, and XrdOucTList::next.

Member Function Documentation

◆ EchoLine()

void XrdOucGatherConf::EchoLine ( )

Echo the last line retrieved using GetLine() using proper framing.

@notes 1) An exception is thrown if a XrdSysError object was not supplied.

Definition at line 121 of file XrdOucGatherConf.cc.

122 {
123 
124 // Make sure we can actually display anything
125 //
126  if (!(gcP->eDest))
127  throw std::invalid_argument("XrdSysError object not supplied!");
128 
129 // Echo only when we have something to echo
130 //
131  if (gcP->lline.length()) gcP->eDest->Say("=====> ", gcP->lline.c_str());
132 }
void Say(const char *text1, const char *text2=0, const char *txt3=0, const char *text4=0, const char *text5=0, const char *txt6=0)
Definition: XrdSysError.cc:141

References XrdOucString::c_str(), XrdOucGatherConfData::eDest, XrdOucString::length(), XrdOucGatherConfData::lline, and XrdSysError::Say().

+ Here is the call graph for this function:

◆ EchoOrder()

void XrdOucGatherConf::EchoOrder ( bool  doBefore)

Specift the order in which the last line is displayed vs a message.

@paramn doBefore - When true, the line is displayed before the message. When false, it is displayed after the message (default).

@notes 1) This call is only relevant to calls to MsgE(), MsgW(), MsgfE(), and MsgfW.

Definition at line 138 of file XrdOucGatherConf.cc.

139 {
140  gcP->echobfr = doBefore;
141 }

References XrdOucGatherConfData::echobfr.

◆ Gather()

int XrdOucGatherConf::Gather ( const char *  cfname,
Level  lvl,
const char *  parms = 0 
)

Definition at line 147 of file XrdOucGatherConf.cc.

148 {
149  XrdOucEnv myEnv;
150  XrdOucStream Config(gcP->eDest, getenv("XRDINSTANCE"), &myEnv, "=====> ");
151  XrdOucTList *tP;
152  XrdOucString theGrab;
153  char *var, drctv[64], body[4096];
154  int cfgFD, n, rc;
155  bool trim = false, addKey = true;
156 
157 // Make sure we have something to compare
158 //
159  if (!(gcP->Match)) return 0;
160 
161 // Reset the buffer if it has been set
162 //
163  if (gcP->gBuff)
164  {free(gcP->gBuff);
165  gcP->gBuff = 0;
166  gcP->Tokenizer.Attach(0);
167  }
168 
169 // Open the config file
170 //
171  if ( (cfgFD = open(cfname, O_RDONLY, 0)) < 0)
172  {rc = errno;
173  if (gcP->eDest) gcP->eDest->Emsg("Gcf", rc, "open config file", cfname);
174  return -rc;
175  }
176 
177 // Attach the file to our stream object and size the grab buffer
178 //
179  Config.Attach(cfgFD, 4096);
180  theGrab.resize(4096);
181  if (parms && *parms) theGrab = parms;
182 
183 // Setup for processing
184 //
185  switch(lvl)
186  {case full_lines: *drctv = '\n'; trim = false; addKey = true; break;
187  case trim_lines: *drctv = '\n'; trim = true; addKey = true; break;
188  case only_body: *drctv = ' '; trim = false; addKey = false; break;
189  case trim_body: *drctv = ' '; trim = true; addKey = true; break;
190  default: break; return 0; // Should never happen
191  break;
192  }
193 
194 // Process the config file
195 //
196  while((var = Config.GetMyFirstWord()))
197  {tP = gcP->Match;
198  while(tP && ((tP->val && strncmp(var, tP->text, tP->val)) ||
199  (!tP->val && strcmp( var, tP->text)))) tP = tP->next;
200 
201  if (tP)
202  {if (addKey)
203  {if (trim)
204  {char *dot = index(var, '.');
205  if (dot && *(dot+1)) var = dot+1;
206  }
207  int n = snprintf(drctv+1, sizeof(drctv)-1, "%s ", var);
208  if (n >= (int)sizeof(drctv)-1)
209  {if (gcP->eDest) gcP->eDest->Emsg("Gcf", E2BIG, "handle", var);
210  return -E2BIG;
211  }
212  } else drctv[1] = 0;
213 
214  if (!Config.GetRest(body, sizeof(body)))
215  {if (gcP->eDest) gcP->eDest->Emsg("Gcf", E2BIG, "handle arguments");
216  return -E2BIG;
217  }
218 
219  if (*body || addKey)
220  {theGrab += drctv;
221  theGrab += body;
222  }
223  }
224  }
225 
226 // Now check if any errors occurred during file i/o
227 //
228  if ((rc = Config.LastError()))
229  {if (gcP->eDest) gcP->eDest->Emsg("Gcf", rc, "read config file", cfname);
230  return (rc < 0 ? rc : -rc);
231  }
232 
233 
234 // Copy the grab to a modifiable buffer.
235 //
236  if ((n = theGrab.length()) <= 1) n = 0;
237  else {gcP->gBuff = (char *)malloc(n);
238  strcpy(gcP->gBuff, theGrab.c_str()+1); // skip 1st byte but add null
239  gcP->Tokenizer.Attach(gcP->gBuff);
240  n--;
241  }
242  return n;
243 }
void trim(std::string &str)
Definition: XrdHttpReq.cc:77
#define open
Definition: XrdPosix.hh:76
void resize(int lmx=0)
char * text
Definition: XrdOucTList.hh:46
void Attach(char *bp)
int Emsg(const char *esfx, int ecode, const char *text1, const char *text2=0)
Definition: XrdSysError.cc:95
XrdCmsConfig Config
XrdOucTokenizer Tokenizer

References XrdOucTokenizer::Attach(), XrdOucString::c_str(), XrdCms::Config, XrdOucGatherConfData::eDest, XrdSysError::Emsg(), full_lines, XrdOucGatherConfData::gBuff, XrdOucString::length(), XrdOucGatherConfData::Match, XrdOucTList::next, only_body, open, XrdOucString::resize(), XrdOucTList::text, XrdOucGatherConfData::Tokenizer, trim(), trim_body, and trim_lines.

Referenced by XrdOssStats::FileSystem::Config(), XrdHttpCorsHandler::Configure(), and XrdOfsgetPrepare().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ GetLine()

char * XrdOucGatherConf::GetLine ( )

Sequence to the next line in the configuration file.

Returns
Pointer to the next line that will be tokenized or NIL if there are no more lines left.

@notes 1) You must call GetLine() before calling GetToken().

Definition at line 249 of file XrdOucGatherConf.cc.

250 {
251  char* theLine = gcP->Tokenizer.GetLine();
252 
253  while(theLine && *theLine == 0) theLine = gcP->Tokenizer.GetLine();
254 
255  if (!theLine) gcP->lline = "";
256  else gcP->lline = theLine;
257 
258  return theLine;
259 }

References XrdOucTokenizer::GetLine(), XrdOucGatherConfData::lline, and XrdOucGatherConfData::Tokenizer.

Referenced by XrdOssStats::FileSystem::Config(), XrdHttpCorsHandler::Configure(), and XrdOfsgetPrepare().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ GetToken()

char * XrdOucGatherConf::GetToken ( char **  rest = 0,
int  lowcase = 0 
)

Get the next blank-delimited token in the record returned by Getline().

Parameters
rest- Address of a char pointer. When specified, a pointer to the first non-blank character after the returned token.
lowcasee- When 1, all characters are converted to lower case. When 0, the default, the characters are not changed.
Returns
A pointer to the next token. If the end of the line has been reached, a NIL pointer is returned.

Definition at line 265 of file XrdOucGatherConf.cc.

266 {
267  return gcP->Tokenizer.GetToken(rest, lowcase);
268 }
char * GetToken(char **rest=0, int lowcase=0)

References XrdOucTokenizer::GetToken(), and XrdOucGatherConfData::Tokenizer.

Referenced by XrdOssStats::FileSystem::Config(), XrdHttpCorsHandler::Configure(), and XrdOfsgetPrepare().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ hasData()

bool XrdOucGatherConf::hasData ( )

Check if data is present.

Returns
True if data is present and false, otherwise.

Definition at line 274 of file XrdOucGatherConf.cc.

275 {
276  return gcP->gBuff != 0 && *(gcP->gBuff) != 0;
277 }

References XrdOucGatherConfData::gBuff.

◆ LastLine()

const char * XrdOucGatherConf::LastLine ( )

Get the last line.

Returns
pointer to the last line. If no last line exists a null string is returned. The pointer is valid until GetLine() is called.

Definition at line 283 of file XrdOucGatherConf.cc.

284 {
285  if (gcP->lline.capacity() == 0) return "";
286  return gcP->lline.c_str();
287 }
int capacity() const

References XrdOucString::c_str(), XrdOucString::capacity(), and XrdOucGatherConfData::lline.

+ Here is the call graph for this function:

◆ MsgE()

void XrdOucGatherConf::MsgE ( const char *  txt1,
const char *  txt2 = 0,
const char *  txt3 = 0,
const char *  txt4 = 0,
const char *  txt5 = 0,
const char *  txt6 = 0 
)

Display a space delimited error/warning message.

Parameters
txt1,txt2,txt3,txt4,txt5,txt6the message components formatted as "txt1 [txt2] [txt3] [txt4] [txt5] [txt6]"

@notes 1) This methods throws an exception if a XrdSysError object was not passed to the constructor. 2) The last line returned by this object will be displayed either before or after the message (see EchoOrder()). 3) Messages are truncated at 2048 bytes. 4} Use MsgE for errors. The text is prefixed by "Config mistake:" Use MsgW for warnings. The text is prefixed by "Config warning:"

Definition at line 293 of file XrdOucGatherConf.cc.

295 {
296  const char* mVec[7];
297  int n = 0;
298 
299  mVec[n++] = "Config mistake:";
300  if (txt1) mVec[n++] = txt1;
301  if (txt2) mVec[n++] = txt2;
302  if (txt3) mVec[n++] = txt3;
303  if (txt4) mVec[n++] = txt4;
304  if (txt5) mVec[n++] = txt5;
305  if (txt6) mVec[n++] = txt6;
306 
307  MsgX(mVec, n+1);
308 }

◆ MsgfE()

void XrdOucGatherConf::MsgfE ( const char *  fmt,
  ... 
)

Display a formated error/warning message using variable args (i.e. vprintf).

Parameters
fmtthe message formatting template (i.e. printf format).
...the arguments that should be used with the template. The formatted message is truncated at 2048 bytes.

@notes 1) This methods throws an exception if a XrdSysError object was not passed to the constructor. 2) The last line returned by this object will be displayed either before or after the message (see EchoOrder()). 3) Messages are truncated at 2048 bytes. 4} Use MsgfE for errors. The text is prefixed by "Config mistake:" Use MsgfW for warnings. The text is prefixed by "Config warning:"

Definition at line 362 of file XrdOucGatherConf.cc.

363 {
364  char buffer[2048];
365  va_list args;
366  va_start (args, fmt);
367 
368 // Format the message
369 //
370  vsnprintf(buffer, sizeof(buffer), fmt, args);
371 
372 // Go print the message
373 //
374  MsgfX("Config mistake: ", buffer);
375 }

◆ MsgfW()

void XrdOucGatherConf::MsgfW ( const char *  fmt,
  ... 
)

Definition at line 381 of file XrdOucGatherConf.cc.

382 {
383  char buffer[2048];
384  va_list args;
385  va_start (args, fmt);
386 
387 // Format the message
388 //
389  vsnprintf(buffer, sizeof(buffer), fmt, args);
390 
391 // Go print the message
392 //
393  MsgfX("Config warning: ", buffer);
394 }

◆ MsgW()

void XrdOucGatherConf::MsgW ( const char *  txt1,
const char *  txt2 = 0,
const char *  txt3 = 0,
const char *  txt4 = 0,
const char *  txt5 = 0,
const char *  txt6 = 0 
)

Definition at line 314 of file XrdOucGatherConf.cc.

316 {
317  const char* mVec[7];
318  int n = 0;
319 
320  mVec[n++] = "Config warning:";
321  if (txt1) mVec[n++] = txt1;
322  if (txt2) mVec[n++] = txt2;
323  if (txt3) mVec[n++] = txt3;
324  if (txt4) mVec[n++] = txt4;
325  if (txt5) mVec[n++] = txt5;
326  if (txt6) mVec[n++] = txt6;
327 
328  MsgX(mVec, n+1);
329 }

◆ RetToken()

void XrdOucGatherConf::RetToken ( )

Backups the token scanner just prior to the last returned token.

@notes 1) Only one backup is allowed. Calling RetToken() more than once without an intervening GetToken() call results in undefined behaviour. 2) This call is useful for backing up due to an overscan.

Definition at line 419 of file XrdOucGatherConf.cc.

420 {
421  return gcP->Tokenizer.RetToken();
422 }

References XrdOucTokenizer::RetToken(), and XrdOucGatherConfData::Tokenizer.

+ Here is the call graph for this function:

◆ Tabs()

void XrdOucGatherConf::Tabs ( int  x = 1)

Specify how tag characters should be handled.

Parameters
x- When 0, tabs are converted to spaces. When 1, tabs are untouched (the default).

Definition at line 428 of file XrdOucGatherConf.cc.

429 {
430  gcP->Tokenizer.Tabs(x);
431 }
void Tabs(int x=1)

References XrdOucTokenizer::Tabs(), and XrdOucGatherConfData::Tokenizer.

+ Here is the call graph for this function:

◆ useData()

bool XrdOucGatherConf::useData ( const char *  data)

Attempt to use pre-existing data.

Parameters
data- Pointer to null terminated pre-existing data.
Returns
False if the pointer is nil or points to a null string; true o/w.

Definition at line 437 of file XrdOucGatherConf.cc.

438 {
439  if (!data || *data == 0) return false;
440 
441  if (gcP->gBuff) free(gcP->gBuff);
442  gcP->gBuff = strdup(data);
443  gcP->Tokenizer.Attach(gcP->gBuff);
444  return true;
445 }

References XrdOucTokenizer::Attach(), XrdOucGatherConfData::gBuff, and XrdOucGatherConfData::Tokenizer.

Referenced by XrdOfsgetPrepare().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

The documentation for this class was generated from the following files: