![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Sample code for the IniFile library
IniFileLibSample.cpp@0:d9d77bf7d923, 2010-11-16 (annotated)
- Committer:
- rinosh2
- Date:
- Tue Nov 16 16:36:14 2010 +0000
- Revision:
- 0:d9d77bf7d923
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
rinosh2 | 0:d9d77bf7d923 | 1 | /////////////////////////////////////////////////////////////////////////////// |
rinosh2 | 0:d9d77bf7d923 | 2 | // Sample IniFileLib program by rinos 2010 |
rinosh2 | 0:d9d77bf7d923 | 3 | /////////////////////////////////////////////////////////////////////////////// |
rinosh2 | 0:d9d77bf7d923 | 4 | |
rinosh2 | 0:d9d77bf7d923 | 5 | // test.ini |
rinosh2 | 0:d9d77bf7d923 | 6 | // | # Sample INI file |
rinosh2 | 0:d9d77bf7d923 | 7 | // | NTPServer = ntp.jst.mfeed.ad.jp |
rinosh2 | 0:d9d77bf7d923 | 8 | // | NTPPort = 0x123 |
rinosh2 | 0:d9d77bf7d923 | 9 | // | EnableNTP = False |
rinosh2 | 0:d9d77bf7d923 | 10 | |
rinosh2 | 0:d9d77bf7d923 | 11 | // Ini file value (int/bool/string) |
rinosh2 | 0:d9d77bf7d923 | 12 | // Key1 = 123 -> 123 (int) |
rinosh2 | 0:d9d77bf7d923 | 13 | // Key2 = 0x123 -> 291 (int) |
rinosh2 | 0:d9d77bf7d923 | 14 | // Key3 = FALSE -> false (bool) |
rinosh2 | 0:d9d77bf7d923 | 15 | // Key4 = TRUE -> true (bool) |
rinosh2 | 0:d9d77bf7d923 | 16 | // Key5 = 123 -> true (bool) |
rinosh2 | 0:d9d77bf7d923 | 17 | // key6 = abc "def -> 'abc "def' (string) |
rinosh2 | 0:d9d77bf7d923 | 18 | // key7 = " ghi "jkl " -> ' ghi "jkl '(string) |
rinosh2 | 0:d9d77bf7d923 | 19 | // #comment line |
rinosh2 | 0:d9d77bf7d923 | 20 | |
rinosh2 | 0:d9d77bf7d923 | 21 | #include "IniFile.h" |
rinosh2 | 0:d9d77bf7d923 | 22 | |
rinosh2 | 0:d9d77bf7d923 | 23 | #define ROOT_PATH "/local/" |
rinosh2 | 0:d9d77bf7d923 | 24 | #define ROOT_LEN (sizeof(ROOT_PATH) - 1) |
rinosh2 | 0:d9d77bf7d923 | 25 | |
rinosh2 | 0:d9d77bf7d923 | 26 | const char INI_FILE[] = ROOT_PATH "test.ini"; |
rinosh2 | 0:d9d77bf7d923 | 27 | |
rinosh2 | 0:d9d77bf7d923 | 28 | const char DEFAULT_NTP_SERVER[] = "ntp.jst.mfeed.ad.jp"; |
rinosh2 | 0:d9d77bf7d923 | 29 | const int DEFAULT_NTP_PORR = 123; |
rinosh2 | 0:d9d77bf7d923 | 30 | const bool DEFAULT_NTP_ENABLE = true; |
rinosh2 | 0:d9d77bf7d923 | 31 | |
rinosh2 | 0:d9d77bf7d923 | 32 | const int INI_BUF = 100; |
rinosh2 | 0:d9d77bf7d923 | 33 | |
rinosh2 | 0:d9d77bf7d923 | 34 | int main() { |
rinosh2 | 0:d9d77bf7d923 | 35 | LocalFileSystem local("local"); |
rinosh2 | 0:d9d77bf7d923 | 36 | |
rinosh2 | 0:d9d77bf7d923 | 37 | {// static function call sample |
rinosh2 | 0:d9d77bf7d923 | 38 | char ntp_server[INI_BUF]; |
rinosh2 | 0:d9d77bf7d923 | 39 | int ntp_port = DEFAULT_NTP_PORR; |
rinosh2 | 0:d9d77bf7d923 | 40 | bool ntp_enable = DEFAULT_NTP_ENABLE; |
rinosh2 | 0:d9d77bf7d923 | 41 | strcpy(ntp_server, DEFAULT_NTP_SERVER); |
rinosh2 | 0:d9d77bf7d923 | 42 | |
rinosh2 | 0:d9d77bf7d923 | 43 | IniFile::getval(INI_FILE, "NTPServer", ntp_server, sizeof(ntp_server)); |
rinosh2 | 0:d9d77bf7d923 | 44 | IniFile::getval(INI_FILE, "NTPPort", ntp_port); |
rinosh2 | 0:d9d77bf7d923 | 45 | IniFile::getval(INI_FILE, "EnableNTP", ntp_enable); |
rinosh2 | 0:d9d77bf7d923 | 46 | |
rinosh2 | 0:d9d77bf7d923 | 47 | printf("IniFile1: server=%s:%d mode=%d\n", ntp_server, ntp_port, ntp_enable); |
rinosh2 | 0:d9d77bf7d923 | 48 | } |
rinosh2 | 0:d9d77bf7d923 | 49 | |
rinosh2 | 0:d9d77bf7d923 | 50 | {// static function call sample |
rinosh2 | 0:d9d77bf7d923 | 51 | char ntp_server[INI_BUF]; |
rinosh2 | 0:d9d77bf7d923 | 52 | int ntp_port = DEFAULT_NTP_PORR; |
rinosh2 | 0:d9d77bf7d923 | 53 | bool ntp_enable = DEFAULT_NTP_ENABLE; |
rinosh2 | 0:d9d77bf7d923 | 54 | strcpy(ntp_server, DEFAULT_NTP_SERVER); |
rinosh2 | 0:d9d77bf7d923 | 55 | |
rinosh2 | 0:d9d77bf7d923 | 56 | IniFile ini(INI_FILE); |
rinosh2 | 0:d9d77bf7d923 | 57 | |
rinosh2 | 0:d9d77bf7d923 | 58 | ini.get("NTPServer", ntp_server, sizeof(ntp_server)); |
rinosh2 | 0:d9d77bf7d923 | 59 | ini.get("NTPPort", ntp_port); |
rinosh2 | 0:d9d77bf7d923 | 60 | ini.get("EnableNTP", ntp_enable); |
rinosh2 | 0:d9d77bf7d923 | 61 | |
rinosh2 | 0:d9d77bf7d923 | 62 | printf("IniFile1: server=%s:%d mode=%d\n", ntp_server, ntp_port, ntp_enable); |
rinosh2 | 0:d9d77bf7d923 | 63 | } |
rinosh2 | 0:d9d77bf7d923 | 64 | |
rinosh2 | 0:d9d77bf7d923 | 65 | {// list function call sample |
rinosh2 | 0:d9d77bf7d923 | 66 | char ntp_server[INI_BUF]; |
rinosh2 | 0:d9d77bf7d923 | 67 | int ntp_port = DEFAULT_NTP_PORR; |
rinosh2 | 0:d9d77bf7d923 | 68 | bool ntp_enable = DEFAULT_NTP_ENABLE; |
rinosh2 | 0:d9d77bf7d923 | 69 | strcpy(ntp_server, DEFAULT_NTP_SERVER); |
rinosh2 | 0:d9d77bf7d923 | 70 | |
rinosh2 | 0:d9d77bf7d923 | 71 | IniFile::IniList INILIST[] = { |
rinosh2 | 0:d9d77bf7d923 | 72 | {"NTPServer", sizeof(ntp_server), ntp_server}, |
rinosh2 | 0:d9d77bf7d923 | 73 | {"NTPPort", IniFile::DTYPE_INT, &ntp_port}, |
rinosh2 | 0:d9d77bf7d923 | 74 | {"EnableNTP", IniFile::DTYPE_BOOL, &ntp_enable}, |
rinosh2 | 0:d9d77bf7d923 | 75 | 0 // END |
rinosh2 | 0:d9d77bf7d923 | 76 | }; |
rinosh2 | 0:d9d77bf7d923 | 77 | |
rinosh2 | 0:d9d77bf7d923 | 78 | IniFile::getval(INI_FILE, INILIST); |
rinosh2 | 0:d9d77bf7d923 | 79 | |
rinosh2 | 0:d9d77bf7d923 | 80 | printf("IniFile1: server=%s:%d mode=%d\n", ntp_server, ntp_port, ntp_enable); |
rinosh2 | 0:d9d77bf7d923 | 81 | } |
rinosh2 | 0:d9d77bf7d923 | 82 | |
rinosh2 | 0:d9d77bf7d923 | 83 | return 0; |
rinosh2 | 0:d9d77bf7d923 | 84 | } |