ctucx.git: nimjpg

parse jpg file header

commit 0d87f77c176d9b73346c865972704a1addfe74ed
parent d95ba692278fc6f8ec4ea5c245af87fda1c8b777
Author: Milan Pässler <milan@petabyte.dev>
Date: Sat, 13 Mar 2021 11:11:15 +0100

parse jpg header
3 files changed, 23 insertions(+), 9 deletions(-)
M
example.nim
|
17
++++++++++++++---
M
exif.nim
|
11
+++++++----
M
libexif.nim
|
4
++--
diff --git a/example.nim b/example.nim
@@ -1,5 +1,7 @@
 import os
-import exif
+import jpg
+import asyncdispatch
+import asyncfile
 
 var result: cint
 

@@ -7,5 +9,14 @@ if paramCount()  == 0:
   echo "Gimme a file pls"
   quit(QuitFailure)
 
-init_exif()
-echo $collect_exif(paramStr(1))
+let path = paramStr(1)
+
+init_jpg()
+
+if fileExists(path):
+  let file = openAsync(path)
+  echo $(waitFor collect_jpg(file))
+else:
+  echo "Gimme an existing file pls"
+
+deinit_jpg()
diff --git a/exif.nim b/exif.nim
@@ -1,21 +1,24 @@
+import asyncdispatch
 import libexif
 import tables
 
 var ed {.threadvar.}: ptr ExifData
 var buf {.threadvar.}: cstring
-var callback_data {.threadvar.}: pointer
 
+# MUST be called once per thread
 proc init_exif*() =
   buf = cast[cstring](alloc(2001))
 
-proc collect_exif*(path: string): Table[string, string] {.gcsafe.} =
+proc deinit_exif*() =
+  dealloc(buf)
+
+proc collect_exif*(path: string): Future[Table[string, string]] {.gcsafe,async.} =
   result = initTable[string, string]()
 
   ed = exif_data_new_from_file(path)
-  callback_data = nil
 
   if ed == nil:
-    echo "error!"
+    # ERROR
     quit(0)
 
   proc process_entries(entry: ptr ExifEntry , callback_data: pointer) {.cdecl.} =
diff --git a/libexif.nim b/libexif.nim
@@ -15,8 +15,8 @@ const
   baseDir = getProjectCacheDir("exif" / "libexif")
 
 when isDefined(libexifLocal):
-  cIncludeDir("libexif/include/libexif")
-  cImport("libexif/include/libexif/exif-data.h", recurse = true, dynlib = "libexif/lib/libexif.so")
+  cIncludeDir("libexif/include")
+  cImport("libexif/include/libexif/exif-data.h" , recurse = true, dynlib = "libexif/lib/libexif.so")
 
 else:
   getHeader(