allow tilde expansion for section names in config file

This commit is contained in:
dockes 2005-11-17 12:47:03 +00:00
parent 22e229fdca
commit 511e513543
7 changed files with 74 additions and 39 deletions

View File

@ -1,7 +1,8 @@
#ifndef lint #ifndef lint
static char rcsid[] = "@(#$Id: rclconfig.cpp,v 1.10 2005-11-05 14:40:50 dockes Exp $ (C) 2004 J.F.Dockes"; static char rcsid[] = "@(#$Id: rclconfig.cpp,v 1.11 2005-11-17 12:47:03 dockes Exp $ (C) 2004 J.F.Dockes";
#endif #endif
#include <unistd.h> #include <unistd.h>
#include <errno.h>
#include <iostream> #include <iostream>
@ -80,6 +81,29 @@ RclConfig::RclConfig()
return; return;
} }
bool RclConfig::getConfParam(const std::string &name, int *ivp)
{
string value;
if (!getConfParam(name, value))
return false;
errno = 0;
long lval = strtol(value.c_str(), 0, 0);
if (lval == 0 && errno)
return 0;
if (ivp)
*ivp = int(lval);
return true;
}
bool RclConfig::getConfParam(const std::string &name, bool *value)
{
int ival;
if (!getConfParam(name, &ival))
return false;
if (*value)
*value = ival ? true : false;
return true;
}
static ConfSimple::WalkerCode mtypesWalker(void *l, static ConfSimple::WalkerCode mtypesWalker(void *l,
const char *nm, const char *value) const char *nm, const char *value)
{ {

View File

@ -1,6 +1,6 @@
#ifndef _RCLCONFIG_H_INCLUDED_ #ifndef _RCLCONFIG_H_INCLUDED_
#define _RCLCONFIG_H_INCLUDED_ #define _RCLCONFIG_H_INCLUDED_
/* @(#$Id: rclconfig.h,v 1.6 2005-11-05 14:40:50 dockes Exp $ (C) 2004 J.F.Dockes */ /* @(#$Id: rclconfig.h,v 1.7 2005-11-17 12:47:03 dockes Exp $ (C) 2004 J.F.Dockes */
#include <list> #include <list>
@ -38,6 +38,13 @@ class RclConfig {
return false; return false;
return conf->get(name, value, keydir); return conf->get(name, value, keydir);
} }
/*
* Variants with autoconversion
*/
bool getConfParam(const std::string &name, int *value);
bool getConfParam(const std::string &name, bool *value);
/// Set current directory reference, and fetch automatic parameters. /// Set current directory reference, and fetch automatic parameters.
void setKeyDir(const string &dir) void setKeyDir(const string &dir)
{ {

View File

@ -1,5 +1,5 @@
#ifndef lint #ifndef lint
static char rcsid[] = "@(#$Id: main.cpp,v 1.15 2005-11-16 15:07:20 dockes Exp $ (C) 2005 J.F.Dockes"; static char rcsid[] = "@(#$Id: main.cpp,v 1.16 2005-11-17 12:47:03 dockes Exp $ (C) 2005 J.F.Dockes";
#endif #endif
#include <unistd.h> #include <unistd.h>
@ -160,11 +160,8 @@ int main( int argc, char ** argv )
exit(1); exit(1);
} }
string tmp; showicons = false;
rclconfig->getConfParam("showicons", tmp); rclconfig->getConfParam("showicons", &showicons);
if (tmp.empty())
tmp = "0";
showicons = atoi(tmp.c_str()) ? true : false;
rclconfig->getConfParam("iconsdir", iconsdir); rclconfig->getConfParam("iconsdir", iconsdir);
if (iconsdir.empty()) if (iconsdir.empty())
iconsdir = "/usr/local/share/recoll/images"; iconsdir = "/usr/local/share/recoll/images";

View File

@ -206,7 +206,7 @@
<string>E&amp;xit</string> <string>E&amp;xit</string>
</property> </property>
<property name="accel"> <property name="accel">
<string></string> <string>Ctrl+Q</string>
</property> </property>
</action> </action>
<action> <action>

View File

@ -1,4 +1,4 @@
# @(#$Id: recoll.conf,v 1.7 2005-11-16 15:07:20 dockes Exp $ (C) 2004 J.F.Dockes # @(#$Id: recoll.conf,v 1.8 2005-11-17 12:47:03 dockes Exp $ (C) 2004 J.F.Dockes
# Recoll default configuration file. This should be copied to # Recoll default configuration file. This should be copied to
# ~/.recoll/recoll.conf # ~/.recoll/recoll.conf
@ -52,8 +52,7 @@ guesscharset = 0
# indexation of many bogus 'text' files # indexation of many bogus 'text' files
usesystemfilecommand = 1 usesystemfilecommand = 1
# You could specify different parameters for a subdirectory like this. No # You could specify different parameters for a subdirectory like this.
# tilde substitution there for now, sorry: #[~/englishdocs/plain]
#[/home/me/englishdocs/plain]
#defaultcharset = iso-8859-2 #defaultcharset = iso-8859-2

View File

@ -1,5 +1,5 @@
#ifndef lint #ifndef lint
static char rcsid [] = "@(#$Id: conftree.cpp,v 1.1 2005-11-12 14:24:33 dockes Exp $ (C) 2003 J.F.Dockes"; static char rcsid [] = "@(#$Id: conftree.cpp,v 1.2 2005-11-17 12:47:03 dockes Exp $ (C) 2003 J.F.Dockes";
#endif #endif
#ifdef HAVE_CONFIG_H #ifdef HAVE_CONFIG_H
#include "config.h" #include "config.h"
@ -14,6 +14,7 @@ static char rcsid [] = "@(#$Id: conftree.cpp,v 1.1 2005-11-12 14:24:33 dockes Ex
#include <sstream> #include <sstream>
#include "conftree.h" #include "conftree.h"
#include "pathut.h"
#ifndef NO_NAMESPACES #ifndef NO_NAMESPACES
using namespace std; using namespace std;
@ -48,11 +49,10 @@ void ConfSimple::parseinput(istream &input)
for (;;) { for (;;) {
input.getline(cline, LL-1); input.getline(cline, LL-1);
//fprintf(stderr, "Line: '%s'\n", cline); // fprintf(stderr, "Line: '%s' status %d\n", cline, int(status));
if (!input.good()) { if (!input.good()) {
if (input.bad()) { if (input.bad()) {
status = STATUS_ERROR; status = STATUS_ERROR;
//fprintf(stderr, "ConfSimple:parseinput: fatal error\n");
return; return;
} }
// Must be eof ? // Must be eof ?
@ -84,6 +84,9 @@ void ConfSimple::parseinput(istream &input)
if (line[0] == '[') { if (line[0] == '[') {
trimstring(line, "[]"); trimstring(line, "[]");
if (dotildexpand)
submapkey = path_tildexpand(line);
else
submapkey = line; submapkey = line;
continue; continue;
} }
@ -118,25 +121,28 @@ void ConfSimple::parseinput(istream &input)
} }
} }
ConfSimple::ConfSimple(string *d, int readonly)
ConfSimple::ConfSimple(string *d, int readonly, bool tildexp)
{ {
filename = "";
data = d; data = d;
dotildexpand = tildexp;
status = readonly ? STATUS_RO : STATUS_RW; status = readonly ? STATUS_RO : STATUS_RW;
stringstream input(*d, ios::in); stringstream input(*d, ios::in);
parseinput(input); parseinput(input);
} }
ConfSimple::ConfSimple(const char *fname, int readonly)
ConfSimple::ConfSimple(const char *fname, int readonly, bool tildexp)
{ {
filename = string(fname);
data = 0; data = 0;
filename = string(fname);
dotildexpand = tildexp;
status = readonly ? STATUS_RO : STATUS_RW;
ifstream input; ifstream input;
if (readonly) { if (readonly) {
input.open(fname, ios::in); input.open(fname, ios::in);
status = STATUS_RO;
} else { } else {
ios::openmode mode = ios::in|ios::out; ios::openmode mode = ios::in|ios::out;
// It seems that there is no separate 'create if not exists' // It seems that there is no separate 'create if not exists'
@ -331,11 +337,6 @@ list<string> ConfSimple::getKeys()
return mylist; return mylist;
} }
static inline void path_catslash(std::string &s) {
if (s.empty() || s[s.length() - 1] != '/')
s += '/';
}
int ConfTree::get(const std::string &name, string &value, const string &sk) int ConfTree::get(const std::string &name, string &value, const string &sk)
{ {
if (sk.empty() || sk[0] != '/') { if (sk.empty() || sk[0] != '/') {
@ -653,7 +654,7 @@ int main(int argc, char **argv)
} }
} else { } else {
ConfSimple parms(filename, 1); ConfTree parms(filename, 1);
if (parms.getStatus() == ConfSimple::STATUS_ERROR) { if (parms.getStatus() == ConfSimple::STATUS_ERROR) {
fprintf(stderr, "Open failed\n"); fprintf(stderr, "Open failed\n");
exit(1); exit(1);

View File

@ -37,23 +37,22 @@ using std::map;
class ConfSimple { class ConfSimple {
public: public:
enum StatusCode {STATUS_ERROR=0, STATUS_RO=1, STATUS_RW=2}; enum StatusCode {STATUS_ERROR=0, STATUS_RO=1, STATUS_RW=2};
private:
string filename; // set if we're working with a file
string *data; // set if we're working with an in-memory string
map<string, map<string, string> > submaps;
StatusCode status;
void parseinput(std::istream &input);
public:
/** /**
* Build the object by reading content from file. * Build the object by reading content from file.
* @param filename file to open
* @param readonly if true open readonly, else rw
* @param tildexp try tilde (home dir) expansion for subkey values
*/ */
ConfSimple(const char *fname, int readonly = 0); ConfSimple(const char *fname, int readonly = 0, bool tildexp = false);
/** /**
* Build the object by reading content from a string * Build the object by reading content from a string
* @param data points to the data to parse
* @param readonly if true open readonly, else rw
* @param tildexp try tilde (home dir) expansion for subsection names
*/ */
ConfSimple(string *data, int readonly = 0); ConfSimple(string *data, int readonly = 0, bool tildexp = false);
virtual ~ConfSimple() {}; virtual ~ConfSimple() {};
@ -98,6 +97,15 @@ class ConfSimple {
* Return all key names: * Return all key names:
*/ */
std::list<string> getKeys(); std::list<string> getKeys();
protected:
bool dotildexpand;
private:
string filename; // set if we're working with a file
string *data; // set if we're working with an in-memory string
map<string, map<string, string> > submaps;
StatusCode status;
void parseinput(std::istream &input);
}; };
/** /**
@ -124,7 +132,7 @@ class ConfTree : public ConfSimple {
* Build the object by reading content from file. * Build the object by reading content from file.
*/ */
ConfTree(const char *fname, int readonly = 0) ConfTree(const char *fname, int readonly = 0)
: ConfSimple(fname, readonly) {} : ConfSimple(fname, readonly, true) {}
virtual ~ConfTree() {}; virtual ~ConfTree() {};
/** /**
@ -147,5 +155,4 @@ class ConfTree : public ConfSimple {
static bool stringToBool(const string &s); static bool stringToBool(const string &s);
}; };
#endif /*_CONFTREE_H_ */ #endif /*_CONFTREE_H_ */