G
Geometer
Hello!
<Background only>
Globals cannot always be avoided - in OS's with a graphical user interface
functions in an application are called from the OS asynchronously, and if
one of these functions needs the result of another function there seems to
be no way to avoid storing the result of function 1 in some global and
access it later from function 2
</Background only>
My way to deal with this situation is, to put those and only those functions
which need the same bunch of globals together into one compilation unit and
declare the globals for them static and as late as possible like in:
(pseudocode example, not necessarily "real life")
-Is there any better way to do this?
#include <whatever necessary>
static FILE *fp;
static unsigned int stream_ready;
void OpenFile(char *file_name, char mode) /*called when the user requests
the action*/
{
<open the file, store the filepointer and a success indicator>
} /* OpenFile */
static char read_buffer[<SOME_SIZE>];
static size_t buffer_size = sizeof line_buffer;
static int line_complete;
static char *line_buffer;
void ReadLine( size_t buffer_size)
{
char *fgets_result = NULL;
if(stream_ready)
{
fgets_result = fgets(read_buffer, buffer_size, fp);
if(fgets_result)
{
<pre - process the read_buffer, strip '\n', cat with the next line
if no '\n',
allocate memory for the complete line etc>
}
else
{
<the usual eof / error handling>
}
}
else
{
<do the necessary recovery actions>
}
} /* ReadLine */
void ProcessLine(void)
{
<do whatever necessary with the content of the line_buffer>
} /* ProcessLine */
void CloseFile(void)
{
if(fp)
{
fclose(fp);
fp = NULL;
}
} /* CloseFile */
Thank you for taking the time
Robert
<Background only>
Globals cannot always be avoided - in OS's with a graphical user interface
functions in an application are called from the OS asynchronously, and if
one of these functions needs the result of another function there seems to
be no way to avoid storing the result of function 1 in some global and
access it later from function 2
</Background only>
My way to deal with this situation is, to put those and only those functions
which need the same bunch of globals together into one compilation unit and
declare the globals for them static and as late as possible like in:
(pseudocode example, not necessarily "real life")
-Is there any better way to do this?
#include <whatever necessary>
static FILE *fp;
static unsigned int stream_ready;
void OpenFile(char *file_name, char mode) /*called when the user requests
the action*/
{
<open the file, store the filepointer and a success indicator>
} /* OpenFile */
static char read_buffer[<SOME_SIZE>];
static size_t buffer_size = sizeof line_buffer;
static int line_complete;
static char *line_buffer;
void ReadLine( size_t buffer_size)
{
char *fgets_result = NULL;
if(stream_ready)
{
fgets_result = fgets(read_buffer, buffer_size, fp);
if(fgets_result)
{
<pre - process the read_buffer, strip '\n', cat with the next line
if no '\n',
allocate memory for the complete line etc>
}
else
{
<the usual eof / error handling>
}
}
else
{
<do the necessary recovery actions>
}
} /* ReadLine */
void ProcessLine(void)
{
<do whatever necessary with the content of the line_buffer>
} /* ProcessLine */
void CloseFile(void)
{
if(fp)
{
fclose(fp);
fp = NULL;
}
} /* CloseFile */
Thank you for taking the time
Robert