I haven't been writing much for the past week with work but I have been making some progress and started writing this program. it still has errors like memory leaks and it's only half complete (I need to re-organize it also), it only pulls out the polygon offsets in the file. I still have to pull out the vertices and translate the offsets to memory locations, pull out other stuff like vertex vectors, and either write the data to a binary file in a personal format, display the mesh or even do both. I think I really like linked lists, they seem to be really great for dealing with data structures that have an unknown size. This program loads the file into memory and then searches for data, instead of searching for data in the file, I figured it would be a lot faster that way. It looks messy now, it's just a scratch pad at the moment, i should start getting it organized before i go much further. any way here's the code so far
Code:
#include "stdio.h"
#include "conio.h"
#include "stdlib.h"
#include "string.h"
#include "math.h"
int loadfile();
int getpolly();
char buffer[128];
char *cptr;
int loaded;
typedef struct pollystruct{
struct pointxyz *p1;
struct pointxyz *p2;
struct pointxyz *p3;
struct pointxyz *p4;
struct pollystruct *next;
} pollystruct;
typedef struct pointxyz{
double x;
double y;
double z;
struct pointxyz *next;
} pointxyz;
int main(void)
{
while(1)
{
puts("\nmain menu:\n\t load file: 1\n\tprint file to screen: 2\n\t exit: 3\n\t system(): 4\n\t get pollygons: 5 ");
switch(getch())
{
case '1':
if(loaded == 1)
{
free(cptr);
}
loadfile();
break;
case '2':
if(loaded != 1)
{
puts("\n please load a file first\n");
break;
}
printf("\n%s", cptr);
break;
case '3':
if(loaded == 1)
{
free(cptr);
}
exit(0);
case '4':
fgets(buffer, 128, stdin);
system(buffer);
break;
case '5':
if(loaded != 1)
{
printf("\nPlease load a file first\n");
break;
}
getpolly();
break;
}
}
}
int loadfile()
{
long int ctr;
FILE *fptr;
char filename[128];
puts("\nfile name?: ");
gets(filename);
if(!(fptr = fopen(filename, "r")))
{
puts("\n----Error opening file-----\n\b\b");
return -1;
}
for(ctr = 0; !(feof(fptr)); ctr++)
{
fgetc(fptr);
}
if(!(cptr = (char*)malloc(sizeof(char)*(ctr))))
{
puts("\n-----error allocating memory-----\n");
return 1;
}
rewind(fptr);
fread(cptr, sizeof(char), ctr-1, fptr);
cptr[ctr-1]= '\0';
puts("\nfile loaded\n");
loaded = 1;
}
int getpolly()
{
char search1[] = "PolygonVertexIndex:";
char search2[] = "a:";
char search3[] = ",";
char search4[] = "}";
char conbuf[128];
int ctr;
int offsetbuf;
int ctr2 = 1;
char *pollyptr;
char *conptr;
struct pollystruct *head;
struct pollystruct *conduit;
void *freeptr;
pollyptr = strstr(cptr, search1); // moving around file pointer to the right data in file
pollyptr = strstr(pollyptr, search2);
pollyptr += 3;
head=(pollystruct*)malloc(sizeof(pollystruct));// create head list
conduit = head;
conduit->next = (pollystruct*)"\0"; // setting first link to null
while(strstr(pollyptr, search4)-pollyptr > strstr(pollyptr, search3)-pollyptr ) // checking to see if we are still in the data block
{
conptr = strstr(pollyptr, search3); // finding the end of the number
for(ctr = conptr - pollyptr; ctr > 0; ctr--) // writing the offset into a char buffer
{
conbuf[ctr-1] = pollyptr[ctr-1];
}
conbuf[conptr - pollyptr] = '\0'; // keeping the string from running out
pollyptr = conptr; // setting up for the next number
pollyptr += 1;
offsetbuf = atoi(conbuf); // converting the character string into a real number
if(offsetbuf != abs(offsetbuf)) // seeing if we are at the end of the first pollygon
{
if(ctr2 < 4 || ctr2 > 5) // shouldn't be true if only quads are in the file
{
printf("\nInvalid pollygon, quads only\n");
conduit = head;
while(conduit->next != (pollystruct*)"\0") // getting rid of memmory leaks
{
freeptr = conduit;
conduit = conduit->next;
free(freeptr);
}
free(conduit);
return -1;
}
offsetbuf = abs(offsetbuf)-1; // converts last point from notation used in fbx file
conduit->p4 = (struct pointxyz*)offsetbuf;// write to p4 in current list
conduit->next= (pollystruct*)malloc(sizeof(pollystruct)); //create next list,
conduit = conduit->next;
conduit->next = (pollystruct*)"\0"; //set last link too null
ctr2 = 1; // reseting for the next list
continue;
}
switch(ctr2)
{
case 1:
conduit->p1 = (struct pointxyz*)offsetbuf;// write to p1 in current list
break;
case 2:
conduit->p2 = (struct pointxyz*)offsetbuf;// write to p2 in current list
break;
case 3:
conduit->p3 = (struct pointxyz*)offsetbuf;// write to p3 in current list
break;
}
ctr2++;
}
conduit = head;
for(ctr2= 0; conduit->next != (pollystruct*)"\0"; ctr2++) // printing out pointer offsets for testing
{
printf("\n%d", conduit->p1);
printf("\n%d", conduit->p2);
printf("\n%d", conduit->p3);
printf("\n%d", conduit->p4);
conduit = conduit->next;
}
printf("\npolly tottal: %d", ctr2); // printing out tottal number of linked lists created
return 0;
}