/* john pfeiffer 08jan09 store a file into memory by lines
must be sure to terminate each line with a \0 or \n
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define LINE_BUFFER_MAX 512
#define CMDLINE_PARAMETERS 2
typedef struct node tnode;
typedef struct node* nodeptr;
struct node
{ int node_id;
char line[LINE_BUFFER_MAX];
nodeptr next;
nodeptr prev;
};
nodeptr talloc(void) /* allocate space in memory for the size of the structure */
{ return( (nodeptr)malloc( sizeof( struct node)) ); }
nodeptr create_node(nodeptr parent, char buffer[LINE_BUFFER_MAX])
{
int i=0;
nodeptr newnode = talloc();
newnode->node_id = (parent->node_id) + 1; /* number our node as parent# + 1*/
while( (i<512) && (buffer[i] != '\n') ) /* a line must be less than 512 */
{ newnode->line[i] = buffer[i];
i++;
}
newnode->prev = parent;
newnode->next = NULL;
return newnode;
}/* end create_node function */
void forward_traversal( nodeptr current )
{
do{ printf("Line #%d = %s\n",current->node_id, current->line);
current = current->next;
}while( current != NULL );
printf("NULL");
}/* end forward_traversal function */
void reverse_traversal( nodeptr current )
{
while( current->next != NULL) /* find the last node */
{ current = current->next; }
do{
printf("Node #%d -> ",current->node_id);
current = current->prev; /* travel the list backwards */
}while( current != NULL );
printf("NULL");
}/* end reverse_traversal function */
void free_list( nodeptr current )
{
nodeptr temp;
do{
temp = current;
current = current->next;
free( temp );
printf(".");
}while( current != NULL );
}/* end reverse_traversal function */
void safe_parameters( int argc, char* argv[], int parameter_size_max, char safe_argv[512][512] )
{
int i=0, k=0;
for(i=0; i<argc; i++)
{
while( (k < 512) && (argv[0][i] != '\0') )
{
safe_argv[i][k] = argv[i][k];
k++;
}
safe_argv[i][k] = '\0'; /* we must always terminate a string! */
k=0;
printf("\nsafe_argv[%d]=%s\n",i,safe_argv[i]);
}
}/* end safe_parameters */
void test_commandline_parameters( int argc, char safe_argv[512][512] )
{ int i = 0;
printf("\n\n TESTING COMMANDLINE PARAMETERS... \n");
if( (argc < CMDLINE_PARAMETERS) || (argc > CMDLINE_PARAMETERS) )
{ printf("\nCorrect usage is:\n");
printf("%s inputfile",safe_argv[0]);
exit(1);
}
else
{ while( i < argc )
{ printf("%s ", safe_argv[i]);
i++;
}
printf("in progress...\n\n");
}
}/* end test_commandline_parameters */
/* MAIN ------------------------------------------------------- */
int main(int argc, char* argv[])
{
char buffer[LINE_BUFFER_MAX];
char safe_argv[512][512];
FILE* fpsrc;
nodeptr current; /* to traverse the list */
nodeptr root; /* top of the list */
int i=0;
safe_parameters(argc, argv, 512, safe_argv); /* each parameter up to 512 chars */
test_commandline_parameters( argc, safe_argv );
fpsrc = fopen(argv[1],"r");
if(fpsrc == NULL)
{ printf("Error opening file(s)\n"); }
else
{
fgets(buffer, LINE_BUFFER_MAX, fpsrc);
printf("\n%c\n",buffer[0]);
root = talloc(); /* we must create a special "head" node */
root->node_id = 1; /* To count the lines (1 node per line) */
i=0;
while( (i<512) && (buffer[i] != '\n') ) /* a line must be less than 512 */
{ root->line[i] = buffer[i];
i++;
}
root->prev = NULL; /* so backwards doesn't crash the program */
root->next = NULL;
current = root; /* current now points to top of the list */
while( fgets(buffer, LINE_BUFFER_MAX, fpsrc) != NULL )
{
current->next = create_node(current, buffer); /* link to next line */
printf("%s",buffer);
current = current->next;
}
printf("\n\nList from root to the last element\n");
forward_traversal( root );
fclose(fpsrc);
}/* end else (file open error) */
return 0;
}/* end main */