Skip to content

Commit

Permalink
Add --stringparam=key=value and --param=key=value to transform command.
Browse files Browse the repository at this point in the history
  • Loading branch information
weetmuts committed May 19, 2024
1 parent 3eb293e commit 8db4517
Show file tree
Hide file tree
Showing 8 changed files with 234 additions and 28 deletions.
73 changes: 69 additions & 4 deletions dist/xmq.c
Original file line number Diff line number Diff line change
Expand Up @@ -779,16 +779,24 @@ size_t print_utf8(XMQPrintState *ps, XMQColor c, size_t num_pairs, ...);
struct HashMap;
typedef struct HashMap HashMap;

struct HashMapIterator;
typedef struct HashMapIterator HashMapIterator;

HashMap *hashmap_create(size_t max_size);
void hashmap_free_and_values(HashMap *map);
// Returns NULL if no key is found.
void *hashmap_get(HashMap* map, const char* key);
// Putting a non-NULL value.
void hashmap_put(HashMap* map, const char* key, void *val);
// Remove a key-val.
void hashmap_remove(HashMap* map, const char* key);
// How many key-vals are there?
size_t hashmap_size();
// Free it.
void hashmap_free(HashMap* map);

HashMapIterator *hashmap_iterate(HashMap *map);
bool hashmap_next_key_value(HashMapIterator *i, const char **key, void **val);
void hashmap_free_iterator(HashMapIterator *i);

#define HASHMAP_MODULE

// PARTS MEMBUFFER ////////////////////////////////////////
Expand Down Expand Up @@ -5805,7 +5813,7 @@ typedef struct HashMapNode HashMapNode;

struct HashMapNode
{
const char *key_;
char *key_;
void *val_;
struct HashMapNode *next_;
};
Expand All @@ -5817,6 +5825,13 @@ struct HashMap
HashMapNode** nodes_;
};

struct HashMapIterator
{
HashMap *hm_;
int offset_;
struct HashMapNode *node_;
};

// FUNCTION DECLARATIONS //////////////////////////////////////////////////

size_t hash_code(const char *str);
Expand Down Expand Up @@ -5844,7 +5859,7 @@ HashMapNode* hashmap_create_node(const char *key)
{
HashMapNode* new_node = (HashMapNode*) malloc(sizeof(HashMapNode));
memset(new_node, 0, sizeof(*new_node));
new_node->key_ = key;
new_node->key_ = strdup(key);

return new_node;
}
Expand All @@ -5869,6 +5884,8 @@ void hashmap_free_and_values(HashMap *map)
map->nodes_[i] = NULL;
while (node)
{
if (node->key_) free((char*)node->key_);
node->key_ = NULL;
if (node->val_) free(node->val_);
node->val_ = NULL;
HashMapNode *next = node->next_;
Expand Down Expand Up @@ -5897,6 +5914,7 @@ void hashmap_put(HashMap* table, const char* key, void *val)
HashMapNode* new_node = hashmap_create_node(key);
new_node->val_ = val;
table->nodes_[index] = new_node;
table->size_++;
return;
}

Expand All @@ -5916,6 +5934,7 @@ void hashmap_put(HashMap* table, const char* key, void *val)
HashMapNode* new_node = hashmap_create_node(key);
new_node->val_ = val;
prev_slot->next_ = new_node;
table->size_++;
return;
}

Expand All @@ -5936,6 +5955,11 @@ void *hashmap_get(HashMap* table, const char* key)
return NULL;
}

size_t hashmap_size(HashMap* table)
{
return table->size_;
}

void hashmap_free(HashMap* table)
{
for (int i=0; i < table->max_size_; i++)
Expand All @@ -5953,6 +5977,47 @@ void hashmap_free(HashMap* table)
free(table);
}

HashMapIterator *hashmap_iterate(HashMap *map)
{
HashMapIterator* new_iterator = (HashMapIterator*) malloc(sizeof(HashMapIterator));
memset(new_iterator, 0, sizeof(*new_iterator));
new_iterator->hm_ = map;
new_iterator->offset_ = -1;

return new_iterator;
}

bool hashmap_next_key_value(HashMapIterator *i, const char **key, void **val)
{
if (i->node_)
{
i->node_ = i->node_->next_;
}
if (i->node_)
{
*key = i->node_->key_;
*val = i->node_->val_;
return true;
}
do
{
i->offset_++;
}
while (i->offset_ < i->hm_->max_size_ && i->hm_->nodes_[i->offset_] == NULL);

if (i->offset_ >= i->hm_->max_size_) return false;

i->node_ = i->hm_->nodes_[i->offset_];
*key = i->node_->key_;
*val = i->node_->val_;
return true;
}

void hashmap_free_iterator(HashMapIterator *i)
{
free(i);
}

#endif // HASHMAP_MODULE

// PARTS STACK_C ////////////////////////////////////////
Expand Down
5 changes: 4 additions & 1 deletion doc/xmq.1
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,10 @@ Replace parts of the DOM.
Transform the document using xslt. The xslt can of course be provided in xmq format.

.br
\fB--file=<xslt-file-name>\fP Use this style sheet to transform the document.
\fB--param=key=value\fP Provide value as non-quoted content to parameter key.

.br
\fB--stringparam=key=value\fP Provide value as quoted (single or double) content to parameter key.

.TP
\fIfor-each\fP\fP
Expand Down
61 changes: 59 additions & 2 deletions src/main/c/parts/hashmap.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ typedef struct HashMapNode HashMapNode;

