IonicWind Software

IWBasic => General Questions => Topic started by: Doc on October 21, 2011, 09:05:06 AM

Title: String processing in general
Post by: Doc on October 21, 2011, 09:05:06 AM
Hey folks,
It's been a really long time since I used any of the IW languages for much of anything, which means that I'm horribly rusty at "thinking in BASIC" of any brand or variety. Anyway, I'd really like to try to do a new project using IWB, so hopefully you folks won't mind a few "newbie" type questions from an old hand.

I know that I can use @EDGETLINECOUNT to get the number of lines in a given text control, but what if I wanted to load a text file directly into an ISTRING variable for instance, choosing to work directly with whatever ends up in there...

With the language I've been using primarily for the last few years, I would do something like this (literally):
put the number of lines in MyVariable into LineCount
repeat with i = 1 to LineCount
do something to the text in each line
end repeat


-or-

put the number of words in MyVariable into WordCount
repeat with i = 1 to WordCount
do something to the text in each word
end repeat


With IWB, how would I go about finding the number of words and/or lines that the ISTRING variable contains and can I operate on each line (or word) individually as I would do using the code I posted above?

Parsing has never been a strong point in my BASIC experience, so I'll surely appreciate your patience and/or advice...

-Doc-
Title: Re: String processing in general
Post by: LarryMc on October 21, 2011, 09:32:17 AM
The fastest way to load lines and then be able to process a line at a time with the least amount of parsing that I can think of is to use a hidden richedit control.

create the richedit off screen so it can't be seen.
then load it with something like this:
If FileName<>"" Then
File MyFile
If OpenFile(MyFile,sFile,"R")=0 Then
ControlCMD(wndMain,1,@RTLoad,MyFile,False)
CloseFile MyFile
EndIf
EndIf


Then use this to get the number of lines:
count = CONTROLCMD ( window | dialog, ID, @RTGETLINECOUNT)
set up your loop and process:
for x=0 to count-1
CONTROLCMD ( window | dialog, ID, @RTGETLINE, x, line$ {,cchLine})
   'process line$
next x


LarryMc
Title: Re: String processing in general
Post by: Doc on October 21, 2011, 09:51:04 AM
Okay, thank you Larry...
I had already thought of using an edit control rather than a richedit, but was hoping to find a way to do my work directly in memory, which is much faster than working inside an edit/richedit control.

My goal is to re-write an existing program that I've built in a different language. The silly thing involves a massive amount of text processing and subsequently produces what in some cases can be thousands of individual text files as a result. My existing program is functional now, but I'm looking to gain some badly needed extra processing speed.

Guess I'll play around with some ideas to see what I come up with.

Thank you sir!
-Doc-
Title: Re: String processing in general
Post by: LarryMc on October 21, 2011, 09:56:58 AM
I'll keep looking in my old stuff and see if I stumble across anything from the 'old' masters.

LarryMc
Title: Re: String processing in general
Post by: LarryMc on October 21, 2011, 11:54:48 AM
this will load a file into memory then parse it into lines which can be processed

BFILE f
int length,x
memory buffer
string k$
istring out$[10000]
buffer= NULL
if OpenFile(f,path,"R") = 0
length = len(f)
AllocMem buffer,length,1
__read f,buffer,length
out$=""
for x =1 to length
__readMEM buffer,x,k$,1
IF K$<>chr$(13) & K$<>chr$(10)
OUT$=out$+k$
k$=""
else
'process line
out$=""
endif
next x
CloseFile f
endif
freemem buffer


LarryMc
Title: Re: String processing in general
Post by: aurelCB on October 21, 2011, 02:20:35 PM
Good example Larry.. ;)
Or maby Doc can use this :
IF( Openfile(hFile,filename,"R") = 0)
hsize=0
hsize = Len(hFile)
Allocmem htext,hsize,1
hzero=0
Writemem htext,hsize,hzero
Read hFile,htext
'load memory to pointer
pMem=htext
'load stringPointer to Istring
buffer=#<string>pMem
Freemem htext
Closefile hFile
ENDIF
Title: Re: String processing in general
Post by: LarryMc on October 21, 2011, 03:35:55 PM
aurel

I can't get yours to work.

Can you post a full working example of your method so I can see how it works?

thanks

LarryMc
Title: Re: String processing in general
Post by: aurelCB on October 23, 2011, 03:23:19 AM
Larry
Oups ...maby is not very good aproach to do this  :-\
maby next code explain better,im not sure...
def buffer[32766]:ISTRING
DEF pMem:pointer
def hFile:BFILE:'binary file
def htext:MEMORY : ' memory variable
def hsize:int
def hzero:char
...
...
IF( Openfile(hFile,filename,"R") = 0)
hsize=0
hsize = Len(hFile)
Allocmem htext,hsize,1
hzero=0
Writemem htext,hsize,hzero
Read hFile,htext
'load memory to pointer
pMem=htext
'load stringPointer to Istring
buffer=#<string>pMem
Freemem htext
Closefile hFile
ENDIF

'and now for example:
Setcontroltext w1,1,buffer