├── .bumpversion.cfg ├── LICENSE ├── README.md ├── examples ├── IniBrowseExample │ ├── IniBrowseExample.ino │ ├── README.md │ └── lunch.ini └── IniFileExample │ ├── IniFileExample.ino │ ├── README.md │ └── net.ini ├── extras └── test │ ├── .gitignore │ ├── Ethernet.h │ ├── File.cpp │ ├── File.h │ ├── IPAddress.cpp │ ├── IPAddress.h │ ├── Makefile │ ├── SD.h │ ├── arduino_compat.h │ ├── browsetest.ini │ ├── ini_test.cpp │ ├── ini_test.regressiontest │ ├── readme.md │ ├── readtest.cpp │ └── test.ini ├── keywords.txt ├── library.properties └── src ├── IniFile.cpp └── IniFile.h /.bumpversion.cfg: -------------------------------------------------------------------------------- 1 | [bumpversion] 2 | current_version = 1.3.0 3 | commit = True 4 | tag = True 5 | 6 | [bumpversion:file:library.properties] 7 | 8 | [bumpversion:file:src/IniFile.h] 9 | 10 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU LESSER GENERAL PUBLIC LICENSE 2 | Version 2.1, February 1999 3 | 4 | Copyright (C) 1991, 1999 Free Software Foundation, Inc. 5 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 6 | Everyone is permitted to copy and distribute verbatim copies 7 | of this license document, but changing it is not allowed. 8 | 9 | (This is the first released version of the Lesser GPL. It also counts 10 | as the successor of the GNU Library Public License, version 2, hence 11 | the version number 2.1.) 12 | 13 | Preamble 14 | 15 | The licenses for most software are designed to take away your 16 | freedom to share and change it. By contrast, the GNU General Public 17 | Licenses are intended to guarantee your freedom to share and change 18 | free software--to make sure the software is free for all its users. 19 | 20 | This license, the Lesser General Public License, applies to some 21 | specially designated software packages--typically libraries--of the 22 | Free Software Foundation and other authors who decide to use it. You 23 | can use it too, but we suggest you first think carefully about whether 24 | this license or the ordinary General Public License is the better 25 | strategy to use in any particular case, based on the explanations below. 26 | 27 | When we speak of free software, we are referring to freedom of use, 28 | not price. Our General Public Licenses are designed to make sure that 29 | you have the freedom to distribute copies of free software (and charge 30 | for this service if you wish); that you receive source code or can get 31 | it if you want it; that you can change the software and use pieces of 32 | it in new free programs; and that you are informed that you can do 33 | these things. 34 | 35 | To protect your rights, we need to make restrictions that forbid 36 | distributors to deny you these rights or to ask you to surrender these 37 | rights. These restrictions translate to certain responsibilities for 38 | you if you distribute copies of the library or if you modify it. 39 | 40 | For example, if you distribute copies of the library, whether gratis 41 | or for a fee, you must give the recipients all the rights that we gave 42 | you. You must make sure that they, too, receive or can get the source 43 | code. If you link other code with the library, you must provide 44 | complete object files to the recipients, so that they can relink them 45 | with the library after making changes to the library and recompiling 46 | it. And you must show them these terms so they know their rights. 47 | 48 | We protect your rights with a two-step method: (1) we copyright the 49 | library, and (2) we offer you this license, which gives you legal 50 | permission to copy, distribute and/or modify the library. 51 | 52 | To protect each distributor, we want to make it very clear that 53 | there is no warranty for the free library. Also, if the library is 54 | modified by someone else and passed on, the recipients should know 55 | that what they have is not the original version, so that the original 56 | author's reputation will not be affected by problems that might be 57 | introduced by others. 58 | 59 | Finally, software patents pose a constant threat to the existence of 60 | any free program. We wish to make sure that a company cannot 61 | effectively restrict the users of a free program by obtaining a 62 | restrictive license from a patent holder. Therefore, we insist that 63 | any patent license obtained for a version of the library must be 64 | consistent with the full freedom of use specified in this license. 65 | 66 | Most GNU software, including some libraries, is covered by the 67 | ordinary GNU General Public License. This license, the GNU Lesser 68 | General Public License, applies to certain designated libraries, and 69 | is quite different from the ordinary General Public License. We use 70 | this license for certain libraries in order to permit linking those 71 | libraries into non-free programs. 72 | 73 | When a program is linked with a library, whether statically or using 74 | a shared library, the combination of the two is legally speaking a 75 | combined work, a derivative of the original library. The ordinary 76 | General Public License therefore permits such linking only if the 77 | entire combination fits its criteria of freedom. The Lesser General 78 | Public License permits more lax criteria for linking other code with 79 | the library. 80 | 81 | We call this license the "Lesser" General Public License because it 82 | does Less to protect the user's freedom than the ordinary General 83 | Public License. It also provides other free software developers Less 84 | of an advantage over competing non-free programs. These disadvantages 85 | are the reason we use the ordinary General Public License for many 86 | libraries. However, the Lesser license provides advantages in certain 87 | special circumstances. 88 | 89 | For example, on rare occasions, there may be a special need to 90 | encourage the widest possible use of a certain library, so that it becomes 91 | a de-facto standard. To achieve this, non-free programs must be 92 | allowed to use the library. A more frequent case is that a free 93 | library does the same job as widely used non-free libraries. In this 94 | case, there is little to gain by limiting the free library to free 95 | software only, so we use the Lesser General Public License. 96 | 97 | In other cases, permission to use a particular library in non-free 98 | programs enables a greater number of people to use a large body of 99 | free software. For example, permission to use the GNU C Library in 100 | non-free programs enables many more people to use the whole GNU 101 | operating system, as well as its variant, the GNU/Linux operating 102 | system. 103 | 104 | Although the Lesser General Public License is Less protective of the 105 | users' freedom, it does ensure that the user of a program that is 106 | linked with the Library has the freedom and the wherewithal to run 107 | that program using a modified version of the Library. 108 | 109 | The precise terms and conditions for copying, distribution and 110 | modification follow. Pay close attention to the difference between a 111 | "work based on the library" and a "work that uses the library". The 112 | former contains code derived from the library, whereas the latter must 113 | be combined with the library in order to run. 114 | 115 | GNU LESSER GENERAL PUBLIC LICENSE 116 | TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 117 | 118 | 0. This License Agreement applies to any software library or other 119 | program which contains a notice placed by the copyright holder or 120 | other authorized party saying it may be distributed under the terms of 121 | this Lesser General Public License (also called "this License"). 122 | Each licensee is addressed as "you". 123 | 124 | A "library" means a collection of software functions and/or data 125 | prepared so as to be conveniently linked with application programs 126 | (which use some of those functions and data) to form executables. 127 | 128 | The "Library", below, refers to any such software library or work 129 | which has been distributed under these terms. A "work based on the 130 | Library" means either the Library or any derivative work under 131 | copyright law: that is to say, a work containing the Library or a 132 | portion of it, either verbatim or with modifications and/or translated 133 | straightforwardly into another language. (Hereinafter, translation is 134 | included without limitation in the term "modification".) 135 | 136 | "Source code" for a work means the preferred form of the work for 137 | making modifications to it. For a library, complete source code means 138 | all the source code for all modules it contains, plus any associated 139 | interface definition files, plus the scripts used to control compilation 140 | and installation of the library. 141 | 142 | Activities other than copying, distribution and modification are not 143 | covered by this License; they are outside its scope. The act of 144 | running a program using the Library is not restricted, and output from 145 | such a program is covered only if its contents constitute a work based 146 | on the Library (independent of the use of the Library in a tool for 147 | writing it). Whether that is true depends on what the Library does 148 | and what the program that uses the Library does. 149 | 150 | 1. You may copy and distribute verbatim copies of the Library's 151 | complete source code as you receive it, in any medium, provided that 152 | you conspicuously and appropriately publish on each copy an 153 | appropriate copyright notice and disclaimer of warranty; keep intact 154 | all the notices that refer to this License and to the absence of any 155 | warranty; and distribute a copy of this License along with the 156 | Library. 157 | 158 | You may charge a fee for the physical act of transferring a copy, 159 | and you may at your option offer warranty protection in exchange for a 160 | fee. 161 | 162 | 2. You may modify your copy or copies of the Library or any portion 163 | of it, thus forming a work based on the Library, and copy and 164 | distribute such modifications or work under the terms of Section 1 165 | above, provided that you also meet all of these conditions: 166 | 167 | a) The modified work must itself be a software library. 168 | 169 | b) You must cause the files modified to carry prominent notices 170 | stating that you changed the files and the date of any change. 171 | 172 | c) You must cause the whole of the work to be licensed at no 173 | charge to all third parties under the terms of this License. 174 | 175 | d) If a facility in the modified Library refers to a function or a 176 | table of data to be supplied by an application program that uses 177 | the facility, other than as an argument passed when the facility 178 | is invoked, then you must make a good faith effort to ensure that, 179 | in the event an application does not supply such function or 180 | table, the facility still operates, and performs whatever part of 181 | its purpose remains meaningful. 182 | 183 | (For example, a function in a library to compute square roots has 184 | a purpose that is entirely well-defined independent of the 185 | application. Therefore, Subsection 2d requires that any 186 | application-supplied function or table used by this function must 187 | be optional: if the application does not supply it, the square 188 | root function must still compute square roots.) 189 | 190 | These requirements apply to the modified work as a whole. If 191 | identifiable sections of that work are not derived from the Library, 192 | and can be reasonably considered independent and separate works in 193 | themselves, then this License, and its terms, do not apply to those 194 | sections when you distribute them as separate works. But when you 195 | distribute the same sections as part of a whole which is a work based 196 | on the Library, the distribution of the whole must be on the terms of 197 | this License, whose permissions for other licensees extend to the 198 | entire whole, and thus to each and every part regardless of who wrote 199 | it. 200 | 201 | Thus, it is not the intent of this section to claim rights or contest 202 | your rights to work written entirely by you; rather, the intent is to 203 | exercise the right to control the distribution of derivative or 204 | collective works based on the Library. 205 | 206 | In addition, mere aggregation of another work not based on the Library 207 | with the Library (or with a work based on the Library) on a volume of 208 | a storage or distribution medium does not bring the other work under 209 | the scope of this License. 210 | 211 | 3. You may opt to apply the terms of the ordinary GNU General Public 212 | License instead of this License to a given copy of the Library. To do 213 | this, you must alter all the notices that refer to this License, so 214 | that they refer to the ordinary GNU General Public License, version 2, 215 | instead of to this License. (If a newer version than version 2 of the 216 | ordinary GNU General Public License has appeared, then you can specify 217 | that version instead if you wish.) Do not make any other change in 218 | these notices. 219 | 220 | Once this change is made in a given copy, it is irreversible for 221 | that copy, so the ordinary GNU General Public License applies to all 222 | subsequent copies and derivative works made from that copy. 223 | 224 | This option is useful when you wish to copy part of the code of 225 | the Library into a program that is not a library. 226 | 227 | 4. You may copy and distribute the Library (or a portion or 228 | derivative of it, under Section 2) in object code or executable form 229 | under the terms of Sections 1 and 2 above provided that you accompany 230 | it with the complete corresponding machine-readable source code, which 231 | must be distributed under the terms of Sections 1 and 2 above on a 232 | medium customarily used for software interchange. 233 | 234 | If distribution of object code is made by offering access to copy 235 | from a designated place, then offering equivalent access to copy the 236 | source code from the same place satisfies the requirement to 237 | distribute the source code, even though third parties are not 238 | compelled to copy the source along with the object code. 239 | 240 | 5. A program that contains no derivative of any portion of the 241 | Library, but is designed to work with the Library by being compiled or 242 | linked with it, is called a "work that uses the Library". Such a 243 | work, in isolation, is not a derivative work of the Library, and 244 | therefore falls outside the scope of this License. 245 | 246 | However, linking a "work that uses the Library" with the Library 247 | creates an executable that is a derivative of the Library (because it 248 | contains portions of the Library), rather than a "work that uses the 249 | library". The executable is therefore covered by this License. 250 | Section 6 states terms for distribution of such executables. 251 | 252 | When a "work that uses the Library" uses material from a header file 253 | that is part of the Library, the object code for the work may be a 254 | derivative work of the Library even though the source code is not. 255 | Whether this is true is especially significant if the work can be 256 | linked without the Library, or if the work is itself a library. The 257 | threshold for this to be true is not precisely defined by law. 258 | 259 | If such an object file uses only numerical parameters, data 260 | structure layouts and accessors, and small macros and small inline 261 | functions (ten lines or less in length), then the use of the object 262 | file is unrestricted, regardless of whether it is legally a derivative 263 | work. (Executables containing this object code plus portions of the 264 | Library will still fall under Section 6.) 265 | 266 | Otherwise, if the work is a derivative of the Library, you may 267 | distribute the object code for the work under the terms of Section 6. 268 | Any executables containing that work also fall under Section 6, 269 | whether or not they are linked directly with the Library itself. 270 | 271 | 6. As an exception to the Sections above, you may also combine or 272 | link a "work that uses the Library" with the Library to produce a 273 | work containing portions of the Library, and distribute that work 274 | under terms of your choice, provided that the terms permit 275 | modification of the work for the customer's own use and reverse 276 | engineering for debugging such modifications. 277 | 278 | You must give prominent notice with each copy of the work that the 279 | Library is used in it and that the Library and its use are covered by 280 | this License. You must supply a copy of this License. If the work 281 | during execution displays copyright notices, you must include the 282 | copyright notice for the Library among them, as well as a reference 283 | directing the user to the copy of this License. Also, you must do one 284 | of these things: 285 | 286 | a) Accompany the work with the complete corresponding 287 | machine-readable source code for the Library including whatever 288 | changes were used in the work (which must be distributed under 289 | Sections 1 and 2 above); and, if the work is an executable linked 290 | with the Library, with the complete machine-readable "work that 291 | uses the Library", as object code and/or source code, so that the 292 | user can modify the Library and then relink to produce a modified 293 | executable containing the modified Library. (It is understood 294 | that the user who changes the contents of definitions files in the 295 | Library will not necessarily be able to recompile the application 296 | to use the modified definitions.) 297 | 298 | b) Use a suitable shared library mechanism for linking with the 299 | Library. A suitable mechanism is one that (1) uses at run time a 300 | copy of the library already present on the user's computer system, 301 | rather than copying library functions into the executable, and (2) 302 | will operate properly with a modified version of the library, if 303 | the user installs one, as long as the modified version is 304 | interface-compatible with the version that the work was made with. 305 | 306 | c) Accompany the work with a written offer, valid for at 307 | least three years, to give the same user the materials 308 | specified in Subsection 6a, above, for a charge no more 309 | than the cost of performing this distribution. 310 | 311 | d) If distribution of the work is made by offering access to copy 312 | from a designated place, offer equivalent access to copy the above 313 | specified materials from the same place. 314 | 315 | e) Verify that the user has already received a copy of these 316 | materials or that you have already sent this user a copy. 317 | 318 | For an executable, the required form of the "work that uses the 319 | Library" must include any data and utility programs needed for 320 | reproducing the executable from it. However, as a special exception, 321 | the materials to be distributed need not include anything that is 322 | normally distributed (in either source or binary form) with the major 323 | components (compiler, kernel, and so on) of the operating system on 324 | which the executable runs, unless that component itself accompanies 325 | the executable. 326 | 327 | It may happen that this requirement contradicts the license 328 | restrictions of other proprietary libraries that do not normally 329 | accompany the operating system. Such a contradiction means you cannot 330 | use both them and the Library together in an executable that you 331 | distribute. 332 | 333 | 7. You may place library facilities that are a work based on the 334 | Library side-by-side in a single library together with other library 335 | facilities not covered by this License, and distribute such a combined 336 | library, provided that the separate distribution of the work based on 337 | the Library and of the other library facilities is otherwise 338 | permitted, and provided that you do these two things: 339 | 340 | a) Accompany the combined library with a copy of the same work 341 | based on the Library, uncombined with any other library 342 | facilities. This must be distributed under the terms of the 343 | Sections above. 344 | 345 | b) Give prominent notice with the combined library of the fact 346 | that part of it is a work based on the Library, and explaining 347 | where to find the accompanying uncombined form of the same work. 348 | 349 | 8. You may not copy, modify, sublicense, link with, or distribute 350 | the Library except as expressly provided under this License. Any 351 | attempt otherwise to copy, modify, sublicense, link with, or 352 | distribute the Library is void, and will automatically terminate your 353 | rights under this License. However, parties who have received copies, 354 | or rights, from you under this License will not have their licenses 355 | terminated so long as such parties remain in full compliance. 356 | 357 | 9. You are not required to accept this License, since you have not 358 | signed it. However, nothing else grants you permission to modify or 359 | distribute the Library or its derivative works. These actions are 360 | prohibited by law if you do not accept this License. Therefore, by 361 | modifying or distributing the Library (or any work based on the 362 | Library), you indicate your acceptance of this License to do so, and 363 | all its terms and conditions for copying, distributing or modifying 364 | the Library or works based on it. 365 | 366 | 10. Each time you redistribute the Library (or any work based on the 367 | Library), the recipient automatically receives a license from the 368 | original licensor to copy, distribute, link with or modify the Library 369 | subject to these terms and conditions. You may not impose any further 370 | restrictions on the recipients' exercise of the rights granted herein. 371 | You are not responsible for enforcing compliance by third parties with 372 | this License. 373 | 374 | 11. If, as a consequence of a court judgment or allegation of patent 375 | infringement or for any other reason (not limited to patent issues), 376 | conditions are imposed on you (whether by court order, agreement or 377 | otherwise) that contradict the conditions of this License, they do not 378 | excuse you from the conditions of this License. If you cannot 379 | distribute so as to satisfy simultaneously your obligations under this 380 | License and any other pertinent obligations, then as a consequence you 381 | may not distribute the Library at all. For example, if a patent 382 | license would not permit royalty-free redistribution of the Library by 383 | all those who receive copies directly or indirectly through you, then 384 | the only way you could satisfy both it and this License would be to 385 | refrain entirely from distribution of the Library. 386 | 387 | If any portion of this section is held invalid or unenforceable under any 388 | particular circumstance, the balance of the section is intended to apply, 389 | and the section as a whole is intended to apply in other circumstances. 390 | 391 | It is not the purpose of this section to induce you to infringe any 392 | patents or other property right claims or to contest validity of any 393 | such claims; this section has the sole purpose of protecting the 394 | integrity of the free software distribution system which is 395 | implemented by public license practices. Many people have made 396 | generous contributions to the wide range of software distributed 397 | through that system in reliance on consistent application of that 398 | system; it is up to the author/donor to decide if he or she is willing 399 | to distribute software through any other system and a licensee cannot 400 | impose that choice. 401 | 402 | This section is intended to make thoroughly clear what is believed to 403 | be a consequence of the rest of this License. 404 | 405 | 12. If the distribution and/or use of the Library is restricted in 406 | certain countries either by patents or by copyrighted interfaces, the 407 | original copyright holder who places the Library under this License may add 408 | an explicit geographical distribution limitation excluding those countries, 409 | so that distribution is permitted only in or among countries not thus 410 | excluded. In such case, this License incorporates the limitation as if 411 | written in the body of this License. 412 | 413 | 13. The Free Software Foundation may publish revised and/or new 414 | versions of the Lesser General Public License from time to time. 415 | Such new versions will be similar in spirit to the present version, 416 | but may differ in detail to address new problems or concerns. 417 | 418 | Each version is given a distinguishing version number. If the Library 419 | specifies a version number of this License which applies to it and 420 | "any later version", you have the option of following the terms and 421 | conditions either of that version or of any later version published by 422 | the Free Software Foundation. If the Library does not specify a 423 | license version number, you may choose any version ever published by 424 | the Free Software Foundation. 425 | 426 | 14. If you wish to incorporate parts of the Library into other free 427 | programs whose distribution conditions are incompatible with these, 428 | write to the author to ask for permission. For software which is 429 | copyrighted by the Free Software Foundation, write to the Free 430 | Software Foundation; we sometimes make exceptions for this. Our 431 | decision will be guided by the two goals of preserving the free status 432 | of all derivatives of our free software and of promoting the sharing 433 | and reuse of software generally. 434 | 435 | NO WARRANTY 436 | 437 | 15. BECAUSE THE LIBRARY IS LICENSED FREE OF CHARGE, THERE IS NO 438 | WARRANTY FOR THE LIBRARY, TO THE EXTENT PERMITTED BY APPLICABLE LAW. 439 | EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR 440 | OTHER PARTIES PROVIDE THE LIBRARY "AS IS" WITHOUT WARRANTY OF ANY 441 | KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE 442 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 443 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE 444 | LIBRARY IS WITH YOU. SHOULD THE LIBRARY PROVE DEFECTIVE, YOU ASSUME 445 | THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 446 | 447 | 16. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN 448 | WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY 449 | AND/OR REDISTRIBUTE THE LIBRARY AS PERMITTED ABOVE, BE LIABLE TO YOU 450 | FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR 451 | CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE 452 | LIBRARY (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING 453 | RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A 454 | FAILURE OF THE LIBRARY TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF 455 | SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH 456 | DAMAGES. 457 | 458 | END OF TERMS AND CONDITIONS 459 | 460 | How to Apply These Terms to Your New Libraries 461 | 462 | If you develop a new library, and you want it to be of the greatest 463 | possible use to the public, we recommend making it free software that 464 | everyone can redistribute and change. You can do so by permitting 465 | redistribution under these terms (or, alternatively, under the terms of the 466 | ordinary General Public License). 467 | 468 | To apply these terms, attach the following notices to the library. It is 469 | safest to attach them to the start of each source file to most effectively 470 | convey the exclusion of warranty; and each file should have at least the 471 | "copyright" line and a pointer to where the full notice is found. 472 | 473 | {description} 474 | Copyright (C) {year} {fullname} 475 | 476 | This library is free software; you can redistribute it and/or 477 | modify it under the terms of the GNU Lesser General Public 478 | License as published by the Free Software Foundation; either 479 | version 2.1 of the License, or (at your option) any later version. 480 | 481 | This library is distributed in the hope that it will be useful, 482 | but WITHOUT ANY WARRANTY; without even the implied warranty of 483 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 484 | Lesser General Public License for more details. 485 | 486 | You should have received a copy of the GNU Lesser General Public 487 | License along with this library; if not, write to the Free Software 488 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 489 | USA 490 | 491 | Also add information on how to contact you by electronic and paper mail. 492 | 493 | You should also get your employer (if you work as a programmer) or your 494 | school, if any, to sign a "copyright disclaimer" for the library, if 495 | necessary. Here is a sample; alter the names: 496 | 497 | Yoyodyne, Inc., hereby disclaims all copyright interest in the 498 | library `Frob' (a library for tweaking knobs) written by James Random 499 | Hacker. 500 | 501 | {signature of Ty Coon}, 1 April 1990 502 | Ty Coon, President of Vice 503 | 504 | That's all there is to it! 505 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # IniFile 2 | 3 | IniFile is an Arduino library for reading ini files. The format is 4 | similar to that seen in Microsoft `.ini` files but the implementation 5 | is completely independent. IniFile is designed to use minimal memory 6 | requirements, and the only buffer used is one supplied by the user, 7 | thus the user remains in charge of memory usage. 8 | 9 | The ini file is separated into sections, where the section names are 10 | written inside square brackets. If you don't wish to use sections then 11 | pass a `NULL` pointer for the section name. Under each section are a 12 | set of key-value pairs, separated by an equals sign (`=`). Spaces 13 | around keys and values are ignored, but extra spaces inside the key 14 | are significant. Whitespace inside the value string is preserved; if 15 | leading or trailing whitespace is important you must quote the value 16 | string inside the ini file, and you must strip out the quotes 17 | yourself. If multiple entries for the same key exist inside the 18 | selected section (if using) then only the first value is returned. If 19 | a section is defined more than once only the first is used. The .ini 20 | file can contain comments, which begin with a semicolon (`;`) or hash 21 | (`#`). The user-supplied buffer must be large enough to accomodate the 22 | longest line in the file. 23 | 24 | ## Example file format 25 | 26 | ; Semi-colon comment 27 | [network] 28 | mac = 01:23:45:67:89:AB 29 | 30 | # hash comment, leading spaces below 31 | gateway = 192.168.1.1 32 | 33 | # extraneous spaces before and after key and value 34 | ip = 192.168.1.2 35 | 36 | hosts allow = example.com 37 | 38 | # A similarly-named section 39 | [network2] 40 | mac = ee:ee:ee:ee:ee:ee 41 | subnet mask=255.255.255.0 42 | 43 | ; Extra whitespace around the key and value is permitted 44 | ; (and ignored) 45 | hosts allow = sloppy.example.com 46 | 47 | [misc] 48 | 49 | string = 123456789012345678901234567890123456789001234567890 50 | string2 = a string with spaces in it 51 | 52 | ; This section is a repeat of en existing section and will be ignored. 53 | [network] 54 | mac = 01:23:45:67:89:ab 55 | ip = 192.168.1.2 56 | gateway = 192.168.1.1 57 | 58 | 59 | ## Write support 60 | 61 | Write support is a feature that has been requested on several 62 | occasions but as I am no longer using the IniFile library I will not 63 | add this feature. For anyone planning to add such support the 64 | information below may be useful. 65 | 66 | One goal of the `IniFile` implementation was to limit the amount of 67 | memory required. For use in embedded systems `malloc` and `new` are 68 | deliberately not used. Another goal was that tasks which take a longer 69 | duration were broken down into smaller chunks of work, eg 70 | `IniFile::getValue(const char* section, const char* key, char* buffer, 71 | int len, IniFileState &state)`. This was because I wanted my `WwwServer` 72 | library, which uses `IniFile`, to avoid interfering with time-critical 73 | code. 74 | 75 | I don't think that write support can meet the time-critical goal but 76 | that doesn't prevent its inclusion. I think the way I would choose to 77 | implement write support is to use `IniFile::findKey()` to find where the 78 | desired key is located in the file. I'd then copy everything up to 79 | that point to a temporary file, insert a line for the value and new 80 | key, skip the current line in the existing file (using 81 | `IniFile::readline()`) and then write out the reminder of the existing 82 | file into the temporary file. I'd like to move or rename the temporary 83 | file over the existing file but the Arduino SD library doesn't provide 84 | this functionality; I'd probably just copy the temporary file over the 85 | old one and then delete the temporary one. 86 | 87 | The code has been written under a standard Linux environment, using a 88 | compatibility header file to mimic the SD library. This was far more 89 | convenient than doing everything on the Arduino and enabled me to use 90 | `gdb` to debug the code and core dumps. You'll find `arduino_compat.h` 91 | inside the test directory, along with a `Makefile` which can be used for 92 | regression testing. Any proposed changes must pass the regression 93 | tests. 94 | 95 | ## Contributors 96 | 97 | * [Steve Marple](https://github.com/stevemarple) 98 | * [per1234](https://github.com/per1234) 99 | * [OscarVanL](https://github.com/OscarVanL) 100 | * [MikuX2](https://github.com/toybox01) 101 | * [kaixxx](https://github.com/kaixxx) 102 | -------------------------------------------------------------------------------- /examples/IniBrowseExample/IniBrowseExample.ino: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include 4 | #include 5 | #include 6 | 7 | // The select pin used for the SD card 8 | #define SD_SELECT 10 9 | //#define ETHERNET_SELECT 10 10 | 11 | void printErrorMessage(uint8_t e, bool eol = true) 12 | { 13 | switch (e) { 14 | case IniFile::errorNoError: 15 | Serial.print("no error"); 16 | break; 17 | case IniFile::errorFileNotFound: 18 | Serial.print("file not found"); 19 | break; 20 | case IniFile::errorFileNotOpen: 21 | Serial.print("file not open"); 22 | break; 23 | case IniFile::errorBufferTooSmall: 24 | Serial.print("buffer too small"); 25 | break; 26 | case IniFile::errorSeekError: 27 | Serial.print("seek error"); 28 | break; 29 | case IniFile::errorSectionNotFound: 30 | Serial.print("section not found"); 31 | break; 32 | case IniFile::errorKeyNotFound: 33 | Serial.print("key not found"); 34 | break; 35 | case IniFile::errorEndOfFile: 36 | Serial.print("end of file"); 37 | break; 38 | case IniFile::errorUnknownError: 39 | Serial.print("unknown error"); 40 | break; 41 | default: 42 | Serial.print("unknown error value"); 43 | break; 44 | } 45 | if (eol) 46 | Serial.println(); 47 | } 48 | 49 | void setup() 50 | { 51 | // Configure all of the SPI select pins as outputs and make SPI 52 | // devices inactive, otherwise the earlier init routines may fail 53 | // for devices which have not yet been configured. 54 | pinMode(SD_SELECT, OUTPUT); 55 | digitalWrite(SD_SELECT, HIGH); // disable SD card 56 | 57 | // pinMode(ETHERNET_SELECT, OUTPUT); 58 | // digitalWrite(ETHERNET_SELECT, HIGH); // disable Ethernet 59 | 60 | const size_t bufferLen = 80; 61 | char buffer[bufferLen]; 62 | 63 | const char *filename = "/lunch.ini"; 64 | Serial.begin(9600); 65 | SPI.begin(); 66 | if (!SD.begin(SD_SELECT)) 67 | while (1) 68 | Serial.println("SD.begin() failed"); 69 | 70 | IniFile ini(filename); 71 | if (!ini.open()) { 72 | Serial.print("Ini file "); 73 | Serial.print(filename); 74 | Serial.println(" does not exist"); 75 | // Cannot do anything else 76 | while (1) 77 | ; 78 | } 79 | Serial.println("Ini file exists"); 80 | 81 | // Check the file is valid. This can be used to warn if any lines 82 | // are longer than the buffer. 83 | if (!ini.validate(buffer, bufferLen)) { 84 | Serial.print("ini file "); 85 | Serial.print(ini.getFilename()); 86 | Serial.print(" not valid: "); 87 | printErrorMessage(ini.getError()); 88 | // Cannot do anything else 89 | while (1) 90 | ; 91 | } 92 | 93 | // Browse through all sections and print contents: 94 | IniFileState state; 95 | char sectName[bufferLen]; 96 | Serial.println(); 97 | 98 | while (ini.browseSections(sectName, bufferLen, state)) { 99 | Serial.print("> "); 100 | Serial.print(sectName); 101 | 102 | if (ini.getValue(sectName, "meal", buffer, bufferLen)) { 103 | Serial.print(" eats "); 104 | Serial.print(buffer); 105 | } else 106 | Serial.print(" eats nothing"); 107 | 108 | if (ini.getValue(sectName, "drinks", buffer, bufferLen)) { 109 | Serial.print(", drinks "); 110 | Serial.print(buffer); 111 | } else 112 | Serial.print(", drinks nothing"); 113 | 114 | if (ini.getValue(sectName, "dessert", buffer, bufferLen)) { 115 | Serial.print(" and has "); 116 | Serial.print(buffer); 117 | Serial.println(" for dessert."); 118 | } else 119 | Serial.println(" and has no dessert."); 120 | } 121 | 122 | // finished! 123 | Serial.println(); 124 | printErrorMessage(ini.getError()); 125 | // Cannot do anything else 126 | while (1) 127 | ; 128 | } 129 | 130 | 131 | void loop() 132 | { 133 | 134 | 135 | } 136 | -------------------------------------------------------------------------------- /examples/IniBrowseExample/README.md: -------------------------------------------------------------------------------- 1 | # IniFile browsing example 2 | 3 | Shows how to browse through an ini file with an unknown number of sections 4 | which all contain the same elements. This can be useful to store different 5 | profiles which are selectable on your device. 6 | 7 | 8 | ## Instructions for use 9 | 10 | * Copy the `lunch.ini` file to the root directory of your (micro)SD card. 11 | * Modify the `IniBrowseExample.ino` file so that `SD_SELECT` defines 12 | the correct pin number. 13 | * Compile and upload the sketch. 14 | 15 | 16 | ## Expected output 17 | 18 | It may take a few seconds from the sketch starting before anything is 19 | printed to the serial port, be patient. If the sketch runs correctly 20 | the output should appear as below: 21 | 22 | Ini file exists 23 | 24 | > Karen eats burger, drinks beer and has chocolate for dessert. 25 | > Peter eats falafel, drinks tea without milk and has vegan icecream for dessert. 26 | > Noel eats sushi, drinks water and has no dessert. 27 | > Jessica eats sandwich, drinks nothing and has muffin for dessert. 28 | 29 | end of file 30 | 31 | If the SD card is missing or cannot be read the sketch will print: 32 | 33 | SD.begin() failed 34 | 35 | 36 | -------------------------------------------------------------------------------- /examples/IniBrowseExample/lunch.ini: -------------------------------------------------------------------------------- 1 | ; A list of settings/profiles stored as sections. 2 | ; Each profile has the same elements. 3 | 4 | [Karen] 5 | meal = burger 6 | drinks = beer 7 | dessert = chocolate 8 | 9 | [Peter] 10 | meal = falafel 11 | drinks = tea without milk 12 | dessert = vegan icecream 13 | 14 | [Noel] 15 | meal = sushi 16 | drinks = water 17 | ; note: dessert is missing here 18 | 19 | [Jessica] 20 | meal = sandwich 21 | ; no drinks 22 | dessert = muffin -------------------------------------------------------------------------------- /examples/IniFileExample/IniFileExample.ino: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include 4 | #include 5 | #include 6 | 7 | // The select pin used for the SD card 8 | //#define SD_SELECT 4 9 | #define SD_SELECT 22 10 | #define ETHERNET_SELECT 10 11 | 12 | void printErrorMessage(uint8_t e, bool eol = true) 13 | { 14 | switch (e) { 15 | case IniFile::errorNoError: 16 | Serial.print("no error"); 17 | break; 18 | case IniFile::errorFileNotFound: 19 | Serial.print("file not found"); 20 | break; 21 | case IniFile::errorFileNotOpen: 22 | Serial.print("file not open"); 23 | break; 24 | case IniFile::errorBufferTooSmall: 25 | Serial.print("buffer too small"); 26 | break; 27 | case IniFile::errorSeekError: 28 | Serial.print("seek error"); 29 | break; 30 | case IniFile::errorSectionNotFound: 31 | Serial.print("section not found"); 32 | break; 33 | case IniFile::errorKeyNotFound: 34 | Serial.print("key not found"); 35 | break; 36 | case IniFile::errorEndOfFile: 37 | Serial.print("end of file"); 38 | break; 39 | case IniFile::errorUnknownError: 40 | Serial.print("unknown error"); 41 | break; 42 | default: 43 | Serial.print("unknown error value"); 44 | break; 45 | } 46 | if (eol) 47 | Serial.println(); 48 | } 49 | 50 | void setup() 51 | { 52 | // Configure all of the SPI select pins as outputs and make SPI 53 | // devices inactive, otherwise the earlier init routines may fail 54 | // for devices which have not yet been configured. 55 | pinMode(SD_SELECT, OUTPUT); 56 | digitalWrite(SD_SELECT, HIGH); // disable SD card 57 | 58 | pinMode(ETHERNET_SELECT, OUTPUT); 59 | digitalWrite(ETHERNET_SELECT, HIGH); // disable Ethernet 60 | 61 | const size_t bufferLen = 80; 62 | char buffer[bufferLen]; 63 | 64 | const char *filename = "/net.ini"; 65 | Serial.begin(9600); 66 | SPI.begin(); 67 | if (!SD.begin(SD_SELECT)) 68 | while (1) 69 | Serial.println("SD.begin() failed"); 70 | 71 | IniFile ini(filename); 72 | if (!ini.open()) { 73 | Serial.print("Ini file "); 74 | Serial.print(filename); 75 | Serial.println(" does not exist"); 76 | // Cannot do anything else 77 | while (1) 78 | ; 79 | } 80 | Serial.println("Ini file exists"); 81 | 82 | // Check the file is valid. This can be used to warn if any lines 83 | // are longer than the buffer. 84 | if (!ini.validate(buffer, bufferLen)) { 85 | Serial.print("ini file "); 86 | Serial.print(ini.getFilename()); 87 | Serial.print(" not valid: "); 88 | printErrorMessage(ini.getError()); 89 | // Cannot do anything else 90 | while (1) 91 | ; 92 | } 93 | 94 | // Fetch a value from a key which is present 95 | if (ini.getValue("network", "mac", buffer, bufferLen)) { 96 | Serial.print("section 'network' has an entry 'mac' with value "); 97 | Serial.println(buffer); 98 | } 99 | else { 100 | Serial.print("Could not read 'mac' from section 'network', error was "); 101 | printErrorMessage(ini.getError()); 102 | } 103 | 104 | // Try fetching a value from a missing key (but section is present) 105 | if (ini.getValue("network", "nosuchkey", buffer, bufferLen)) { 106 | Serial.print("section 'network' has an entry 'nosuchkey' with value "); 107 | Serial.println(buffer); 108 | } 109 | else { 110 | Serial.print("Could not read 'nosuchkey' from section 'network', error was "); 111 | printErrorMessage(ini.getError()); 112 | } 113 | 114 | // Try fetching a key from a section which is not present 115 | if (ini.getValue("nosuchsection", "nosuchkey", buffer, bufferLen)) { 116 | Serial.print("section 'nosuchsection' has an entry 'nosuchkey' with value "); 117 | Serial.println(buffer); 118 | } 119 | else { 120 | Serial.print("Could not read 'nosuchkey' from section 'nosuchsection', error was "); 121 | printErrorMessage(ini.getError()); 122 | } 123 | 124 | // Fetch a boolean value 125 | bool allowPut; // variable where result will be stored 126 | bool found = ini.getValue("/upload", "allow put", buffer, bufferLen, allowPut); 127 | if (found) { 128 | Serial.print("The value of 'allow put' in section '/upload' is "); 129 | // Print value, converting boolean to a string 130 | Serial.println(allowPut ? "TRUE" : "FALSE"); 131 | } 132 | else { 133 | Serial.print("Could not get the value of 'allow put' in section '/upload': "); 134 | printErrorMessage(ini.getError()); 135 | } 136 | } 137 | 138 | 139 | void loop() 140 | { 141 | 142 | 143 | } 144 | -------------------------------------------------------------------------------- /examples/IniFileExample/README.md: -------------------------------------------------------------------------------- 1 | # IniFile example 2 | 3 | ## Instructions for use 4 | 5 | * Copy the `net.ini` file to the root directory of your (micro)SD card. 6 | * Modify the `IniFileExample.ino` file so that `SD_SELECT` defines 7 | the correct pin number. 8 | * Compile and upload the sketch. 9 | 10 | 11 | ## Expected output 12 | 13 | It may take a few seconds from the sketch starting before anything is 14 | printed to the serial port, be patient. If the sketch runs correctly 15 | the output should appear as below: 16 | 17 | 18 | Ini file exists 19 | section 'network' has an entry 'mac' with value 01:23:45:67:89:AB 20 | Could not read 'nosuchkey' from section 'network', error was key not found 21 | Could not read 'nosuchkey' from section 'nosuchsection', error was section not found 22 | The value of 'allow put' in section '/upload' is TRUE 23 | 24 | 25 | If the SD card is missing or cannot be read the sketch will print: 26 | 27 | SD.begin() failed 28 | 29 | 30 | -------------------------------------------------------------------------------- /examples/IniFileExample/net.ini: -------------------------------------------------------------------------------- 1 | ; Semi-colon comment 2 | [network] 3 | mac = 01:23:45:67:89:AB 4 | 5 | # hash comment, leading spaces below 6 | gateway = 192.168.1.1 7 | 8 | # extraneous spaces before and after key and value 9 | ip = 192.168.1.2 10 | 11 | hosts allow = example.com 12 | 13 | # A similarly-named section 14 | [network2] 15 | mac = ee:ee:ee:ee:ee:ee 16 | subnet mask=255.255.255.0 17 | 18 | ; Test extra whitespace in keys and value 19 | hosts allow = sloppy.example.com 20 | 21 | [misc] 22 | 23 | string = 123456789012345678901234567890123456789001234567890 24 | string2 = a string with spaces in it 25 | 26 | ; ini file for WwwServerExample 27 | 28 | [mime types] 29 | default = text/plain 30 | htm = text/html 31 | bin = application/octet-stream 32 | pdf = application/pdf 33 | 34 | [/] 35 | ; no access to root of SD filesystem 36 | handler = default 37 | error document 403 = /errordoc/403.htm 38 | 39 | [/www.ini] 40 | handler = default 41 | 42 | [/data] 43 | handler = default 44 | 45 | [/data/private] 46 | ; Block access to this directory 47 | handler = prohibit 48 | error document 403 = /data/private/403.htm 49 | 50 | [/data/noaccess.txt] 51 | ; Block access to this file 52 | handler = prohibit 53 | 54 | [/status] 55 | ; built-in status handler 56 | handler = status 57 | 58 | [/cgi] 59 | ; User-defined handler 60 | handler = cgi 61 | 62 | [/src] 63 | ; A redirect 64 | handler = temporary redirect 65 | location = http://github.com/stevemarple/WwwServer 66 | 67 | [/upload] 68 | allow put = true 69 | -------------------------------------------------------------------------------- /extras/test/.gitignore: -------------------------------------------------------------------------------- 1 | # Ignore intermediate files 2 | *.o 3 | 4 | # Ignore test file 5 | ini_test 6 | 7 | # Ignore source files made from our standard src files 8 | IniFile.cpp 9 | IniFile.h 10 | 11 | # Ignore regression test output file 12 | ini_test.regressiontest.tmp 13 | -------------------------------------------------------------------------------- /extras/test/Ethernet.h: -------------------------------------------------------------------------------- 1 | #ifndef _ETHERNET_H 2 | #define _ETHERNET_H 3 | 4 | 5 | #include "IPAddress.h" 6 | 7 | #endif 8 | -------------------------------------------------------------------------------- /extras/test/File.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #include "File.h" 6 | 7 | #include // DEBUGGING 8 | using namespace::std; 9 | 10 | SDClass SD; 11 | 12 | File SDClass::open(const char *filename, uint8_t mode) const 13 | { 14 | return File(filename, mode); 15 | } 16 | 17 | File::File(void) 18 | { 19 | _f = NULL; 20 | } 21 | 22 | File::File(const char *filename, uint8_t mode) 23 | { 24 | //cout << "Opening " << filename << " with mode " 25 | // << (mode & O_WRONLY ? "r+" : "r") << endl; 26 | _f = fopen(filename, mode & O_WRONLY ? "r+" : "r"); 27 | } 28 | 29 | File::File(const File &a) 30 | { 31 | _f = a._f; 32 | } 33 | 34 | File::~File() 35 | { 36 | //close(); 37 | } 38 | 39 | File& File::operator=(const File &a) 40 | { 41 | _f = a._f; 42 | return *this; 43 | } 44 | 45 | void File::close(void) 46 | { 47 | if (_f != NULL) 48 | fclose(_f); 49 | _f = NULL; 50 | } 51 | 52 | bool File::isOpen(void) const 53 | { 54 | return _f != NULL; 55 | } 56 | 57 | File::operator bool() const 58 | { 59 | return isOpen(); 60 | } 61 | 62 | 63 | long File::size(void) 64 | { 65 | if (!isOpen()) 66 | return -1; 67 | //return size() - position(); 68 | long cur = ftell(_f); 69 | fseek(_f, 0, SEEK_END); // seek to eof 70 | long sz = ftell(_f); 71 | fseek(_f, cur, SEEK_SET); // return to initial position 72 | return sz; 73 | } 74 | 75 | long File::position(void) 76 | { 77 | if (!isOpen()) 78 | return -1; 79 | return ftell(_f); 80 | } 81 | 82 | 83 | int File::available(void) 84 | { 85 | if (!isOpen()) 86 | return 0; 87 | return size() - position(); 88 | } 89 | 90 | int File::read(void) 91 | { 92 | if (!available()) 93 | return -1; 94 | char c; 95 | fread(&c, 1, 1, _f); 96 | return c; 97 | } 98 | 99 | int File::read(void *buf, int n) 100 | { 101 | return fread(buf, 1, n ,_f); 102 | } 103 | 104 | int File::peek(void) 105 | { 106 | if (!available()) 107 | return -1; 108 | 109 | char c; 110 | fread(&c, 1, 1, _f); 111 | ungetc(c, _f); 112 | return c; 113 | } 114 | 115 | bool File::seek(int pos) 116 | { 117 | if (_f == NULL) 118 | return false; 119 | return (fseek(_f, pos, SEEK_SET) == -1 ? false : true); 120 | } 121 | -------------------------------------------------------------------------------- /extras/test/File.h: -------------------------------------------------------------------------------- 1 | #ifndef _FILE_H 2 | #define _FILE_H 3 | 4 | #include "arduino_compat.h" 5 | 6 | class File { 7 | public: 8 | File(void); 9 | File(const char *filename, uint8_t mode); 10 | File(const File &a); 11 | ~File(); 12 | 13 | File& operator=(const File &a); 14 | 15 | void close(void); 16 | 17 | int read(void); 18 | int read(void *buf, int n); 19 | 20 | int peek(void); 21 | bool seek(int pos); 22 | 23 | bool isOpen(void) const; 24 | operator bool() const; 25 | 26 | long size(void); 27 | long position(void); 28 | int available(void); 29 | 30 | private: 31 | FILE *_f; 32 | 33 | }; 34 | 35 | class SDClass { 36 | public: 37 | SDClass(void) { }; 38 | 39 | File open(const char *filename, uint8_t mode) const; 40 | 41 | private: 42 | 43 | }; 44 | 45 | extern SDClass SD; 46 | 47 | 48 | #endif 49 | -------------------------------------------------------------------------------- /extras/test/IPAddress.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | 4 | IPAddress::IPAddress() 5 | { 6 | memset(_address, 0, sizeof(_address)); 7 | } 8 | 9 | IPAddress::IPAddress(uint8_t first_octet, uint8_t second_octet, uint8_t third_octet, uint8_t fourth_octet) 10 | { 11 | _address[0] = first_octet; 12 | _address[1] = second_octet; 13 | _address[2] = third_octet; 14 | _address[3] = fourth_octet; 15 | } 16 | 17 | IPAddress::IPAddress(uint32_t address) 18 | { 19 | memcpy(_address, &address, sizeof(_address)); 20 | } 21 | 22 | IPAddress::IPAddress(const uint8_t *address) 23 | { 24 | memcpy(_address, address, sizeof(_address)); 25 | } 26 | 27 | IPAddress& IPAddress::operator=(const uint8_t *address) 28 | { 29 | memcpy(_address, address, sizeof(_address)); 30 | return *this; 31 | } 32 | 33 | IPAddress& IPAddress::operator=(uint32_t address) 34 | { 35 | memcpy(_address, (const uint8_t *)&address, sizeof(_address)); 36 | return *this; 37 | } 38 | 39 | bool IPAddress::operator==(const uint8_t* addr) 40 | { 41 | return memcmp(addr, _address, sizeof(_address)) == 0; 42 | } 43 | 44 | -------------------------------------------------------------------------------- /extras/test/IPAddress.h: -------------------------------------------------------------------------------- 1 | #ifndef _IPADDRESS_H 2 | #define _IPADDRESS_H 3 | 4 | #include "arduino_compat.h" 5 | 6 | class IPAddress { 7 | public: 8 | // Constructors 9 | IPAddress(); 10 | IPAddress(uint8_t first_octet, uint8_t second_octet, uint8_t third_octet, uint8_t fourth_octet); 11 | IPAddress(uint32_t address); 12 | IPAddress(const uint8_t *address); 13 | 14 | // Overloaded cast operator to allow IPAddress objects to be used where a pointer 15 | // to a four-byte uint8_t array is expected 16 | operator uint32_t() { return *((uint32_t*)_address); }; 17 | 18 | bool operator==(const IPAddress& addr) { return (*((uint32_t*)_address)) == (*((uint32_t*)addr._address)); }; 19 | bool operator==(const uint8_t* addr); 20 | 21 | // Overloaded index operator to allow getting and setting individual octets of the address 22 | uint8_t operator[](int index) const { return _address[index]; }; 23 | uint8_t& operator[](int index) { return _address[index]; }; 24 | 25 | // Overloaded copy operators to allow initialisation of IPAddress objects from other types 26 | IPAddress& operator=(const uint8_t *address); 27 | IPAddress& operator=(uint32_t address); 28 | 29 | private: 30 | uint8_t _address[4]; // IPv4 address 31 | 32 | uint8_t* raw_address() { return _address; }; 33 | 34 | 35 | }; 36 | 37 | #endif 38 | -------------------------------------------------------------------------------- /extras/test/Makefile: -------------------------------------------------------------------------------- 1 | DIFF = diff 2 | RM = rm -f 3 | CXXFLAGS += -ggdb -Wall -I. 4 | 5 | default: regressiontest 6 | 7 | .PHONY : IniFile.h 8 | IniFile.h : ../../src/IniFile.h 9 | echo '' > $@ 10 | echo '#include "arduino_compat.h"' >> $@ 11 | echo '#include ' >> $@ 12 | echo 'using namespace ::std;' >> $@ 13 | cat $< >> $@ 14 | 15 | IniFile.cpp : ../../src/IniFile.cpp 16 | cp $< $@ 17 | 18 | IniFile.o : IniFile.cpp IniFile.h 19 | $(CXX) $(CXXFLAGS) -c $< -o $@ 20 | 21 | File.o : File.cpp File.h 22 | $(CXX) $(CXXFLAGS) -c $< -o $@ 23 | 24 | IPAddress.o : IPAddress.cpp IPAddress.h 25 | $(CXX) $(CXXFLAGS) -c $< -o $@ 26 | 27 | ini_test.o : ini_test.cpp IniFile.h 28 | 29 | ini_test : ini_test.o IniFile.o File.o IPAddress.o 30 | $(CXX) $(CXXFLAGS) $^ $(LDFLAGS) -o $@ 31 | 32 | # Regression testing. Run as "make regressiontest", should display 33 | # "TEST PASSED" if everything ok. 34 | .PHONY : regressiontest 35 | regressiontest : 36 | $(MAKE) realclean 37 | $(MAKE) ini_test 38 | ./ini_test > ini_test.regressiontest.tmp 39 | $(DIFF) -s -u ini_test.regressiontest ini_test.regressiontest.tmp 40 | -$(RM) ini_test.regressiontest.tmp 41 | @echo 42 | @echo TEST PASSED 43 | 44 | .PHONY : clean 45 | clean : 46 | -$(RM) *.o IniFile.h IniFile.cpp ini_test.regressiontest.tmp 47 | 48 | .PHONY : realclean 49 | realclean : clean 50 | -$(RM) -f ini_test 51 | 52 | readtest : readtest.o File.o IniFile.o IPAddress.o 53 | $(CXX) $(CXXFLAGS) $^ $(LDFLAGS) -o $@ 54 | 55 | readtest.o : readtest.cpp IniFile.h 56 | 57 | -------------------------------------------------------------------------------- /extras/test/SD.h: -------------------------------------------------------------------------------- 1 | #ifndef _SD_H 2 | #define _SD_H 3 | #include "File.h" 4 | 5 | #endif 6 | 7 | -------------------------------------------------------------------------------- /extras/test/arduino_compat.h: -------------------------------------------------------------------------------- 1 | #ifndef _ARDUINO_COMPAT_H 2 | #define _ARDUINO_COMPAT_H 3 | 4 | #include 5 | #include 6 | #include 7 | //#include 8 | //#include 9 | #include 10 | 11 | #include 12 | 13 | #include 14 | 15 | typedef bool boolean; 16 | //#define uint8_t unsigned char 17 | 18 | #define FILE_READ O_RDONLY 19 | #define FILE_WRITE (O_RDWR | O_CREAT) 20 | 21 | #endif 22 | -------------------------------------------------------------------------------- /extras/test/browsetest.ini: -------------------------------------------------------------------------------- 1 | ; A list of settings/profiles stored as sections. 2 | ; Each profile has the same elements. 3 | 4 | [Karen] 5 | meal = burger 6 | drinks = beer 7 | dessert = chocolate 8 | 9 | [Peter] 10 | meal = falafel 11 | drinks = tea without milk 12 | dessert = vegan icecream 13 | 14 | [Noel] 15 | meal = sushi 16 | drinks = water 17 | ; note: dessert is missing here 18 | 19 | [Jessica] 20 | meal = sandwich 21 | ; no drinks 22 | dessert = muffin -------------------------------------------------------------------------------- /extras/test/ini_test.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "IniFile.h" 4 | 5 | using namespace std; 6 | 7 | 8 | const char noError[] = "no error"; 9 | const char fileNotFound[] = "file not found"; 10 | const char fileNotOpen[] = "file not open"; 11 | const char bufferTooSmall[] = "buffer too small"; 12 | const char seekError[] = "seek error"; 13 | const char sectionNotFound[] = "section not found"; 14 | const char keyNotFound[] = "key not found"; 15 | const char unknownError[] = "unknown error"; 16 | const char unknownErrorValue[] = "unknown error value"; 17 | 18 | const char* getErrorMessage(int e) 19 | { 20 | const char *cp = unknownError; 21 | 22 | switch (e) { 23 | case IniFile::errorNoError: 24 | cp = noError; 25 | break; 26 | case IniFile::errorFileNotFound: 27 | cp = fileNotFound; 28 | break; 29 | case IniFile::errorFileNotOpen: 30 | cp = fileNotOpen; 31 | break; 32 | case IniFile::errorBufferTooSmall: 33 | cp = bufferTooSmall; 34 | break; 35 | case IniFile::errorSeekError: 36 | cp = seekError; 37 | break; 38 | case IniFile::errorSectionNotFound: 39 | cp = sectionNotFound; 40 | break; 41 | case IniFile::errorKeyNotFound: 42 | cp = keyNotFound; 43 | break; 44 | default: 45 | cp = unknownErrorValue; 46 | break; 47 | }; 48 | return cp; 49 | } 50 | 51 | void testForKey(IniFile &ini, const char *key, const char *section = NULL) 52 | { 53 | cout << " Looking for key \"" << key << '"'; 54 | if (section) 55 | cout << " in section \"" << section << "\""; 56 | cout << endl; 57 | 58 | const int len = 80; 59 | char buffer[len]; 60 | 61 | bool b = ini.getValue(section, key, buffer, len); 62 | if (b == false) { 63 | int e = ini.getError(); 64 | cout << " Error: " << getErrorMessage(e) << " (" << int(e) << ")" 65 | << endl; 66 | if (ini.getError() == IniFile::errorBufferTooSmall) 67 | cout << "Buffer too small for line \"" << buffer << "...\"" << endl; 68 | } 69 | else 70 | cout << " Value of " << key << " is \"" << buffer << '"' << endl; 71 | 72 | } 73 | 74 | void runTest(IniFile &ini) 75 | { 76 | cout << "Using file " << ini.getFilename() << endl; 77 | cout << " File open? " << (ini.isOpen() ? "true" : "false") << endl; 78 | 79 | testForKey(ini, "mac"); 80 | testForKey(ini, "mac", "network"); 81 | testForKey(ini, "mac", "network2"); 82 | testForKey(ini, "mac", "fake"); 83 | testForKey(ini, "ip"); 84 | testForKey(ini, "gateway"); 85 | testForKey(ini, "hosts allow", "network"); 86 | testForKey(ini, "hosts allow", "network"); 87 | testForKey(ini, "hosts allow", "network2"); 88 | testForKey(ini, "hosts allow", "network2"); 89 | testForKey(ini, "string", "misc"); 90 | testForKey(ini, "string2", "misc"); 91 | testForKey(ini, "pi", "misc"); 92 | float iniPi = 0.0; 93 | char buffer[80]; 94 | if (ini.getValue("misc", "pi", buffer, sizeof(buffer), iniPi)) { 95 | cout << " Pi: " << iniPi << endl; 96 | if (iniPi <= 3.1410 || iniPi >= 3.1416) 97 | cout << " Pi out of range" << endl; 98 | } 99 | else { 100 | int e = ini.getError(); 101 | cout << " Could not read \"pi\" from section \"misc\" in " << ini.getFilename() 102 | << endl 103 | << " Error: " << getErrorMessage(e) << " (" << int(e) << ")" 104 | << endl; 105 | } 106 | cout << "----" << endl; 107 | 108 | 109 | } 110 | 111 | // Test the browseSections code contributed by kaixxx. This is based on the 112 | // IniBrowseExample.ino by kaixxx but adapted to work under a standard C++ 113 | // environment. 114 | void browseTest(IniFile &ini) 115 | { 116 | cout << "Using file " << ini.getFilename() << endl; 117 | cout << " File open? " << (ini.isOpen() ? "true" : "false") << endl; 118 | 119 | const int bufferLen = 100; 120 | char buffer[bufferLen]; 121 | IniFileState state; 122 | char sectName[bufferLen]; 123 | 124 | while (ini.browseSections(sectName, bufferLen, state)) { 125 | cout << sectName; 126 | 127 | if (ini.getValue(sectName, "meal", buffer, bufferLen)) { 128 | cout << " eats " << buffer; 129 | } else 130 | cout << " eats nothing"; 131 | 132 | if (ini.getValue(sectName, "drinks", buffer, bufferLen)) { 133 | cout << ", drinks " << buffer; 134 | } else 135 | cout << ", drinks nothing"; 136 | 137 | if (ini.getValue(sectName, "dessert", buffer, bufferLen)) { 138 | cout << ", and has " << buffer << " for dessert." << endl; 139 | } else 140 | cout << ", and has no dessert." << endl; 141 | } 142 | 143 | } 144 | 145 | int main(void) 146 | { 147 | 148 | // Cannot cleanly pass string constants to IniFile constructor 149 | // because is doesn't take const char*, but that is because 150 | // SD.open() isn't const char*. 151 | char missingIniFilename[] = "missing.ini"; 152 | char testIniFilename[] = "test.ini"; 153 | char browseTestIniFilename[] = "browsetest.ini"; 154 | 155 | // Try the construtor which opens a file 156 | IniFile missingIni(missingIniFilename); 157 | IniFile testIni(testIniFilename); 158 | IniFile browseTestIni(browseTestIniFilename); 159 | 160 | cout << "*** Testing IniFile(char*) ***" << endl; 161 | missingIni.open(); 162 | runTest(missingIni); 163 | testIni.open(); 164 | runTest(testIni); 165 | browseTestIni.open(); 166 | browseTest(browseTestIni); 167 | cout << "Done" << endl; 168 | 169 | } 170 | 171 | 172 | -------------------------------------------------------------------------------- /extras/test/ini_test.regressiontest: -------------------------------------------------------------------------------- 1 | *** Testing IniFile(char*) *** 2 | Using file missing.ini 3 | File open? false 4 | Looking for key "mac" 5 | Error: file not open (2) 6 | Looking for key "mac" in section "network" 7 | Error: file not open (2) 8 | Looking for key "mac" in section "network2" 9 | Error: file not open (2) 10 | Looking for key "mac" in section "fake" 11 | Error: file not open (2) 12 | Looking for key "ip" 13 | Error: file not open (2) 14 | Looking for key "gateway" 15 | Error: file not open (2) 16 | Looking for key "hosts allow" in section "network" 17 | Error: file not open (2) 18 | Looking for key "hosts allow" in section "network" 19 | Error: file not open (2) 20 | Looking for key "hosts allow" in section "network2" 21 | Error: file not open (2) 22 | Looking for key "hosts allow" in section "network2" 23 | Error: file not open (2) 24 | Looking for key "string" in section "misc" 25 | Error: file not open (2) 26 | Looking for key "string2" in section "misc" 27 | Error: file not open (2) 28 | Looking for key "pi" in section "misc" 29 | Error: file not open (2) 30 | Could not read "pi" from section "misc" in missing.ini 31 | Error: file not open (2) 32 | ---- 33 | Using file test.ini 34 | File open? true 35 | Looking for key "mac" 36 | Value of mac is "01:23:45:67:89:AB" 37 | Looking for key "mac" in section "network" 38 | Value of mac is "01:23:45:67:89:AB" 39 | Looking for key "mac" in section "network2" 40 | Value of mac is "ee:ee:ee:ee:ee:ee" 41 | Looking for key "mac" in section "fake" 42 | Error: section not found (5) 43 | Looking for key "ip" 44 | Value of ip is "192.168.1.2" 45 | Looking for key "gateway" 46 | Value of gateway is "192.168.1.1" 47 | Looking for key "hosts allow" in section "network" 48 | Value of hosts allow is "example.com" 49 | Looking for key "hosts allow" in section "network" 50 | Value of hosts allow is "example.com" 51 | Looking for key "hosts allow" in section "network2" 52 | Value of hosts allow is "sloppy.example.com" 53 | Looking for key "hosts allow" in section "network2" 54 | Value of hosts allow is "sloppy.example.com" 55 | Looking for key "string" in section "misc" 56 | Value of string is "123456789012345678901234567890123456789001234567890" 57 | Looking for key "string2" in section "misc" 58 | Value of string2 is "a string with spaces in it" 59 | Looking for key "pi" in section "misc" 60 | Value of pi is "3.141592653589793" 61 | Pi: 3.14159 62 | ---- 63 | Using file browsetest.ini 64 | File open? true 65 | Karen eats burger, drinks beer, and has chocolate for dessert. 66 | Peter eats falafel, drinks tea without milk, and has vegan icecream for dessert. 67 | Noel eats sushi, drinks water, and has no dessert. 68 | Jessica eats sandwich, drinks nothing, and has muffin for dessert. 69 | Done 70 | -------------------------------------------------------------------------------- /extras/test/readme.md: -------------------------------------------------------------------------------- 1 | # Test files 2 | 3 | Files to allow the IniFile library to be developed and tested under a 4 | standard g++ environment. 5 | 6 | ## Makefile targets 7 | 8 | make ini_test 9 | Make the test program. 10 | 11 | make run 12 | Make and run the test program. 13 | 14 | make regressiontest 15 | Run regression tests. 16 | 17 | make clean 18 | Remove some non-source files. 19 | 20 | make realclean 21 | Remove all non-versioned files. 22 | 23 | -------------------------------------------------------------------------------- /extras/test/readtest.cpp: -------------------------------------------------------------------------------- 1 | #include "arduino_compat.h" 2 | 3 | #include 4 | using namespace std; 5 | 6 | #include "File.h" 7 | #include "IniFile.h" 8 | 9 | enum { 10 | errorFileNotOpen = -1, 11 | errorBufferTooShort = -2, 12 | errorSeekError = -3, 13 | }; 14 | 15 | 16 | int main(void) 17 | { 18 | const int len = 80; 19 | char buffer[len] = {'\0'}; 20 | uint32_t pos = 0; 21 | 22 | IniFile ini("test.ini"); 23 | IniFileState state; 24 | ini.open(); 25 | 26 | state = IniFileState(); 27 | while (ini.getValue("network", "mac", buffer, len, state) == 0) 28 | ; 29 | cout << "Found: " << buffer << endl; 30 | 31 | return 0; 32 | 33 | // ***************************************************************** 34 | const char filename[] = "test.ini"; 35 | File file = SD.open(filename, FILE_READ); 36 | if (!file) { 37 | cout << "Cannot open " << filename << endl; 38 | return 1; 39 | } 40 | while (1) { 41 | int done = IniFile::readLine(file, buffer, len, pos); 42 | //cout << "---\nREAD: " << buffer << endl; 43 | cout << buffer << endl; 44 | //cout << "pos: " << pos << endl; 45 | if (done) { 46 | //cout << "done: " << done << endl; 47 | break; 48 | } 49 | } 50 | return 0; 51 | } 52 | -------------------------------------------------------------------------------- /extras/test/test.ini: -------------------------------------------------------------------------------- 1 | ; Semi-colon comment 2 | [network] 3 | mac = 01:23:45:67:89:AB 4 | 5 | # hash comment, leading spaces below 6 | gateway = 192.168.1.1 7 | 8 | # extraneous spaces before and after key and value 9 | ip = 192.168.1.2 10 | 11 | hosts allow = example.com 12 | 13 | # A similarly-named section 14 | [network2] 15 | mac = ee:ee:ee:ee:ee:ee 16 | subnet mask=255.255.255.0 17 | 18 | ; Test extra whitespace in keys and value 19 | hosts allow = sloppy.example.com 20 | 21 | [misc] 22 | 23 | string = 123456789012345678901234567890123456789001234567890 24 | string2 = a string with spaces in it 25 | pi = 3.141592653589793 26 | 27 | ; ini file for WwwServerExample 28 | 29 | [mime types] 30 | default = text/plain 31 | htm = text/html 32 | bin = application/octet-stream 33 | pdf = application/pdf 34 | 35 | [/] 36 | ; no access to root of SD filesystem 37 | handler = default 38 | error document 403 = /errordoc/403.htm 39 | 40 | [/www.ini] 41 | handler = default 42 | 43 | [/data] 44 | handler = default 45 | 46 | [/data/private] 47 | ; Block access to this directory 48 | handler = prohibit 49 | error document 403 = /data/private/403.htm 50 | 51 | [/data/noaccess.txt] 52 | ; Block access to this file 53 | handler = prohibit 54 | 55 | [/status] 56 | ; built-in status handler 57 | handler = status 58 | 59 | [/cgi] 60 | ; User-defined handler 61 | handler = cgi 62 | 63 | [/src] 64 | ; A redirect 65 | handler = temporary redirect 66 | location = http://github.com/stevemarple/WwwServer 67 | 68 | [/upload] 69 | allow put = true 70 | -------------------------------------------------------------------------------- /keywords.txt: -------------------------------------------------------------------------------- 1 | ####################################### 2 | # Syntax Coloring Map For IniFile 3 | ####################################### 4 | 5 | ####################################### 6 | # Datatypes (KEYWORD1) 7 | ####################################### 8 | IniFile KEYWORD1 9 | 10 | ####################################### 11 | # Methods and Functions (KEYWORD2) 12 | ####################################### 13 | clearError KEYWORD2 14 | close KEYWORD2 15 | isOpen KEYWORD2 16 | getCaseSensitive KEYWORD2 17 | getError KEYWORD2 18 | getFilename KEYWORD2 19 | getIPAddress KEYWORD2 20 | getMACAddress KEYWORD2 21 | getMode KEYWORD2 22 | getValue KEYWORD2 23 | isCommentChar KEYWORD2 24 | open KEYWORD2 25 | readLine KEYWORD2 26 | removeTrailingWhiteSpace KEYWORD2 27 | setCaseSensitive KEYWORD2 28 | skipWhiteSpace KEYWORD2 29 | validate KEYWORD2 30 | 31 | ####################################### 32 | # Constants (LITERAL1) 33 | ####################################### 34 | -------------------------------------------------------------------------------- /library.properties: -------------------------------------------------------------------------------- 1 | name=IniFile 2 | version=1.3.0 3 | author=Steve Marple 4 | maintainer=Steve Marple 5 | sentence=Library to read and parse .ini files. 6 | paragraph=IniFile is a library to read and parse .ini files as used by Microsoft Windows. IniFile is designed to use minimal memory requirements, and the only buffer used is one supplied by the user, thus the user remains in charge of memory usage. GNU LGPL v2.1. 7 | category=Other 8 | url=https://github.com/stevemarple/IniFile 9 | architectures=* 10 | depends=SD 11 | -------------------------------------------------------------------------------- /src/IniFile.cpp: -------------------------------------------------------------------------------- 1 | #include "IniFile.h" 2 | 3 | #include 4 | 5 | const uint8_t IniFile::maxFilenameLen = INI_FILE_MAX_FILENAME_LEN; 6 | 7 | IniFile::IniFile(const char* filename, mode_t mode, 8 | bool caseSensitive) 9 | { 10 | if (strlen(filename) <= maxFilenameLen) 11 | strcpy(_filename, filename); 12 | else 13 | _filename[0] = '\0'; 14 | _mode = mode; 15 | _caseSensitive = caseSensitive; 16 | } 17 | 18 | IniFile::~IniFile() 19 | { 20 | //if (_file) 21 | // _file.close(); 22 | } 23 | 24 | 25 | bool IniFile::validate(char* buffer, size_t len) const 26 | { 27 | uint32_t pos = 0; 28 | error_t err; 29 | while ((err = readLine(_file, buffer, len, pos)) == errorNoError) 30 | ; 31 | if (err == errorEndOfFile) { 32 | _error = errorNoError; 33 | return true; 34 | } 35 | else { 36 | _error = err; 37 | return false; 38 | } 39 | } 40 | 41 | bool IniFile::getValue(const char* section, const char* key, 42 | char* buffer, size_t len, IniFileState &state) const 43 | { 44 | char *cp = nullptr; 45 | bool done = false; 46 | if (!_file) { 47 | _error = errorFileNotOpen; 48 | return true; 49 | } 50 | 51 | switch (state.getValueState) { 52 | case IniFileState::funcUnset: 53 | state.getValueState = (section == NULL ? IniFileState::funcFindKey 54 | : IniFileState::funcFindSection); 55 | state.readLinePosition = 0; 56 | break; 57 | 58 | case IniFileState::funcFindSection: 59 | if (findSection(section, buffer, len, state)) { 60 | if (_error != errorNoError) 61 | return true; 62 | state.getValueState = IniFileState::funcFindKey; 63 | } 64 | break; 65 | 66 | case IniFileState::funcFindKey: 67 | if (findKey(section, key, buffer, len, &cp, state)) { 68 | if (_error != errorNoError) 69 | return true; 70 | // Found key line in correct section 71 | cp = skipWhiteSpace(cp); 72 | removeTrailingWhiteSpace(cp); 73 | 74 | // Copy from cp to buffer, but the strings overlap so strcpy is out 75 | while (*cp != '\0') 76 | *buffer++ = *cp++; 77 | *buffer = '\0'; 78 | return true; 79 | } 80 | break; 81 | 82 | default: 83 | // How did this happen? 84 | _error = errorUnknownError; 85 | done = true; 86 | break; 87 | } 88 | 89 | return done; 90 | } 91 | 92 | bool IniFile::getValue(const char* section, const char* key, 93 | char* buffer, size_t len) const 94 | { 95 | IniFileState state; 96 | while (!getValue(section, key, buffer, len, state)) 97 | ; 98 | return _error == errorNoError; 99 | } 100 | 101 | 102 | bool IniFile::getValue(const char* section, const char* key, 103 | char* buffer, size_t len, char *value, size_t vlen) const 104 | { 105 | if (!getValue(section, key, buffer, len)) 106 | return false; // error 107 | if (strlen(buffer) >= vlen) 108 | return false; 109 | strcpy(value, buffer); 110 | return true; 111 | } 112 | 113 | 114 | // For true accept: true, yes, 1 115 | // For false accept: false, no, 0 116 | bool IniFile::getValue(const char* section, const char* key, 117 | char* buffer, size_t len, bool& val) const 118 | { 119 | if (!getValue(section, key, buffer, len)) 120 | return false; // error 121 | 122 | if (strcasecmp(buffer, "true") == 0 || 123 | strcasecmp(buffer, "yes") == 0 || 124 | strcasecmp(buffer, "1") == 0) { 125 | val = true; 126 | return true; 127 | } 128 | if (strcasecmp(buffer, "false") == 0 || 129 | strcasecmp(buffer, "no") == 0 || 130 | strcasecmp(buffer, "0") == 0) { 131 | val = false; 132 | return true; 133 | } 134 | return false; // does not match any known strings 135 | } 136 | 137 | bool IniFile::getValue(const char* section, const char* key, 138 | char* buffer, size_t len, int& val) const 139 | { 140 | if (!getValue(section, key, buffer, len)) 141 | return false; // error 142 | 143 | val = atoi(buffer); 144 | return true; 145 | } 146 | 147 | bool IniFile::getValue(const char* section, const char* key, 148 | char* buffer, size_t len, double& val) const 149 | { 150 | if (!getValue(section, key, buffer, len)) 151 | return false; // error 152 | 153 | val = atof(buffer); 154 | return true; 155 | } 156 | 157 | bool IniFile::getValue(const char* section, const char* key, 158 | char* buffer, size_t len, uint8_t& val) const 159 | { 160 | long longval; 161 | bool r = getValue(section, key, buffer, len, longval); 162 | if (r) 163 | val = uint8_t(longval); 164 | return r; 165 | } 166 | 167 | bool IniFile::getValue(const char* section, const char* key, 168 | char* buffer, size_t len, uint16_t& val) const 169 | { 170 | long longval; 171 | bool r = getValue(section, key, buffer, len, longval); 172 | if (r) 173 | val = uint16_t(longval); 174 | return r; 175 | } 176 | 177 | bool IniFile::getValue(const char* section, const char* key, 178 | char* buffer, size_t len, long& val) const 179 | { 180 | if (!getValue(section, key, buffer, len)) 181 | return false; // error 182 | 183 | val = atol(buffer); 184 | return true; 185 | } 186 | 187 | bool IniFile::getValue(const char* section, const char* key, 188 | char* buffer, size_t len, unsigned long& val) const 189 | { 190 | if (!getValue(section, key, buffer, len)) 191 | return false; // error 192 | 193 | char *endptr; 194 | unsigned long tmp = strtoul(buffer, &endptr, 10); 195 | if (endptr == buffer) 196 | return false; // no conversion 197 | if (*endptr == '\0') { 198 | val = tmp; 199 | return true; // valid conversion 200 | } 201 | // buffer has trailing non-numeric characters, and since the buffer 202 | // already had whitespace removed discard the entire results 203 | return false; 204 | } 205 | 206 | 207 | bool IniFile::getValue(const char* section, const char* key, 208 | char* buffer, size_t len, float & val) const 209 | { 210 | if (!getValue(section, key, buffer, len)) 211 | return false; // error 212 | 213 | char *endptr; 214 | float tmp = strtod(buffer, &endptr); 215 | if (endptr == buffer) 216 | return false; // no conversion 217 | if (*endptr == '\0') { 218 | val = tmp; 219 | return true; // valid conversion 220 | } 221 | // buffer has trailing non-numeric characters, and since the buffer 222 | // already had whitespace removed discard the entire results 223 | return false; 224 | } 225 | 226 | 227 | bool IniFile::getIPAddress(const char* section, const char* key, 228 | char* buffer, size_t len, uint8_t* ip) const 229 | { 230 | // Need 16 chars minimum: 4 * 3 digits, 3 dots and a null character 231 | if (len < 16) 232 | return false; 233 | 234 | if (!getValue(section, key, buffer, len)) 235 | return false; // error 236 | 237 | int i = 0; 238 | char* cp = buffer; 239 | ip[0] = ip[1] = ip[2] = ip[3] = 0; 240 | while (*cp != '\0' && i < 4) { 241 | if (*cp == '.') { 242 | ++i; 243 | ++cp; 244 | continue; 245 | } 246 | if (isdigit(*cp)) { 247 | ip[i] *= 10; 248 | ip[i] += (*cp - '0'); 249 | } 250 | else { 251 | ip[0] = ip[1] = ip[2] = ip[3] = 0; 252 | return false; 253 | } 254 | ++cp; 255 | } 256 | return true; 257 | } 258 | 259 | 260 | #if defined(ARDUINO) && ARDUINO >= 100 261 | bool IniFile::getIPAddress(const char* section, const char* key, 262 | char* buffer, size_t len, IPAddress& ip) const 263 | { 264 | // Need 16 chars minimum: 4 * 3 digits, 3 dots and a null character 265 | if (len < 16) 266 | return false; 267 | 268 | if (!getValue(section, key, buffer, len)) 269 | return false; // error 270 | 271 | int i = 0; 272 | char* cp = buffer; 273 | ip = IPAddress(0, 0, 0, 0); 274 | while (*cp != '\0' && i < 4) { 275 | if (*cp == '.') { 276 | ++i; 277 | ++cp; 278 | continue; 279 | } 280 | if (isdigit(*cp)) { 281 | ip[i] *= 10; 282 | ip[i] += (*cp - '0'); 283 | } 284 | else { 285 | ip = IPAddress(0, 0, 0, 0); 286 | return false; 287 | } 288 | ++cp; 289 | } 290 | return true; 291 | } 292 | #endif 293 | 294 | bool IniFile::getMACAddress(const char* section, const char* key, 295 | char* buffer, size_t len, uint8_t mac[6]) const 296 | { 297 | // Need 18 chars: 6 * 2 hex digits, 5 : or - and a null char 298 | if (len < 18) 299 | return false; 300 | 301 | if (!getValue(section, key, buffer, len)) 302 | return false; // error 303 | 304 | int i = 0; 305 | char* cp = buffer; 306 | memset(mac, 0, 6); 307 | 308 | while (*cp != '\0' && i < 6) { 309 | if (*cp == ':' || *cp == '-') { 310 | ++i; 311 | ++cp; 312 | continue; 313 | } 314 | if (isdigit(*cp)) { 315 | mac[i] *= 16; // working in hex! 316 | mac[i] += (*cp - '0'); 317 | } 318 | else { 319 | if (isxdigit(*cp)) { 320 | mac[i] *= 16; // working in hex! 321 | mac[i] += (toupper(*cp) - 55); // convert A to 0xA, F to 0xF 322 | } 323 | else { 324 | memset(mac, 0, 6); 325 | return false; 326 | } 327 | } 328 | ++cp; 329 | } 330 | return true; 331 | } 332 | 333 | // From the file location saved in 'state' look for the next section and read its name. 334 | // The name will be in the buffer. Returns false if no section found. 335 | bool IniFile::browseSections(char* buffer, size_t len, IniFileState &state) const 336 | { 337 | error_t err = errorNoError; 338 | 339 | do { 340 | err = IniFile::readLine(_file, buffer, len, state.readLinePosition); 341 | 342 | if (err != errorNoError) { 343 | // end of file or other error 344 | _error = err; 345 | return false; 346 | } else { 347 | char *cp = skipWhiteSpace(buffer); 348 | 349 | if (*cp == '[') { 350 | // Found a section, read the name 351 | ++cp; 352 | cp = skipWhiteSpace(cp); 353 | char *ep = strchr(cp, ']'); 354 | if (ep != NULL) { 355 | *ep = '\0'; // make ] be end of string 356 | removeTrailingWhiteSpace(cp); 357 | // Copy from cp to buffer, but the strings overlap so strcpy is out 358 | while (*cp != '\0') 359 | *buffer++ = *cp++; 360 | *buffer = '\0'; 361 | _error = errorNoError; 362 | return true; 363 | } 364 | } 365 | } 366 | // continue searching 367 | } while (err == errorNoError); 368 | 369 | // we should never get here... 370 | _error = err; 371 | return false; 372 | } 373 | 374 | //int8_t IniFile::readLine(File &file, char *buffer, size_t len, uint32_t &pos) 375 | IniFile::error_t IniFile::readLine(File &file, char *buffer, size_t len, uint32_t &pos) 376 | { 377 | if (!file) 378 | return errorFileNotOpen; 379 | 380 | if (len < 3) 381 | return errorBufferTooSmall; 382 | 383 | if (!file.seek(pos)) 384 | return errorSeekError; 385 | 386 | #if defined(ARDUINO_ARCH_ESP32) && !defined(PREFER_SDFAT_LIBRARY) 387 | size_t bytesRead = file.readBytes(buffer, len); 388 | #else 389 | size_t bytesRead = file.read(buffer, len); 390 | #endif 391 | if (!bytesRead) { 392 | buffer[0] = '\0'; 393 | //return 1; // done 394 | return errorEndOfFile; 395 | } 396 | 397 | for (size_t i = 0; i < bytesRead && i < len-1; ++i) { 398 | // Test for '\n' with optional '\r' too 399 | // if (endOfLineTest(buffer, len, i, '\n', '\r') 400 | 401 | if (buffer[i] == '\n' || buffer[i] == '\r') { 402 | char match = buffer[i]; 403 | char otherNewline = (match == '\n' ? '\r' : '\n'); 404 | // end of line, discard any trailing character of the other sort 405 | // of newline 406 | buffer[i] = '\0'; 407 | 408 | if (buffer[i+1] == otherNewline) 409 | ++i; 410 | pos += (i + 1); // skip past newline(s) 411 | //return (i+1 == bytesRead && !file.available()); 412 | return errorNoError; 413 | } 414 | } 415 | if (!file.available()) { 416 | // end of file without a newline 417 | buffer[bytesRead] = '\0'; 418 | // return 1; //done 419 | return errorEndOfFile; 420 | } 421 | 422 | buffer[len-1] = '\0'; // terminate the string 423 | return errorBufferTooSmall; 424 | } 425 | 426 | bool IniFile::isCommentChar(char c) 427 | { 428 | return (c == ';' || c == '#'); 429 | } 430 | 431 | char* IniFile::skipWhiteSpace(char* str) 432 | { 433 | char *cp = str; 434 | if (cp) 435 | while (isspace(*cp)) 436 | ++cp; 437 | return cp; 438 | } 439 | 440 | void IniFile::removeTrailingWhiteSpace(char* str) 441 | { 442 | if (str == nullptr) 443 | return; 444 | char *cp = str + strlen(str) - 1; 445 | while (cp >= str && isspace(*cp)) 446 | *cp-- = '\0'; 447 | } 448 | 449 | bool IniFile::findSection(const char* section, char* buffer, size_t len, 450 | IniFileState &state) const 451 | { 452 | if (section == NULL) { 453 | _error = errorSectionNotFound; 454 | return true; 455 | } 456 | 457 | error_t err = IniFile::readLine(_file, buffer, len, state.readLinePosition); 458 | 459 | if (err != errorNoError && err != errorEndOfFile) { 460 | // Signal to caller to stop looking and any error value 461 | _error = err; 462 | return true; 463 | } 464 | 465 | char *cp = skipWhiteSpace(buffer); 466 | //if (isCommentChar(*cp)) 467 | //return (done ? errorSectionNotFound : 0); 468 | if (isCommentChar(*cp)) { 469 | // return (err == errorEndOfFile ? errorSectionNotFound : errorNoError); 470 | if (err == errorEndOfFile) { 471 | _error = errorSectionNotFound; 472 | return true; 473 | } 474 | else 475 | return false; // Continue searching 476 | } 477 | 478 | if (*cp == '[') { 479 | // Start of section 480 | ++cp; 481 | cp = skipWhiteSpace(cp); 482 | char *ep = strchr(cp, ']'); 483 | if (ep != NULL) { 484 | *ep = '\0'; // make ] be end of string 485 | removeTrailingWhiteSpace(cp); 486 | if (_caseSensitive) { 487 | if (strcmp(cp, section) == 0) { 488 | _error = errorNoError; 489 | return true; 490 | } 491 | } 492 | else { 493 | if (strcasecmp(cp, section) == 0) { 494 | _error = errorNoError; 495 | return true; 496 | } 497 | } 498 | } 499 | } 500 | 501 | // Not a valid section line 502 | //return (done ? errorSectionNotFound : 0); 503 | if (err == errorEndOfFile) { 504 | _error = errorSectionNotFound; 505 | return true; 506 | } 507 | 508 | return false; 509 | } 510 | 511 | // From the current file location look for the matching key. If 512 | // section is non-NULL don't look in the next section 513 | bool IniFile::findKey(const char* section, const char* key, 514 | char* buffer, size_t len, char** keyptr, 515 | IniFileState &state) const 516 | { 517 | if (key == NULL || *key == '\0') { 518 | _error = errorKeyNotFound; 519 | return true; 520 | } 521 | 522 | error_t err = IniFile::readLine(_file, buffer, len, state.readLinePosition); 523 | if (err != errorNoError && err != errorEndOfFile) { 524 | _error = err; 525 | return true; 526 | } 527 | 528 | char *cp = skipWhiteSpace(buffer); 529 | // if (isCommentChar(*cp)) 530 | // return (done ? errorKeyNotFound : 0); 531 | if (isCommentChar(*cp)) { 532 | if (err == errorEndOfFile) { 533 | _error = errorKeyNotFound; 534 | return true; 535 | } 536 | else 537 | return false; // Continue searching 538 | } 539 | 540 | if (section && *cp == '[') { 541 | // Start of a new section 542 | _error = errorKeyNotFound; 543 | return true; 544 | } 545 | 546 | // Find '=' 547 | char *ep = strchr(cp, '='); 548 | if (ep != NULL) { 549 | *ep = '\0'; // make = be the end of string 550 | removeTrailingWhiteSpace(cp); 551 | if (_caseSensitive) { 552 | if (strcmp(cp, key) == 0) { 553 | *keyptr = ep + 1; 554 | _error = errorNoError; 555 | return true; 556 | } 557 | } 558 | else { 559 | if (strcasecmp(cp, key) == 0) { 560 | *keyptr = ep + 1; 561 | _error = errorNoError; 562 | return true; 563 | } 564 | } 565 | } 566 | 567 | // Not the valid key line 568 | if (err == errorEndOfFile) { 569 | _error = errorKeyNotFound; 570 | return true; 571 | } 572 | return false; 573 | } 574 | 575 | bool IniFile::getCaseSensitive(void) const 576 | { 577 | return _caseSensitive; 578 | } 579 | 580 | void IniFile::setCaseSensitive(bool cs) 581 | { 582 | _caseSensitive = cs; 583 | } 584 | 585 | IniFileState::IniFileState() 586 | { 587 | readLinePosition = 0; 588 | getValueState = funcUnset; 589 | } 590 | -------------------------------------------------------------------------------- /src/IniFile.h: -------------------------------------------------------------------------------- 1 | #ifndef _INIFILE_H 2 | #define _INIFILE_H 3 | 4 | #include 5 | 6 | #if defined(PREFER_SDFAT_LIBRARY) 7 | #include "SdFat.h" 8 | extern SdFat SD; 9 | #else 10 | #include "SD.h" 11 | #endif 12 | #include "IPAddress.h" 13 | 14 | #define INIFILE_VERSION "1.3.0" 15 | 16 | // Maximum length for filename, excluding NULL char 26 chars allows an 17 | // 8.3 filename instead and 8.3 directory with a leading slash 18 | #define INI_FILE_MAX_FILENAME_LEN 26 19 | 20 | class IniFileState; 21 | 22 | class IniFile { 23 | public: 24 | #if defined(PREFER_SDFAT_LIBRARY) 25 | typedef oflag_t mode_t; 26 | #elif defined(ARDUINO_ARCH_ESP32) 27 | typedef const char* mode_t; 28 | #else 29 | typedef uint8_t mode_t; 30 | #endif 31 | 32 | enum error_t { 33 | errorNoError = 0, 34 | errorFileNotFound, 35 | errorFileNotOpen, 36 | errorBufferTooSmall, 37 | errorSeekError, 38 | errorSectionNotFound, 39 | errorKeyNotFound, 40 | errorEndOfFile, 41 | errorUnknownError, 42 | }; 43 | 44 | static const uint8_t maxFilenameLen; 45 | 46 | // Create an IniFile object. It isn't opened until open() is called on it. 47 | IniFile(const char* filename, mode_t mode = FILE_READ, 48 | bool caseSensitive = false); 49 | ~IniFile(); 50 | 51 | inline bool open(void); // Returns true if open succeeded 52 | inline void close(void); 53 | 54 | inline bool isOpen(void) const; 55 | 56 | inline error_t getError(void) const; 57 | inline void clearError(void) const; 58 | // Get the file mode (FILE_READ/FILE_WRITE) 59 | inline mode_t getMode(void) const; 60 | 61 | // Get the filename asscoiated with the ini file object 62 | inline const char* getFilename(void) const; 63 | 64 | bool validate(char* buffer, size_t len) const; 65 | 66 | // Get value from the file, but split into many short tasks. Return 67 | // value: false means continue, true means stop. Call getError() to 68 | // find out if any error 69 | bool getValue(const char* section, const char* key, 70 | char* buffer, size_t len, IniFileState &state) const; 71 | 72 | // Get value, as one big task. Return = true means value is present 73 | // in buffer 74 | bool getValue(const char* section, const char* key, 75 | char* buffer, size_t len) const; 76 | 77 | // Get the value as a string, storing the result in a new buffer 78 | // (not the working buffer) 79 | bool getValue(const char* section, const char* key, 80 | char* buffer, size_t len, char *value, size_t vlen) const; 81 | 82 | // Get a boolean value 83 | bool getValue(const char* section, const char* key, 84 | char* buffer, size_t len, bool& b) const; 85 | 86 | // Get an integer value 87 | bool getValue(const char* section, const char* key, 88 | char* buffer, size_t len, int& val) const; 89 | 90 | // Get a double value 91 | bool getValue(const char* section, const char* key, 92 | char* buffer, size_t len, double& val) const; 93 | 94 | // Get a uint8_t value 95 | bool getValue(const char* section, const char* key, 96 | char* buffer, size_t len, uint8_t& val) const; 97 | 98 | // Get a uint16_t value 99 | bool getValue(const char* section, const char* key, 100 | char* buffer, size_t len, uint16_t& val) const; 101 | 102 | // Get a long value 103 | bool getValue(const char* section, const char* key, 104 | char* buffer, size_t len, long& val) const; 105 | 106 | bool getValue(const char* section, const char* key, 107 | char* buffer, size_t len, unsigned long& val) const; 108 | 109 | // Get a float value 110 | bool getValue(const char* section, const char* key, 111 | char* buffer, size_t len, float& val) const; 112 | 113 | bool getIPAddress(const char* section, const char* key, 114 | char* buffer, size_t len, uint8_t* ip) const; 115 | 116 | #if defined(ARDUINO) && ARDUINO >= 100 117 | bool getIPAddress(const char* section, const char* key, 118 | char* buffer, size_t len, IPAddress& ip) const; 119 | #endif 120 | 121 | bool getMACAddress(const char* section, const char* key, 122 | char* buffer, size_t len, uint8_t mac[6]) const; 123 | 124 | // From the file location saved in 'state' look for the next section and read its name. 125 | // The name will be in the buffer. Returns false if no section found. 126 | bool browseSections(char* buffer, size_t len, IniFileState &state) const; 127 | 128 | // Utility function to read a line from a file, make available to all 129 | //static int8_t readLine(File &file, char *buffer, size_t len, uint32_t &pos); 130 | static error_t readLine(File &file, char *buffer, size_t len, uint32_t &pos); 131 | static bool isCommentChar(char c); 132 | static char* skipWhiteSpace(char* str); 133 | static void removeTrailingWhiteSpace(char* str); 134 | 135 | bool getCaseSensitive(void) const; 136 | void setCaseSensitive(bool cs); 137 | 138 | protected: 139 | // True means stop looking, false means not yet found 140 | bool findSection(const char* section, char* buffer, size_t len, 141 | IniFileState &state) const; 142 | bool findKey(const char* section, const char* key, char* buffer, 143 | size_t len, char** keyptr, IniFileState &state) const; 144 | 145 | 146 | private: 147 | char _filename[INI_FILE_MAX_FILENAME_LEN]; 148 | mode_t _mode; 149 | mutable error_t _error; 150 | mutable File _file; 151 | bool _caseSensitive; 152 | }; 153 | 154 | bool IniFile::open(void) 155 | { 156 | if (_file) 157 | _file.close(); 158 | _file = SD.open(_filename, _mode); 159 | if (isOpen()) { 160 | _error = errorNoError; 161 | return true; 162 | } 163 | else { 164 | _error = errorFileNotFound; 165 | return false; 166 | } 167 | } 168 | 169 | void IniFile::close(void) 170 | { 171 | if (_file) 172 | _file.close(); 173 | } 174 | 175 | bool IniFile::isOpen(void) const 176 | { 177 | return (_file == true); 178 | } 179 | 180 | IniFile::error_t IniFile::getError(void) const 181 | { 182 | return _error; 183 | } 184 | 185 | void IniFile::clearError(void) const 186 | { 187 | _error = errorNoError; 188 | } 189 | 190 | IniFile::mode_t IniFile::getMode(void) const 191 | { 192 | return _mode; 193 | } 194 | 195 | const char* IniFile::getFilename(void) const 196 | { 197 | return _filename; 198 | } 199 | 200 | 201 | 202 | class IniFileState { 203 | public: 204 | IniFileState(); 205 | 206 | private: 207 | enum {funcUnset = 0, 208 | funcFindSection, 209 | funcFindKey, 210 | }; 211 | 212 | uint32_t readLinePosition; 213 | uint8_t getValueState; 214 | 215 | friend class IniFile; 216 | }; 217 | 218 | 219 | #endif 220 | 221 | --------------------------------------------------------------------------------