readconfig.c.save
ncohafmuta-1.4.2/utils/code_fragments/new_config/readconfig.c.save · 9.4 KB
From the archive ncoh142-dist-full.tar.gz
Download the archive « Back to the archive contents
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define DEF_CONFIG_FILE "running_config.dat"
#define GLOBALS 0
#define ROOMS 1
struct room_struct_exits {
char name[80];
struct room_struct_exits *prev,*next;
};
struct room_struct_exits *exit_first,*exit_last;
struct room_struct {
char name[80];
char owner[8];
char descfile[256];
struct room_struct_exits *exit;
int security;
int windy;
int hidden;
int boardsecurity;
int atmos;
int atmos_timer;
int atmos_factor;
struct room_struct *prev,*next;
};
struct room_struct *room_first,*room_last;
struct config_struct {
char name[256];
int type;
int valuedata;
char stringdata[256];
struct config_struct *prev,*next;
};
struct config_struct *run_config_first,*run_config_last;
void init_config_struct(struct config_struct *ptr);
void init_room_struct(struct room_struct *ptr);
void init_room_struct_exits(struct room_struct_exits *ptr);
void remove_first(char *inpstr);
int check_config_opt(int mode, char *inpstr);
int main(void) {
int aa=0;
char configtype[256];
char configname[256];
char line[512];
char tempvalue[512];
char *tempptr;
FILE *fp;
struct config_struct *config_ptr;
struct room_struct *room_ptr;
struct room_struct_exits *room_exit_ptr;
struct config_struct *tempconfig;
if (!(fp=fopen(DEF_CONFIG_FILE,"r"))) {
printf("Can't open config file!\n");
return 0;
}
while (fgets(line,512,fp) != NULL) {
line[strlen(line)-1]=0;
if (line[0]=='#' || !strlen(line)) continue;
sleep(0);
printf("READ LINE: %s\n",line);
sscanf(line,"%s",configtype);
remove_first(line);
if (!strcmp(configtype,"value")) {
sscanf(line,"%s",configname);
remove_first(line);
strcpy(tempvalue,line);
printf("PARSED: %s %s %s\n",configtype,configname,tempvalue);
if ((config_ptr=(struct config_struct *)malloc(sizeof(struct config_struct)))==NULL) {
printf("Malloc failed for config_struct\n");
return 0;
}
if (run_config_first==NULL) {
run_config_first=config_ptr;
config_ptr->prev=NULL;
}
else {
run_config_last->next=config_ptr;
config_ptr->prev=run_config_last;
}
config_ptr->next=NULL;
run_config_last=config_ptr;
init_config_struct(config_ptr);
strncpy(config_ptr->name,configname,sizeof(config_ptr->name));
config_ptr->type=1;
config_ptr->valuedata=atoi(tempvalue);
printf("VALUES: NAME %s TYPE %d VALUE %d\n",config_ptr->name,config_ptr->type,config_ptr->valuedata);
for (tempconfig=run_config_first;tempconfig!=NULL;tempconfig=tempconfig->next) {
if (tempconfig->type==1) printf("V %s %d\n",tempconfig->name,tempconfig->valuedata);
else if (tempconfig->type==2) printf("S %s %s\n",tempconfig->name,tempconfig->stringdata);
}
} /* value */
else if (!strcmp(configtype,"string")) {
sscanf(line,"%s",configname);
remove_first(line);
strcpy(tempvalue,line);
printf("PARSED: %s %s %s\n",configtype,configname,tempvalue);
if ((config_ptr=(struct config_struct *)malloc(sizeof(struct config_struct)))==NULL) {
printf("Malloc failed for config_struct\n");
return 0;
}
if (run_config_first==NULL) {
run_config_first=config_ptr;
config_ptr->prev=NULL;
}
else {
run_config_last->next=config_ptr;
config_ptr->prev=run_config_last;
}
config_ptr->next=NULL;
run_config_last=config_ptr;
init_config_struct(config_ptr);
strncpy(config_ptr->name,configname,sizeof(config_ptr->name));
config_ptr->type=2;
strncpy(config_ptr->stringdata,tempvalue,sizeof(config_ptr->stringdata));
printf("VALUES: NAME %s TYPE %d VALUE %s\n",config_ptr->name,config_ptr->type,config_ptr->stringdata);
for (tempconfig=run_config_first;tempconfig!=NULL;tempconfig=tempconfig->next) {
if (tempconfig->type==1) printf("V %s %d\n",tempconfig->name,tempconfig->valuedata);
else if (tempconfig->type==2) printf("S %s %s\n",tempconfig->name,tempconfig->stringdata);
}
} /* string */
else if (!strcmp(configtype,"room")) {
if ((room_ptr=(struct room_struct *)malloc(sizeof(struct room_struct)))==NULL) {
printf("Malloc failed for room_struct\n");
return 0;
}
if (room_first==NULL) {
room_first=room_ptr;
room_ptr->prev=NULL;
}
else {
room_last->next=room_ptr;
room_ptr->prev=room_last;
}
room_ptr->next=NULL;
room_last=room_ptr;
init_room_struct(room_ptr);
while (strlen(line)) {
sscanf(line,"%s",configname);
remove_first(line);
printf("PARSED: %s %s %s\n",configtype,configname,line);
if (!strcmp(configname,"NAME")) {
if (sscanf(line,"%s",tempvalue) != 1) printf("BAD!\n");
else {
remove_first(line);
strncpy(room_ptr->name,tempvalue,sizeof(room_ptr->name));
printf("ROOM NAME: %s\n",room_ptr->name);
}
} /* if NAME */
else if (!strcmp(configname,"OWNER")) {
if (sscanf(line,"%s",tempvalue) != 1) printf("BAD!\n");
else {
remove_first(line);
strncpy(room_ptr->owner,tempvalue,sizeof(room_ptr->owner));
printf("ROOM OWNER: %s\n",room_ptr->owner);
}
} /* if OWNER */
else if (!strcmp(configname,"DESC")) {
if (sscanf(line,"%s",tempvalue) != 1) printf("BAD!\n");
else {
remove_first(line);
strncpy(room_ptr->descfile,tempvalue,sizeof(room_ptr->descfile));
printf("ROOM DESCFILE: %s\n",room_ptr->descfile);
}
} /* if DESC */
else if (!strcmp(configname,"SECURITY")) {
if (sscanf(line,"%s",tempvalue) != 1) printf("BAD!\n");
else {
remove_first(line);
if (!strcmp(tempvalue,"public")) room_ptr->security=0;
else if (!strcmp(tempvalue,"private")) room_ptr->security=1;
else if (!strcmp(tempvalue,"lockedpublic")) room_ptr->security=2;
printf("ROOM SECURITY: %d\n",room_ptr->security);
}
} /* if SECURITY */
else if (!strcmp(configname,"EXITS")) {
if (sscanf(line,"%s",tempvalue) != 1) printf("BAD!\n");
while(!check_config_opt(ROOMS,tempvalue)) {
if ((room_ptr->exit=(struct room_struct_exits *)malloc(sizeof(struct room_struct_exits)))==NULL) {
printf("Malloc failed for room_struct_exits\n");
return 0;
}
if (exit_first==NULL) {
exit_first=room_ptr->exit;
room_ptr->exit->prev=NULL;
}
else {
exit_last->next=room_ptr->exit;
room_ptr->exit->prev=exit_last;
}
room_ptr->exit->next=NULL;
exit_last=room_ptr->exit;
init_room_struct_exits(room_ptr->exit);
remove_first(line);
strncpy(room_ptr->exit->name,tempvalue,sizeof(room_ptr->exit->name));
printf("ROOM EXIT: %s\n",room_ptr->exit->name);
if (sscanf(line,"%s",tempvalue) != 1) { printf("BAD!\n"); break; }
} /* while */
} /* if EXITS */
else if (!strcmp(configname,"MESSBOARD")) {
if (sscanf(line,"%s",tempvalue) != 1) printf("BAD!\n");
else {
remove_first(line);
if (!strcmp(tempvalue,"changeable")) room_ptr->boardsecurity=0;
else if (!strcmp(tempvalue,"readable")) room_ptr->boardsecurity=1;
printf("ROOM BOARDSECURITY: %d\n",room_ptr->boardsecurity);
}
} /* if MESSBOARD */
else if (!strcmp(configname,"FLAGS")) {
if (sscanf(line,"%s",tempvalue) != 1) printf("BAD!\n");
while(!check_config_opt(ROOMS,tempvalue)) {
remove_first(line);
if (!strcmp(tempvalue,"windy")) room_ptr->windy=1;
else if (!strcmp(tempvalue,"hidden")) room_ptr->hidden=1;
printf("ROOM FLAG: %s\n",tempvalue);
if (sscanf(line,"%s",tempvalue) != 1) { printf("BAD!\n"); break; }
} /* while */
} /* if FLAGS */
} /* while strlen */
} /* room */
} /* while */
printf("--- GLOBAL CONFIG ---\n");
for (config_ptr=run_config_first;config_ptr!=NULL;config_ptr=config_ptr->next) {
printf("%s %d %d %s\n",config_ptr->name,config_ptr->type,config_ptr->valuedata,config_ptr->stringdata);
}
printf("--- ROOM CONFIG ---\n");
for (room_ptr=room_first;room_ptr!=NULL;room_ptr=room_ptr->next) {
printf("%s %s\n",room_ptr->name,room_ptr->owner);
printf("\t--- ROOM EXITS ---\n");
for (room_ptr->exit=exit_first;room_ptr->exit!=NULL;room_ptr->exit=room_ptr->exit->next)
printf("\t%s\n",room_ptr->exit->name);
}
} /* main */
void init_config_struct(struct config_struct *ptr) {
ptr->name[0]='\0';
ptr->type=0;
ptr->valuedata=0;
ptr->stringdata[0]='\0';
}
void init_room_struct(struct room_struct *ptr) {
ptr->name[0]='\0';
ptr->owner[0]='\0';
ptr->descfile[0]='\0';
ptr->exit=NULL;
ptr->windy=0;
ptr->hidden=0; /* visible */
ptr->security=0; /* public */
ptr->boardsecurity=0; /* changeable */
ptr->atmos=0; /* off */
ptr->atmos_timer=5;
ptr->atmos_factor=5;
}
void init_room_struct_exits(struct room_struct_exits *ptr) {
ptr->name[0]='\0';
}
/*** removes first word at front of string and moves rest down ***/
void remove_first(char *inpstr)
{
int newpos,oldpos;
newpos=0; oldpos=0;
/* find first word */
while(inpstr[oldpos]==' ') {
if (!inpstr[oldpos]) { inpstr[0]=0; return; }
oldpos++;
}
/* find end of first word */
while(inpstr[oldpos]!=' ') {
if (!inpstr[oldpos]) { inpstr[0]=0; return; }
oldpos++;
}
/* find second word */
while(inpstr[oldpos]==' ') {
if (!inpstr[oldpos]) { inpstr[0]=0; return; }
oldpos++;
}
while(inpstr[oldpos]!=0)
inpstr[newpos++]=inpstr[oldpos++];
inpstr[newpos]='\0';
}
int check_config_opt(int mode, char *inpstr) {
if (mode==GLOBALS) {
} /* GLOBALS */
else if (mode==ROOMS) {
if (!strcmp(inpstr,"NAME") ||
!strcmp(inpstr,"DESC") ||
!strcmp(inpstr,"SECURITY") ||
!strcmp(inpstr,"MESSBOARD") ||
!strcmp(inpstr,"OWNER") ||
!strcmp(inpstr,"FLAGS") ||
!strcmp(inpstr,"EXITS")) return 1;
} /* ROOMS */
return 0;
}