Re: Help the helpless.. code read
I'm so greatful for all the help, encouragement, and kicks in the arse
that I've received here. As a result, I tried to apply all the good
advice, and worked through the problem as carefully as my ability and
experience has enabled. It was especially helpful to try and build a
test application which duplicates the functions in the application I'm
attempting to understand. I found that this is a really great way to
learn and breaks things down into understandable segments.
I'd really appreciate it if you might have a look at this code and offer
up criticisms, or reiterate those that I unintentionally overlooked.
I'm certain that better minds than mine will have a few.
Thanks!
(To generate the executable:
gcc -o testapp testapp.c && ./testapp && rm -f testapp.o
Save the source as testapp.c)
// BEGIN testapp.c
//This generates a small file called file.txt. file.txt is a replica of
//a segment of the file that the larger application uses to store
//various data, including the ROM address.
#include <strings.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
struct _roms
{
unsigned char *roms;
int max;
};
int initlist(struct _roms *sensor_list)
{
int sensors;
sensors = 1;
sensor_list->roms = malloc( sensors * 8 );
sensor_list->max = sensors;
return 0;
}
int loadstring(char *fname, struct _roms *sensor_list)
{
int x;
char astring[17];
char *ptrstring;
char *ptr;
int sensors=0;
char temp[1024];
FILE *fp;
// create the file that contains the string address
if( ( fp = fopen( fname, "w" ) ) == NULL )
{
printf("Error creating file\n");
return 1;
}
// write string address to file
if (fputs("0x12 0xBA 0xA9 0x1C 0x00 0x00 0x00 0xA3",fp) == 0)
{
printf("Error writing file\n");
return 1;
}
fclose(fp);
// re-open file and read contents into temp
if( ( fp = fopen( fname, "r" ) ) == NULL )
{
printf("Error opening file\n");
return 1;
}
while( fgets( temp, 80, fp ) != 0 )
{
if( (temp[0] == '\n') || (temp[0] == '#') ) continue;
ptr = strtok(temp, " \t\n");
sensors = 0;
for(x = 0; x < 8; x++)
{
sensor_list->roms[(sensors * 8) + x] = strtol(ptr, (char **) NULL,0);
ptr = strtok(NULL, " \t\n");
}
sensor_list->max = 1;
}
fclose(fp);
return 0;
}
int printit(struct _roms *sensor_list)
{
unsigned char *ptr;
int x,y;
unsigned char cstring[17];
for( x = 0; x < sensor_list->max; x++ )
{
printf("ROM %d = ", x );
for( y = 0; y < 8; y++ )
{
printf( "0x%02X ", sensor_list->roms[(x * 8) + y] );
}
printf( "\n" );
}
for( x = 0; x < sensor_list->max; x++ )
{
printf("ROM %d = ", x );
for( y = 0; y < 8; y++ )
{
printf( "%d ", sensor_list->roms[(x * 8) + y] );
}
printf( "\n" );
}
return 0;
}
int main(int argc, char *argv[])
{
// *roms is an array of 8 bytes
// max is the maximum number of entries
struct _roms sensor_list;
int sensors;
int x;
char conf_file[1024];
char *ptr;
printf("Start of test application\n") ;
strcpy(conf_fil e, "file.txt") ;
bzero(&sensor_l ist, sizeof(struct _roms));
x = initlist(&senso r_list);
x = loadstring(conf _file,&sensor_l ist);
x = printit(&sensor _list);
printf("End of test application\n") ;
return 0;
}
// END testapp.c
I'm so greatful for all the help, encouragement, and kicks in the arse
that I've received here. As a result, I tried to apply all the good
advice, and worked through the problem as carefully as my ability and
experience has enabled. It was especially helpful to try and build a
test application which duplicates the functions in the application I'm
attempting to understand. I found that this is a really great way to
learn and breaks things down into understandable segments.
I'd really appreciate it if you might have a look at this code and offer
up criticisms, or reiterate those that I unintentionally overlooked.
I'm certain that better minds than mine will have a few.
Thanks!
(To generate the executable:
gcc -o testapp testapp.c && ./testapp && rm -f testapp.o
Save the source as testapp.c)
// BEGIN testapp.c
//This generates a small file called file.txt. file.txt is a replica of
//a segment of the file that the larger application uses to store
//various data, including the ROM address.
#include <strings.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
struct _roms
{
unsigned char *roms;
int max;
};
int initlist(struct _roms *sensor_list)
{
int sensors;
sensors = 1;
sensor_list->roms = malloc( sensors * 8 );
sensor_list->max = sensors;
return 0;
}
int loadstring(char *fname, struct _roms *sensor_list)
{
int x;
char astring[17];
char *ptrstring;
char *ptr;
int sensors=0;
char temp[1024];
FILE *fp;
// create the file that contains the string address
if( ( fp = fopen( fname, "w" ) ) == NULL )
{
printf("Error creating file\n");
return 1;
}
// write string address to file
if (fputs("0x12 0xBA 0xA9 0x1C 0x00 0x00 0x00 0xA3",fp) == 0)
{
printf("Error writing file\n");
return 1;
}
fclose(fp);
// re-open file and read contents into temp
if( ( fp = fopen( fname, "r" ) ) == NULL )
{
printf("Error opening file\n");
return 1;
}
while( fgets( temp, 80, fp ) != 0 )
{
if( (temp[0] == '\n') || (temp[0] == '#') ) continue;
ptr = strtok(temp, " \t\n");
sensors = 0;
for(x = 0; x < 8; x++)
{
sensor_list->roms[(sensors * 8) + x] = strtol(ptr, (char **) NULL,0);
ptr = strtok(NULL, " \t\n");
}
sensor_list->max = 1;
}
fclose(fp);
return 0;
}
int printit(struct _roms *sensor_list)
{
unsigned char *ptr;
int x,y;
unsigned char cstring[17];
for( x = 0; x < sensor_list->max; x++ )
{
printf("ROM %d = ", x );
for( y = 0; y < 8; y++ )
{
printf( "0x%02X ", sensor_list->roms[(x * 8) + y] );
}
printf( "\n" );
}
for( x = 0; x < sensor_list->max; x++ )
{
printf("ROM %d = ", x );
for( y = 0; y < 8; y++ )
{
printf( "%d ", sensor_list->roms[(x * 8) + y] );
}
printf( "\n" );
}
return 0;
}
int main(int argc, char *argv[])
{
// *roms is an array of 8 bytes
// max is the maximum number of entries
struct _roms sensor_list;
int sensors;
int x;
char conf_file[1024];
char *ptr;
printf("Start of test application\n") ;
strcpy(conf_fil e, "file.txt") ;
bzero(&sensor_l ist, sizeof(struct _roms));
x = initlist(&senso r_list);
x = loadstring(conf _file,&sensor_l ist);
x = printit(&sensor _list);
printf("End of test application\n") ;
return 0;
}
// END testapp.c
Comment