This is what I used in a project which included larger data blocks, it will hide your text even if its purpose was a slightly other.
Code: Select all
;***********************************
;* Author : Kent Jofur
;* Name : Insert Packed-Text
;* Arguments: %TEMPFILE %CURSOR
;***********************************
EnableExplicit
;- Declares
Declare BestPackAndInsert(Text$, line, file$)
Declare ErrWin(msg$)
#MAXPERLINE = 12
Define *ptr, line, Text$, Win, w, h, i
If CountProgramParameters()<>2
ErrWin("Usage: %TEMPFILE %CURSOR")
Else
win = OpenWindow(#PB_Any, #PB_Ignore, #PB_Ignore, 600, 400, "Enter Text", #PB_Window_SystemMenu)
If Win=#NUL
ErrWin("Failed to open window")
EndIf
w= WindowWidth(win)
h= WindowHeight(win)
EditorGadget(0, 0, 0, w, h-22)
ButtonGadget(1, w/2-42, h-20, 42<<1, 18, "Process")
Repeat
Select WaitWindowEvent()
Case #PB_Event_CloseWindow
End
Case #PB_Event_Gadget
If EventGadget()=1 And EventType()=#PB_EventType_LeftClick
Break
EndIf
EndSelect
ForEver
BestPackAndInsert(GetGadgetText(0), Val(StringField(ProgramParameter(1), 1, "x")), ProgramParameter(0))
CloseWindow(win)
EndIf
Procedure BestPackAndInsert(Text$, line, file$)
Protected *p1, *p2, l, i, j=-1, bestP=#MAXLONG, p, *p
Protected fh, filetype, dtype, LineStarted, datasize, s.s
l = Len(Text$)
*p1 = @Text$
*p2 = AllocateMemory(l*SizeOf(Character) + 2*SizeOf(Integer))
If *p1=#NUL Or *p2= #NUL
ErrWin("Failed to allocate memory")
EndIf
For i=0 To 9
p=PackMemory(*p1, *p2, l*SizeOf(Character), i)
If p=0: Continue: EndIf
If p < bestP
bestP = p
j = i
EndIf
Next i
If bestP=#MAXLONG: ProcedureReturn : EndIf
p=PackMemory(*p1, *p2, l*SizeOf(Character), j)
*p=ReAllocateMemory(*p2, p)
;-------------------------------------------------------
datasize = MemorySize(*p)
If datasize=0: ProcedureReturn: EndIf
NewList Line$()
fh = ReadFile(#PB_Any, file$)
If fh
filetype = ReadStringFormat(fh)
While Eof(fh)=#False
AddElement(Line$())
Line$()=ReadString(fh)
Wend
CloseFile(fh)
EndIf
fh = CreateFile(#PB_Any, file$)
If fh
FileBuffersSize(fh, 12*1024)
i=0
ForEach Line$()
i+1
WriteStringN(fh, Line$())
If i=line
s="PackedText_"+FormatDate("%yyyy_%mm_%dd_%hh_%ii_%ss", Date())+"_p"+Str(j)
WriteStringN(fh, "DataSection"+#CRLF$+"Size"+s+":")
WriteStringN(fh, "Data.i "+Str(l*SizeOf(Character))+"; packed to "+Str(p)+" bytes.")
WriteStringN(fh, "Start"+s+": ;PackLevel=" + Str(j)+ ", Packed to=" + StrD(100.0*p/(1.0+l*SizeOf(Character)),2)+"%")
While datasize > 0
If datasize >= SizeOf(Quad)
If LineStarted=#False
dtype=SizeOf(Quad)
LineStarted + 1
WriteString(fh, "Data.q $"+RSet(Hex(PeekQ(*p)), SizeOf(Quad)*2,"0"))
*p+SizeOf(Quad): datasize-SizeOf(Quad)
Else
LineStarted + 1
WriteString(fh, ", $"+RSet(Hex(PeekQ(*p)), SizeOf(Quad)*2,"0"))
*p+SizeOf(Quad): datasize-SizeOf(Quad)
EndIf
If LineStarted >= #MAXPERLINE
WriteStringN(fh, "")
LineStarted=#False
EndIf
Else ; < Quad
If dtype=SizeOf(Quad)
WriteStringN(fh, "")
LineStarted=#False
EndIf
If LineStarted=#False
dtype=SizeOf(Ascii)
LineStarted=#True
WriteString(fh, "Data.a $"+RSet(Hex(PeekA(*p)), SizeOf(Ascii)*2,"0"))
*p+SizeOf(Ascii): datasize-SizeOf(Ascii)
Else
WriteString(fh, ", $"+RSet(Hex(PeekA(*p)), SizeOf(Ascii)*2,"0"))
*p+SizeOf(Ascii): datasize-SizeOf(Ascii)
EndIf
If LineStarted >= #MAXPERLINE
WriteStringN(fh, "")
LineStarted=#False
EndIf
EndIf
Wend
WriteStringN(fh, #CRLF$+"End"+s+":"+#CRLF$+"EndDataSection")
EndIf
Next
CloseFile(fh)
EndIf
EndProcedure
Procedure ErrWin(msg$)
Protected win, timer
win = OpenWindow(#PB_Any, #PB_Ignore, #PB_Ignore, 200, 25, "Error", #PB_Window_SystemMenu)
If win
If LoadFont(0, "Segoe UI", 9)
SetGadgetFont(#PB_Default, FontID(0))
EndIf
AddWindowTimer(win, 0, 20*1000)
TextGadget(0, 2, 2, WindowWidth(win)-4, WindowHeight(win)-4, msg$, #PB_Text_Border )
Repeat
Select WaitWindowEvent()
Case #PB_Event_Timer, #PB_Event_CloseWindow
Break
EndSelect
ForEver
CloseWindow(win)
EndIf
End
EndProcedure
Code: Select all
*mem = AllocateMemory(?SizePackedText_2012_08_28_19_14_27_p0)
len = UnpackMemory(?StartPackedText_2012_08_28_19_14_27_p0, *mem)
If OpenConsole()
Print(PeekS(*mem))
Input()
EndIf
DataSection
SizePackedText_2012_08_28_19_14_27_p0:
Data.i 100; packed to 70 bytes.
StartPackedText_2012_08_28_19_14_27_p0: ;PackLevel=0, Packed to=69.31%
Data.q $073800000064434A, $D24720006A1106CD, $23E8CC8D0303E823, $0C6C47940C405C20, $AB8848D911B362B0, $3DC1C6918A23608D, $479863400A20981A, $AE22066CD86921DE
Data.a $40, $E0, $00, $00, $00, $00
EndPackedText_2012_08_28_19_14_27_p0:
EndDataSection