Copied on 09 Aug 2012.
Fork of ConfigFile by
ConfigFile.h@0:6b4ba48753b9, 2010-09-12 (annotated)
- Committer:
- shintamainjp
- Date:
- Sun Sep 12 07:11:54 2010 +0000
- Revision:
- 0:6b4ba48753b9
- Child:
- 1:f02e081afe42
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
shintamainjp | 0:6b4ba48753b9 | 1 | #include "mbed.h" |
shintamainjp | 0:6b4ba48753b9 | 2 | |
shintamainjp | 0:6b4ba48753b9 | 3 | #ifndef _CONFIG_FILE_H_ |
shintamainjp | 0:6b4ba48753b9 | 4 | #define _CONFIG_FILE_H_ |
shintamainjp | 0:6b4ba48753b9 | 5 | |
shintamainjp | 0:6b4ba48753b9 | 6 | /** |
shintamainjp | 0:6b4ba48753b9 | 7 | * Configuration File class. |
shintamainjp | 0:6b4ba48753b9 | 8 | */ |
shintamainjp | 0:6b4ba48753b9 | 9 | class ConfigFile { |
shintamainjp | 0:6b4ba48753b9 | 10 | public: |
shintamainjp | 0:6b4ba48753b9 | 11 | |
shintamainjp | 0:6b4ba48753b9 | 12 | /** |
shintamainjp | 0:6b4ba48753b9 | 13 | * Create a configuration file class. |
shintamainjp | 0:6b4ba48753b9 | 14 | */ |
shintamainjp | 0:6b4ba48753b9 | 15 | ConfigFile(); |
shintamainjp | 0:6b4ba48753b9 | 16 | |
shintamainjp | 0:6b4ba48753b9 | 17 | /** |
shintamainjp | 0:6b4ba48753b9 | 18 | * Destroy a configuration file class. |
shintamainjp | 0:6b4ba48753b9 | 19 | */ |
shintamainjp | 0:6b4ba48753b9 | 20 | ~ConfigFile(); |
shintamainjp | 0:6b4ba48753b9 | 21 | |
shintamainjp | 0:6b4ba48753b9 | 22 | /** |
shintamainjp | 0:6b4ba48753b9 | 23 | * Get a value for a key. |
shintamainjp | 0:6b4ba48753b9 | 24 | * |
shintamainjp | 0:6b4ba48753b9 | 25 | * @param key A target key name. |
shintamainjp | 0:6b4ba48753b9 | 26 | * |
shintamainjp | 0:6b4ba48753b9 | 27 | * @return A value or NULL. |
shintamainjp | 0:6b4ba48753b9 | 28 | */ |
shintamainjp | 0:6b4ba48753b9 | 29 | char *getValue(char *key); |
shintamainjp | 0:6b4ba48753b9 | 30 | |
shintamainjp | 0:6b4ba48753b9 | 31 | /** |
shintamainjp | 0:6b4ba48753b9 | 32 | * Set a set of a key and value. |
shintamainjp | 0:6b4ba48753b9 | 33 | * |
shintamainjp | 0:6b4ba48753b9 | 34 | * @param key A key. |
shintamainjp | 0:6b4ba48753b9 | 35 | * @param value A value. |
shintamainjp | 0:6b4ba48753b9 | 36 | * |
shintamainjp | 0:6b4ba48753b9 | 37 | * @return True if it succeed. |
shintamainjp | 0:6b4ba48753b9 | 38 | */ |
shintamainjp | 0:6b4ba48753b9 | 39 | bool setValue(char *key, char *value); |
shintamainjp | 0:6b4ba48753b9 | 40 | |
shintamainjp | 0:6b4ba48753b9 | 41 | /** |
shintamainjp | 0:6b4ba48753b9 | 42 | * Remove a config. |
shintamainjp | 0:6b4ba48753b9 | 43 | * |
shintamainjp | 0:6b4ba48753b9 | 44 | * @param key A key. |
shintamainjp | 0:6b4ba48753b9 | 45 | * |
shintamainjp | 0:6b4ba48753b9 | 46 | * @return True if it succeed. |
shintamainjp | 0:6b4ba48753b9 | 47 | */ |
shintamainjp | 0:6b4ba48753b9 | 48 | bool remove(char *key); |
shintamainjp | 0:6b4ba48753b9 | 49 | |
shintamainjp | 0:6b4ba48753b9 | 50 | /** |
shintamainjp | 0:6b4ba48753b9 | 51 | * Remove all config. |
shintamainjp | 0:6b4ba48753b9 | 52 | * |
shintamainjp | 0:6b4ba48753b9 | 53 | * @return True if it succeed. |
shintamainjp | 0:6b4ba48753b9 | 54 | */ |
shintamainjp | 0:6b4ba48753b9 | 55 | bool removeAll(void); |
shintamainjp | 0:6b4ba48753b9 | 56 | |
shintamainjp | 0:6b4ba48753b9 | 57 | /** |
shintamainjp | 0:6b4ba48753b9 | 58 | * Read from the target file. |
shintamainjp | 0:6b4ba48753b9 | 59 | * |
shintamainjp | 0:6b4ba48753b9 | 60 | * @param file A target file name. |
shintamainjp | 0:6b4ba48753b9 | 61 | */ |
shintamainjp | 0:6b4ba48753b9 | 62 | bool read(char *file); |
shintamainjp | 0:6b4ba48753b9 | 63 | |
shintamainjp | 0:6b4ba48753b9 | 64 | /** |
shintamainjp | 0:6b4ba48753b9 | 65 | * Write from the target file. |
shintamainjp | 0:6b4ba48753b9 | 66 | * |
shintamainjp | 0:6b4ba48753b9 | 67 | * @param file A target file name. |
shintamainjp | 0:6b4ba48753b9 | 68 | */ |
shintamainjp | 0:6b4ba48753b9 | 69 | bool write(char *file); |
shintamainjp | 0:6b4ba48753b9 | 70 | |
shintamainjp | 0:6b4ba48753b9 | 71 | /** |
shintamainjp | 0:6b4ba48753b9 | 72 | * Output for debugging. |
shintamainjp | 0:6b4ba48753b9 | 73 | * |
shintamainjp | 0:6b4ba48753b9 | 74 | * @deprecated Please do not use this method. |
shintamainjp | 0:6b4ba48753b9 | 75 | */ |
shintamainjp | 0:6b4ba48753b9 | 76 | void debout(void) { |
shintamainjp | 0:6b4ba48753b9 | 77 | printf("===========================================================================\n"); |
shintamainjp | 0:6b4ba48753b9 | 78 | for (int i = 0; i < MAXCONFIG; i++) { |
shintamainjp | 0:6b4ba48753b9 | 79 | config_t *cfg = configlist[i]; |
shintamainjp | 0:6b4ba48753b9 | 80 | printf("[%03d]:", i); |
shintamainjp | 0:6b4ba48753b9 | 81 | if (cfg == NULL) { |
shintamainjp | 0:6b4ba48753b9 | 82 | printf("NULL\n"); |
shintamainjp | 0:6b4ba48753b9 | 83 | } else { |
shintamainjp | 0:6b4ba48753b9 | 84 | printf("'%s'='%s'\n", cfg->key, cfg->value); |
shintamainjp | 0:6b4ba48753b9 | 85 | } |
shintamainjp | 0:6b4ba48753b9 | 86 | } |
shintamainjp | 0:6b4ba48753b9 | 87 | printf("===========================================================================\n"); |
shintamainjp | 0:6b4ba48753b9 | 88 | } |
shintamainjp | 0:6b4ba48753b9 | 89 | private: |
shintamainjp | 0:6b4ba48753b9 | 90 | typedef struct { |
shintamainjp | 0:6b4ba48753b9 | 91 | char *key; |
shintamainjp | 0:6b4ba48753b9 | 92 | char *value; |
shintamainjp | 0:6b4ba48753b9 | 93 | } config_t; |
shintamainjp | 0:6b4ba48753b9 | 94 | config_t **configlist; |
shintamainjp | 0:6b4ba48753b9 | 95 | static const int MAXCONFIG = 64; |
shintamainjp | 0:6b4ba48753b9 | 96 | static const int MAXLEN_KEY = 64; |
shintamainjp | 0:6b4ba48753b9 | 97 | static const int MAXLEN_VALUE = 128; |
shintamainjp | 0:6b4ba48753b9 | 98 | static const char SEPARATOR = '='; |
shintamainjp | 0:6b4ba48753b9 | 99 | |
shintamainjp | 0:6b4ba48753b9 | 100 | config_t *search(char *key); |
shintamainjp | 0:6b4ba48753b9 | 101 | bool add(config_t *cfg); |
shintamainjp | 0:6b4ba48753b9 | 102 | }; |
shintamainjp | 0:6b4ba48753b9 | 103 | |
shintamainjp | 0:6b4ba48753b9 | 104 | #endif |