37 #define ASSUMED_MAX_LINE_LENGTH 256
125 const char * EntryName,
127 const char * Comment)
131 assert(CurrentSection != NULL);
134 newEntry = malloc(
sizeof(*newEntry));
136 if (newEntry == NULL)
139 memset(newEntry, 0,
sizeof(*newEntry));
142 newEntry->
Next = NULL;
148 newEntry->
Value = NULL;
151 if (PreviousEntry != NULL) {
152 PreviousEntry->
Next = newEntry;
156 CurrentSection->
Entries = newEntry;
177 assert(Entry != NULL);
180 next_entry = Entry->
Next;
226 const char *
const SectionName,
227 const char *
const Comment)
232 newSection = malloc(
sizeof(*newSection));
234 if (newSection == NULL) {
238 memset(newSection, 0,
sizeof(*newSection));
241 newSection->
Next = NULL;
247 newSection->
Name = NULL;
250 if (PreviousSection != NULL) {
251 PreviousSection->
Next = newSection;
255 Configuration->
Sections = newSection;
276 assert(Section != NULL);
278 if (Section != NULL) {
281 next_section = Section->
Next;
286 entry = configuration_entry_free(entry);
312 configuration_read_line_handle_comment(
char * Buffer,
char ** Comment)
314 unsigned int handledComment = 0;
315 char * commentBuffer = NULL;
318 if (Comment == NULL || Buffer == NULL || *Buffer == 0)
321 commentBuffer = malloc(strlen(Buffer) + 1);
323 if (commentBuffer == NULL) {
327 strcpy(commentBuffer, Buffer);
329 *Comment = commentBuffer;
335 return handledComment;
342 read_a_complete_line(FILE * File)
344 char * buffer = NULL;
345 char * addbuffer = NULL;
347 unsigned int error = 1;
350 unsigned int bufferLength;
354 if (addbuffer == NULL) {
362 error = ferror(File) ? 1 : 0;
368 if (buffer == NULL) {
382 if (tmpbuffer == NULL) {
390 bufferLength = strlen(buffer);
392 if ( (bufferLength > 0) && buffer[bufferLength - 1] ==
'\n')
394 buffer[bufferLength - 1] = 0;
407 if (buffer == NULL) {
443 char * buffer = NULL;
453 if (feof(ConfigFile))
458 if (ferror(ConfigFile))
463 buffer = read_a_complete_line(ConfigFile);
465 if (buffer == NULL) {
469 if (buffer[0] ==
'#') {
470 if (configuration_read_line_handle_comment(buffer, Comment)) {
482 p = strchr(buffer,
'#');
487 p = buffer + strlen(buffer);
489 while (p && (p > buffer))
502 configuration_read_line_handle_comment(++p, Comment);
549 assert(ConfigFile != NULL);
553 fseek(ConfigFile, 0, SEEK_SET);
556 Handle->
Sections = section_alloc_new(Handle, NULL, NULL,
"");
566 char * comment = NULL;
572 line = configuration_read_line(Handle, &comment, ConfigFile);
578 if (line == NULL && comment == NULL) {
588 if (line && (line[0] ==
'['))
590 char * sectionName = NULL;
594 if (sectionName == NULL)
597 p = strrchr(sectionName,
']');
612 currentSection = section_alloc_new(Handle, currentSection, sectionName, comment);
615 if (currentSection == NULL) {
622 previousEntry = NULL;
627 char * entryName = NULL;
637 p = strchr(line,
'=');
654 previousEntry = entry_alloc_new(currentSection, previousEntry,
655 entryName, value, comment);
662 if (previousEntry == NULL) {
698 FILE * configfile = NULL;
712 if (configfile == NULL) {
719 fseek(configfile, 0, SEEK_SET);
721 for (currentSection = Handle->
Sections;
722 (currentSection != NULL) && (error == 0);
723 currentSection = currentSection->
Next) {
731 if (currentSection != Handle->
Sections) {
732 if (fprintf(configfile,
"[%s]%s\n",
733 currentSection->
Name, currentSection->
Comment) < 0)
739 for (currentEntry = currentSection->
Entries;
740 (currentEntry != NULL) && (error == 0);
741 currentEntry = currentEntry->
Next)
743 if (fprintf(configfile,
"%s%s%s%s\n",
744 (currentEntry->
Name ? currentEntry->
Name :
""),
745 (currentEntry->
Name && *(currentEntry->
Name)) ?
"=" :
"",
746 (currentEntry->
Value ? currentEntry->
Value :
""),
770 if (arch_unlink(Handle->
FileName)) {
807 unsigned int error = 1;
809 FILE * configFile = NULL;
812 handle = malloc(
sizeof(*handle));
818 memset(handle, 0,
sizeof(*handle));
830 configFile = fopen(handle->
FileName,
"rt");
832 if (configFile == NULL) {
836 opencbm_configuration_parse_file(handle, configFile);
844 if (error && handle) {
876 if (handle == NULL) {
879 filehandle = fopen(Filename,
"wt");
881 if (filehandle == NULL)
909 while (section != NULL)
911 section = configuration_section_free(section);
939 if (Handle == NULL) {
944 error = opencbm_configuration_write_file(Handle);
972 if (Handle == NULL) {
978 opencbm_configuration_free_all(Handle);
1008 const char Section[],
1009 unsigned int Create,
1018 if (Section == NULL) {
1022 for (currentSection = Handle->
Sections;
1023 currentSection != NULL;
1024 currentSection = currentSection->
Next)
1026 int foundSection = 0;
1028 if (currentSection->
Name == NULL) {
1029 foundSection = Section == NULL;
1033 foundSection = (strcmp(currentSection->
Name, Section) == 0);
1041 lastSection = currentSection;
1044 if (Create && currentSection == NULL) {
1048 currentSection = section_alloc_new(Handle, lastSection, Section, NULL);
1053 return currentSection;
1090 const char Section[],
const char Entry[],
1091 unsigned int Create,
1101 assert(LastEntry != NULL);
1102 assert(LastSection != NULL);
1106 *LastSection = NULL;
1110 if (Section == NULL || Entry == NULL) {
1114 currentSection = opencbm_configuration_find_section(Handle, Section, Create, &lastSection);
1116 if (currentSection == NULL) {
1120 *LastSection = currentSection;
1123 for (currentEntry = currentSection->
Entries;
1124 currentEntry != NULL;
1125 currentEntry = currentEntry->
Next)
1127 if (strcmp(currentEntry->
Name, Entry) == 0) {
1135 if (currentEntry->
Name != NULL) {
1136 lastEntry = currentEntry;
1137 *LastEntry = currentEntry;
1142 if (currentEntry || Create == 0) {
1146 if (currentSection == NULL) {
1150 currentSection = section_alloc_new(Handle, lastSection, Section, NULL);
1153 currentEntry = entry_alloc_new(currentSection, lastEntry, Entry, NULL, NULL);
1157 return currentEntry;
1186 const char Section[],
const char Entry[],
1187 unsigned int Create)
1192 return opencbm_configuration_find_data_ex(Handle, Section, Entry, Create, &last_entry, §ion);
1227 const char Section[],
const char Entry[],
1228 char ** ReturnBuffer)
1230 unsigned int error = 1;
1234 opencbm_configuration_find_data(Handle, Section, Entry, 0);
1236 if (entry == NULL) {
1244 if (ReturnBuffer != 0) {
1289 unsigned int error = 0;
1294 for (currentSection = Handle->
Sections;
1295 currentSection != NULL;
1296 currentSection = currentSection->
Next)
1298 error = error || Callback( Handle, currentSection->
Name, Data);
1336 const char Section[],
1340 unsigned int error = 0;
1346 currentSection = opencbm_configuration_find_section(Handle,
1349 if ( ! currentSection ) {
1354 for (currentEntry = currentSection->
Entries;
1355 currentEntry != NULL;
1356 currentEntry = currentEntry->
Next)
1358 error = error || Callback( Handle, currentSection->
Name, currentEntry->
Name, Data);
1391 const char Section[],
const char Entry[],
1394 unsigned int error = 1;
1397 char * newValue = NULL;
1400 opencbm_configuration_find_data(Handle, Section, Entry, 1);
1402 if (entry == NULL) {
1410 if (newValue == NULL) {
1415 entry->
Value = newValue;
1445 const char Section[])
1453 section = opencbm_configuration_find_section(Handle, Section, 0, &previous_section);
1459 if (previous_section == NULL) {
1460 Handle->
Sections = configuration_section_free(section);
1463 assert( previous_section->
Next == section );
1465 previous_section->
Next = configuration_section_free(section);
1499 const char Section[],
const char EntryName[])
1508 entry = opencbm_configuration_find_data_ex(Handle, Section, EntryName, 0,
1509 &last_entry, §ion);
1515 assert(section->
Entries == entry);
1517 if (last_entry == NULL) {
1522 section->
Entries = configuration_entry_free(entry);
1526 last_entry->
Next = configuration_entry_free(entry);
1538 #ifdef OPENCBM_STANDALONE_TEST
1547 EnableCrtDebug(
void)
1554 tmpFlag = _CrtSetDbgFlag(_CRTDBG_REPORT_FLAG);
1557 tmpFlag |= _CRTDBG_LEAK_CHECK_DF;
1558 tmpFlag |= _CRTDBG_CHECK_ALWAYS_DF;
1560 tmpFlag |= _CRTDBG_ALLOC_MEM_DF;
1563 _CrtSetDbgFlag(tmpFlag);
1568 static unsigned int started_an_op = 0;
1573 fprintf(stderr,
"success.\n\n");
1581 fprintf(stderr,
"FAILED!\n\n");
1594 OpStart(
const char *
const Operation)
1600 fprintf(stderr,
"%s() ... ", Operation);
1605 const char Section[],
1611 fprintf(stderr,
"\n enum_data_callback(Handle, %s, %s, 0x%p\n", Section, Entry, Data);
1618 const char Section[],
1623 fprintf(stderr,
"\n enum_sections_callback(Handle, %s, 0x%p\n", Section, Data);
1639 int ARCH_MAINDECL
main(
void)
1641 int errorcode = EXIT_FAILURE;
1650 OpStart(
"opencbm_configuration_create()");
1653 if (handle == NULL) {
1658 OpStart(
"opencbm_configuration_set_data(\"SectTest\", \"EntryTest\", \"VALUE\")");
1663 OpStart(
"opencbm_configuration_set_data(\"SectTest\", \"NewTest\", \"AnotherVALUE\")");
1669 OpStart(
"opencbm_configuration_get_data(handle, \"SectTest\", \"NewTest\")");
1674 fprintf(stderr,
" returned: %s\n", buffer);
1677 OpStart(
"opencbm_configuration_set_data(\"NewSect\", \"AEntryTest\", \"aVALUE\")");
1682 OpStart(
"opencbm_configuration_set_data(\"NewSect\", \"BNewTest\", \"bAnotherVALUE\")");
1688 OpStart(
"opencbm_configuration_set_data(\"SectTest\", \"NewTest\", \"RewrittenVALUE\")");
1694 OpStart(
"opencbm_configuration_get_data(handle, \"SectTest\", \"NewTest\")");
1699 fprintf(stderr,
" returned: %s\n", buffer);
1701 OpStart(
"opencbm_configuration_enum_sections(handle, ..., NULL)");
1705 OpStart(
"opencbm_configuration_enum_sections(handle, ..., 0x12345678)");
1717 OpStart(
"opencbm_configuration_close()");
1722 errorcode = EXIT_SUCCESS;
1726 if (errorcode == EXIT_SUCCESS) {
struct opencbm_configuration_s opencbm_configuration_t
char * cbmlibmisc_strdup(const char *const OldString)
Duplicate a given string.
void cbmlibmisc_strfree(const char *String)
Free a string.
int ARCH_MAINDECL main(int argc, char **argv)
Initialize the xum1541 device This function tries to find and identify the xum1541 device...
opencbm_configuration_handle opencbm_configuration_create(const char *Filename)
Creates the configuration file for reading and writing.
int opencbm_configuration_get_data(opencbm_configuration_handle Handle, const char Section[], const char Entry[], char **ReturnBuffer)
Read data from the configuration file.
opencbm_configuration_section_t * Next
int opencbm_configuration_set_data(opencbm_configuration_handle Handle, const char Section[], const char Entry[], const char Value[])
Write/Change data to/in the configuration file.
#define ASSUMED_MAX_LINE_LENGTH
the maximum line length we expect in a configuration file
char * cbmlibmisc_stralloc(unsigned int Length)
allocate memory for a string of a given size
Shared library / DLL for accessing the driver Read configuration file.
char * cbmlibmisc_strcat(const char *First, const char *Second)
Concatenate two strings.
opencbm_configuration_entry_t * Entries
const char * FileNameForWrite
int opencbm_configuration_enum_data(opencbm_configuration_handle Handle, const char Section[], opencbm_configuration_enum_data_callback_t Callback, void *Data)
Enumerate data in the configuration file.
opencbm_configuration_entry_t * Next
int opencbm_configuration_enum_data_callback_t(opencbm_configuration_handle Handle, const char Section[], const char Entry[], void *Context)
Callback type for entry enumeration.
int opencbm_configuration_section_remove(opencbm_configuration_handle Handle, const char Section[])
Remove a complete section from the configuration file.
opencbm_configuration_handle opencbm_configuration_open(const char *Filename)
Open the configuration file.
int opencbm_configuration_flush(opencbm_configuration_handle Handle)
Flush the configuration file.
Define makros and functions which account for differences between the different architectures.
int opencbm_configuration_enum_sections_callback_t(opencbm_configuration_handle Handle, const char Section[], void *Context)
Callback type for section enumeration.
opencbm_configuration_section_t * Sections
Some functions for string handling.
int opencbm_configuration_entry_remove(opencbm_configuration_handle Handle, const char Section[], const char EntryName[])
Remove an entry from the configuration file.
int opencbm_configuration_enum_sections(opencbm_configuration_handle Handle, opencbm_configuration_enum_sections_callback_t Callback, void *Data)
Enumerate sections in the configuration file.
int opencbm_configuration_close(opencbm_configuration_handle Handle)
Close the configuration file.