Skip to content

Commit

Permalink
Update allsky_common.cpp: add readFileIntoBuffer()
Browse files Browse the repository at this point in the history
  • Loading branch information
EricClaeys authored May 5, 2024
1 parent e685b54 commit 3dc7cfc
Showing 1 changed file with 44 additions and 29 deletions.
73 changes: 44 additions & 29 deletions src/allsky_common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1375,9 +1375,17 @@ void delayBetweenImages(config cg, long lastExposure_us, std::string sleepType)
// NULL-out CR or LF, or both if they are in a row.
// Keep track of the beginning of the next line.
// Return a pointer to the beginning of the line or NULL if at the end of the file.

// Calling getLine(NULL) resets the pointer so the next call
// starts at the beginning of the buffer
char *getLine(char *buffer)
{
static char *nextLine = NULL;
if (buffer == NULL)
{
nextLine = NULL;
return(NULL);
}
char *startOfLine;
char *ptr;

Expand Down Expand Up @@ -1409,57 +1417,64 @@ char *getLine(char *buffer)
return(startOfLine);
}

// Get settings from a configuration file.
bool called_from_getConfigFileArguments = false;
bool getConfigFileArguments(config *cg)
// Read the specified file into the specified buffer.
char * readFileIntoBuffer(config *cg, const char *file)
{
if (called_from_getConfigFileArguments)
{
Log(-1, "*** %s: WARNING: Configuration file calls itself; ignoring!\n", cg->ME);
return true;
}

if (cg->configFile[0] == '\0') {
Log(0, "*** %s: ERROR: Unable to read configuration file: no file specified!\n", cg->ME);
return false;
}

// Read the whole configuration file into memory so we can create argv with pointers
static char *buf = NULL;
int fd;
if ((fd = open(cg->configFile, O_RDONLY)) == -1)
if ((fd = open(file, O_RDONLY)) == -1)
{
int e = errno;
Log(0, "*** %s: ERROR: Could not open configuration file '%s': %s!",
cg->ME, cg->configFile, strerror(e));
return false;
Log(0, "*** %s: ERROR: Could not open file '%s': %s!", cg->ME, file, strerror(e));
return NULL;
}
struct stat statbuf;
if (fstat(fd, &statbuf) == 1) // This should never fail
{
int e = errno;
Log(0, "*** %s: ERROR: Could not fstat() configuration file '%s': %s!",
cg->ME, cg->configFile, strerror(e));
return false;
Log(0, "*** %s: ERROR: Could not fstat() file '%s': %s!", cg->ME, file, strerror(e));
return NULL;
}
// + 1 for trailing NULL
char *buf = NULL;
if ((buf = (char *) realloc(buf, statbuf.st_size + 1)) == NULL)
{
int e = errno;
Log(0, "*** %s: ERROR: Could not malloc() configuration file '%s': %s!",
cg->ME, cg->configFile, strerror(e));
return false;
Log(0, "*** %s: ERROR: Could not realloc() file '%s': %s!", cg->ME, file, strerror(e));
return NULL;
}
if (read(fd, buf, statbuf.st_size) != statbuf.st_size)
{
int e = errno;
Log(0, "*** %s: ERROR: Could not read() configuration file '%s': %s!",
cg->ME, cg->configFile, strerror(e));
return false;
Log(0, "*** %s: ERROR: Could not read() file '%s': %s!", cg->ME, file, strerror(e));
return NULL;
}

buf[statbuf.st_size] = '\0';
(void) close(fd);

return(buf);
}

// Get settings from a configuration file.
bool called_from_getConfigFileArguments = false;
bool getConfigFileArguments(config *cg)
{
if (called_from_getConfigFileArguments)
{
Log(-1, "*** %s: WARNING: Configuration file calls itself; ignoring!\n", cg->ME);
return true;
}

if (cg->configFile[0] == '\0') {
Log(0, "*** %s: ERROR: Unable to read configuration file: no file specified!\n", cg->ME);
return false;
}

// Read the whole configuration file into memory so we can create argv with pointers.
static char *buf = readFileIntoBuffer(cg, cg->configFile);
if (buf == NULL)
return(false);

int const numSettings = 500 * 2; // some settings take an argument
char *argv[numSettings];

Expand Down

0 comments on commit 3dc7cfc

Please sign in to comment.