struct HashMapNode
{
const char *key_;
char *key_;
void *val_;
struct HashMapNode *next_;
};
Expand All @@ -41,6 +41,13 @@ struct HashMap
HashMapNode** nodes_;
};

struct HashMapIterator
{
HashMap *hm_;
int offset_;
struct HashMapNode *node_;
};

// FUNCTION DECLARATIONS //////////////////////////////////////////////////

size_t hash_code(const char *str);
Expand Down Expand Up @@ -68,7 +75,7 @@ HashMapNode* hashmap_create_node(const char *key)
{
HashMapNode* new_node = (HashMapNode*) malloc(sizeof(HashMapNode));
memset(new_node, 0, sizeof(*new_node));
new_node->key_ = key;
new_node->key_ = strdup(key);

return new_node;
}
Expand All @@ -93,6 +100,8 @@ void hashmap_free_and_values(HashMap *map)
map->nodes_[i] = NULL;
while (node)
{
if (node->key_) free((char*)node->key_);
node->key_ = NULL;
if (node->val_) free(node->val_);
node->val_ = NULL;
HashMapNode *next = node->next_;
Expand Down Expand Up @@ -121,6 +130,7 @@ void hashmap_put(HashMap* table, const char* key, void *val)
HashMapNode* new_node = hashmap_create_node(key);
new_node->val_ = val;
table->nodes_[index] = new_node;
table->size_++;
return;
}

Expand All @@ -140,6 +150,7 @@ void hashmap_put(HashMap* table, const char* key, void *val)
HashMapNode* new_node = hashmap_create_node(key);
new_node->val_ = val;
prev_slot->next_ = new_node;
table->size_++;
return;
}

Expand All @@ -160,6 +171,11 @@ void *hashmap_get(HashMap* table, const char* key)
return NULL;
}

size_t hashmap_size(HashMap* table)
{
return table->size_;
}

void hashmap_free(HashMap* table)
{
for (int i=0; i < table->max_size_; i++)
Expand All @@ -177,4 +193,45 @@ void hashmap_free(HashMap* table)
free(table);
}

HashMapIterator *hashmap_iterate(HashMap *map)
{
HashMapIterator* new_iterator = (HashMapIterator*) malloc(sizeof(HashMapIterator));
memset(new_iterator, 0, sizeof(*new_iterator));
new_iterator->hm_ = map;
new_iterator->offset_ = -1;

return new_iterator;
}

bool hashmap_next_key_value(HashMapIterator *i, const char **key, void **val)
{
if (i->node_)
{
i->node_ = i->node_->next_;
}
if (i->node_)
{
*key = i->node_->key_;
*val = i->node_->val_;
return true;
}
do
{
i->offset_++;
}
while (i->offset_ < i->hm_->max_size_ && i->hm_->nodes_[i->offset_] == NULL);

if (i->offset_ >= i->hm_->max_size_) return false;

i->node_ = i->hm_->nodes_[i->offset_];
*key = i->node_->key_;
*val = i->node_->val_;
return true;
}

void hashmap_free_iterator(HashMapIterator *i)
{
free(i);
}

#endif // HASHMAP_MODULE
12 changes: 10 additions & 2 deletions src/main/c/parts/hashmap.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,24 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
struct HashMap;
typedef struct HashMap HashMap;

struct HashMapIterator;
typedef struct HashMapIterator HashMapIterator;

HashMap *hashmap_create(size_t max_size);
void hashmap_free_and_values(HashMap *map);
// Returns NULL if no key is found.
void *hashmap_get(HashMap* map, const char* key);
// Putting a non-NULL value.
void hashmap_put(HashMap* map, const char* key, void *val);
// Remove a key-val.
void hashmap_remove(HashMap* map, const char* key);
// How many key-vals are there?
size_t hashmap_size();
// Free it.
void hashmap_free(HashMap* map);

HashMapIterator *hashmap_iterate(HashMap *map);
bool hashmap_next_key_value(HashMapIterator *i, const char **key, void **val);
void hashmap_free_iterator(HashMapIterator *i);

#define HASHMAP_MODULE

#endif // HASHMAP_H
24 changes: 12 additions & 12 deletions src/main/c/parts/quicksort_strings.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,28 +33,28 @@ void swap(const char **a, const char **b);

void swap(const char **a, const char **b)
{
const char *temp = *a;
*a = *b;
*b = temp;
const char *temp = *a;
*a = *b;
*b = temp;
}

void quicksort_strings(char const *arr[], size_t length)
{
size_t i, piv = 0;
if (length <= 1) return;
size_t i, piv = 0;
if (length <= 1) return;

for (i = 0; i < length; i++)
for (i = 0; i < length; i++)
{
if (strcmp(arr[i], arr[length -1]) < 0)
if (strcmp(arr[i], arr[length -1]) < 0)
{
swap(arr + i, arr + piv++);
swap(arr + i, arr + piv++);
}
}
}

swap(arr + piv, arr + length - 1);
swap(arr + piv, arr + length - 1);

quicksort_strings(arr, piv++);
quicksort_strings(arr + piv, length - piv);
quicksort_strings(arr, piv++);
quicksort_strings(arr + piv, length - piv);
}

#endif // QUICKSORT_STRINGS_MODULE
Loading

0 comments on commit 8db4517

Please sign in to comment.