News:

  • August 22, 2026, 03:51:19 PM

Login with username, password and session length

Author Topic: File Handling functions  (Read 6423 times)

Jim77

  • Newbie
  • *
  • Posts: 3
File Handling functions
« on: May 16, 2018, 11:52:08 AM »
First of all, I will start with what I am trying to do. When I saw that the Do-More BRX's have microSD built in, I decided it would be a perfect medium to implement a new network tolerant data-logging system.

I connect my BRX to the LAN and log to a SQL server via a simple webpage and pull values out of the QueryString on the server side. The server then gives me a simple confirmation that all values are valid and recorded properly.

If I don't get this positive confirmation, the exact GET string plus a delimiter is simply appended to the bottom of the backup text file on the microSD card.

When a value is written successfully (server back online) if the backup file isn't empty, step through it, write the values, and delete the line from the file if it took.

Where I am running into problems is with the available file handling functions. The only function that really does something similar to what I want is the truncate file function. This means I have to start from the bottom of the file, read the last line, then truncate on success.
Unfortunately, there is not a good way of starting from the bottom and working my way up for a variable line length. Is there a way to seek backward from the current position to find the start of the last line? Since my string size varies depending on the exact data, I am basically trying to start at the end, seek backward 1.5x the last line length, and seeking forward to find the end of line from there to find the start position of the last line.

Ideally, I would be able to have an easy box to do the equivalent of the Unix "tail" command, where everything above the current file position is deleted. This should just be a small variation on the already available truncate command.

Am I missing and easer way of doing this? Should I just pad my string with garbage (which the server won't care about) to guarantee the same length of every line?

jcottrill

  • Full Member
  • ***
  • Posts: 43
Re: File Handling functions
« Reply #1 on: May 16, 2018, 04:58:55 PM »
I don't have much experience with the BRX file system but I have a dirty solution for you.  What if you use two files and a pointer/variable to define which one is active?  Log your failures to the active file.  When you are ready to process your re-send log do the following.

  • Truncate the secondary file
  • Mark the secondary file as the active
  • Process your re-sends and log any failures in the new "active" file

Instead of trying to delete from the existing file, build a new one.  The downside would be the additional writes incurred on the flash storage if you experience outages regularly.

Do you have/need a way to prevent an infinite resend loop?  What happens if the connection is good but the data is malformed for some reason?

Finally, in regards to string padding, this was often done back in the day with fixed width DB records.  Just be careful when padding with garbage.  Spaces can be easier to trim off at the server end.

franji1

  • Bit Weenie
  • Host Moderator
  • Hero Member
  • *****
  • Posts: 3843
    • Host Engineering
Re: File Handling functions
« Reply #2 on: May 17, 2018, 04:57:31 PM »
For padding purposes, STRPRINT has a Fill() command:

Fill a String with a Character
Fill(hexadecimal constant, fill length)

Example:
"My dog has fleas" Fill(0x20,5) "and so does mine."
This inserts 5 spaces (ASCII 0x20 is a space)

The fill length can be an element (like D42), it doesn't have to be a constant.

Jim77

  • Newbie
  • *
  • Posts: 3
Re: File Handling functions
« Reply #3 on: May 29, 2018, 11:58:21 AM »
As an update to this topic. I got my ladder logic running with the method described in the original post. No need to fill the string to constant length for now.

It was just my sloppy re-use of File_Handle's during recursive program execution that was tripping me up.

But, it would still be nice to get a couple more file manipulation functions. From what I can tell, the only way I can change the contents of a file right now are to cut from current File Position to the end off the file, delete the whole thing, or add to the file.

One function to delete X bytes starting from Y file position would do wonders to round out the file feature set.