47 lines
751 B
Odin
47 lines
751 B
Odin
package pdf
|
|
|
|
import "core:os"
|
|
import "core:fmt"
|
|
|
|
PDF_MAGIC :: "%PDF-"
|
|
|
|
Error :: enum {
|
|
Ok,
|
|
File_Read_Error,
|
|
InvalidFile,
|
|
}
|
|
|
|
Document :: struct {
|
|
version: string
|
|
}
|
|
|
|
open :: proc{
|
|
open_file,
|
|
open_bytes,
|
|
}
|
|
|
|
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
|
|
}
|
|
|
|
return open_bytes(data)
|
|
}
|
|
|
|
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)
|
|
}
|