hello I am trying to create an robot and I have been having a problem parsing the full string from the conf. As example the IRCServer, botnick, botname parameter does not contain the all the data. as the output shows:

#####################
#    Ablaze Bot     #
#####################

Loading conf file: Success.... 
        Botnick= 
        Botname= 
        IRCServer= aBlazeNet
        IRCPort= 6667
        BindAdr= 
        BindPort= 9050
        Channels= #aBlazeNet
        Debug= 2

An example conf:

Botnick=aBlaze
Botname=aBlazeNet IRC Bot
IRCServer=localhost
IRCPort=6667
BindAdr=0.0.0.0
BindPort=9050
Channels=#aBlazeNet
Debug=2

The program itself:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
FILE *conffp;
char confbuffer[1024];

char *Botnick;
char *Botname;
char *IRCServer;
int IRCPort;
char *BindAdr;
int BindPort;
char *Channels;
int Debug;


int main(int argc, char *argv[]) {
     printf("\n#####################\n#    Ablaze Bot     #\n#####################\n\n");
     
     printf("Loading conf file: ");
     if ((conffp = fopen ("ablaze.conf","r")) != NULL) {
     char *key, *value;
     while(fgets(confbuffer, 1024, conffp)!=NULL) {
     if(confbuffer[0] == ';') continue;
     key = strtok( confbuffer, "=" );
     value = strtok( NULL, "\n");
     
     if(strcmp(key,"Botnick")== 0) Botnick = value;
     if(strcmp(key,"Botname")== 0) Botname = value;
     if(strcmp(key,"IRCServer")== 0) IRCServer = value;
     if(strcmp(key,"IRCPort")== 0) IRCPort = atoi(value);
     if(strcmp(key,"BindAdr")== 0) BindAdr = value;
     if(strcmp(key,"BindPort")== 0) BindPort = atoi(value);
     if(strcmp(key,"Channels")== 0) Channels = value;
     if(strcmp(key,"Debug")== 0) Debug = atoi(value);
     //printf(value);
     }
     
     printf("Success.... %s\n", Botnick);
     if(Debug > 1) {
          printf("\tBotnick= %s\n", Botnick);
          printf("\tBotname= %s\n", Botname);
          printf("\tIRCServer= %s\n", IRCServer);
          printf("\tIRCPort= %i\n", IRCPort);
          printf("\tBindAdr= %s\n", BindAdr);
          printf("\tBindPort= %i\n", BindPort);
          printf("\tChannels= %s\n", Channels);
          printf("\tDebug= %i\n", Debug);

     }
     
     
     fclose(conffp);
     } else {
     printf ("could not be opened...\n");
     }
     return 0;
}

I was wondering if there is a way to improve and/or fix this conf system? Thank you for your time.

Watch for spaces around '='.

I am kind of new to C can you give me an example on how to do that?


Thank you!

check your loop how this program assign key and value;

debug your loop.

if(strcmp(key,"Botnick")== 0) Botnick = value;

   /* add this */
       printf("\n%s", Botnick);

     if(strcmp(key,"Botname")== 0) Botname = value;
     if(strcmp(key,"IRCServer")== 0) IRCServer = value;
     if(strcmp(key,"IRCPort")== 0) IRCPort = atoi(value);
     if(strcmp(key,"BindAdr")== 0) BindAdr = value;
     if(strcmp(key,"BindPort")== 0) BindPort = atoi(value);
     if(strcmp(key,"Channels")== 0) Channels = value;
     if(strcmp(key,"Debug")== 0) Debug = atoi(value);

Edited 6 Years Ago by phpbeginners: n/a

all the values assign to Botnick;

you try to add on the loop 

printf("%s" = "%s", key, value)

Edited 6 Years Ago by phpbeginners: n/a

here is the new while loop and debug statements:

