Version light
Revision 2:d8febae84a45, committed 2010-09-12
- Comitter:
- shintamainjp
- Date:
- Sun Sep 12 07:35:25 2010 +0000
- Parent:
- 1:f02e081afe42
- Child:
- 3:7250e339328c
- Commit message:
Changed in this revision
ConfigFile.cpp | Show annotated file Show diff for this revision Revisions of this file |
ConfigFile.h | Show annotated file Show diff for this revision Revisions of this file |
--- a/ConfigFile.cpp Sun Sep 12 07:22:00 2010 +0000 +++ b/ConfigFile.cpp Sun Sep 12 07:35:25 2010 +0000 @@ -1,5 +1,9 @@ #include "ConfigFile.h" +#define NEWLINE_UNIX "\n" +#define NEWLINE_DOS "\r\n" +#define NEWLINE_MAC "\r" + ConfigFile::ConfigFile() { /* * Allocation for a config_t list. @@ -46,7 +50,7 @@ if (p == NULL) { return false; } - + /* * Check the storage size. */ @@ -212,7 +216,7 @@ if (buf[0] == '#') { continue; } - + /* * Trim a return. */ @@ -225,7 +229,7 @@ /* * Separate key and value. - */ + */ char k[MAXLEN_KEY]; char v[MAXLEN_VALUE]; char *sp = strchr(buf, SEPARATOR); @@ -240,19 +244,41 @@ return true; } -bool ConfigFile::write(char *file) { +bool ConfigFile::write(char *file, FileFormat ff) { + /* + * Open the target file. + */ FILE *fp = fopen(file, "w"); if (fp == NULL) { return false; } - static const char *NEWLINE_UNIX = "\n"; - static const char *NEWLINE_DOS = "\r\n"; - static const char *NEWLINE_MAC = "\r"; + /* + * Set a type of new line. + */ + char *newline = NEWLINE_UNIX; + switch (ff) { + case UNIX: + newline = NEWLINE_UNIX; + break; + case MAC: + newline = NEWLINE_MAC; + break; + case DOS: + newline = NEWLINE_DOS; + break; + default: + newline = NEWLINE_UNIX; + break; + } + + /* + * Write the data. + */ for (int i = 0; i < MAXCONFIG; i++) { config_t *cfg = configlist[i]; if (cfg != NULL) { - fprintf(fp, "%s=%s%s", cfg->key, cfg->value, NEWLINE_UNIX); + fprintf(fp, "%s=%s%s", cfg->key, cfg->value, newline); } } fclose(fp);
--- a/ConfigFile.h Sun Sep 12 07:22:00 2010 +0000 +++ b/ConfigFile.h Sun Sep 12 07:35:25 2010 +0000 @@ -62,12 +62,19 @@ */ bool read(char *file); + typedef enum { + UNIX, + MAC, + DOS + } FileFormat; + /** * Write from the target file. * * @param file A target file name. + * @param ff File format. */ - bool write(char *file); + bool write(char *file, FileFormat ff = UNIX); /** * Output for debugging.