
; Win32 Console Application

format PE Console
entry Start

include '%finc%\win32\win32a.inc'

uglobal
  hInstance dd ?
  hHeap     dd ?
  ptrDBase  dd ?
  hSQL      dd ?
  ptrDummy  dd ?

  findData  FINDDATA
  hFind     dd ?
endg

iglobal
  cDBName  db 'Pictures.sqlite', 0
  files    db '.\pictures\*.bmp', 0

  filePath db '.\pictures\'
  filename rb 100

  sqlCreateDB file 'createdb.sql'
              db 0

  sqlInsert db 'insert into Pictures (Name, Picture) values (?, ?);', 0
endg

Start:
        invoke  GetModuleHandleA,0
        mov     [hInstance],eax
        invoke  GetProcessHeap
        mov     [hHeap], eax
        InitializeAll

        ; TODO: Insert code here
        cinvoke sqliteOpen, cDBName, ptrDBase
        cinvoke sqliteExec, [ptrDBase], sqlCreateDB, NULL, NULL, NULL

        invoke  FindFirstFileA, files, findData
        cmp     eax, INVALID_HANDLE_VALUE
        je      .finish

        mov     [hFind], eax

.readloop:
        lea     esi, [findData.cFileName]
        lea     edi, [filename]
        cld
.copy:
        lodsb
        stosb
        test    al,al
        jnz     .copy

        stdcall LoadBinaryFile, filePath

        mov     esi, eax
        mov     ebx, ecx

        cinvoke sqlitePrepare, [ptrDBase], sqlInsert, -1, hSQL, ptrDummy

        cinvoke sqliteBindText, [hSQL], 1, findData.cFileName, -1, NULL
        cinvoke sqliteBindBlob, [hSQL], 2, esi, ebx, SQLITE_TRANSIENT
        cinvoke sqliteStep, [hSQL]
        cinvoke sqliteFinalize, [hSQL]

        invoke  HeapFree, [hHeap], 0, esi

        invoke  FindNextFileA, [hFind], findData
        test    eax, eax
        jnz     .readloop


.finish:
        cinvoke sqliteClose, [ptrDBase]

Exit:
        push    eax
        FinalizeAll
        invoke  ExitProcess ; exit code from the stack.


;--------------------------------------------------------------------
; Allocates needed memory and loads the entire file in this memory.
; Returns pointer to memory block or NULL if there is error.
; Returns size in ecx
;--------------------------------------------------------------------
proc LoadBinaryFile, .ptrFileName
.bytes dd ?
begin
        push    esi edi ebx

        invoke  CreateFileA, [.ptrFileName], GENERIC_READ,FILE_SHARE_READ, 0, \
                OPEN_EXISTING, 0, 0

        cmp     eax, INVALID_HANDLE_VALUE
        je      .error
        mov     edi, eax

        invoke  GetFileSize, edi, NULL
        mov     ebx, eax

        lea     ecx, [ebx+2]    ; It is for the case that the file is
                                ; text and we want to use it as null terminated
                                ; string.

        invoke  HeapAlloc, [hHeap], HEAP_ZERO_MEMORY, ecx
        mov     esi, eax
        test    esi, esi
        jz      .close

        lea     ecx, [.bytes]
        invoke  ReadFile, edi, esi, ebx, ecx, NULL

.close:
        invoke  CloseHandle, edi

        test    esi, esi
        jz      .error

        cmp     [.bytes], ebx
        je      .ok

        invoke  HeapFree, [hHeap], 0, esi

.error:
        xor     eax, eax
        xor     ecx, ecx
        pop     ebx edi esi
        return

.ok:
        mov     eax, esi
        mov     ecx, ebx
        pop     ebx edi esi
        return
endp







data import
  include '%finc%/win32/allimports.asm'
end data

IncludeAllGlobals