Hey guys !
First thing first, i am sorry about this post about malloc ... but i have serious problem with it ... I am programming Sokoban Console Game and i need to load levels from file (levels.dat) and then read line by line and parse every line in some blocks ...
Example of level : <name>;<password>;<description>;<map>
chicago;addie;story begins here;-----#####-----------|-----#@$.#-----------|-----#####-----------
I have the struct :
Code:
typedef struct level
{
char * name;
char * password;
char * description;
char * map;
struct level * next;
}
My problem is, when i am tryin to allocate memory for map ...
Do not read the comments , i am from slovakia ...
Here is the code :
Code:
Level * load_levels(const char * path)
{
// docasne udaje
Level *prvy, *posl, *novy;
// nacitaj subor
FILE * f = fopen(path, "r");
// ak subor neexistuje tak ukonci
if(!f)
return NULL;
// nacitaj riadok
int i = 0,j;
// sem citam retazce
char t[128][512];
// citaj retazce
while(fgets(t[i], 512, f) != NULL)
i++;
// rozparsuj level
prvy = NULL;
prvy= parse_level(t[0]);
prvy->next_level = NULL;
posl = prvy;
//pokial som nacital level nacitaj dalsi
for(j = 1; j < i; j++)
{
novy = parse_level(t[j]);
if(novy != NULL)
{
novy->next_level = NULL;
//posl->next_level = novy;
posl = novy;
}
else
return NULL;
}
// zatvor subor
fclose(f);
// nacital som levely
return (prvy);
}
Level * parse_level(char s[])
{
// pridel pamat
Level * t = (Level*)malloc(sizeof(Level)) ;
// ak sa nepodarilo naalokovat pamat tak skoncime s NULL
if(t == NULL)
return NULL;
// docasne premenne
int i;
// alokuj potrebnu pamat
char * buffer = malloc( sizeof *s + 1);
// ak sa nenaalokoval buffer konci
if(buffer == NULL)
return NULL;
// kopiruj do bafra
strcpy(buffer , "");
// hladaci algoritmus
// hladaj meno
for(i = 0; i < strlen(s); i++)
{
if(s[i] != ';')
{
char znak = s[i];
strcat(buffer, &znak);
}
else break;
}
// ak som nenacital nic do buffra skonci
if(!strcmp(buffer, "") || !strcmp(buffer, " "))
return NULL;
// pridel pamat
if((t->name) != NULL) free(t->name);
t->name = malloc( sizeof *buffer + 1 );
// ak sa pamat neda pridelit tak skonci
if(t->name == NULL)
return NULL;
// skopiruj obsah z buffra do name
strcpy(t->name, buffer);
// kopiruj do buffra
strcpy(buffer , "");
// hladaj heslo
for(i++; i < strlen(s); i++)
{
if(s[i] != ';')
{
char znak = s[i];
strcat(buffer, &znak);
}
else break;
}
// ak som nic nenasiel konci
if(!strcmp(buffer, "") || !strcmp(buffer, " "))
return NULL;
// pridel pamat
if((t->password) != NULL) free(t->password);
t->password = malloc( sizeof *buffer + 1 );
// ak sa nepridelila pamat konci
if(t->password == NULL)
return NULL;
// kopiruj obsah buffra
strcpy(t->password, buffer);
// kopiruj do buffra
strcpy(buffer , "");
// hladaj description
for(i++; i < strlen(s); i++)
{
if(s[i] != ';')
{
char znak = s[i];
strcat(buffer, &znak);
}
else break;
}
// ak sa v buffrinic nenachadya konci
if(!strcmp(buffer, "") || !strcmp(buffer, "'"))
return NULL;
// vyhrad pamat
if((t->description) != NULL) free(t->description);
t->description = malloc( sizeof *buffer + 1 );
// ak nemam nic v description
if(t->description == NULL)
return NULL;
// kopirujobsah z buffra
strcpy(t->description, buffer);
// kopiruj do buffra
strcpy(buffer , "");
// hladaj mapu
for(i++; i < strlen(s); i++)
{
char znak = s[i];
strcat(buffer, &znak);
}
// ak nemam nic v buffri konci
if(!strcmp(buffer, "") || !strcmp(buffer, " "))
return NULL;
// vyhrad pamat
if((t->map) != NULL) free(t->map);
t->map = malloc( sizeof *buffer + 1 );
// t->map = malloc( (strlen(buffer) + 1) * sizeof *t->map ); ak nemam pamatkonci
if(t->map == NULL)
return NULL;
// kopiruj obsah buffra
strcpy(t->map, buffer);
// vrat rozparsovany level
return (t);
}