ReadText() and WriteText() procedures

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
uwekel
Enthusiast
Enthusiast
Posts: 740
Joined: Sat Dec 03, 2011 5:54 pm
Location: Oldenburg (Germany)

ReadText() and WriteText() procedures

Post by uwekel »

Hi,

today i needed an easy way to save and reload strings which contained #LF$, #CR$ and even #CRLF$. With the built-in WriteString() method one can save the string to file, but reloading with ReadString() will fail because the string will be split into multiple pieces (lines). Therefore i wrote the following procedures to write strings and reload them exactly as they have been written.

Code: Select all

Procedure WriteText(File, Text.s)
  ;write a text including linefeeds to a file
  Protected l.l = Len(Text)
  WriteLong(File, l)
  WriteData(File, @Text, l)
EndProcedure

Procedure.s ReadText(File)
  ;reads a text including linefeads to a file
  Protected l.l = ReadLong(File)
  Protected s.s = Space(l)
  ReadData(File, @s, l)
  ProcedureReturn s
EndProcedure
Am i wrong and is there a better solution to do this, or is it possible and senseful to add this procedures to the Purebasic capabilities?

Best regards
Uwe
PB 5.70 LTS (x64) - Debian Testing, Gnome 3.30.2
Little John
Addict
Addict
Posts: 4527
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: ReadText() and WriteText() procedures

Post by Little John »

uwekel wrote:With the built-in WriteString() method one can save the string to file, but reloading with ReadString() will fail because the string will be split into multiple pieces (lines).
:?:

WriteStringN() writes one line, and ReadString() reads one line. So what is exactly the problem?

Besides that, your procedure WriteText() does NOT create a normal text file.

This thread should be moved to "Coding questions".
User avatar
STARGÅTE
Addict
Addict
Posts: 2084
Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:

Re: ReadText() and WriteText() procedures

Post by STARGÅTE »

and ReadString() reads one line
yes, but he will read some lines, also the characters LF and CR ...

@uwekel:
Your code isn't run with unicode!
PB 6.01 ― Win 10, 21H2 ― Ryzen 9 3900X, 32 GB ― NVIDIA GeForce RTX 3080 ― Vivaldi 6.0 ― www.unionbytes.de
Lizard - Script language for symbolic calculations and moreTypeface - Sprite-based font include/module
Little John
Addict
Addict
Posts: 4527
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: ReadText() and WriteText() procedures

Post by Little John »

STARGÅTE wrote:
and ReadString() reads one line
yes, but he will read some lines, also the characters LF and CR ...
Little John wrote:your procedure WriteText() does NOT create a normal text file.
For reading and writing any kind of binary data, there are ReadData(), WriteData() and other functions, so I still don't see any need for a feature request here.
uwekel
Enthusiast
Enthusiast
Posts: 740
Joined: Sat Dec 03, 2011 5:54 pm
Location: Oldenburg (Germany)

Re: ReadText() and WriteText() procedures

Post by uwekel »

Of course ReadDate() and WriteData() works, but my idea was to have a single-line command for this.
Your code isn't run with unicode!
Oh, sorry. Ii have not checked that. I guess for unicode it must be taken into account that the string memory length is twice the string length, right?
PB 5.70 LTS (x64) - Debian Testing, Gnome 3.30.2
Little John
Addict
Addict
Posts: 4527
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: ReadText() and WriteText() procedures

Post by Little John »

uwekel wrote:but my idea was to have a single-line command for this.
Yes, obviously. And probably 100 other people also would like to have built-in commands for their personally preferred binary file format ...
Just put your procedures into an include file, and voilà: you've got your single-line commands for your special file format.
It doesn't make sense to build commands for each and any file format into PB.
Last edited by Little John on Sat Mar 10, 2012 12:02 am, edited 1 time in total.
uwekel
Enthusiast
Enthusiast
Posts: 740
Joined: Sat Dec 03, 2011 5:54 pm
Location: Oldenburg (Germany)

Re: ReadText() and WriteText() procedures

