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 }
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 119 of file XrdOucGatherConf.cc.

120 {
121 
122 // Make sure we can actually display anything
123 //
124  if (!(gcP->eDest))
125  throw std::invalid_argument("XrdSysError object not supplied!");
126 
127 // Echo only when we have something to echo
128 //
129  if (gcP->lline.length()) gcP->eDest->Say("=====> ", gcP->lline.c_str());
130 }
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 136 of file XrdOucGatherConf.cc.

137 {
138  gcP->echobfr = doBefore;
139 }

References XrdOucGatherConfData::echobfr.

◆ Gather()

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

Definition at line 145 of file XrdOucGatherConf.cc.

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

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

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

Referenced by StatsFileSystem::Config(), 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 263 of file XrdOucGatherConf.cc.

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

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

Referenced by StatsFileSystem::Config(), 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 272 of file XrdOucGatherConf.cc.

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

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 281 of file XrdOucGatherConf.cc.

282 {
283  if (gcP->lline.capacity() == 0) return "";
284  return gcP->lline.c_str();
285 }
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 291 of file XrdOucGatherConf.cc.

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

◆ 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 360 of file XrdOucGatherConf.cc.

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

◆ MsgfW()

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

Definition at line 379 of file XrdOucGatherConf.cc.

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

◆ 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 312 of file XrdOucGatherConf.cc.

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

◆ 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 417 of file XrdOucGatherConf.cc.

418 {
419  return gcP->Tokenizer.RetToken();
420 }

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 426 of file XrdOucGatherConf.cc.

427 {
428  gcP->Tokenizer.Tabs(x);
429 }
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 435 of file XrdOucGatherConf.cc.

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

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: