It is currently Wed May 22, 2013 9:37 pm

All times are UTC + 1 hour




Post new topic Reply to topic  [ 13 posts ] 
Author Message
 Post subject: ReadText() and WriteText() procedures
PostPosted: Thu Mar 08, 2012 7:14 pm 
Offline
Enthusiast
Enthusiast
User avatar

Joined: Sat Dec 03, 2011 5:54 pm
Posts: 252
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:
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

_________________
Purebasic 5.11 | Fedora 18 (32-bit)


Top
 Profile  
 
 Post subject: Re: ReadText() and WriteText() procedures
PostPosted: Fri Mar 09, 2012 2:53 am 
Offline
Addict
Addict
User avatar

Joined: Thu Jun 07, 2007 3:25 pm
Posts: 1573
Location: Berlin, Germany
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".

_________________
Math problems?
Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x].


Top
 Profile  
 
 Post subject: Re: ReadText() and WriteText() procedures
PostPosted: Fri Mar 09, 2012 3:02 am 
Offline
Enthusiast
Enthusiast
User avatar

Joined: Thu Jan 10, 2008 1:30 pm
Posts: 711
Location: Germany, Glienicke
Quote:
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!

_________________
Image


Top
 Profile  
 
 Post subject: Re: ReadText() and WriteText() procedures
PostPosted: Fri Mar 09, 2012 3:08 am 
Offline
Addict
Addict
User avatar

Joined: Thu Jun 07, 2007 3:25 pm
Posts: 1573
Location: Berlin, Germany
STARGÅTE wrote:
Quote:
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.

_________________
Math problems?
Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x].


Top
 Profile  
 
 Post subject: Re: ReadText() and WriteText() procedures
PostPosted: Fri Mar 09, 2012 9:48 pm 
Offline
Enthusiast
Enthusiast
User avatar

Joined: Sat Dec 03, 2011 5:54 pm
Posts: 252
Of course ReadDate() and WriteData() works, but my idea was to have a single-line command for this.

Quote:
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?

_________________
Purebasic 5.11 | Fedora 18 (32-bit)


Top
 Profile  
 
 Post subject: Re: ReadText() and WriteText() procedures
PostPosted: Fri Mar 09, 2012 11:39 pm 
Offline
Addict
Addict
User avatar

Joined: Thu Jun 07, 2007 3:25 pm
Posts: 1573
Location: Berlin, Germany
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.

_________________
Math problems?
Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x].


Last edited by Little John on Sat Mar 10, 2012 12:02 am, edited 1 time in total.

Top
 Profile  
 
 Post subject: Re: ReadText() and WriteText() procedures
PostPosted: Sat Mar 10, 2012 12:02 am 
Offline
Enthusiast
Enthusiast
User avatar

Joined: Sat Dec 03, 2011 5:54 pm
Posts: 252
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.

_________________
Purebasic 5.11 | Fedora 18 (32-bit)


Top
 Profile  
 
 Post subject: Re: ReadText() and WriteText() procedures
PostPosted: Sat Mar 10, 2012 12:04 am 
Offline
Addict
Addict
User avatar

Joined: Thu Jun 07, 2007 3:25 pm
Posts: 1573
Location: Berlin, Germany
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.

_________________
Math problems?
Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x].


Last edited by Little John on Sat Mar 10, 2012 12:11 am, edited 1 time in total.

Top
 Profile  
 
 Post subject: Re: ReadText() and WriteText() procedures
PostPosted: Sat Mar 10, 2012 12:10 am 
Offline
Addict
Addict
User avatar

Joined: Wed Dec 23, 2009 10:14 pm
Posts: 1385
Location: Boston, MA
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?

_________________
To understand recursion, you must first understand recursion. ~ unknown
I never make stupid mistakes. Only very, very clever ones. ~ John Peel


Top
 Profile  
 
 Post subject: Re: ReadText() and WriteText() procedures
PostPosted: Sat Mar 10, 2012 12:12 am 
Online
Enthusiast
Enthusiast
User avatar

Joined: Fri Jan 21, 2011 8:25 am
Posts: 548
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
ImageImageImage
"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


Top
 Profile  
 
 Post subject: Re: ReadText() and WriteText() procedures
PostPosted: Sat Mar 10, 2012 10:28 pm 
Offline
Enthusiast
Enthusiast
User avatar

Joined: Sat Dec 03, 2011 5:54 pm
Posts: 252
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.

Quote:
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.

_________________
Purebasic 5.11 | Fedora 18 (32-bit)


Top
 Profile  
 
 Post subject: Re: ReadText() and WriteText() procedures
PostPosted: Sun Mar 11, 2012 12:57 am 
Online
Enthusiast
Enthusiast
User avatar

Joined: Fri Jan 21, 2011 8:25 am
Posts: 548
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:
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:
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
ImageImageImage
"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


Top
 Profile  
 
 Post subject: Re: ReadText() and WriteText() procedures
PostPosted: Sun Mar 11, 2012 9:28 am 
Offline
Enthusiast
Enthusiast
User avatar

Joined: Sat Dec 03, 2011 5:54 pm
Posts: 252
That's very interesting. I didn't know that. Cool :-)

_________________
Purebasic 5.11 | Fedora 18 (32-bit)


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 13 posts ] 

All times are UTC + 1 hour


Who is online

Users browsing this forum: woki and 0 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  

 


Powered by phpBB © 2008 phpBB Group
subSilver+ theme by Canver Software, sponsor Sanal Modifiye