printf("Loading conf file:\n");
     if ((conffp = fopen ("ablaze.conf","r")) != NULL) {
     char *key, *value;
     while(fgets(confbuffer, 1024, conffp)!=NULL) {
     if(confbuffer[0] == ';') continue;
     key = strtok( confbuffer, "=" );
     value = strtok( NULL, "\n");
     printf("in config: %s = %s\n", key, value);
     
     if(strcmp(key,"Botnick")== 0) { 
          Botnick = value;
          printf("in mem: Botnick= %s\n", Botnick);
     }
     
     if(strcmp(key,"Botname")== 0) {
          Botname = value;
          printf("in mem: Botname= %s\n", Botname);
     }
     
     if(strcmp(key,"IRCServer")== 0) {
          IRCServer = value;
          printf("in mem: IRCServer= %s\n", IRCServer);
     }
     
     if(strcmp(key,"IRCPort")== 0) {
          IRCPort = atoi(value);
          printf("in mem: IRCPort= %i\n", IRCPort);
     }
     
     if(strcmp(key,"BindAdr")== 0) {
          BindAdr = value;
          printf("in mem: BindAdr= %s\n", BindAdr);
     }
     
     if(strcmp(key,"BindPort")== 0) {
          BindPort = atoi(value);
          printf("in mem: BindPort= %i\n", BindPort);
     }
     
     if(strcmp(key,"Channels")== 0) {
          Channels = value;
          printf("in mem: Channels= %s\n", Channels);
     }
     
     if(strcmp(key,"Debug")== 0){
          Debug = atoi(value);
          printf(" in mem: Debug= %i\n", Debug);
     }
     
     
     }
     
     printf("Success.... %s\n", Botnick);
     if(Debug > 1) {
          printf("\tBotnick= %s\n", Botnick);
          printf("\tBotname= %s\n", Botname);
          printf("\tIRCServer= %s\n", IRCServer);
          printf("\tIRCPort= %i\n", IRCPort);
          printf("\tBindAdr= %s\n", BindAdr);
          printf("\tBindPort= %i\n", BindPort);
          printf("\tChannels= %s\n", Channels);
          printf("\tDebug= %i\n", Debug);

     }

my new results:

Loading conf file:
in config: Botnick = aBlaze
in mem: Botnick= aBlaze
in config: Botname = aBlazeNet IRC Bot
in mem: Botname= aBlazeNet IRC Bot
in config: IRCServer = localhost
in mem: IRCServer= localhost
in config: IRCPort = 6667
in mem: IRCPort= 6667
in config: BindAdr = 0.0.0.0
in mem: BindAdr= 0.0.0.0
in config: BindPort = 9050
in mem: BindPort= 9050
in config: Channels = #aBlazeNet
in mem: Channels= #aBlazeNet
in config: Debug = 2
 in mem: Debug= 2
Success.... 
        Botnick= 
        Botname= 
        IRCServer= aBlazeNet
        IRCPort= 6667
        BindAdr= 
        BindPort= 9050
        Channels= #aBlazeNet
        Debug= 2

In a way it is like the variables fall out of scrope but there globally set. :/

Edited 6 Years Ago by matthewl: n/a

OK, now I really see your problem (please disregard my previous post).
Pay attention to the lines 11, 16, 21, 31 and 41. There you assign value, which points somewhere into confbuffer, which content keeps changing, and eventually becomes nothing (try to print both Botnick and Botname at line 17 - you'd be surprised). By contrast, in lines 26, 36 and 46 you convert the value into an integer right away, and the result of conversion stays put.
My recommendation is to deploy strdup, such as Botname = strdup(value); etc.

Edited 6 Years Ago by nezachem: n/a

Oh my goodness it was as simple as strdup()! I am shaking my head that I ignored using that function/I really did not know what it really did.

this is my result of it:

Loading conf file:
in config: Botnick = aBlaze
in mem: Botnick= aBlaze
in config: Botname = aBlazeNet IRC Bot
in mem: Botname= aBlazeNet IRC Bot
in config: IRCServer = localhost
in mem: IRCServer= localhost
in config: IRCPort = 6667
in mem: IRCPort= 6667
in config: BindAdr = 0.0.0.0
in mem: BindAdr= 0.0.0.0
in config: BindPort = 9050
in mem: BindPort= 9050
in config: Channels = #aBlazeNet
in mem: Channels= #aBlazeNet
in config: Debug = 2
 in mem: Debug= 2
Success.... aBlaze
        Botnick= aBlaze
        Botname= aBlazeNet IRC Bot
        IRCServer= localhost
        IRCPort= 6667
        BindAdr= 0.0.0.0
        BindPort= 9050
        Channels= #aBlazeNet
        Debug= 2

On to my next challenge the sockets. LOL!

This question has already been answered. Start a new discussion instead.