June 16, 2024, 01:16:29 AM

News:

IonicWind Snippit Manager 2.xx Released!  Install it on a memory stick and take it with you!  With or without IWBasic!


String processing in general

Started by Doc, October 21, 2011, 09:05:06 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Doc

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-

LarryMc

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
LarryMc
Larry McCaughn :)
Author of IWB+, Custom Button Designer library, Custom Chart Designer library, Snippet Manager, IWGrid control library, LM_Image control library

Doc

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-

LarryMc

I'll keep looking in my old stuff and see if I stumble across anything from the 'old' masters.

LarryMc
LarryMc
Larry McCaughn :)
Author of IWB+, Custom Button Designer library, Custom Chart Designer library, Snippet Manager, IWGrid control library, LM_Image control library

LarryMc

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
LarryMc
Larry McCaughn :)
Author of IWB+, Custom Button Designer library, Custom Chart Designer library, Snippet Manager, IWGrid control library, LM_Image control library

aurelCB

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

LarryMc

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
LarryMc
Larry McCaughn :)
Author of IWB+, Custom Button Designer library, Custom Chart Designer library, Snippet Manager, IWGrid control library, LM_Image control library

aurelCB

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