Lesser General Public License for more details.
*/
-#include <arpa/inet.h>
+#include <ctype.h>
+#include <endian.h>
#include <errno.h>
#include <stdint.h>
#include <stdlib.h>
+#include <string.h>
#include <loc/libloc.h>
#include <loc/as.h>
struct loc_ctx* ctx;
int refcount;
- struct loc_stringpool* pool;
-
uint32_t number;
- off_t name;
+ char* name;
};
-LOC_EXPORT int loc_as_new(struct loc_ctx* ctx, struct loc_stringpool* pool, struct loc_as** as, uint32_t number) {
+LOC_EXPORT int loc_as_new(struct loc_ctx* ctx, struct loc_as** as, uint32_t number) {
struct loc_as* a = calloc(1, sizeof(*a));
if (!a)
return -ENOMEM;
a->ctx = loc_ref(ctx);
a->refcount = 1;
- a->pool = loc_stringpool_ref(pool);
a->number = number;
+ a->name = NULL;
DEBUG(a->ctx, "AS%u allocated at %p\n", a->number, a);
*as = a;
static void loc_as_free(struct loc_as* as) {
DEBUG(as->ctx, "Releasing AS%u %p\n", as->number, as);
- loc_stringpool_unref(as->pool);
- loc_unref(as->ctx);
+ if (as->name)
+ free(as->name);
+ loc_unref(as->ctx);
free(as);
}
}
LOC_EXPORT const char* loc_as_get_name(struct loc_as* as) {
- if (as->name)
- return loc_stringpool_get(as->pool, as->name);
-
- return NULL;
+ return as->name;
}
LOC_EXPORT int loc_as_set_name(struct loc_as* as, const char* name) {
- // Add the string to the string pool
- off_t offset = loc_stringpool_add(as->pool, name);
- if (offset < 0)
- return offset;
+ as->name = strdup(name);
- as->name = offset;
return 0;
}
int loc_as_new_from_database_v0(struct loc_ctx* ctx, struct loc_stringpool* pool,
struct loc_as** as, const struct loc_database_as_v0* dbobj) {
- uint32_t number = ntohl(dbobj->number);
+ uint32_t number = be32toh(dbobj->number);
- int r = loc_as_new(ctx, pool, as, number);
+ int r = loc_as_new(ctx, as, number);
if (r)
return r;
- (*as)->name = ntohl(dbobj->name);
+ const char* name = loc_stringpool_get(pool, be32toh(dbobj->name));
+ r = loc_as_set_name(*as, name);
+ if (r) {
+ loc_as_unref(*as);
+ return r;
+ }
return 0;
}
-int loc_as_to_database_v0(struct loc_as* as, struct loc_database_as_v0* dbobj) {
- dbobj->number = htonl(as->number);
- dbobj->name = htonl(as->name);
+int loc_as_to_database_v0(struct loc_as* as, struct loc_stringpool* pool,
+ struct loc_database_as_v0* dbobj) {
+ dbobj->number = htobe32(as->number);
+
+ // Save the name string in the string pool
+ off_t name = loc_stringpool_add(pool, as->name ? as->name : "");
+ dbobj->name = htobe32(name);
+
+ return 0;
+}
+
+int loc_as_match_string(struct loc_as* as, const char* string) {
+ // Search if string is in name
+ if (strcasestr(as->name, string) != NULL)
+ return 1;
return 0;
}