Post by uwekel »

What's wrong with it? It's more an extension to the ReadString() and WriteString() methods than a personally preferred file format.

Personally for me, i have already stored the procedures in a common source file. The disadvantage of a common lib is, that it is - once included - completely (with all other stuff) embedded in the target executable. PB commands are only linked if used.
PB 5.70 LTS (x64) - Debian Testing, Gnome 3.30.2
Little John
Addict
Addict
Posts: 4527
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: ReadText() and WriteText() procedures

Post by Little John »

uwekel wrote:It's more an extension to the ReadString() and WriteString() methods than a personally preferred file format.
No. Those files are not normal text files, they can't be edited with standard text editors. I already wrote that.
Last edited by Little John on Sat Mar 10, 2012 12:11 am, edited 1 time in total.
User avatar
skywalk
Addict
Addict
Posts: 3994
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: ReadText() and WriteText() procedures

Post by skywalk »

You created a custom file format in 3 lines of code :wink:
Doing it in 1 line doesn't seem worth the effort of a dedicated function?
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
User avatar
Shield
Addict
Addict
Posts: 1021
Joined: Fri Jan 21, 2011 8:25 am
Location: 'stralia!
Contact:

Re: ReadText() and WriteText() procedures

Post by Shield »

uwekel wrote:The disadvantage of a common lib is, that it is - once included - completely (with all other stuff) embedded in the target executable. PB commands are only linked if used.
Apparently procedures from source files are only linked if they reference each other, which isn't the case with those two functions for example.
Image
Blog: Why Does It Suck? (http://whydoesitsuck.com/)
"You can disagree with me as much as you want, but during this talk, by definition, anybody who disagrees is stupid and ugly."
- Linus Torvalds
uwekel
Enthusiast
Enthusiast
Posts: 740
Joined: Sat Dec 03, 2011 5:54 pm
Location: Oldenburg (Germany)

Re: ReadText() and WriteText() procedures

Post by uwekel »

At office i work with VB.Net and so i was a little inspirated from the ReadAllLines() method from the System.IO namespace. From my opinion it is a helpful and easy to use command i often use.
Apparently procedures from source files are only linked if they reference each other, which isn't the case with those two functions for example.
I just tested this. Just the use of IncludeFile "mycommonlib.pb" without using any code enlarges the target executable a lot. It seems that everything is linked into the executable.
PB 5.70 LTS (x64) - Debian Testing, Gnome 3.30.2
User avatar
Shield
Addict
Addict
Posts: 1021
Joined: Fri Jan 21, 2011 8:25 am
Location: 'stralia!
Contact:

Re: ReadText() and WriteText() procedures

Post by Shield »

Nope, I just tested it myself. Seems to be correct what I said. :wink:
I think in your case the different procedures are referencing each other, as I said.


Have a look at this code:

Code: Select all

Procedure.i AAA()
	; A LOT OF CODE HERE.
EndProcedure

Procedure.i BBB()
	AAA()
EndProcedure

Procedure.i CCC()
	ProcedureReturn -1
EndProcedure
Assuming you include this "common" file into your project...now:
Because the procedure BBB() calls AAA(), both will be compiled into the executable,
even if you only use function CCC(). That's what's happening.

If procedure BBB() looked like this...

Code: Select all

Procedure.i BBB()
	; EVEN MORE CODE.
EndProcedure
...and you only use CCC() in your program, both AAA() and BBB() will not be included in your executable.
Image
Blog: Why Does It Suck? (http://whydoesitsuck.com/)
"You can disagree with me as much as you want, but during this talk, by definition, anybody who disagrees is stupid and ugly."
- Linus Torvalds
uwekel
Enthusiast
Enthusiast
Posts: 740
Joined: Sat Dec 03, 2011 5:54 pm
Location: Oldenburg (Germany)

Re: ReadText() and WriteText() procedures

Post by uwekel »

That's very interesting. I didn't know that. Cool :-)
PB 5.70 LTS (x64) - Debian Testing, Gnome 3.30.2
Post Reply