|
|
|
DD383300.HTM DD-Software.StringsFiles2 Add this page to your favorites Save this document |
|
|
|
|
udfFileAdd (InFileA, InFileB, OutFile) |
;------------------------------------------------------------------------------------------------------------------------------------------
If ItemLocate("udffileadd",IntControl(77,103,0,0,0),@TAB) Then Goto skip_udffileadd
#DefineFunction udfFileAdd (InFileA, InFileB, OutFile)
; We must have an OutFilename.
If (OutFile=="") Then Return @FALSE
fsizeA=FileSize(InFileA)
fsizeB=FileSize(InFileB)
Select 1
Case((fsizeA==0)&&(fsizeB==0))
Return @FALSE
Break
Case((fsizeA==0)&&(fsizeB>0))
Return FileCopy(InFileB,OutFile,@FALSE)
Break
Case((fsizeA>0)&&(fsizeB==0))
Return FileCopy(InFileA,OutFile,@FALSE)
Break
Case((fsizeA>0)&&(fsizeB>0))
If FileCopy(InFileA,OutFile,@FALSE)
Return FileAppend(InFileB,OutFile)
EndIf
Break
EndSelect
Return @FALSE
; Detlev Dalitz.20010713
#EndFunction
:skip_udffileadd
;------------------------------------------------------------------------------------------------------------------------------------------
;--- test ---
; Create test environment.
InFileA=FileCreateTemp("TMP")
InFileB=FileCreateTemp("TMP")
; We use this script to create test data.
ThisFile=IntControl(1004,0,0,0,0)
FileCopy(ThisFile,InFileA,@FALSE)
FileCopy(ThisFile,InFileB,@FALSE)
OutFile=FileCreateTemp("TMP")
For i=1 To 5
Select i
Case 1
result = udfFileAdd(InFileA,InFileB,OutFile)
Break
Case 2
result = udfFileAdd("",InFileB,OutFile)
Break
Case 3
result = udfFileAdd(InFileA,"",OutFile)
Break
Case 4
result = udfFileAdd("","",OutFile)
Break
Case 5
result = udfFileAdd("","","")
Break
EndSelect
Display(2,"Demo udfFileAdd (InFileA, InFileB, OutFile)",StrCat("Test",i,@TAB," Result=",result))
If FileExist(OutFile)
; Take a look and wait for closing notepad.
RunZoomWait("notepad",OutFile)
FileDelete(OutFile)
Else
Display(2,"Demo udfFileAdd (InFileA, InFileB, OutFile)","No OutFile to view ...")
EndIf
Next
; Cleaning.
FileDelete(InFileA)
FileDelete(InFileB)
Exit
;------------------------------------------------------------------------------------------------------------------------------------------
|
|
If you have questions about WinBatch, you are encouraged to use online WebBoard BBS at http://webboard.windowware.com
|
|
|
|
|
udfBinaryFill (handle, offset, count, filler) |
;------------------------------------------------------------------------------------------------------------------------------------------
If ItemLocate("udfbinaryfill",IntControl(77,103,0,0,0),@TAB) Then Goto skip_udfbinaryfill
#DefineFunction udfBinaryFill (handle, offset, count, filler)
Return (BinaryPokeStr(handle,offset,StrFill(filler,count)))
; Detlev Dalitz.20010713
#EndFunction
:skip_udfbinaryfill
;------------------------------------------------------------------------------------------------------------------------------------------
; --- test ---
bb = BinaryAlloc(1000)
num = udfBinaryFill(bb,0,1000,"QQQ ")
num = udfBinaryFill(bb,11,20,"#")
num = udfBinaryFill(bb,90,10,"#")
str = BinaryPeekStr(bb,0,BinaryEodGet(bb))
BinaryFree(bb)
Message("Demo udfBinaryFill (handle, offset, count, filler)",str)
Exit
;------------------------------------------------------------------------------------------------------------------------------------------
|
|
If you have questions about WinBatch, you are encouraged to use online WebBoard BBS at http://webboard.windowware.com
|
|
|
|
|
udfFileGetMaxLineLen (sFilename) |
;------------------------------------------------------------------------------------------------------------------------------------------
If ItemLocate("udffilegetmaxlinelen",IntControl(77,103,0,0,0),@TAB) Then Goto skip_udffilegetmaxlinelen
#DefineFunction udfFileGetMaxLineLen (sFilename)
iFileSize = FileSize(sFilename)
If (iFileSize==0) Then Return (0) ; Nothing to do.
sMsgText = StrCat("udfFileGetMaxLineLen (",sFilename,") LineSize=")
InStudio = (RtStatus()==10)
hBB = BinaryAlloc(iFileSize+2)
BinaryReadEx(hBB,1,sFilename,0,iFileSize)
BinaryPokeStr(hBB,0,@LF)
BinaryPokeStr(hBB,BinaryEodGet(hBB),@CR)
iLineCount = BinaryStrCnt(hBB,0,BinaryEodGet(hBB)-1,@CRLF)
iLineAvg = iFileSize/Max(iLineCount,1)
iLineSize = 0
Select ((iLineAvg < 30) && (iLineCount > 1000))
; The magic is to set the proper values for auto switching between the methods.
; Is there any rule?
Case @TRUE
BinaryPokeStr(hBB,0,StrClean(BinaryPeekStr(hBB,0,BinaryEodGet(hBB)-1),@CRLF," ",@FALSE,2))
sSearch = StrCat(@LF,@CR)
While @TRUE
If InStudio Then wStatusMsg(StrCat(sMsgText,iLineSize))
BinaryReplace(hBB,sSearch,"",@FALSE)
If (BinaryEodGet(hBB)<=iLineSize) Then Break
iLineSize = iLineSize + 1
sSearch = StrCat(@LF,StrFill(" ",iLineSize),@CR)
EndWhile
Break
Case @FALSE
sBBTag = BinaryTagInit(hBB,@LF,@CR)
While @TRUE
If InStudio Then wStatusMsg(StrCat(sMsgText,iLineSize))
sBBTag = BinaryTagFind(sBBTag)
If (sBBTag=="") Then Break
iLineSize = Max(iLinesize,BinaryTagLen(sBBTag,0))
EndWhile
Break
EndSelect
BinaryFree(hBB)
Return (iLineSize)
;..........................................................................................................................................
; This Function "udfFileGetMaxLineLen" returns the maximal line length of lines in a file.
; This udf combines two lookup algorithms:
; 1. If the file has many many lines with each small line length (I claim this special algorithm as my invention).
; 2. If the file has not so many lines but each with larger length (standard algorithm).
;..........................................................................................................................................
; Detlev Dalitz.20010714
;..........................................................................................................................................
#EndFunction
:skip_udffilegetmaxlinelen
;------------------------------------------------------------------------------------------------------------------------------------------
; --- test ---
sFilename1 = IntControl(1004,0,0,0,0) ; We use this file as test input.
iLineLengthMax1 = udfFileGetMaxLineLen(sFilename1)
sFilename2 = StrCat(DirHome(),"wil.clr") ; We use "wil.clr" file as test input.
iLineLengthMax2 = udfFileGetMaxLineLen(sFilename2)
Exit
;------------------------------------------------------------------------------------------------------------------------------------------
;*EOF*
|
|
If you have questions about WinBatch, you are encouraged to use online WebBoard BBS at http://webboard.windowware.com
|
|
|
|
|
udfOneWordPerLine (sFilenameIn, sFilenameOut) |
;------------------------------------------------------------------------------------------------------------------------------------------
If ItemLocate("udfonewordperline",IntControl(77,103,0,0,0),@TAB) Then Goto skip_udfonewordperline
#DefineFunction udfOneWordPerLine (sFilenameIn, sFilenameOut)
iFileSize = FileSize(sFilenameIn)
If (iFileSize==0) Then Return (@FALSE)
If (sFilenameOut=="") Then sFilenameOut = sFilenameIn ; Caution: Overwrite input file.
hBBXlate = BinaryAlloc(256)
sFilenameXlate = StrCat(Environment("temp"),"\myxlate.tmp")
; sFilenameXlate = FileCreateTemp("XLT")
If !FileExist(sFilenameXlate)
; Writing xlate table.
GoSub PokeTable
BinaryWrite(hBBXlate,sFilenameXlate)
Else
; Loading xlate table.
BinaryRead(hBBXlate,sFilenameXlate)
EndIf
; Transforming.
hBB = BinaryAlloc(iFileSize)
BinaryRead(hBB,sFilenameIn)
BinaryConvert(hBB,0,1,0,0) ; From ansi to oem, because my table is designed for ascii chars.
BinaryXlate(hBB,hBBXlate,0) ; Apply Xlate table.
BinaryFree(hBBXlate)
BinaryConvert(hBB,1,0,0,0) ; From oem to ansi.
While BinaryReplace(hBB," "," ",@TRUE)
EndWhile
iFileSize = BinaryWrite(hBB,sFilenameOut)
iBlankCount = BinaryStrCnt(hBB,0,BinaryEodGet(hBB)-1," ")
BinaryFree(hBB)
hBB = BinaryAlloc(iFileSize+iBlankCount+2+1)
BinaryReadEx(hBB,1,sFilenameOut,0,iFileSize)
BinaryPokeStr(hBB,0,@LF)
BinaryPokeStr(hBB,BinaryEodGet(hBB),@CR)
BinaryReplace(hBB," ",@CRLF,@TRUE)
; Delete all blank lines.
sSearch = StrCat(@LF,@CR)
While BinaryReplace(hBB,sSearch,"",@TRUE)
EndWhile
BinaryPokeStr(hBB,BinaryEodGet(hBB),@LF)
BinaryWriteEx(hBB,0,sFilenameOut,0,-1)
BinaryWriteEx(hBB,1,sFilenameOut,0,BinaryEodGet(hBB)-1)
BinaryFree(hBB)
Return (@TRUE)
;..........................................................................................................................................
:PokeTable
;--- translation table ascii -----------------------------------------------------------------------
;Codes 0 1 2 3 4 5 6 7 8 9 A B C D E F
sRow0 ="032 032 032 032 032 032 032 032 032 032 032 032 032 032 032 032" ; 0x0- ; " "
sRow1 ="032 032 032 032 032 032 032 032 032 032 032 032 032 032 032 032" ; 0x1- ; " "
sRow2 ="032 032 032 035 036 037 038 032 032 032 032 032 032 045 032 032" ; 0x2- ; " #$ & - " ; [pct]=037
sRow3 ="048 049 050 051 052 053 054 055 056 057 032 032 032 032 032 032" ; 0x3- ; "0123456789 "
sRow4 ="064 065 066 067 068 069 070 071 072 073 074 075 076 077 078 079" ; 0x4- ; "@ABCDEFGHIJKLMNO"
sRow5 ="080 081 082 083 084 085 086 087 088 089 090 032 032 032 032 095" ; 0x5- ; "PQRSTUVWXYZ _"
sRow6 ="032 097 098 099 100 101 102 103 104 105 106 107 108 109 110 111" ; 0x6- ; " abcdefghijklmno"
sRow7 ="112 113 114 115 116 117 118 119 120 121 122 032 032 032 032 032" ; 0x7- ; "pqrstuvwxyz "
sRow8 ="128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143" ; 0x8- ; "ÇüéâäàåçêëèïîìÄÅ"
sRow9 ="144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159" ; 0x9- ; "ÉæÆôöòûùÿÖÜø£Ø×"
sRow10="160 161 162 163 164 165 166 167 032 032 032 032 032 032 032 032" ; 0xA- ; "áíóúñѪº "
sRow11="032 032 032 032 032 032 032 032 032 032 032 032 032 032 032 032" ; 0xB- ; " "
sRow12="032 032 032 032 032 032 032 032 032 032 032 032 032 032 032 032" ; 0xC- ; " "
sRow13="032 032 032 032 032 032 032 032 032 032 032 032 032 032 032 032" ; 0xD- ; " "
sRow14="224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239" ; 0xE- ; "ÓßÔÒõÕµþÞÚÛÙýݯ´"
sRow15="032 032 032 032 032 032 032 032 032 032 032 032 032 032 032 032" ; 0xF- ; " "
;--- translation table ascii -----------------------------------------------------------------------
For iRow=0 To 15
For iCol=0 To 15
BinaryPoke(hBBXlate,(16*iRow)+iCol,ItemExtract(1+iCol,sRow%iRow%," "))
Next
Drop(sRow%iRow%)
Next
Return ; from GoSub
;..........................................................................................................................................
; This function "udfOneWordPerLine" creates a file with a single word on each line from a given textfile.
; This function can be used as a first step for creating an indexlist or "Konkordanz"
; (Note: Duplicates have to removed afterwards).
;
; Detlev Dalitz.20010101.20030702
;..........................................................................................................................................
#EndFunction
;------------------------------------------------------------------------------------------------------------------------------------------
:skip_udfonewordperline
;------------------------------------------------------------------------------------------------------------------------------------------
;------------------------------------------------------------------------------------------------------------------------------------------
If ItemLocate("udffiletoitemlist",IntControl(77,103,0,0,0),@TAB) Then Goto skip_udffiletoitemlist
#DefineFunction udfFileToItemList (sFilename, sDelimiter)
iBBSize = FileSize(sFilename)
If !iBBSize Then Return ("")
hBB = BinaryAlloc(iBBSize)
BinaryRead(hBB,sFilename)
sItemList = BinaryPeekStr(hBB,0,iBBSize)
BinaryFree(hBB)
sItemList = StrReplace(sItemList,@CRLF,@CR)
sItemList = StrReplace(sItemList,@LF,@CR)
sItemList = StrReplace(sItemList,@CR,sDelimiter)
Return (sItemList)
;..........................................................................................................................................
; Create itemlist from txtfile with EOL=CR or EOL=LF or EOL=CRLF
; Example: MyItemList = udfFileToItemList(myfile,@TAB)
;..........................................................................................................................................
#EndFunction
:skip_udffiletoitemlist
;------------------------------------------------------------------------------------------------------------------------------------------
;------------------------------------------------------------------------------------------------------------------------------------------
If ItemLocate("udfitemlisttofile",IntControl(77,103,0,0,0),@TAB) Then Goto skip_udfitemlisttofile
#DefineFunction udfItemListToFile (sItemList, sDelimiter, sFilename)
If (sItemList=="") Then Return (0)
sItemList = StrReplace(sItemList,sDelimiter,@CRLF)
hBB = BinaryAlloc(StrLen(sItemList))
BinaryPokeStr(hBB,0,sItemList)
iResult = BinaryWrite(hBB,sFilename)
BinaryFree(hBB)
Return (iResult)
;..........................................................................................................................................
; Create txtfile with EOL=CRLF from ItemList
; Example: ItemListToFile(mylist,@TAB,myfile)
;..........................................................................................................................................
#EndFunction
:skip_udfitemlisttofile
;------------------------------------------------------------------------------------------------------------------------------------------
;------------------------------------------------------------------------------------------------------------------------------------------
If ItemLocate("udfsorttextfile",IntControl(77,103,0,0,0),@TAB) Then Goto skip_udfsorttextfile
#DefineFunction udfSortTextFile (sFilenameIn, sFilenameOut, iDirection)
Select iDirection
Case @ASCENDING
Return (udfItemListToFile(ItemSort(udfFileToItemList(sFilenameIn,@CR),@CR),@CR,sFilenameOut))
Break
Case @DESCENDING
Return (udfItemListToFile(udfReverseList(ItemSort(udfFileToItemList(sFilenameIn,@CR),@CR),@CR),@CR,sFilenameOut))
Break
EndSelect
Return (@FALSE)
#EndFunction
:skip_udfsorttextfile
;------------------------------------------------------------------------------------------------------------------------------------------
;------------------------------------------------------------------------------------------------------------------------------------------
If ItemLocate("udftrimlistdup",IntControl(77,103,0,0,0),@TAB) Then Goto skip_udftrimlistdup
#DefineFunction udfTrimListDup (sItemList, sDelimiter)
sTrimList = ""
iCount = ItemCount(sItemList,sDelimiter)
For i=1 To iCount
sItem = ItemExtract(i,sItemList,sDelimiter)
If (sItem>"") Then If !ItemLocate(sItem,sTrimList,sDelimiter) Then sTrimList = ItemInsert(sItem,-1,sTrimList,sDelimiter)
Next
Return (sTrimList)
#EndFunction
:skip_udftrimlistdup
;------------------------------------------------------------------------------------------------------------------------------------------
;--- test ---
sFilenameIn = IntControl(1004,0,0,0,0) ; We use this script for test input.
sFilenameTmp = FileCreateTemp("TMP") ; Temporary file for test output.
; Build a file with single word on each line.
; Words are listed in order of original occurance.
udfOneWordPerLine (sFilenameIn, sFilenameTmp)
RunWait("notepad", sFilenameTmp) ; Wait for closing notepad.
; Build a file with single word on each line but sorted.
udfSortTextFile (sFilenameTmp, sFilenameTmp, @ASCENDING)
RunWait("notepad", sFilenameTmp) ; Wait for closing notepad.
; Build a condensed file with unique word on each line.
sItemList = udfFileToItemList (sFilenameTmp, @LF)
sItemList = udfTrimListDup (sItemList, @LF)
udfItemListToFile (sItemList, @LF, sFilenameTmp)
RunWait("notepad", sFilenameTmp) ; Wait for closing notepad.
FileDelete(sFilenameTmp) ; Cleaning.
Exit
;------------------------------------------------------------------------------------------------------------------------------------------
;*EOF*
|
|
If you have questions about WinBatch, you are encouraged to use online WebBoard BBS at http://webboard.windowware.com
|
|
|
|
|
udfFileSetEOLTo (sFilename, sDelimiter) |
;------------------------------------------------------------------------------------------------------------------------------------------
If ItemLocate("udffileseteolto",IntControl(77,103,0,0,0),@TAB) Then Goto skip_udffileseteolto
#DefineFunction udfFileSetEOLTo (sFilename, sDelimiter)
iBBsize = FileSize(sFilename)
If (iBBsize==0) Then Return (0) ; Nothing to do.
If (sDelimiter!=@CRLF) Then If (sDelimiter!=@CR) Then If (sDelimiter!=@LF) Then Return (0) ; Nothing to do.
hBB = BinaryAlloc(iBBsize)
BinaryRead(hBB,sFilename)
iAdd = -2 * BinaryStrCnt(hBB,0,iBBsize-1,@CRLF)
iAdd = iAdd + BinaryStrCnt(hBB,0,iBBsize-1,@CR)
iAdd = iAdd + BinaryStrCnt(hBB,0,iBBsize-1,@LF)
BinaryFree(hBB)
iBBsize = iBBsize + iAdd
bb = BinaryAlloc(iBBsize)
BinaryRead(hBB,sFilename)
BinaryReplace(hBB, @CRLF, @LF, @TRUE)
BinaryReplace(hBB, @CR, @LF, @TRUE)
BinaryReplace(hBB, @LF, @CRLF, @TRUE)
If (sDelimiter!=@CRLF) Then BinaryReplace(hBB,@CRLF,sDelimiter,@TRUE)
iBBsize = BinaryWrite(hBB,sFilename)
BinaryFree(hBB)
Return (iBBsize)
;..........................................................................................................................................
; This function udfFileSetEOLTo() normalizes a given textfile's End-Of-Line sequences,
; and set the End-Of-Line sequences to the given eol-delimiter, that can be @CRLF or @CR or @LF.
;..........................................................................................................................................
; Detlev Dalitz.20010713.20020715
;..........................................................................................................................................
#EndFunction
:skip_udffileseteolto
;------------------------------------------------------------------------------------------------------------------------------------------
;--- test ---
sFilenameFrom = IntControl(1004,0,0,0,0) ; We use a copy of this file as test input.
sFilename = FileMapName(sFilenameFrom, Environment("temp"))
iResult = FileCopy(sFilenameFrom,sFilename,@FALSE)
sDelimiter = @TAB ; No change, @TAB is not allowed in this case.
iFileSize = udfFileSetEOLTo (sFilename, sDelimiter)
sDelimiter = @CR
iFileSize = udfFileSetEOLTo (sFilename, sDelimiter)
sDelimiter = @CRLF
iFileSize = udfFileSetEOLTo (sFilename, sDelimiter)
Exit
;------------------------------------------------------------------------------------------------------------------------------------------
;*EOF*
|
|
If you have questions about WinBatch, you are encouraged to use online WebBoard BBS at http://webboard.windowware.com
|
|
|
|
|
udfFileSetEOLToCRLF (sFilename) |
;------------------------------------------------------------------------------------------------------------------------------------------
If ItemLocate("udffileseteoltocrlf",IntControl(77,103,0,0,0),@TAB) Then Goto skip_udffileseteoltocrlf
#DefineFunction udfFileSetEOLToCRLF (sFilename)
iBBsize = FileSize(sFilename)
If (iBBsize==0) Then Return (iBBsize) ; Nothing to do.
iBBmin = 0
iBBmax = iBBsize - 1
hBB = BinaryAlloc(iBBsize)
BinaryRead(hBB,sFilename)
iAdd = -2 * BinaryStrCnt(hBB,iBBmin,iBBmax,@CRLF)
iAdd = iAdd + BinaryStrCnt(hBB,iBBmin,iBBmax,@CR)
iAdd = iAdd + BinaryStrCnt(hBB,iBBmin,iBBmax,@LF)
BinaryFree(hBB)
If (iAdd==0) Then Return (iBBsize) ; Nothing to do.
iBBsize = iBBsize + iAdd
hBB = BinaryAlloc(iBBsize)
BinaryRead(hBB,sFilename)
BinaryReplace(hBB, @CRLF, @LF, @TRUE)
BinaryReplace(hBB, @CR, @LF, @TRUE)
BinaryReplace(hBB, @LF, @CRLF, @TRUE)
iBBsize = BinaryWrite(hBB,sFilename)
BinaryFree(hBB)
Return (iBBsize)
;..........................................................................................................................................
; This function udfFileSetEOLToCRLF() normalizes a given
; textfile's End-Of-Line sequences to the standard DOS two byte sequence @CRLF.
; Single @CR or @LF will be changed to @CRLF.
;..........................................................................................................................................
; Detlev Dalitz.20010722.20020715
;..........................................................................................................................................
#EndFunction
:skip_udffileseteoltocrlf
;------------------------------------------------------------------------------------------------------------------------------------------
; --- test ---
sFilename = IntControl(1004,0,0,0,0) ; We use this file as test input.
iFileSize = udfFileSetEOLToCRLF (sFilename)
Exit
;------------------------------------------------------------------------------------------------------------------------------------------
;*EOF*
|
|
If you have questions about WinBatch, you are encouraged to use online WebBoard BBS at http://webboard.windowware.com
|