From afac83530f69b5b9d4ebb370f2e67c5da428c4ea Mon Sep 17 00:00:00 2001 From: Michael R Sweet Date: Fri, 25 Oct 2024 17:48:19 -0400 Subject: [PATCH] Add pdfioDictGetKey and pdfioDictGetNumPairs APIs (Issue #63) Add pdfioArrayRemove and pdfioDictClear APIs (Issue #74) --- CHANGES.md | 2 ++ pdfio-array.c | 24 +++++++++++++++++++++++- pdfio-dict.c | 42 ++++++++++++++++++++++++++++++++++++------ pdfio-object.c | 2 +- pdfio-private.h | 1 - pdfio.h | 4 ++++ 6 files changed, 66 insertions(+), 9 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index 1d4fccd..5e99770 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -5,6 +5,8 @@ Changes in PDFio v1.4.0 - YYYY-MM-DD ------------------- +- Added new `pdfioDictGetKey` and `pdfioDictGetNumPairs` APIs (Issue #63) +- Added new `pdfioArrayRemove` and `pdfioDictClear` APIs (Issue #74) - Added new `pdfioFileCreateNameObj` and `pdfioObjGetName` APIs for creating and getting name object values (Issue #76) - Updated documentation (Issue #78) diff --git a/pdfio-array.c b/pdfio-array.c index 3497731..67b89d7 100644 --- a/pdfio-array.c +++ b/pdfio-array.c @@ -1,7 +1,7 @@ // // PDF array functions for PDFio. // -// Copyright © 2021 by Michael R Sweet. +// Copyright © 2021-2024 by Michael R Sweet. // // Licensed under Apache License v2.0. See the file "LICENSE" for more // information. @@ -637,6 +637,28 @@ _pdfioArrayRead(pdfio_file_t *pdf, // I - PDF file } +// +// 'pdfioArrayRemove()' - Remove an array entry. +// + +bool // O - `true` on success, `false` otherwise +pdfioArrayRemove(pdfio_array_t *a, // I - Array + size_t n) // I - Index +{ + if (!a || n >= a->num_values) + return (false); + + if (a->values[n].type == PDFIO_VALTYPE_BINARY) + free(a->values[n].value.binary.data); + + a->num_values --; + if (n < a->num_values) + memmove(a->values + n, a->values + n + 1, (a->num_values - n) * sizeof(_pdfio_value_t)); + + return (true); +} + + // // '_pdfioArrayWrite()' - Write an array to a PDF file. // diff --git a/pdfio-dict.c b/pdfio-dict.c index a0b4240..a559c71 100644 --- a/pdfio-dict.c +++ b/pdfio-dict.c @@ -1,7 +1,7 @@ // // PDF dictionary functions for PDFio. // -// Copyright © 2021-2023 by Michael R Sweet. +// Copyright © 2021-2024 by Michael R Sweet. // // Licensed under Apache License v2.0. See the file "LICENSE" for more // information. @@ -18,19 +18,22 @@ static int compare_pairs(_pdfio_pair_t *a, _pdfio_pair_t *b); // -// '_pdfioDictClear()' - Remove a key/value pair from a dictionary. +// 'pdfioDictClear()' - Remove a key/value pair from a dictionary. // -void -_pdfioDictClear(pdfio_dict_t *dict, // I - Dictionary - const char *key) // I - Key +bool // O - `true` if cleared, `false` otherwise +pdfioDictClear(pdfio_dict_t *dict, // I - Dictionary + const char *key) // I - Key { size_t idx; // Index into pairs _pdfio_pair_t *pair, // Current pair pkey; // Search key - PDFIO_DEBUG("_pdfioDictClear(dict=%p, key=\"%s\")\n", dict, key); + PDFIO_DEBUG("pdfioDictClear(dict=%p, key=\"%s\")\n", dict, key); + + if (!dict || !key) + return (false); // See if the key is already set... if (dict->num_pairs > 0) @@ -48,8 +51,12 @@ _pdfioDictClear(pdfio_dict_t *dict, // I - Dictionary if (idx < dict->num_pairs) memmove(pair, pair + 1, (dict->num_pairs - idx) * sizeof(_pdfio_pair_t)); + + return (true); } } + + return (false); } @@ -335,6 +342,18 @@ pdfioDictGetDict(pdfio_dict_t *dict, // I - Dictionary } +// +// 'pdfioDictGetKey()' - Get the key for the specified pair. +// + +const char * // O - Key for specified pair +pdfioDictGetKey(pdfio_dict_t *dict, // I - Dictionary + size_t n) // I - Pair index (`0`-based) +{ + return ((dict && n < dict->num_pairs) ? dict->pairs[n].key : NULL); +} + + // // 'pdfioDictGetName()' - Get a key name value from a dictionary. // @@ -353,6 +372,17 @@ pdfioDictGetName(pdfio_dict_t *dict, // I - Dictionary } +// +// 'pdfioDictGetNumPairs()' - Get the number of key/value pairs in a dictionary. +// + +size_t // O - Number of pairs +pdfioDictGetNumPairs(pdfio_dict_t *dict)// I - Dictionary +{ + return (dict ? dict->num_pairs : 0); +} + + // // 'pdfioDictGetNumber()' - Get a key number value from a dictionary. // diff --git a/pdfio-object.c b/pdfio-object.c index f1c94e2..fb0b6df 100644 --- a/pdfio-object.c +++ b/pdfio-object.c @@ -99,7 +99,7 @@ pdfioObjCopy(pdfio_file_t *pdf, // I - PDF file return (NULL); if (dstobj->value.type == PDFIO_VALTYPE_DICT) - _pdfioDictClear(dstobj->value.value.dict, "Length"); + pdfioDictClear(dstobj->value.value.dict, "Length"); if (srcobj->stream_offset) { diff --git a/pdfio-private.h b/pdfio-private.h index d03b54f..1e4148d 100644 --- a/pdfio-private.h +++ b/pdfio-private.h @@ -353,7 +353,6 @@ extern void _pdfioCryptoSHA256Init(_pdfio_sha256_t *ctx) _PDFIO_INTERNAL; extern void _pdfioCryptoSHA256Finish(_pdfio_sha256_t *ctx, uint8_t *Message_Digest) _PDFIO_INTERNAL; extern bool _pdfioCryptoUnlock(pdfio_file_t *pdf, pdfio_password_cb_t password_cb, void *password_data) _PDFIO_INTERNAL; -extern void _pdfioDictClear(pdfio_dict_t *dict, const char *key) _PDFIO_INTERNAL; extern bool _pdfioDictDecrypt(pdfio_file_t *pdf, pdfio_obj_t *obj, pdfio_dict_t *dict, size_t depth) _PDFIO_INTERNAL; extern void _pdfioDictDebug(pdfio_dict_t *dict, FILE *fp) _PDFIO_INTERNAL; extern void _pdfioDictDelete(pdfio_dict_t *dict) _PDFIO_INTERNAL; diff --git a/pdfio.h b/pdfio.h index e96c471..f546e92 100644 --- a/pdfio.h +++ b/pdfio.h @@ -151,7 +151,9 @@ extern pdfio_obj_t *pdfioArrayGetObj(pdfio_array_t *a, size_t n) _PDFIO_PUBLIC; extern size_t pdfioArrayGetSize(pdfio_array_t *a) _PDFIO_PUBLIC; extern const char *pdfioArrayGetString(pdfio_array_t *a, size_t n) _PDFIO_PUBLIC; extern pdfio_valtype_t pdfioArrayGetType(pdfio_array_t *a, size_t n) _PDFIO_PUBLIC; +extern bool pdfioArrayRemove(pdfio_array_t *a, size_t n) _PDFIO_PUBLIC; +extern bool pdfioDictClear(pdfio_dict_t *dict, const char *key) _PDFIO_PUBLIC; extern pdfio_dict_t *pdfioDictCopy(pdfio_file_t *pdf, pdfio_dict_t *dict) _PDFIO_PUBLIC; extern pdfio_dict_t *pdfioDictCreate(pdfio_file_t *pdf) _PDFIO_PUBLIC; extern pdfio_array_t *pdfioDictGetArray(pdfio_dict_t *dict, const char *key) _PDFIO_PUBLIC; @@ -159,7 +161,9 @@ extern unsigned char *pdfioDictGetBinary(pdfio_dict_t *dict, const char *key, si extern bool pdfioDictGetBoolean(pdfio_dict_t *dict, const char *key) _PDFIO_PUBLIC; extern time_t pdfioDictGetDate(pdfio_dict_t *dict, const char *key) _PDFIO_PUBLIC; extern pdfio_dict_t *pdfioDictGetDict(pdfio_dict_t *dict, const char *key) _PDFIO_PUBLIC; +extern const char *pdfioDictGetKey(pdfio_dict_t *dict, size_t n) _PDFIO_PUBLIC; extern const char *pdfioDictGetName(pdfio_dict_t *dict, const char *key) _PDFIO_PUBLIC; +extern size_t pdfioDictGetNumPairs(pdfio_dict_t *dict) _PDFIO_PUBLIC; extern double pdfioDictGetNumber(pdfio_dict_t *dict, const char *key) _PDFIO_PUBLIC; extern pdfio_obj_t *pdfioDictGetObj(pdfio_dict_t *dict, const char *key) _PDFIO_PUBLIC; extern pdfio_rect_t *pdfioDictGetRect(pdfio_dict_t *dict, const char *key, pdfio_rect_t *rect) _PDFIO_PUBLIC;