Shop OBEX P1 Docs P2 Docs Learn Events
Advice for searching a file for a particular string — Parallax Forums

Advice for searching a file for a particular string

Timothy D. SwieterTimothy D. Swieter Posts: 1,613
edited 2010-07-03 06:39 in Propeller 1
I'm using Kye's SD2.0_FATEngine and his string object, STREngine. I'm setting up the core portion of the SPINNERET Module which will pull address settings (if not specified in firmware and not specified to be DHCP) from a config.txt file on an SD card. I imagine I will have other settings on the SD card as well.

What I am looking to do is to search through a ascii text file to find the settings. I want there to be comments in the file, so the system should ignore those lines. Here is a sample file, but the file could have much more elaborate comments:

' this is a comment line
' another comment line

MAC: 00:00:00:00:00:00
IP: 192.168.1.13
SUBNET: 255.255.255.0
GATWAY: 192.168.1.1




What is the most efficient way to extract the data - the address settings?

I was thinking something along the lines of searching for the NewLine character, noting the position and then, copying a whole line from the file to a buffer and then using the functions in the STREngine to see if it is a comment or a variable. if a comment, discard and move on to the next line. If variable gather the data using ASCII to DEC or hex conversion appropriately.

Good idea?

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Timothy D. Swieter, P.E.
www.brilldea.com - Prop Blade, LED Painter, RGB LEDs, 3.0" 16:9 LCD Composite video display, eProto for SunSPOT, PropNET, PolkaDOT-51
www.tdswieter.com

Comments

  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2010-07-03 04:46
    Timoth,

    Have you looked at my regular expression engine? It would make quick work of skipping the comments and ferreting out the variable/value pairs:

    http://forums.parallax.com/showthread.php?p=855253

    -Phil
  • jazzedjazzed Posts: 11,803
    edited 2010-07-03 05:57
    Phil's regex parser is a good generalized tool especially if you know regular expressions. Considering how simple your data is, a small string tokenizer should be adequate. The attached strtok.spin compiles to less than 200 bytes. The methods work like strtok and strtok_r in the C library click here for more info. The StringTest.spin program shows a tokenizing example.

    Cheers,
    --Steve

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Propeller Pages: Propeller JVM
  • Timothy D. SwieterTimothy D. Swieter Posts: 1,613
    edited 2010-07-03 05:58
    This is a great function Phil! I read, reread, tried and reread your post to understand the building of the regular expression. So far I have created the following:

      pattern := string("\MAC\s*:\s*($1[noparse][[/noparse]\d]+)\s*:($2[noparse][[/noparse]\d]+)\s*:($3[noparse][[/noparse]\d]+)\s*:($4[noparse][[/noparse]\d]+)\s*:($5[noparse][[/noparse]\d]+)\s*:($6[noparse][[/noparse]\d]+)\s*")
      pattern := string("\IP\s*:\s*($1[noparse][[/noparse]\d]+)\s*.($2[noparse][[/noparse]\d]+)\s*.($3[noparse][[/noparse]\d]+)\s*.($4[noparse][[/noparse]\d]+)\s*")
      pattern := string("\SUBNET\s*:\s*($1[noparse][[/noparse]\d]+)\s*.($2[noparse][[/noparse]\d]+)\s*.($3[noparse][[/noparse]\d]+)\s*.($4[noparse][[/noparse]\d]+)\s*")
    
    



    The test program works. Now, getting the file data to an array to work with, what is suggested? I mean, I don't want to have a huge buffer to pull the whole file into, so it should probably be worked on in chunks. Should the routine be something like:


    -Check file size.
    -calculate the number of chunks based on buffer size
    -read a chunk, check it
    -repeat

    With this method though, you risk breaking a line into two. So perhaps the process of checking the beginning of each line for a comment character, then skip to the next line is best way to go. If a line doesn't start with a comment character, it can be processed with the regular expression.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Timothy D. Swieter, P.E.
    www.brilldea.com - Prop Blade, LED Painter, RGB LEDs, 3.0" 16:9 LCD Composite video display, eProto for SunSPOT, PropNET, PolkaDOT-51
    www.tdswieter.com

    Post Edited (Timothy D. Swieter) : 7/3/2010 6:03:05 AM GMT
  • Timothy D. SwieterTimothy D. Swieter Posts: 1,613
    edited 2010-07-03 05:59
    Jazz - I think our post just crossed in cyber-space. I will review what you posted.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Timothy D. Swieter, P.E.
    www.brilldea.com - Prop Blade, LED Painter, RGB LEDs, 3.0" 16:9 LCD Composite video display, eProto for SunSPOT, PropNET, PolkaDOT-51
    www.tdswieter.com
  • kuronekokuroneko Posts: 3,623
    edited 2010-07-03 06:15
    jazzed said...
    Considering how simple your data is, a small string tokenizer should be adequate. The attached strtok.spin compiles to less than 200 bytes.
    The non-reentrant method corrupts memory. You probably meant to use this:
    return strrtok(str,ds,[b]@[/b]strtokp)
    
  • jazzedjazzed Posts: 11,803
    edited 2010-07-03 06:32
    kuroneko said...
    ... You probably meant to use this:

    return strrtok(str,ds,[b]@[/b]strtokp)
    

    Hey thanks. I did originally have it that way. Last minute change [noparse]:)[/noparse]

    Quick analysis of kuroneko's point:
    Most likely the variable strtokp contains 0 at startup.
    Using strtokp instead of @strtokp would damage the clock frequency.

    Cheers,
    --Steve

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Propeller Pages: Propeller JVM

    Post Edited (jazzed) : 7/3/2010 1:35:33 PM GMT
  • MagIO2MagIO2 Posts: 2,243
    edited 2010-07-03 06:39
    You could have a look at the ConfigReader:

    http://obex.parallax.com/objects/598/

    Instead of having a list of regular expressions or a list of strings which name the configurations, you only need a list of hash-values. It already parses numbers and returns a string address for text-parameters.

    It's based on FSRW, but it should be easy to change it to Kye's SD driver.
Sign In or Register to comment.