-
Notifications
You must be signed in to change notification settings - Fork 5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
SD file block adjustments #529
Comments
@VonSzarvas, looks like maybe you've been using the blocks successfully with a different approach. Please review and comment if this conflicts with other usage cases. Thanks! |
One issue with using a fixed width of bytes when writing is that the additional bytes will be written as null. First reaction... I'd prefer to see these functions in the blocks:
|
Thanks @VonSzarvas! I especially like the append idea. Adding to the list, another possible feature would be have the open blocks return their fopen call's status return value. |
@AndyLindsay, reference your 5 items:
4 & 5) The SD file block actually has a Close option in the drop-down list. I updated the code to check fp is not null before closing, as you recommended.
|
@VonSzarvas, Your first item will depend on what file modes are available in the C compiler fopen() implementation. I don't know if we are getting the standard fopen() or a skinny version of it. Items 2 can easily be addressed by including a strlen(string_buffer) in the length setting. That will write the string to the card. You can add terminators with additional SD write blocks. Item 3 is possible but needs to be vetted first. I would not want to develop the block unless there is a demand for it. |
Many of the suggestions listed above have been implemented in v1.5.7, released on 1/16/2021. Please evaluate this issue against the new release and then let's determine if we can close this issue. |
When close is chosen from the SD file block, a new SD file [close] block appears in addition to the SD file [write] block that was placed on the canvas. I think it would be both easier to fix and better for the user if SD file close was its own block. |
@AndyLindsay,in Solo 1.5.7, If I put an SD file block on the canvas and then select 'close' from the drop-down, the SD file write block mutates to an SD file close block and the value (10) block becomes orphaned. You description indicates that there would be two SD file blocks - write and close - after the 'close' type is selected. Can you verify that there are two SD file blocks after that operation? We could create an SD file close block. There are also a number of other possible SD blocks to support more direct access to the file system on the card to support different data types. |
@zfi, I just tried it again, and this time, the block successfully mutated, and only orphaned the 10 block. There is no second instance of the SD file write block. |
@zfi, regarding "There are also a number of other possible SD blocks to support more direct access to the file system on the card to support different data types." Yes, I've been trying to just focus on correcting what was there, but all the while thinking that it would be great to see some additional blocks. For now though, I would just like to bump suggestion 1 that @VonSzarvas made earlier:
SD file open "filename.txt" as [append] could emit fopen(filename.txt, "a"); |
I do think having SD file close as its own block would be good. |
We can track the new close file block in #537. |
Altering an existing block is likely going to break existing projects that contain the block. The SD File Open block has two modes, read and read/write. @VonSzarvas says there is a write-only mode but I don't see that in the code.
I should point out that the read-write mode is not properly implemented. Fopen("file", "w") should create a new file or truncate an existing file and only allow writes to the file. I have verified that this is in fact what it is doing. The correct file mode for read/write is "w+" if the intent is to open a file and position the pointer to the beginning of the file for read or write operations. To address the request for an "append" mode, we could add one or more MODE combinations to the existing block by extending the MODE list without breaking any project that includes this block. All I really need is a definitive list of the modes we want to implement. |
These are the official options:
With the mode specifiers above the file is open as a text file. In order to open a file as a binary file, a "b" character has to be included in the mode string. This additional "b" character can either be appended at the end of the string (thus making the following compound modes: "rb", "wb", "ab", "r+b", "w+b", "a+b") or be inserted between the letter and the "+" sign for the mixed modes ("rb+", "wb+", "ab+"). |
Thank you @zfi, yes, I've been pouring over that one for a bit now. @zfi and @VonSzarvas, I am not done testing all the options in C yet, but could still use a reality check on the direction of the blocks. I think that just having the current SD open file block could lead to confusion if "w" is used because it will overwrite an existing file. This in mind, there should probably be separate SD create file block that creates an empty text (w) or binary (wb) file and then closes it. Then, the SD open file could could open in one of the following modes:
I think the binary option could be really useful, but it might also lead to the temptation of mixing binary and text data in a text file. Any suggestions on how to make the binary option available and minimize the coding error risks would be great. For example, is there block smarts that can be incorporated to warn users when they try to mix modes? Would it be better to make separate blocks, some dedicated to text and others dedicated to binary? P.S. Also, are the separate SD file create and SD file open blocks a viable solution for keeping users from inadvertently deleting their data? |
File creation and truncation are inherent in the SD File Open block. Perhaps we need a new block that breaks out these options visually and then let the code generator emit the correct mode based on the user input. Something like: (These are just prototypes, warts included at no extra charge) |
I like that! (Warts and all.) Would this still be in addition to a SD create file, or do you think it might be better to take the 'open' and and make it a dropdown with and options? ...or some other approach? |
I really don't see why we would need a block the simply creates an empty file. SD open file does that now. Map out an alternative if you don't think this will do. I should also note that the block I proposed would be a new block and the existing SD open file block would be tagged as deprecated. |
I really think that file creation and file opening/editing need to be separated for BlocklyProp programmers. Sure, C programmers have to learn that fopen doesn't really mean "open" in the file browser sense, and that it could also accidentally nuke all the info they had in an existing file. This is not a "hard lesson" we want to set up for beginning BP programmers. That is why I am suggesting separate create, open, and close blocks. Under that approach, we would have: SD file create [filename.txt] type <text, binary> SD file open "filename.txt/bin" Type <Text, Binary> Mode <Read/Write, Append, Read Only> SD file close |
I can see that "Destructive Create" can convey the risk, but from the tutorial classroom standpoint, it's kind of hard to say: "The first step is to create a file. We do that using a block set to 'Destructive Create'." In the classroom setting, that could lead questions that really only have answers related to underlying C nuances. Now, this is not ideal either, but I do still think it's safer in a tutorial to say: "If you haven't already put text.txt on the SD from your computer, you'll need to use SD file create "test.txt" Type . (Be careful, if you already had test.txt, this will overwrite it, and you will lose any data that was in it.) After creating the file, you can open it and add to it in read/write, add to the end with append, or get data from it with Read Only." |
Regarding this program:
The user defined code currently emits fclose(fp); and the program emits this code:
After the changes are made, the blocks could look like this:
...and the code generated would look like this:
The text was updated successfully, but these errors were encountered: