Re: how to organize my main file ?
- From: Richard Heathfield <rjh@xxxxxxxxxxxxxxx>
- Date: Tue, 17 Jun 2008 16:13:15 +0000
pereges said:
By main file, I mean the one which contains the main routine. Can some
one please provide suggestions as to how I can improve the
organization of main file ? I have just though about a rough skeleton.
In my ray tracing project, I have to carry out following task (in
sequence)
1. Read the mesh from an ascii file and store it in the mesh data
structure.
2. Create a ray list and store it in a ray list.
3. Create the binary space partitioning tree for fast mesh traversal.
4. Trace all the rays and calculate the scattered and incident
electric fields.
I'm thinking of writing a function for every task.
It seems to me that tasks 1-3 might reasonably be called initialisation.
Task 4 appears to be the bit that does the useful processing.
A function for every task is always a good idea, but why not abstract your
first three tasks into a function called initialise() or something like
that. It might look something like this:
#include "whatever.h"
int initialise(const char *infile)
{
int rc = read_mesh(infile); /* change read_mesh() to take const char *
*/
if(0 == rc)
{
rc = init_plane_wave();
}
if(0 == rc)
{
rc = create_bsp_tree();
}
return rc;
}
and then main would look something like this:
#include <stdio.h>
#include <stdlib.h>
#include "whatever.h"
int main(int argc, char **argv)
{
int result = EXIT_FAILURE;
if(argc != 2)
{
printf("%s arguments\n", argc < 2 ? "Insufficient" : "Too many");
}
else
{
if(0 == initialise(argv[1]))
{
if(0 == calc_e_fields())
{
result = EXIT_SUCCESS;
}
}
}
return result;
}
You might want to put main() and initialise() in one source file (your
"main file" as you call it), the initialisation routines (read_mesh and
the other two) in, say, initialise.c, and the calcs in calcs.c - this will
help to keep each source file down to a manageable size and make things
easier for you to find.
Note that a -1 return value from main isn't guaranteed to be meaningful,
whereas EXIT_FAILURE is.
<snip>
--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
.
- Follow-Ups:
- Re: how to organize my main file ?
- From: pereges
- Re: how to organize my main file ?
- References:
- how to organize my main file ?
- From: pereges
- how to organize my main file ?
- Prev by Date: Re: how to organize my main file ?
- Next by Date: Re: Reentrant code
- Previous by thread: Re: how to organize my main file ?
- Next by thread: Re: how to organize my main file ?
- Index(es):
Relevant Pages
|