0

I have a string in the format of

"SERVERID=12345\nSERVERKEY=asdasw\nSERVERAPPID=123213\n"

I've parsed this line by line using strtok(). Is there a way to make a key-value pair of each line? Like maybe store them in some sort of an array? What I mean is, if I want SERVERID, I just say Array['SERVERID'] and it'll return 12345 as the value. Is there a way to do it in C?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • 6
    No. Welcome to C. – melpomene Jul 03 '18 at 05:03
  • There are (non-standard) ways to achieve more or less the result, but not using the notation you suggest. The string should be in double quotes, and you'd need to use a function call to do the lookup, and you'd use a structure behind the scenes, and you'd have other functions to create the array of key-value pairs, and to add key-value pairs, and to remove them, and so on. It would be done using functions all the way around. – Jonathan Leffler Jul 03 '18 at 05:06
  • will `find(dict, "SERVERID")` do? – n. m. could be an AI Jul 03 '18 at 05:06
  • You need to write your own data structure for that in C. If you can write it in C++ i would use that and use a map that's built in –  Jul 03 '18 at 05:08
  • @JonathanLeffler Is there an example code of such an implementation? – Shantanu Mhapankar Jul 03 '18 at 05:10
  • I'm thinking of the code in "C Interfaces and Implementations: Techniques for Creating Reusable Software" by D R Hanson (Addison-Wesley Professional Computing Series, 1997, ISBN 0-201-49841-3). Source etc available from GitHub (https://github.com/drh/cii). – Jonathan Leffler Jul 03 '18 at 05:16
  • Depending on your implementation, your could try the functions `hsearch` or `lsearch`. They are not standard C but exist on most posix systems. – Serge Ballesta Jul 03 '18 at 05:26

2 Answers2

3

Standard C doesn't give you any "container types" like the one you're looking for. Still you have several options to solve your problem:

  1. Might sound silly, but you could switch to a different language. It's at least something to think about. For example, C++ comes to mind, offering the std::map type.

  2. Use some third-party library. A popular choice is GLib, it offers a hash table you could use.

  3. There's always the option to implement it yourself. A very simple idea would be to use an array of a struct, e.g.

    struct stringpair {
        char *key;
        char *value;
    };
    
    struct stringpair *table;
    
    // ...
    size_t entries = 42; // number of entries you found
    table = malloc(entries * sizeof *table); // check for NULL after this
    
    // look for an entry:
    char *value = 0;
    for (size_t i = 0; i < entries; ++i)
    {
        if (!strcmp(table[i].key, "SERVERID"))
        {
            value = table[i].value;
            break;
        }
    }
    

    This is of course a very basic solution and doesn't perform well with a lot of entries. I also didn't test this code, it's just to give you a rough idea. If you need fast lookups, you will need to implement a hashtable instead, or use option 1 or 2.

0

What you're looking for here would be a form of a dictionary. Typically, but not always, these use hashtables. There are many ways of creating a hashtable in C

You can find your answer here: What is a hash table and how do you make it in C?

You could also see other implementations such as a combined data structures, like a dictionary-linked list, and more. Just find a way to convert your string (char *) into a number, and find a way to use that number as an index.

Nova Ardent
  • 161
  • 1
  • 16
  • 2
    This looks more as a comment instead of answer. – unalignedmemoryaccess Jul 03 '18 at 06:47
  • I don't have commenting privileges yet, but would have liked to answer this question anyways. Do you recommend I delete it? if so, could I have a further explanation as not to make the same mistake down the road? – Nova Ardent Jul 03 '18 at 06:49
  • I'd say it's not a good answer, still the link and short explanation could be helpful, so, it's not a *bad* one either -> keep it. –  Jul 03 '18 at 09:31