Linux - SoftwareThis forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I wrote a program. The first iteration I passed every parameter on the command line. That got unwieldy as I had to add more. I'd like to have it read a configuration file instead. If it were a script, I'd just have it
There isn't a convenient function included with the standard library, but using existing functions you can roll your own. If you want I'll share a function that I use when I need simple pattern matching.
I want a program to read parameters from a configuration file instead of the command line. For instance I want to have a plain-text initialization file that looks like:
Quote:
Fruit: Apple
Fish: Trout
Vegetable: Asparagus
and have the program assign the value Apple to the variable Fruit...
Quote:
Originally Posted by rtmistler
One can also look at the source code for each of grep and also strstr().
Duh. I can also write it in assembly and use no libraries.
Quote:
Originally Posted by individual
There isn't a convenient function included with the standard library
It's done so often that surprises me. I found getconf and confstr, but they don't seem to apply
Quote:
Originally Posted by individual
If you want I'll share a function that I use when I need simple pattern matching.
That'd be nice. I started by fopen-ing it, reading it line-ly with getline, picking out the first word, running that through switch on all the possible variables.
Later: discovered strtok, the function perfect for separating elements on a line.
Last edited by RandomTroll; 07-13-2019 at 10:05 AM.
Reason: Add information
Yes. It would have been nice to have a switch that worked on strings, but a cascade of else ifs isn't awful. If I were really serious I'd convert to a database and query that.
Useful note: fgets, getline, etc., emit the newline with the last token on the line. That took a while to figure out and strip out.
In one of my C books I'm studying the author provides his own function for testing for the newline and removing it if it's been read by fgets().
I'll post his solution in case you might find it useful.
Code:
char *s_gets(char *st, int n)
{
char *ret_val;
char *find;
ret_val = fgets(st, n, stdin);
if (ret_val)
{
find = strchr(st, '\n'); /* look for newline */
if (find) /* if the address is not null... */
*find = '\0'; /* ...place a null character there */
else
while (getchar() != '\n') /* dispose of rest of line */
continue;
}
return ret_val;
}
There isn't a convenient function included with the standard library, but using existing functions you can roll your own. If you want I'll share a function that I use when I need simple pattern matching.
To get more grep-like behaviour, you can use the functions contained in the pcre(3) library
Quote:
The PCRE library is a set of functions that implement regular expression pattern matching using the same syntax and semantics as Perl, with just a few differences.
Some features that appeared in Python and PCRE before they appeared in Perl are also available using the Python syntax, there is some support for one or two .NET and Oniguruma syntax items, and there is an option for requesting some minor changes that give better JavaScript compatibility.
This probably goes much further then what the OP requested, but it is a possible answer to the subject.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.