[futurebasic] Re: Clipboard Text nab and seek?

Message: < previous - next > : Reply : Subscribe : Cleanse
Home   : September 2003 : Group Archive : Group : All Groups

From: Ken Shmidheiser <kshmidheiser@...>
Date: Mon, 22 Sep 2003 09:07:46 -0400
Robert Covington asked:

>Does anybody have, or can anyone hack up a quickie (For Carbon
>Clipboard) that does the following:
>
>Get a handle to TEXT or Styled Text clipboard scrap, and then seeks in
>that handle, "RegName"
>
>RegName: <Tester  McGillicuddy >
>
>and returns the string contained within "<" and ">" and then seeks
>"RegNumber:<"
>
>RegNumber: <ATC2-2523-3255-5499>
>
>and then returns the code as well?



Robert,

This is a very rough hack with no error checking 
(for instance, in your example above, you have 
two spaces between "Tester" and "McGillicuddy" 
and an extra space between the end of 
"McGillicuddy" and the final tag, all of which I 
assume are typos but are not addressed in this 
code). The code does take into account that the 
user may accidentally copy a few extra lines, and 
parses out any bad lines.

Hope it is of some help.

Ken

p.s. Watch for e-mail line breaks and lost underscores on the Associate server


_splitArraySize = 10

begin globals

// Small array to hold clipboard text
dynamic splitArray( _splitArraySize ) as str255

// Container to hold clipboard handle
dim as container gC

end globals

// This function splits container into an array of strings
// using designated delimiter character as split point
local fn Split( @CPtr as ptr, splitChar as str15 )
dim as pointer startOfDataPtr, endOfDataPtr, itemPtr
dim as pointer p1, p2
dim as long    size, splits

splits = 0

if CPtr.nil& = _nil then exit fn

startOfDataPtr = [CPtr.nil&]
endOfDataPtr   = startOfDataPtr + Fn GetHandleSize(CPtr.nil&)
Long If endOfDataPtr > startOfDataPtr
splits = 0
splitArray( _splitArraySize ) = ""

for p1 = startOfDataPtr to endOfDataPtr
itemPtr = @splitArray(splits)
p2      = itemPtr
while p1.0`` != splitChar[1] and p1 < endOfDataPtr
p2++
p2.0`` = p1.0``
p1++
wend
size =  p2 - itemPtr
long if size < sizeof(splitArray(0))
itemPtr.0`` = size
xelse
itemPtr.0`` = sizeof(splitArray(0)) - 1
end if
splits++
next

compress dynamic splitArray
end if

end fn = splits

// Get handle to clipboard text
local mode
local fn GetClipboardText
dim as handle     scrapTextH
dim as long     @ length
dim as ScrapRef @ theScrapRef

scrapTextH = _nil
long if fn GetCurrentScrap( theScrapRef ) = _noErr
long if fn GetScrapFlavorSize( theScrapRef,¬
_"TEXT", length ) = _noErr
long if length
scrapTextH = fn NewHandleClear( length )
long if scrapTextH
HLock( scrapTextH )
Long if fn GetScrapFlavorData( theScrapRef,¬
_"TEXT", length, #[scrapTextH] ) != _noErr
end if
HUnlock( scrapTextH )
Xelse
beep : beep
end if
end if
end if
end if
end fn = scrapTextH

// GetTagText returns the data between
// the specified tags, without the tags.
clear local mode
local fn GetTagText$( sourceStr as str255,¬
                  startTagStr as str255,¬
                    endTagStr as str255 )
dim as str255 resultStr
dim as int    start, finish

start  = InStr( 1, sourceStr, startTagStr ) + startTagStr[0]
finish = InStr( start, sourceStr, endTagStr ) - endTagStr[0]
finish = (finish - start) + 1

resultStr = Mid$( sourceStr, start, finish )

end fn = resultStr

local fn GetClipAndSplit
dim as handle clipH
dim as long   elements

gC = ""
kill dynamic splitArray

clipH = fn GetClipboardText
gC    = &clipH

elements = fn Split( gC, chr$(13) )

compress dynamic splitArray

gC = ""

end fn = elements


local fn ParseUserInfoIntoRecord
dim as long      i, elements
dim as str255    nameStr, numStr

elements = fn GetClipAndSplit

for i = 0 to elements

long if left$( splitArray(i), 7 ) = "RegName"
nameStr = fn GetTagText$( splitArray(i), "<", ">" )
print nameStr
end if

long if left$( splitArray(i), 9 ) = "RegNumber"
numStr = fn GetTagText$( splitArray(i), "<", ">" )
print numStr
end if

next i

kill dynamic splitArray

end fn


/*
To perform simple test, copy and paste the following lines
onto the clipboard and then run the program:


RegName: <Tester  McGillicuddy >
RegNumber: <ATC2-2523-3255-5499>


*/

fn ParseUserInfoIntoRecord

include "Subs Quick Event Loop.Incl"