2021-10-02 19:20:18 +08:00
|
|
|
/*
|
2021-11-16 08:46:44 +08:00
|
|
|
* This file is part of the PikaScript project.
|
|
|
|
* http://github.com/pikastech/pikascript
|
|
|
|
*
|
|
|
|
* MIT License
|
|
|
|
*
|
2021-11-16 08:49:33 +08:00
|
|
|
* Copyright (c) 2021 lyon 李昂 liang6516@outlook.com
|
2021-11-16 08:46:44 +08:00
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
* SOFTWARE.
|
|
|
|
*/
|
2021-10-02 19:20:18 +08:00
|
|
|
|
2021-10-01 00:21:50 +08:00
|
|
|
#include "dataArg.h"
|
|
|
|
#include "dataArgs.h"
|
|
|
|
#include "dataMemory.h"
|
|
|
|
#include "dataString.h"
|
|
|
|
#include "stdlib.h"
|
|
|
|
|
|
|
|
void arg_deinit(Arg* self) {
|
2021-10-02 19:20:18 +08:00
|
|
|
arg_freeContent(self);
|
2021-10-01 00:21:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
uint16_t arg_getTotleSize(Arg* self) {
|
2021-10-02 19:20:18 +08:00
|
|
|
return content_totleSize(self);
|
2021-10-01 00:21:50 +08:00
|
|
|
}
|
|
|
|
|
2021-11-13 16:42:01 +08:00
|
|
|
/**
|
|
|
|
* time33 hash
|
|
|
|
*/
|
|
|
|
Hash hash_time33(char* str) {
|
|
|
|
Hash hash = 5381;
|
|
|
|
while (*str) {
|
|
|
|
hash += (hash << 5) + (*str++);
|
|
|
|
}
|
|
|
|
return (hash & 0x7FFFFFFF);
|
|
|
|
}
|
|
|
|
|
2022-01-25 08:39:10 +08:00
|
|
|
static uint8_t* content_init_hash(Hash nameHash,
|
|
|
|
ArgType type,
|
|
|
|
uint8_t* content,
|
|
|
|
uint32_t size,
|
|
|
|
uint8_t* next) {
|
|
|
|
__arg* self = (__arg*)pikaMalloc(sizeof(__arg) + size);
|
2022-01-24 19:50:53 +00:00
|
|
|
|
2022-01-25 08:39:10 +08:00
|
|
|
self->next = (__arg*)next;
|
2022-01-24 18:05:16 +00:00
|
|
|
self->size = size;
|
|
|
|
self->name_hash = nameHash;
|
2022-01-24 19:50:53 +00:00
|
|
|
self->type = type;
|
2022-01-25 08:39:10 +08:00
|
|
|
|
2021-10-02 19:20:18 +08:00
|
|
|
if (NULL != content) {
|
2022-01-24 18:05:16 +00:00
|
|
|
__platform_memcpy(self->content, content, size);
|
2021-10-02 19:20:18 +08:00
|
|
|
} else {
|
2022-01-24 18:05:16 +00:00
|
|
|
__platform_memset(self->content, 0, size);
|
2021-10-02 19:20:18 +08:00
|
|
|
}
|
2022-01-25 08:39:10 +08:00
|
|
|
|
|
|
|
return (uint8_t*)self;
|
|
|
|
}
|
|
|
|
|
|
|
|
static uint8_t* content_init(char* name,
|
|
|
|
ArgType type,
|
|
|
|
uint8_t* content,
|
|
|
|
uint16_t size,
|
|
|
|
uint8_t* next) {
|
2021-11-13 16:42:01 +08:00
|
|
|
Hash nameHash = hash_time33(name);
|
|
|
|
return content_init_hash(nameHash, type, content, size, next);
|
|
|
|
}
|
|
|
|
|
2021-10-01 00:21:50 +08:00
|
|
|
uint16_t content_totleSize(uint8_t* self) {
|
2022-01-25 08:39:10 +08:00
|
|
|
return ((__arg*)self)->size + sizeof(__arg);
|
2021-10-01 00:21:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void arg_freeContent(Arg* self) {
|
2021-10-02 19:20:18 +08:00
|
|
|
if (NULL != self) {
|
|
|
|
content_deinit(self);
|
|
|
|
}
|
2021-10-01 00:21:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t* content_deinit(uint8_t* self) {
|
2021-10-02 19:20:18 +08:00
|
|
|
uint16_t totleSize = content_totleSize(self);
|
|
|
|
pikaFree(self, totleSize);
|
|
|
|
return 0;
|
2021-10-01 00:21:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t* content_setContent(uint8_t* self, uint8_t* content, uint16_t size) {
|
2021-10-02 19:20:18 +08:00
|
|
|
if (NULL == self) {
|
2022-03-15 16:09:18 +08:00
|
|
|
/* malloc */
|
2022-03-02 10:34:33 +08:00
|
|
|
return content_init("", ARG_TYPE_NONE, content, size, NULL);
|
2021-10-02 19:20:18 +08:00
|
|
|
}
|
2022-03-15 16:13:22 +08:00
|
|
|
|
|
|
|
/* only copy */
|
|
|
|
if (content_getSize(self) >= size) {
|
2022-03-15 16:09:18 +08:00
|
|
|
__platform_memcpy(((__arg*)self)->content, content, size);
|
|
|
|
return self;
|
|
|
|
}
|
2022-01-25 08:39:10 +08:00
|
|
|
|
2022-03-15 16:09:18 +08:00
|
|
|
/* realloc */
|
2021-11-13 16:42:01 +08:00
|
|
|
Hash nameHash = content_getNameHash(self);
|
2021-11-15 09:36:46 +08:00
|
|
|
ArgType type = content_getType(self);
|
2021-10-02 19:20:18 +08:00
|
|
|
uint8_t* next = content_getNext(self);
|
2021-11-13 16:42:01 +08:00
|
|
|
uint8_t* newContent =
|
|
|
|
content_init_hash(nameHash, type, content, size, next);
|
|
|
|
content_deinit(self);
|
|
|
|
return newContent;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t* content_setNameHash(uint8_t* self, Hash nameHash) {
|
|
|
|
if (NULL == self) {
|
2022-03-02 10:34:33 +08:00
|
|
|
return content_init_hash(nameHash, ARG_TYPE_NONE, NULL, 0, NULL);
|
2021-11-13 16:42:01 +08:00
|
|
|
}
|
2022-01-25 08:39:10 +08:00
|
|
|
__arg* arg = (__arg*)self;
|
2022-01-24 18:05:16 +00:00
|
|
|
arg->name_hash = nameHash;
|
|
|
|
return self;
|
2021-10-01 00:21:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t* content_setName(uint8_t* self, char* name) {
|
2022-01-24 18:05:16 +00:00
|
|
|
return content_setNameHash(self, hash_time33(name));
|
2021-10-01 00:21:50 +08:00
|
|
|
}
|
|
|
|
|
2021-11-15 09:36:46 +08:00
|
|
|
uint8_t* content_setType(uint8_t* self, ArgType type) {
|
2021-10-02 19:20:18 +08:00
|
|
|
if (NULL == self) {
|
|
|
|
return content_init("", type, NULL, 0, NULL);
|
|
|
|
}
|
2022-01-24 18:05:16 +00:00
|
|
|
|
2022-01-25 08:39:10 +08:00
|
|
|
__arg* arg = (__arg*)self;
|
2022-01-24 19:50:53 +00:00
|
|
|
arg->type = type;
|
2022-01-25 08:39:10 +08:00
|
|
|
|
2022-01-24 18:05:16 +00:00
|
|
|
return self;
|
2021-10-01 00:21:50 +08:00
|
|
|
}
|
|
|
|
|
2022-01-24 19:50:53 +00:00
|
|
|
ArgType content_getType(uint8_t* self) {
|
2022-01-25 08:39:10 +08:00
|
|
|
__arg* arg = (__arg*)self;
|
2022-02-24 14:43:51 +08:00
|
|
|
return (ArgType)arg->type;
|
2022-01-24 19:50:53 +00:00
|
|
|
}
|
|
|
|
|
2021-10-01 00:21:50 +08:00
|
|
|
Arg* arg_newContent(Arg* self, uint32_t size) {
|
2022-03-02 10:34:33 +08:00
|
|
|
uint8_t* newContent = content_init("", ARG_TYPE_NONE, NULL, size, NULL);
|
2021-10-02 19:20:18 +08:00
|
|
|
arg_freeContent(self);
|
|
|
|
return newContent;
|
2021-10-01 00:21:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Arg* arg_setContent(Arg* self, uint8_t* content, uint32_t size) {
|
2021-10-02 19:20:18 +08:00
|
|
|
return content_setContent(self, content, size);
|
2021-10-01 00:21:50 +08:00
|
|
|
}
|
|
|
|
|
2022-03-15 16:09:18 +08:00
|
|
|
Arg* arg_setStruct(Arg* self,
|
|
|
|
char* name,
|
|
|
|
void* struct_ptr,
|
|
|
|
uint32_t struct_size) {
|
|
|
|
if (NULL == struct_ptr) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
Arg* struct_arg = arg_setContent(NULL, (uint8_t*)struct_ptr, struct_size);
|
|
|
|
struct_arg = arg_setName(struct_arg, name);
|
|
|
|
return struct_arg;
|
|
|
|
}
|
|
|
|
|
2021-10-01 00:21:50 +08:00
|
|
|
Arg* arg_setName(Arg* self, char* name) {
|
2021-10-02 19:20:18 +08:00
|
|
|
return content_setName(self, name);
|
2021-10-01 00:21:50 +08:00
|
|
|
}
|
|
|
|
|
2021-11-13 16:42:01 +08:00
|
|
|
Arg* arg_setNameHash(Arg* self, Hash nameHash) {
|
|
|
|
return content_setNameHash(self, nameHash);
|
|
|
|
}
|
|
|
|
|
2021-11-15 09:36:46 +08:00
|
|
|
Arg* arg_setType(Arg* self, ArgType type) {
|
2021-10-02 19:20:18 +08:00
|
|
|
return content_setType(self, type);
|
2021-10-01 00:21:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Arg* arg_setInt(Arg* self, char* name, int64_t val) {
|
2022-03-02 10:34:33 +08:00
|
|
|
return content_init(name, ARG_TYPE_INT, (uint8_t*)&val, sizeof(val), NULL);
|
2021-10-01 00:21:50 +08:00
|
|
|
}
|
|
|
|
|
2021-12-24 13:15:32 +08:00
|
|
|
Arg* arg_setNull(Arg* self) {
|
2022-03-02 10:34:33 +08:00
|
|
|
return content_init("", ARG_TYPE_NULL, NULL, 0, NULL);
|
2021-12-24 13:15:32 +08:00
|
|
|
}
|
|
|
|
|
2021-10-01 00:21:50 +08:00
|
|
|
Arg* arg_setFloat(Arg* self, char* name, float val) {
|
2022-03-12 20:52:25 +08:00
|
|
|
return content_init(name, ARG_TYPE_FLOAT, (uint8_t*)&val, sizeof(val),
|
|
|
|
NULL);
|
2021-10-01 00:21:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
float arg_getFloat(Arg* self) {
|
2021-10-02 19:20:18 +08:00
|
|
|
if (NULL == arg_getContent(self)) {
|
|
|
|
return -999.999;
|
|
|
|
}
|
2021-10-01 00:21:50 +08:00
|
|
|
|
2022-01-25 08:39:10 +08:00
|
|
|
return *(float*)(((__arg*)self)->content);
|
2021-10-01 00:21:50 +08:00
|
|
|
}
|
|
|
|
|
2021-11-15 09:36:46 +08:00
|
|
|
Arg* arg_setPtr(Arg* self, char* name, ArgType type, void* pointer) {
|
2022-01-25 08:39:10 +08:00
|
|
|
return content_init(name, type, (uint8_t*)&pointer, sizeof(uintptr_t),
|
|
|
|
NULL);
|
2021-10-01 00:21:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Arg* arg_setStr(Arg* self, char* name, char* string) {
|
2022-03-02 10:34:33 +08:00
|
|
|
return content_init(name, ARG_TYPE_STRING, (uint8_t*)string,
|
2021-11-15 09:46:41 +08:00
|
|
|
strGetSize(string) + 1, NULL);
|
2021-10-01 00:21:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int64_t arg_getInt(Arg* self) {
|
2021-10-02 19:20:18 +08:00
|
|
|
if (NULL == arg_getContent(self)) {
|
|
|
|
return -999999;
|
|
|
|
}
|
2022-01-24 18:05:16 +00:00
|
|
|
|
2022-01-25 08:39:10 +08:00
|
|
|
return *(int64_t*)(((__arg*)self)->content);
|
2021-10-01 00:21:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void* arg_getPtr(Arg* self) {
|
2021-10-02 19:20:18 +08:00
|
|
|
if (NULL == arg_getContent(self)) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2022-01-25 08:39:10 +08:00
|
|
|
return *(void**)(((__arg*)self)->content);
|
2021-10-01 00:21:50 +08:00
|
|
|
}
|
|
|
|
char* arg_getStr(Arg* self) {
|
2021-10-02 19:20:18 +08:00
|
|
|
return (char*)arg_getContent(self);
|
2021-10-01 00:21:50 +08:00
|
|
|
}
|
|
|
|
|
2021-11-13 16:42:01 +08:00
|
|
|
Hash arg_getNameHash(Arg* self) {
|
2021-10-22 16:09:14 +08:00
|
|
|
if (NULL == self) {
|
2021-11-13 16:42:01 +08:00
|
|
|
return 999999;
|
2021-10-22 16:09:14 +08:00
|
|
|
}
|
2021-11-13 16:42:01 +08:00
|
|
|
return content_getNameHash(self);
|
2021-10-01 00:21:50 +08:00
|
|
|
}
|
|
|
|
|
2021-11-15 09:36:46 +08:00
|
|
|
ArgType arg_getType(Arg* self) {
|
2021-10-22 16:09:14 +08:00
|
|
|
if (NULL == self) {
|
2022-03-02 10:34:33 +08:00
|
|
|
return ARG_TYPE_NONE;
|
2021-10-22 16:09:14 +08:00
|
|
|
}
|
2021-10-02 19:20:18 +08:00
|
|
|
return content_getType(self);
|
2021-10-01 00:21:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
uint16_t arg_getContentSize(Arg* self) {
|
2021-10-02 19:20:18 +08:00
|
|
|
return content_getSize(self);
|
2021-10-01 00:21:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Arg* New_arg(void* voidPointer) {
|
2021-10-02 19:20:18 +08:00
|
|
|
return NULL;
|
2021-10-01 00:21:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Arg* arg_copy(Arg* argToBeCopy) {
|
2021-10-14 23:22:48 +08:00
|
|
|
if (NULL == argToBeCopy) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2021-10-02 19:20:18 +08:00
|
|
|
Arg* argCopied = New_arg(NULL);
|
|
|
|
argCopied = arg_setContent(argCopied, arg_getContent(argToBeCopy),
|
|
|
|
arg_getContentSize(argToBeCopy));
|
2021-11-13 16:42:01 +08:00
|
|
|
argCopied = arg_setNameHash(argCopied, arg_getNameHash(argToBeCopy));
|
2021-10-02 19:20:18 +08:00
|
|
|
argCopied = arg_setType(argCopied, arg_getType(argToBeCopy));
|
|
|
|
return argCopied;
|
2021-10-01 00:21:50 +08:00
|
|
|
}
|
2022-03-12 20:52:25 +08:00
|
|
|
|
|
|
|
Arg* arg_append(Arg* arg_in, void* new_content, size_t new_size) {
|
|
|
|
uint8_t* old_content = arg_getContent(arg_in);
|
|
|
|
size_t old_size = arg_getContentSize(arg_in);
|
|
|
|
/* create arg_out */
|
|
|
|
Arg* arg_out = arg_setContent(NULL, NULL, old_size + new_size);
|
|
|
|
/* copy old content */
|
|
|
|
__platform_memcpy(arg_getContent(arg_out), old_content, old_size);
|
|
|
|
/* copy new content */
|
|
|
|
__platform_memcpy(arg_getContent(arg_out) + old_size, new_content,
|
|
|
|
new_size);
|
|
|
|
arg_deinit(arg_in);
|
|
|
|
return arg_out;
|
|
|
|
}
|