Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: ConfigFile
ConfigurationFile.cpp@3:7177e463ac52, 2016-04-06 (annotated)
- Committer:
- 13075593
- Date:
- Wed Apr 06 20:20:25 2016 +0000
- Revision:
- 3:7177e463ac52
- Parent:
- 2:5af11ca8e085
- Child:
- 4:c8f6d3677937
Self functionnal Class.; Initiate constructor with pathname parameter and the class will get those keys : ; - IP; - MASK; - GATEWAY; - URL; - BROADCAST
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| 13075593 | 0:1405a58a7e8d | 1 | #include "ConfigurationFile.h" |
| 13075593 | 0:1405a58a7e8d | 2 | |
| 13075593 | 3:7177e463ac52 | 3 | ConfigurationFile::ConfigurationFile(char *pathName) |
| 13075593 | 0:1405a58a7e8d | 4 | { |
| 13075593 | 3:7177e463ac52 | 5 | readConfigurationFile(pathName); |
| 13075593 | 3:7177e463ac52 | 6 | readUnitConfiguration(); |
| 13075593 | 3:7177e463ac52 | 7 | } |
| 13075593 | 3:7177e463ac52 | 8 | |
| 13075593 | 3:7177e463ac52 | 9 | void ConfigurationFile::readConfigurationFile(char *pathName) |
| 13075593 | 3:7177e463ac52 | 10 | { |
| 13075593 | 3:7177e463ac52 | 11 | if (!cfg.read(pathName)) |
| 13075593 | 3:7177e463ac52 | 12 | { |
| 13075593 | 3:7177e463ac52 | 13 | error("Failure to read a configuration file.\n"); |
| 13075593 | 3:7177e463ac52 | 14 | } |
| 13075593 | 0:1405a58a7e8d | 15 | } |
| 13075593 | 0:1405a58a7e8d | 16 | |
| 13075593 | 0:1405a58a7e8d | 17 | ConfigurationFile::~ConfigurationFile() |
| 13075593 | 0:1405a58a7e8d | 18 | { |
| 13075593 | 0:1405a58a7e8d | 19 | } |
| 13075593 | 0:1405a58a7e8d | 20 | |
| 13075593 | 0:1405a58a7e8d | 21 | void ConfigurationFile::setIP() |
| 13075593 | 0:1405a58a7e8d | 22 | { |
| 13075593 | 0:1405a58a7e8d | 23 | char value[BUFSIZ]; |
| 13075593 | 0:1405a58a7e8d | 24 | if (cfg.getValue( "IP", &value[0], sizeof(value))) |
| 13075593 | 0:1405a58a7e8d | 25 | { |
| 13075593 | 0:1405a58a7e8d | 26 | printf("'%s'='%s'\n", "IP", value); |
| 13075593 | 0:1405a58a7e8d | 27 | string s = value; |
| 13075593 | 0:1405a58a7e8d | 28 | ip = s; |
| 13075593 | 0:1405a58a7e8d | 29 | } |
| 13075593 | 0:1405a58a7e8d | 30 | else |
| 13075593 | 0:1405a58a7e8d | 31 | { |
| 13075593 | 0:1405a58a7e8d | 32 | error("Failure to read IP key"); |
| 13075593 | 0:1405a58a7e8d | 33 | } |
| 13075593 | 0:1405a58a7e8d | 34 | } |
| 13075593 | 0:1405a58a7e8d | 35 | |
| 13075593 | 0:1405a58a7e8d | 36 | string ConfigurationFile::getIP() |
| 13075593 | 0:1405a58a7e8d | 37 | { |
| 13075593 | 0:1405a58a7e8d | 38 | return ip; |
| 13075593 | 0:1405a58a7e8d | 39 | } |
| 13075593 | 0:1405a58a7e8d | 40 | |
| 13075593 | 0:1405a58a7e8d | 41 | void ConfigurationFile::setMask() |
| 13075593 | 0:1405a58a7e8d | 42 | { |
| 13075593 | 0:1405a58a7e8d | 43 | char value[BUFSIZ]; |
| 13075593 | 0:1405a58a7e8d | 44 | if (cfg.getValue( "MASK", &value[0], sizeof(value))) |
| 13075593 | 0:1405a58a7e8d | 45 | { |
| 13075593 | 0:1405a58a7e8d | 46 | printf("'%s'='%s'\n", "MASK", value); |
| 13075593 | 0:1405a58a7e8d | 47 | string s = value; |
| 13075593 | 0:1405a58a7e8d | 48 | mask = s; |
| 13075593 | 0:1405a58a7e8d | 49 | } |
| 13075593 | 0:1405a58a7e8d | 50 | else |
| 13075593 | 0:1405a58a7e8d | 51 | { |
| 13075593 | 0:1405a58a7e8d | 52 | error("Failure to read MASK key"); |
| 13075593 | 0:1405a58a7e8d | 53 | } |
| 13075593 | 0:1405a58a7e8d | 54 | } |
| 13075593 | 0:1405a58a7e8d | 55 | |
| 13075593 | 0:1405a58a7e8d | 56 | string ConfigurationFile::getMask() |
| 13075593 | 0:1405a58a7e8d | 57 | { |
| 13075593 | 0:1405a58a7e8d | 58 | return mask; |
| 13075593 | 0:1405a58a7e8d | 59 | } |
| 13075593 | 0:1405a58a7e8d | 60 | |
| 13075593 | 0:1405a58a7e8d | 61 | void ConfigurationFile::setGateway() |
| 13075593 | 0:1405a58a7e8d | 62 | { |
| 13075593 | 0:1405a58a7e8d | 63 | char value[BUFSIZ]; |
| 13075593 | 0:1405a58a7e8d | 64 | if (cfg.getValue( "GATEWAY", &value[0], sizeof(value))) |
| 13075593 | 0:1405a58a7e8d | 65 | { |
| 13075593 | 0:1405a58a7e8d | 66 | printf("'%s'='%s'\n", "GATEWAY", value); |
| 13075593 | 0:1405a58a7e8d | 67 | string s = value; |
| 13075593 | 0:1405a58a7e8d | 68 | gateway = s; |
| 13075593 | 0:1405a58a7e8d | 69 | } |
| 13075593 | 0:1405a58a7e8d | 70 | else |
| 13075593 | 0:1405a58a7e8d | 71 | { |
| 13075593 | 0:1405a58a7e8d | 72 | error("Failure to read GATEWAY key"); |
| 13075593 | 0:1405a58a7e8d | 73 | } |
| 13075593 | 0:1405a58a7e8d | 74 | } |
| 13075593 | 0:1405a58a7e8d | 75 | |
| 13075593 | 0:1405a58a7e8d | 76 | string ConfigurationFile::getGateway() |
| 13075593 | 0:1405a58a7e8d | 77 | { |
| 13075593 | 0:1405a58a7e8d | 78 | return gateway; |
| 13075593 | 0:1405a58a7e8d | 79 | } |
| 13075593 | 0:1405a58a7e8d | 80 | |
| 13075593 | 0:1405a58a7e8d | 81 | void ConfigurationFile::setURL() |
| 13075593 | 0:1405a58a7e8d | 82 | { |
| 13075593 | 0:1405a58a7e8d | 83 | char value[BUFSIZ]; |
| 13075593 | 0:1405a58a7e8d | 84 | if (cfg.getValue( "URL", &value[0], sizeof(value))) |
| 13075593 | 0:1405a58a7e8d | 85 | { |
| 13075593 | 0:1405a58a7e8d | 86 | printf("'%s'='%s'\n", "URL", value); |
| 13075593 | 0:1405a58a7e8d | 87 | string s = value; |
| 13075593 | 0:1405a58a7e8d | 88 | url = s; |
| 13075593 | 0:1405a58a7e8d | 89 | } |
| 13075593 | 0:1405a58a7e8d | 90 | else |
| 13075593 | 0:1405a58a7e8d | 91 | { |
| 13075593 | 0:1405a58a7e8d | 92 | error("Failure to read URL key"); |
| 13075593 | 0:1405a58a7e8d | 93 | } |
| 13075593 | 0:1405a58a7e8d | 94 | } |
| 13075593 | 0:1405a58a7e8d | 95 | |
| 13075593 | 0:1405a58a7e8d | 96 | string ConfigurationFile::getURL() |
| 13075593 | 0:1405a58a7e8d | 97 | { |
| 13075593 | 0:1405a58a7e8d | 98 | return url; |
| 13075593 | 0:1405a58a7e8d | 99 | } |
| 13075593 | 0:1405a58a7e8d | 100 | |
| 13075593 | 3:7177e463ac52 | 101 | void ConfigurationFile::setBroadcastPort() |
| 13075593 | 1:5b6c5c2a33d0 | 102 | { |
| 13075593 | 1:5b6c5c2a33d0 | 103 | char value[BUFSIZ]; |
| 13075593 | 2:5af11ca8e085 | 104 | if (cfg.getValue( "BROADCAST", &value[0], sizeof(value))) |
| 13075593 | 1:5b6c5c2a33d0 | 105 | { |
| 13075593 | 2:5af11ca8e085 | 106 | printf("'%s'='%s'\n", "Broadcast Address", value); |
| 13075593 | 1:5b6c5c2a33d0 | 107 | string s = value; |
| 13075593 | 3:7177e463ac52 | 108 | broadcastPort = s; |
| 13075593 | 1:5b6c5c2a33d0 | 109 | } |
| 13075593 | 1:5b6c5c2a33d0 | 110 | else |
| 13075593 | 1:5b6c5c2a33d0 | 111 | { |
| 13075593 | 2:5af11ca8e085 | 112 | error("Failure to read Broadcast Address key"); |
| 13075593 | 1:5b6c5c2a33d0 | 113 | } |
| 13075593 | 1:5b6c5c2a33d0 | 114 | } |
| 13075593 | 1:5b6c5c2a33d0 | 115 | |
| 13075593 | 3:7177e463ac52 | 116 | int ConfigurationFile::getBroadcastPort() |
| 13075593 | 1:5b6c5c2a33d0 | 117 | { |
| 13075593 | 3:7177e463ac52 | 118 | return atoi(broadcastPort.c_str()); |
| 13075593 | 1:5b6c5c2a33d0 | 119 | } |
| 13075593 | 1:5b6c5c2a33d0 | 120 | |
| 13075593 | 0:1405a58a7e8d | 121 | |
| 13075593 | 3:7177e463ac52 | 122 | void ConfigurationFile::readUnitConfiguration() |
| 13075593 | 0:1405a58a7e8d | 123 | { |
| 13075593 | 0:1405a58a7e8d | 124 | setIP(); |
| 13075593 | 0:1405a58a7e8d | 125 | setMask(); |
| 13075593 | 0:1405a58a7e8d | 126 | setGateway(); |
| 13075593 | 0:1405a58a7e8d | 127 | setURL(); |
| 13075593 | 3:7177e463ac52 | 128 | setBroadcastPort(); |
| 13075593 | 1:5b6c5c2a33d0 | 129 | } |
| 13075593 | 1:5b6c5c2a33d0 | 130 | |
| 13075593 | 3:7177e463ac52 | 131 | void ConfigurationFile::getUnitConfiguration() |
| 13075593 | 1:5b6c5c2a33d0 | 132 | { |
| 13075593 | 3:7177e463ac52 | 133 | printf("IP : %s,\n",getIP()); |
| 13075593 | 3:7177e463ac52 | 134 | printf("Mask : %s,\n",getMask()); |
| 13075593 | 3:7177e463ac52 | 135 | printf("Gateway : %s,\n",getGateway()); |
| 13075593 | 3:7177e463ac52 | 136 | printf("URL : %s,\n",getURL()); |
| 13075593 | 3:7177e463ac52 | 137 | printf("BroadcastPort : %i,\n",getBroadcastPort()); |
| 13075593 | 0:1405a58a7e8d | 138 | } |