#include "installOS.h" | |
#include "installConfig.h" | |
#ifdef _WIN32 | |
#include <stdio.h> | |
#else /* Unix like platforms */ | |
#include <string.h> | |
#include <stdlib.h> | |
#include <stdio.h> | |
#include <sys/types.h> | |
#include <unistd.h> | |
#endif | |
int readConfigFile(_TCHAR* config_file, _TCHAR* arg0, int *argc, _TCHAR ***argv) | |
{ | |
_TCHAR buffer[1024]; | |
_TCHAR argument[1024]; | |
FILE *file = NULL; | |
int maxArgs = 128; | |
int index; | |
if (config_file == NULL || argc == NULL || argv == NULL) return -1; | |
/* Open the config file as a text file | |
*/ | |
file = _tfopen(config_file, _T_INSTALL("rt")); | |
if (file == NULL) return -3; | |
*argv = (_TCHAR **)malloc(1 + maxArgs * sizeof(_TCHAR*)); | |
/* Make it look like a regular list of arguments that starts with the executable location and name */ | |
(*argv)[0] = _tcsdup(arg0); | |
index = 1; | |
/* Parse every line */ | |
while (_fgetts(buffer, 1024, file) != NULL) | |
{ | |
/* Extract the string prior to the first newline character. | |
* We don't have to worry about \r\n combinations since the file | |
* is opened in translated mode. | |
*/ | |
if (_stscanf(buffer, _T_INSTALL("%[^\n]"), argument) == 1) | |
{ | |
(*argv)[index] = _tcsdup(argument); | |
index++; | |
/* Grow the array of TCHAR*. Ensure one more entry is | |
* available for the final NULL entry | |
*/ | |
if (index == maxArgs - 1) | |
{ | |
maxArgs += 128; | |
*argv = (_TCHAR **)realloc(*argv, maxArgs * sizeof(_TCHAR*)); | |
} | |
} | |
} | |
(*argv)[index] = NULL; | |
*argc = index; | |
fclose(file); | |
free(config_file); | |
return 0; | |
} | |
void freeConfig(_TCHAR **argv) | |
{ | |
int index = 0; | |
if (argv == NULL) return; | |
while (argv[index] != NULL) | |
{ | |
free(argv[index]); | |
index++; | |
} | |
free(argv); | |
} |