Page 2 of 2

Posted: Sun Jan 22, 2006 12:57 am
by Dare2
hehe. We have produced the ultimate in supportable code. :)


@chen: Sorry - your thread took the scenic detour into spaghetti land.

Posted: Wed Feb 01, 2006 9:42 am
by buzzqw
HELP !

i got a error !

Code: Select all

Procedure ChangeStrings(Filename.s,oldStr.s,newStr.s)
  dif=Len(oldStr)-Len(newStr)
  OpenFile(0, Filename)
  sz=Lof()*3     ; Cheat #1 - assumes file won't grow 2x
  mem=AllocateMemory(sz)
  ReadData(mem,sz)
  ptr=0
  While ptr<=(sz/2)+ctr
    If CompareMemory(@oldStr,mem+ptr,Len(oldStr))
      If dif<0
        CopyMemory(mem+ptr,mem+ptr+(dif * -1),sz-(ptr + (dif * -1)))
        CopyMemory(@newStr,mem+ptr,Len(newStr))
      ElseIf dif>0
        CopyMemory(@newStr,mem+ptr,Len(newStr))
        CopyMemory(mem+ptr+Len(oldStr),mem+ptr+Len(newStr),sz-(ptr + (dif * -1)))
      Else
        CopyMemory(@newStr,mem+ptr,Len(newStr))
      EndIf
      ctr-dif
      ptr+Len(oldStr)+dif
    Else
      ptr+1
    EndIf
  Wend
  FileSeek(0)
  WriteData(mem,Lof()+ctr)
  CloseFile(0)
EndProcedure
ChangeStrings("C:\Programmi\PureBasic\Prove\output.avs","-1526461656","out_width")
ChangeStrings("C:\Programmi\PureBasic\Prove\output.avs","1913716392","out_height")

and this is the "output.avs" file
p_1=last.trim(0,63).Lanczos4Resize(-1526461656,1913716392).RemoveGrain(mode=2)
p_2=last.trim(64,0).BicubicResize(-1526461656,1913716392,0.2,0.4).deen("c3d",0,6,8,3)
p_1+p_2

running the program (in debug mode) i got a "Invalid memory access"

Any help ?

thanks !!!

BHH

Posted: Wed Feb 01, 2006 10:25 am
by buzzqw
Ok, auto-resolved !

put a

Code: Select all

FreeMemory(mem)


after
FileSeek(0)
WriteData(mem,Lof()+ctr)
CloseFile(0)
BHH

Posted: Wed Feb 01, 2006 10:26 am
by Dare2
Phew!

:)

Posted: Wed Feb 01, 2006 3:58 pm
by buzzqw
well not so true...

i try using this routines for changing at least 150 values...

i got memory error

BUT if i use

Code: Select all

sz=Lof()*100 
i got less problem...

BHH

Posted: Wed Feb 01, 2006 4:20 pm
by Dare2
Hiya buzzqw,

Yeah, the thing was a bit of a joke and slung together. One of the issues is the size of memory needed.
Dare2 wrote:Assumes file won't grow to double size.
Worst case is that the the source file is filled with a pattern of the string to be replaced.

If the original string is shorter than the replacement string, then divide the source file size by the length of the original string, round up, and then multiply by the length of the replacement string.

So:

Source file: ABABABAB (8 bytes)
Replacing AB with ABC (2 with 3)
.. 8/2=4
.. 4*3=12
ABCABCABCABC (12 bytes)

Don't rely too much on that code though, it was an in fun thing. I think it is better to do a line-by-liner.


Edit:

If it still crashes, blame Trond. :D

Posted: Wed Feb 01, 2006 4:39 pm
by Trond
Replace

Code: Select all

sz=Lof()*100 
with

Code: Select all

sz = Lof() + (Len(newStr.s)-Len(oldStr.s))*Lof()

Posted: Wed Feb 01, 2006 4:41 pm
by Dare2
Nice and neat. :)

Updated to PB4B3 w/ corrections

Posted: Mon Feb 20, 2006 2:52 am
by Shannara
I updated the code to work with PB4B3 w/ the corrections you guys made, except for Trond's as I couldnt find that sz=Lof()*100 anywhere in the code ..

Code: Select all

Procedure ChangeStrings(Filename.s,oldStr.s,newStr.s)
  OpenFile(0, Filename)
  mem=AllocateMemory(Lof(0)*2)
  ReadData(0,mem,Lof(0)*2)
  While ptr<=((Lof(0)*2)/2)+ctr
    If CompareMemory(@oldStr,mem+ptr,Len(oldStr))
      If Len(oldStr)-Len(newStr)<0
        CopyMemory(mem+ptr,mem+ptr+((Len(oldStr)-Len(newStr)) * -1),(Lof(0)*2)-(ptr + ((Len(oldStr)-Len(newStr)) * -1)))
        CopyMemory(@newStr,mem+ptr,Len(newStr))
      ElseIf Len(oldStr)-Len(newStr)>0
        CopyMemory(@newStr,mem+ptr,Len(newStr))
        CopyMemory(mem+ptr+Len(oldStr),mem+ptr+Len(newStr),(Lof(0)*2)-(ptr + ((Len(oldStr)-Len(newStr)) * -1)))
      Else
        CopyMemory(@newStr,mem+ptr,Len(newStr))
      EndIf
      ctr-(Len(oldStr)-Len(newStr))
      ptr+(Len(oldStr)+(Len(oldStr)-Len(newStr)))-1
    EndIf
    ptr+1
  Wend
  FileSeek(0,0)
  WriteData(0,mem,Lof(0)+ctr)
  CloseFile(0)
  FreeMemory(mem)
EndProcedure
Hmm, even though it starts, and on a 6K file, locks up the cpu to 100% with no progress :) I still updated it to PB4 :D

Posted: Mon Feb 20, 2006 3:43 am
by Dare2
hehe. :)

It deserved an upgrade! :D Welcome to the spaghetti club!