Skip to content

Commit

Permalink
Adapt IniFile to SPIFFS
Browse files Browse the repository at this point in the history
Changed source code to work with SPIFFS
  • Loading branch information
yurilopes committed Feb 17, 2019
1 parent 47f6ab5 commit 7b5b465
Show file tree
Hide file tree
Showing 8 changed files with 1,002 additions and 0 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# SPIFFSIniFile

SPIFFSIniFile is an Arduino library for reading ini files via SPIFFS.
This is just a modification of this library: [IniFile](https://github.com/stevemarple/IniFile)

In case of any questions regarding how this library works, please refer to the previous link.

All credits go to Steven Marple. All I did was adapt the source to work on SPIFFS aiming at providing this feature for ESP8266 and ESP32 platforms.
143 changes: 143 additions & 0 deletions examples/IniFileExample/IniFileExample.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
#include <SD.h>

#include <SPI.h>
#include <IPAddress.h>
#include <IniFile.h>

// The select pin used for the SD card
//#define SD_SELECT 4
#define SD_SELECT 22
#define ETHERNET_SELECT 10

void printErrorMessage(uint8_t e, bool eol = true)
{
switch (e) {
case IniFile::errorNoError:
Serial.print("no error");
break;
case IniFile::errorFileNotFound:
Serial.print("file not found");
break;
case IniFile::errorFileNotOpen:
Serial.print("file not open");
break;
case IniFile::errorBufferTooSmall:
Serial.print("buffer too small");
break;
case IniFile::errorSeekError:
Serial.print("seek error");
break;
case IniFile::errorSectionNotFound:
Serial.print("section not found");
break;
case IniFile::errorKeyNotFound:
Serial.print("key not found");
break;
case IniFile::errorEndOfFile:
Serial.print("end of file");
break;
case IniFile::errorUnknownError:
Serial.print("unknown error");
break;
default:
Serial.print("unknown error value");
break;
}
if (eol)
Serial.println();
}

void setup()
{
// Configure all of the SPI select pins as outputs and make SPI
// devices inactive, otherwise the earlier init routines may fail
// for devices which have not yet been configured.
pinMode(SD_SELECT, OUTPUT);
digitalWrite(SD_SELECT, HIGH); // disable SD card

pinMode(ETHERNET_SELECT, OUTPUT);
digitalWrite(ETHERNET_SELECT, HIGH); // disable Ethernet

const size_t bufferLen = 80;
char buffer[bufferLen];

const char *filename = "/net.ini";
Serial.begin(9600);
SPI.begin();
if (!SD.begin(SD_SELECT))
while (1)
Serial.println("SD.begin() failed");

IniFile ini(filename);
if (!ini.open()) {
Serial.print("Ini file ");
Serial.print(filename);
Serial.println(" does not exist");
// Cannot do anything else
while (1)
;
}
Serial.println("Ini file exists");

// Check the file is valid. This can be used to warn if any lines
// are longer than the buffer.
if (!ini.validate(buffer, bufferLen)) {
Serial.print("ini file ");
Serial.print(ini.getFilename());
Serial.print(" not valid: ");
printErrorMessage(ini.getError());
// Cannot do anything else
while (1)
;
}

// Fetch a value from a key which is present
if (ini.getValue("network", "mac", buffer, bufferLen)) {
Serial.print("section 'network' has an entry 'mac' with value ");
Serial.println(buffer);
}
else {
Serial.print("Could not read 'mac' from section 'network', error was ");
printErrorMessage(ini.getError());
}

// Try fetching a value from a missing key (but section is present)
if (ini.getValue("network", "nosuchkey", buffer, bufferLen)) {
Serial.print("section 'network' has an entry 'nosuchkey' with value ");
Serial.println(buffer);
}
else {
Serial.print("Could not read 'nosuchkey' from section 'network', error was ");
printErrorMessage(ini.getError());
}

// Try fetching a key from a section which is not present
if (ini.getValue("nosuchsection", "nosuchkey", buffer, bufferLen)) {
Serial.print("section 'nosuchsection' has an entry 'nosuchkey' with value ");
Serial.println(buffer);
}
else {
Serial.print("Could not read 'nosuchkey' from section 'nosuchsection', error was ");
printErrorMessage(ini.getError());
}

// Fetch a boolean value
bool allowPut; // variable where result will be stored
bool found = ini.getValue("/upload", "allow put", buffer, bufferLen, allowPut);
if (found) {
Serial.print("The value of 'allow put' in section '/upload' is ");
// Print value, converting boolean to a string
Serial.println(allowPut ? "TRUE" : "FALSE");
}
else {
Serial.print("Could not get the value of 'allow put' in section '/upload': ");
printErrorMessage(ini.getError());
}
}


void loop()
{


}
29 changes: 29 additions & 0 deletions examples/IniFileExample/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# IniFile example

## Instructions for use

* Copy the `net.ini` file to the root directory of your (micro)SD card.
* Modify the `IniFileExample.ino` file so that `SD_SELECT` defines
the correct pin number.
* Compile and upload the sketch.


## Expected output

It may take a few seconds from the sketch starting before anything is
printed to the serial port, be patient. If the sketch runs correctly
the output should appear as below:


Ini file exists
section 'network' has an entry 'mac' with value 01:23:45:67:89:AB
Could not read 'nosuchkey' from section 'network', error was key not found
Could not read 'nosuchkey' from section 'nosuchsection', error was section not found
The value of 'allow put' in section '/upload' is TRUE


If the SD card is missing or cannot be read the sketch will print:

SD.begin() failed


68 changes: 68 additions & 0 deletions examples/IniFileExample/net.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
; Semi-colon comment
[network]
mac = 01:23:45:67:89:AB

# hash comment, leading spaces below
gateway = 192.168.1.1

# extraneous spaces before and after key and value
ip = 192.168.1.2

hosts allow = example.com

# A similarly-named section
[network2]
mac = ee:ee:ee:ee:ee:ee
subnet mask=255.255.255.0

; Test extra whitespace in keys and value
hosts allow = sloppy.example.com

[misc]

string = 123456789012345678901234567890123456789001234567890
string2 = a string with spaces in it

; ini file for WwwServerExample

[mime types]
default = text/plain
htm = text/html
bin = application/octet-stream
pdf = application/pdf

[/]
; no access to root of SD filesystem
handler = default
error document 403 = /errordoc/403.htm

[/www.ini]
handler = default

[/data]
handler = default

[/data/private]
; Block access to this directory
handler = prohibit
error document 403 = /data/private/403.htm

[/data/noaccess.txt]
; Block access to this file
handler = prohibit

[/status]
; built-in status handler
handler = status

[/cgi]
; User-defined handler
handler = cgi

[/src]
; A redirect
handler = temporary redirect
location = http://github.com/stevemarple/WwwServer

[/upload]
allow put = true
33 changes: 33 additions & 0 deletions keywords.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#######################################
# Syntax Coloring Map For SPIFFSIniFile
#######################################

#######################################
# Datatypes (KEYWORD1)
#######################################
SPIFFSIniFile KEYWORD1

#######################################
# Methods and Functions (KEYWORD2)
#######################################
clearError KEYWORD2
close KEYWORD2
isOpen KEYWORD2
getCaseSensitive KEYWORD2
getError KEYWORD2
getFilename KEYWORD2
getIPAddress KEYWORD2
getMACAddress KEYWORD2
getMode KEYWORD2
getValue KEYWORD2
isCommentChar KEYWORD2
open KEYWORD2
readLine KEYWORD2
removeTrailingWhiteSpace KEYWORD2
setCaseSensitive KEYWORD2
skipWhiteSpace KEYWORD2
validate KEYWORD2

#######################################
# Constants (LITERAL1)
#######################################
9 changes: 9 additions & 0 deletions library.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
name=SPIFFSIniFile
version=1.0.0
author=Yuri Lopes, Steve Marple <[email protected]>
maintainer=Yuri Lopes
sentence=Library to read and parse .ini files on ESP8266 and ESP32 platforms
paragraph=SPIFFSIniFile is a library to read and parse .ini files as used by Microsoft Windows. SPIFFSIniFile 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 v3
category=Other
url=https://github.com/yurilopes/SPIFFSIniFile
architectures=esp8266,esp32
Loading

0 comments on commit 7b5b465

Please sign in to comment.