I have a daemon that reads a configuration file to find out where to write something. The following line exists in the configuration file:
output = /tmp/foo/%d/%s/output
Or it might look like this:
output = /tmp/foo/%s/output/%d
... or just like that:
output = /tmp/foo/%s/output
... or finally:
output = /tmp/output
I have this line as cfg-> pathfmt in my program. What I'm trying to do now is come up with some kind of smart way to use it.
A little more explanation, a path can contain up to two components for formatting. % d will be expanded as job identifier (int),% s as job name (string). The user may want to use one or both in the configuration file. I need to know what they want and in what order before I finally pass it snprintf (). I can narrow it down a bit, but I still want to talk to strtok (), and that seems ugly.
I want to give users this kind of flexibility, but I get lost looking for a reasonable, portable way to implement it. I will also completely and completely lose how to start looking for this.
I would be very happy if:
- Someone can help me narrow my search phrase to find good examples.
- Someone may post a link to some OSS project that implements this
- Someone might post some psuedo code
I donβt need the code written for me, I'm just fixated on the fact that (I think) should be something very simple and need some help taking the first bite. It really seems to me that I changed my mind and did not notice the obvious.
The end result should be a logical function such as this:
bool output_sugar(const char *fmt, int jobid, const char *jobname, struct job *j);
Then it will call snprintf () (reasonably) on j-> outpath, returning false if any garbage (i.e.% followed by something other than s, d or%) is in the configuration line (or its zero). Sanity checks are easy, I just get a bit of time for the number (and order) of arguments to format correctly.
Thanks in advance. Also, feel free to edit this headline if you have a reputation to do this, as I said, I'm not quite sure how to ask a question on one line. I think I need a parser , but it feels awkward using a full lexer / parser to process one simple string.