we're getting there

This commit is contained in:
2026-10-11 23:01:37 +02:00
parent 83202070c7
commit 75ff309073
2 changed files with 31 additions and 3 deletions
+10 -1
View File
@@ -1,7 +1,16 @@
package pdf_example
import "core:fmt"
import "core:os"
import pdf "../"
main :: proc() {
pdf.open_file("example.pdf")
doc, err := pdf.open_file(os.args[1])
defer pdf.destroy_document(doc)
if err != nil {
fmt.panicf("Failed to open PDF file: %v", err)
}
fmt.println(doc)
}
+21 -2
View File
@@ -1,12 +1,18 @@
package pdf
import "core:os"
import "core:fmt"
PDF_MAGIC :: "%PDF-"
Error :: enum {
Ok,
File_Read_Error,
InvalidFile,
}
Document :: struct {
version: string
}
open :: proc{
@@ -14,7 +20,7 @@ open :: proc{
open_bytes,
}
open_file :: proc(path: string) -> (pdf: ^Document, err: Error) {
open_file :: proc(path: string) -> (doc: ^Document, err: Error) {
data, read_err := os.read_entire_file(path, context.allocator)
if read_err != nil {
return nil, .File_Read_Error
@@ -23,5 +29,18 @@ open_file :: proc(path: string) -> (pdf: ^Document, err: Error) {
return open_bytes(data)
}
open_bytes :: proc(data: []u8) -> (pdf: ^Document, err: Error) {
open_bytes :: proc(data: []u8) -> (doc: ^Document, err: Error) {
doc = new(Document)
if string(data[:5]) != PDF_MAGIC {
return nil, .InvalidFile
}
doc.version = string(data[5:5+3])
return doc, nil
}
destroy_document :: proc(pdf: ^Document) {
free(pdf)
}