File Manipulation

Everything else that doesn't fall into one of the other PB categories.
Dare2
Moderator
Moderator
Posts: 3321
Joined: Sat Dec 27, 2003 3:55 am
Location: Great Southern Land

Post by Dare2 »

hehe. We have produced the ultimate in supportable code. :)


@chen: Sorry - your thread took the scenic detour into spaghetti land.
@}--`--,-- A rose by any other name ..
buzzqw
Enthusiast
Enthusiast
Posts: 116
Joined: Sat Aug 27, 2005 10:13 pm
Location: Italy
Contact:

Post 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
buzzqw
Enthusiast
Enthusiast
Posts: 116
Joined: Sat Aug 27, 2005 10:13 pm
Location: Italy
Contact:

Post by buzzqw »

Ok, auto-resolved !

put a

Code: Select all

FreeMemory(mem)


after
FileSeek(0)
WriteData(mem,Lof()+ctr)
CloseFile(0)
BHH
Dare2
Moderator
Moderator
Posts: 3321
Joined: Sat Dec 27, 2003 3:55 am
Location: Great Southern Land

Post by Dare2 »

Phew!

:)
@}--`--,-- A rose by any other name ..
buzzqw
Enthusiast
Enthusiast
Posts: 116
Joined: Sat Aug 27, 2005 10:13 pm
Location: Italy
Contact:

Post 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
Dare2
Moderator
Moderator
Posts: 3321
Joined: Sat Dec 27, 2003 3:55 am
Location: Great Southern Land

Post 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
@}--`--,-- A rose by any other name ..
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post by Trond »

Replace

Code: Select all

sz=Lof()*100 
with

Code: Select all

sz = Lof() + (Len(newStr.s)-Len(oldStr.s))*Lof()
Dare2
Moderator
Moderator
Posts: 3321
Joined: Sat Dec 27, 2003 3:55 am
Location: Great Southern Land

Post by Dare2 »

Nice and neat. :)
@}--`--,-- A rose by any other name ..
Shannara
Addict
Addict
Posts: 1808
Joined: Thu Oct 30, 2003 11:19 pm
Location: Emerald Cove, Unformed

Updated to PB4B3 w/ corrections

Post 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
Dare2
Moderator
Moderator
Posts: 3321
Joined: Sat Dec 27, 2003 3:55 am
Location: Great Southern Land

Post by Dare2 »

hehe. :)

It deserved an upgrade! :D Welcome to the spaghetti club!
@}--`--,-- A rose by any other name ..
Post Reply