Hi PaulS-
Understood abotu pointers & arrays (makes sense)
here is my current code (basically yoru codw with some prints in there so I can follwo code flow..etc
//parse data in text file on SD card
if (!params.open(root, "defaults.txt")) {
// handle open failure here
putstring_nl("CANT OPEN DEFAULTS.TXT");
}
// read and print file to serial port
char record[80]; // store the record here
byte index = 0;
uint8_t c;
while(params.read(&c, 1) == 1){
// read until we found the carriage return...
if(c == '\n'){
// carriage return encountered, parse the data we collected so far
if(index > 0){
char *nameToken = strtok(record, "=");
putstring("NAME TOKEN: ");
Serial.println(nameToken);
if(nameToken){
char *valueToken = strtok(NULL, "\0");
putstring("VALUE TOKEN: ");
Serial.println(valueToken);
if(valueToken){
}
}
}
// reset to read next record
index = 0;
record[index] = '\0';
}
// or check if we encounter a line feed too...
else if(c != '\r'){ // ignore line feed
// save the character
record[index++] = c;
// append a NULL
record[index] = '\0';
}
}
params.close();
I think I get the basics..
1.) char record[80];
create a character array, able to hold [80] characters..
2.) byte index = 0;
var to be used as our incremntal counter for following code
3.) while(params.read(&c, 1) == 1)
We read the (charactes by character) until it returns '0' (no more data) as assign that character to 'c'
*We then evaluate/check 'c' to se if it IS a carriage return (\n) or is NOT a line feed (\r)..
*if NOT a carriage return OR a line feed... we save/push that character (byte?) into the record character array next index.
*increment index by 1 (++)
*append a null character (terminating character?)
Am I correct to assume that in the text file each line ends like this: \r\n ?? (as in the \r in first? and \n is last so we knwo when to start fresh again?)
4.) if a \n (carriage return) IS encountered.. we know this is the end of the line...
We can 'stop' (so to speak)... and then parse/evaluate the data we have been capturing up until we got to the carriage return...
Heres where Im getting a bit unclear again, but I think Im just over thinking it..
so the sub-code (parsing code), is as follows:
if(index > 0)
{
char *nameToken = strtok(record, "=");
if(nameToken)
{
char *valueToken = strtok(NULL, "\0");
if(valueToken)
{
}
}
}
and we only enter this 'sub-routine' is we have encountered a \n (carriage return).. meaning the end of a line..and is ok to start parsing the data collected..
since each time we dumped a character into the record[] character array for holding, we incremented the index variable/counter... this will be over/above (greater than) 0
(if greater than 0, it means there is data/characters in the array to evaluate, if 0..nothing is there, exit)
5.) char *nameToken = strtok(record, "=");
create a variable (nameToken) to store the returned 'string' from the strtok() function.. that takes the current data in record[] and parses it by the delimiter (in this case the = sign)
I get that part.. but the next line is a bit confusing..
6.) if(nameToken){
huh? So if nameToken true?? (as in if there IS something? not empty/null/blank?) Is that what this check is for?
*** what does the * (asterik do for these var names?) ***
7.) char *valueToken = strtok(NULL, "\0");
then we do the same again for the valueToken... although Im not clear on what it is you are using for the parameters in the strotok() function?? NULL as the pointer/reference to parse? and what is the delimiter "\0" for? end of line?
I mean I get the print out on the serial monitor correctly and all:
Ready!
NAME TOKEN: safety
VALUE TOKEN: 0
NAME TOKEN: maxAmmo
VALUE TOKEN: 10
NAME TOKEN: mColor
VALUE TOKEN: b
NAME TOKEN: aColor
VALUE TOKEN: r
I guess Im still fuzzy on how/where I shoudl be doing these conditional checks on the valueTokens?
(right after the strtok() functiosn on both name & value tokens?
( the if(nameToken){} / if(valueToken){}portions?? )
so if in the text file I have: maxAmmo=10 on one line..
I need to check to see if nameToken = maxAmmo.. and then assign its valueToken to a var in my sketch..
another side is, if in the text file I have: aColor=r
I need to not only read/know that Im on that nameToken, but then also evaluate the valeToken (although I guess that could be done elsewhere in the script, as long as I create a variable for values?)
I'll stop here.. I know I can ramble on..
thanks for the help.. Your time is not being wasted, I am trying & learning
update:
I remembered your post about using the compare function.. (Im so used to being able to do things like:
if(varA = "some long string"){
//do whatever
}
so I did that and I think Im moving in the right direction... (it is 'ok' practice' to ten just have a few (4) IF statements to check the nameToken? and then a sub-routine on how to handle the valueToken for each nameToken
Im currently having trouble assigning the valueToken to a var already in the sketch:
if(index > 0){
char *nameToken = strtok(record, "=");
putstring("NAME TOKEN: ");
Serial.println(nameToken);
if(nameToken){
//ammo check
if(strcmp(nameToken, "maxAmmo") == 0){
char *valueToken = strtok(NULL, "\0");
putstring("VALUE TOKEN: ");
Serial.println(valueToken);
if(valueToken){
maxAmmo = atoi(valueToken);
}else{
//if param is missing, default to 99
maxAmmo = 99;
}
}