we're getting there
This commit is contained in:
+10
-1
@@ -1,7 +1,16 @@
|
|||||||
package pdf_example
|
package pdf_example
|
||||||
|
|
||||||
|
import "core:fmt"
|
||||||
|
import "core:os"
|
||||||
|
|
||||||
import pdf "../"
|
import pdf "../"
|
||||||
|
|
||||||
main :: proc() {
|
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)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,12 +1,18 @@
|
|||||||
package pdf
|
package pdf
|
||||||
|
|
||||||
import "core:os"
|
import "core:os"
|
||||||
|
import "core:fmt"
|
||||||
|
|
||||||
|
PDF_MAGIC :: "%PDF-"
|
||||||
|
|
||||||
Error :: enum {
|
Error :: enum {
|
||||||
|
Ok,
|
||||||
File_Read_Error,
|
File_Read_Error,
|
||||||
|
InvalidFile,
|
||||||
}
|
}
|
||||||
|
|
||||||
Document :: struct {
|
Document :: struct {
|
||||||
|
version: string
|
||||||
}
|
}
|
||||||
|
|
||||||
open :: proc{
|
open :: proc{
|
||||||
@@ -14,7 +20,7 @@ open :: proc{
|
|||||||
open_bytes,
|
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)
|
data, read_err := os.read_entire_file(path, context.allocator)
|
||||||
if read_err != nil {
|
if read_err != nil {
|
||||||
return nil, .File_Read_Error
|
return nil, .File_Read_Error
|
||||||
@@ -23,5 +29,18 @@ open_file :: proc(path: string) -> (pdf: ^Document, err: Error) {
|
|||||||
return open_bytes(data)
|
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)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user