Page 1 of 1

Includebinary with many many files

Posted: Tue Aug 16, 2011 9:51 pm
by jesperbrannmark
Hi.
I've seen a few examples generate code if you want to do includebinary for 50 images or so (making code and then you copy paste into your own source).
I had to include several thousand images and didn't feel like using a code generating a code that inlines thousand of images.
So I made a code that creates two files (one file containing file info and one containing the raw data) so you just have to include two files. Here it is:

Code to generate the archives:

Code: Select all

CreateFile(0,GetTemporaryDirectory()+"/includes_data.dat")
CreateFile(1,GetTemporaryDirectory()+"/includes_files.dat")
dir.s="/Users/jesperbrannmark/Documents/Files/"
total.q=0
If ExamineDirectory(0, dir.s, "*.*")  
  While NextDirectoryEntry(0)
    FileName$ = DirectoryEntryName(0)
    If DirectoryEntryType(0) <> #PB_DirectoryEntry_Directory
      WriteQuad(1,total)
      filsize.q=FileSize(dir.s+FileName$)
      WriteQuad(1,filsize)
      WriteString(1,FileName$)
      WriteByte(1,0)
      OpenFile(2,dir.s+FileName$)
      *buf=AllocateMemory(filsize)
      ReadData(2,*buf,filsize)
      WriteData(0,*buf,filsize)
      total+filsize
      FreeMemory(*buf)      
      CloseFile(2)
      Debug FileName$+" - "+Str(total)+" - "+Str(filsize)
    EndIf
  Wend  
Else
  MessageRequester("Error","Can't examine this directory: ")
EndIf
WriteQuad(1,0)
CloseFile(0)
CloseFile(1)
Code to be included in your real program:

Code: Select all

Procedure.b find_file(filename$)
  peeker.l=?images_files
  quad1.q=PeekQ(peeker):peeker+8
  Repeat
    quad2.q=PeekQ(peeker):peeker+8
    file_compare$=""
    Repeat
      p.b=PeekB(peeker):peeker+1
      If p.b
        file_compare$+Chr(p.b)
      EndIf
    Until p.b=0    
    If file_compare$=filename$
      ProcedureReturn 1
    EndIf
    quad1.q=PeekQ(peeker):peeker+8
  Until quad1=0
  ProcedureReturn 0
EndProcedure
Procedure save_file(filename$,savefile$)
  peeker.l=?images_files
  quad1.q=PeekQ(peeker):peeker+8
  Repeat
    quad2.q=PeekQ(peeker):peeker+8
    file_compare$=""
    Repeat
      p.b=PeekB(peeker):peeker+1
      If p.b
        file_compare$+Chr(p.b)
      EndIf
    Until p.b=0    
    If file_compare$=filename$      
      fil.l=CreateFile(#PB_Any,savefile$)
      WriteData(fil.l,?images_data+quad1,quad2)
      CloseFile(fil.l)
      ProcedureReturn 1
    EndIf
    quad1.q=PeekQ(peeker):peeker+8
  Until quad1=0
  ProcedureReturn 0  
EndProcedure
DataSection
  images_data: IncludeBinary "includes_data.dat"
  images_files: IncludeBinary "includes_files.dat"
  images_end: Data.b 0
EndDataSection

Re: Includebinary with many many files

Posted: Wed Aug 17, 2011 12:37 am
by IdeasVacuum
Now that is clever, thanks for sharing 8)