diff --git a/CHANGES.md b/CHANGES.md index 98b42d1..6399488 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -6,6 +6,7 @@ v1.1.0 (Month DD, YYYY) ----------------------- - Added `pdfioFileCreateTemporary` function (Issue #29) +- Added `pdfioDictIterateKeys` function (Issue #31) v1.0.1 (March 2, 2022) diff --git a/doc/pdfio.3 b/doc/pdfio.3 index 87e25e7..ad9a061 100644 --- a/doc/pdfio.3 +++ b/doc/pdfio.3 @@ -1,4 +1,4 @@ -.TH pdfio 3 "pdf read/write library" "2022-05-15" "pdf read/write library" +.TH pdfio 3 "pdf read/write library" "2022-06-27" "pdf read/write library" .SH NAME pdfio \- pdf read/write library .SH Introduction @@ -1950,6 +1950,31 @@ pdfio_valtype_t pdfioDictGetType ( const char *key ); .fi +.SS pdfioDictIterateKeys +Iterate the keys in a dictionary. +.PP +.nf +void pdfioDictIterateKeys ( + pdfio_dict_t *dict, + pdfio_dict_cb_t cb, + void *cb_data +); +.fi +.PP +This function iterates the keys in a dictionary, calling the supplied +function "cb": +.PP +.nf + bool + my_dict_cb(pdfio_dict_t *dict, const char *key, void *cb_data) + { + ... "key" contains the dictionary key ... + ... return true to continue or false to stop ... + } + +.fi +The iteration continues as long as the callback returns \fBtrue\fR or all keys +have been iterated. .SS pdfioDictSetArray Set a key array in a dictionary. .PP @@ -2889,6 +2914,12 @@ Standard color spaces .nf typedef enum pdfio_cs_e pdfio_cs_t; .fi +.SS pdfio_dict_cb_t +Dictionary iterator callback +.PP +.nf +typedef bool(*)(pdfio_dict_t *dict, const char *key, void *cb_data) pdfio_dict_cb_t; +.fi .SS pdfio_dict_t Key/value dictionary .PP diff --git a/doc/pdfio.html b/doc/pdfio.html index db000b9..c134456 100644 --- a/doc/pdfio.html +++ b/doc/pdfio.html @@ -360,6 +360,7 @@ span.string {
Value type
+Iterate the keys in a dictionary.
++void pdfioDictIterateKeys(pdfio_dict_t *dict, pdfio_dict_cb_t cb, void *cb_data);
+dict | +Dictionary |
---|---|
cb | +Callback function |
cb_data | +Callback data |
This function iterates the keys in a dictionary, calling the supplied +function "cb": + +
+bool +my_dict_cb(pdfio_dict_t *dict, const char *key, void *cb_data) +{ +... "key" contains the dictionary key ... +... return true to continue or false to stop ... +} ++The iteration continues as long as the callback returns
true
or all keys
+have been iterated.
Set a key array in a dictionary.
@@ -3514,6 +3543,11 @@ typedef struct _pdfio_array_s pdfio_array_t;
typedef enum pdfio_cs_e pdfio_cs_t;
+Dictionary iterator callback
++typedef bool (*pdfio_dict_cb_t)(pdfio_dict_t *dict, const char *key, void *cb_data); +
Key/value dictionary
diff --git a/pdfio-dict.c b/pdfio-dict.c index a5ae739..e15aebd 100644 --- a/pdfio-dict.c +++ b/pdfio-dict.c @@ -464,6 +464,47 @@ _pdfioDictGetValue(pdfio_dict_t *dict, // I - Dictionary } +// +// 'pdfioDictIterateKeys()' - Iterate the keys in a dictionary. +// +// This function iterates the keys in a dictionary, calling the supplied +// function "cb": +// +// ``` +// bool +// my_dict_cb(pdfio_dict_t *dict, const char *key, void *cb_data) +// { +// ... "key" contains the dictionary key ... +// ... return true to continue or false to stop ... +// } +// ``` +// +// The iteration continues as long as the callback returns `true` or all keys +// have been iterated. +// + +void +pdfioDictIterateKeys( + pdfio_dict_t *dict, // I - Dictionary + pdfio_dict_cb_t cb, // I - Callback function + void *cb_data) // I - Callback data +{ + size_t i; // Looping var + _pdfio_pair_t *pair; // Current pair + + + // Range check input... + if (!dict || !cb) + return; + + for (i = dict->num_pairs, pair = dict->pairs; i > 0; i --, pair ++) + { + if (!(cb)(dict, pair->key, cb_data)) + break; + } +} + + // // '_pdfioDictRead()' - Read a dictionary from a PDF file. // diff --git a/pdfio.h b/pdfio.h index 6b88c9a..9ba328b 100644 --- a/pdfio.h +++ b/pdfio.h @@ -55,6 +55,8 @@ typedef struct _pdfio_array_s pdfio_array_t; // Array of PDF values typedef struct _pdfio_dict_s pdfio_dict_t; // Key/value dictionary +typedef bool (*pdfio_dict_cb_t)(pdfio_dict_t *dict, const char *key, void *cb_data); + // Dictionary iterator callback typedef struct _pdfio_file_s pdfio_file_t; // PDF file typedef bool (*pdfio_error_cb_t)(pdfio_file_t *pdf, const char *message, void *data); @@ -165,6 +167,7 @@ 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 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; extern bool pdfioDictSetBoolean(pdfio_dict_t *dict, const char *key, bool value) _PDFIO_PUBLIC; diff --git a/testpdfio.c b/testpdfio.c index dccc451..d17c803 100644 --- a/testpdfio.c +++ b/testpdfio.c @@ -34,6 +34,7 @@ static int do_test_file(const char *filename, int objnum, bool verbose); static int do_unit_tests(void); static int draw_image(pdfio_stream_t *st, const char *name, double x, double y, double w, double h, const char *label); static bool error_cb(pdfio_file_t *pdf, const char *message, bool *error); +static bool iterate_cb(pdfio_dict_t *dict, const char *key, void *cb_data); static ssize_t output_cb(int *fd, const void *buffer, size_t bytes); static const char *password_cb(void *data, const char *filename); static int read_unit_file(const char *filename, size_t num_pages, size_t first_image, bool is_output); @@ -507,6 +508,8 @@ do_unit_tests(void) size_t first_image, // First image object num_pages; // Number of pages written char temppdf[1024]; // Temporary PDF file + pdfio_dict_t *dict; // Test dictionary + int count = 0; // Number of key/value pairs static const char *complex_dict = // Complex dictionary value "<