Added two new fuctions to dict API

This commit is contained in:
David Martin Ferradas 2024-02-09 16:00:18 +01:00
parent 6c1db141a1
commit ff6c402136
4 changed files with 71 additions and 0 deletions

View File

@ -524,6 +524,36 @@ pdfioDictIterateKeys(
}
}
//
// 'pdfioDictGetNumKeys()' - Get the number of keys on the selected dictionary
//
size_t // O - Number of keys
pdfioDictGetNumKeys(pdfio_dict_t *dict) // I - Dictionary
{
return (dict->num_pairs);
}
//
// 'pdfioDictGetKeyByIndex()' - Get name and type of a key by index from a dictionary
//
// Alternative form to enumerate the keys of a dictionary
bool // O - Value
pdfioDictGetKeyByIndex(pdfio_dict_t *dict, // I - Dictionary
size_t index, // I - Index
pdfio_dictKey_t *key) // I - struct pointer
{
if (index < dict->num_pairs)
{
if (key){
key->key = dict->pairs[index].key;
key->type = (pdfio_valtype_t **)(dict->pairs[index].value.type);
return (true);
}
}
return (false);
}
//
// '_pdfioDictRead()' - Read a dictionary from a PDF file.

View File

@ -123,6 +123,12 @@ typedef enum pdfio_valtype_e // PDF value types
PDFIO_VALTYPE_NUMBER, // Number (integer or real)
PDFIO_VALTYPE_STRING // String
} pdfio_valtype_t;
// Alternative PDF dict key enumeration
typedef struct pdfio_dictKey_s
{
const char *key;
pdfio_valtype_t **type;
} pdfio_dictKey_t;
//
@ -165,6 +171,8 @@ extern pdfio_obj_t *pdfioDictGetObj(pdfio_dict_t *dict, const char *key) _PDFIO_
extern pdfio_rect_t *pdfioDictGetRect(pdfio_dict_t *dict, const char *key, pdfio_rect_t *rect) _PDFIO_PUBLIC;
extern const char *pdfioDictGetString(pdfio_dict_t *dict, const char *key) _PDFIO_PUBLIC;
extern pdfio_valtype_t pdfioDictGetType(pdfio_dict_t *dict, const char *key) _PDFIO_PUBLIC;
extern size_t pdfioDictGetNumKeys(pdfio_dict_t *dict) _PDFIO_PUBLIC;
extern bool pdfioDictGetKeyByIndex(pdfio_dict_t *dict, size_t index, pdfio_dictKey_t *keyData) _PDFIO_PUBLIC;
extern void pdfioDictIterateKeys(pdfio_dict_t *dict, pdfio_dict_cb_t cb, void *cb_data) _PDFIO_PUBLIC;
extern bool pdfioDictSetArray(pdfio_dict_t *dict, const char *key, pdfio_array_t *value) _PDFIO_PUBLIC;
extern bool pdfioDictSetBinary(pdfio_dict_t *dict, const char *key, const unsigned char *value, size_t valuelen) _PDFIO_PUBLIC;

BIN
test_mod Executable file

Binary file not shown.

33
test_mod.c Normal file
View File

@ -0,0 +1,33 @@
#include <stdio.h>
#include <stdlib.h>
#include "pdfio.h"
/*
* Usage: ./test_mod [file.pdf]
*
* Compiled as:
* gcc test_mod.c -L. -lpdfio -lm -lz -o test_mod
*
*/
int main (int argc, char **argv)
{
pdfio_file_t *pdf = pdfioFileOpen(argv[1], NULL, NULL, NULL, NULL);
pdfio_obj_t *obj_page = pdfioFileGetPage(pdf, 0);
pdfio_dict_t *dict_page = pdfioObjGetDict(obj_page);
size_t num_keys = pdfioDictGetNumKeys(dict_page);
printf("Number of keys in this page: %d\n", num_keys);
for (unsigned int i = 0; i < num_keys; ++i)
{
pdfio_dictKey_t dict_key;
pdfioDictGetKeyByIndex(dict_page, i, &dict_key);
printf("\t%s (%d)\n", dict_key.key, dict_key.type);
}
pdfioFileClose(pdf);
return 0;
}