This commit is contained in:
Dennis Eichhorn 2022-08-13 20:01:46 +02:00
parent 4d53adb958
commit 246b77ca08
3 changed files with 20 additions and 8 deletions

View File

@ -12,6 +12,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <inttypes.h>
namespace Stdlib {
class AssocArray {
@ -38,7 +39,7 @@ namespace Stdlib {
static
int hash_index(assoc_arr *h, void *key)
{
int i = (int) key % h->size;
int i = (*((int *) key)) % h->size;
while (h->keys[i] && h->keys[i] != key) {
i = (i + 1) % h->size;
}

View File

@ -14,7 +14,6 @@
#include <stdlib.h>
#include <string.h>
#include "../Stdlib/AssocArray.h"
#include "StringUtils.h"
namespace Utils {
@ -23,19 +22,31 @@ namespace Utils {
public:
static inline
char* get_arg (char *id, Stdlib::AssocArray::assoc_arr *args)
char* get_arg (const char *id, char **argv, int length)
{
if (Utils::StringUtils::is_number(id)) {
return (char *) args->values[atoi(id)];
return argv[atoi(id)];
}
return (char *) Stdlib::AssocArray::hash_lookup(args, id);
for (int i = 0; i < length - 1; ++i) {
if (strcmp(id, argv[i]) == 0) {
return argv[i + 1];
}
}
return NULL;
}
static inline
bool has_arg (char *id, Stdlib::AssocArray::assoc_arr *args)
bool has_arg (const char *id, char **argv, int length)
{
return Stdlib::AssocArray::hash_lookup(args, id) == NULL;
for (int i = 0; i < length; ++i) {
if (strcmp(id, argv[i]) == 0) {
return true;
}
}
return false;
}
};
}

View File

@ -19,7 +19,7 @@ namespace Utils {
public:
static inline
bool is_number (char *s)
bool is_number (const char *s)
{
while (*s != '\0') {
if (!isdigit(*s)) {