Last active
June 7, 2020 13:11
-
-
Save carlosdelfino/9e69a22b4c08f5e1f9b956fde6d5b5f0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdlib.h> | |
#include <stdint.h> | |
#include <stdbool.h> | |
#include <string.h> | |
#include "parameters.h" | |
#include "lzd_lib.h" | |
static uint_fast8_t dicType = DIC_NUM; | |
static DicEntryT **dic; | |
static size_dic_t sizeDic = 0; | |
static uint_fast8_t lastIdx = 0; | |
const DicEntryT nullEntry; | |
void useNumDic() | |
{ | |
for (int i = 0; i < 10; i++) | |
{ | |
struct DicEntryT entry; | |
char *token = (char *)malloc(2 * sizeof(char)); | |
token[0] = '0' + i; | |
token[1] = '\0'; | |
entry.tokenP = token; | |
entry.index = i; | |
addEntryDic(entry); | |
} | |
dicType = DIC_NUM; | |
} | |
inline struct DicEntryT getDicEntry(size_dic_t indexEntry) | |
{ | |
return **(dic + indexEntry); | |
} | |
struct DicEntryT *addEntryDic(const struct DicEntryT entry) | |
{ | |
dic = (struct DicEntryT **)realloc(dic, memSizeAllocDic(sizeDic + 1)); | |
if (!dic) | |
{ | |
//Serial.println("######### error addEntryDic -> Falha de alocação de memória!"); | |
while (1) | |
; | |
} | |
struct DicEntryT *newtokenP = (struct DicEntryT *)malloc(sizeof(struct DicEntryT)); | |
if (!newtokenP) | |
{ | |
// Serial.println("######### error addEntryDic -> Falha de alocação de memória!"); | |
while (1) | |
; | |
} | |
memcpy(newtokenP, &entry, sizeof(struct DicEntryT)); | |
(*newtokenP).index = lastIdx++; | |
dic[sizeDic] = newtokenP; | |
sizeDic++; | |
} | |
struct DicEntryT **createDic(size_dic_t sizeDic) | |
{ | |
void *mem = malloc(memSizeAllocDic(sizeDic)); | |
if (!mem) | |
{ | |
// Serial.println("######### error createDic -> Falha de alocação de memória!"); | |
while (1) | |
; | |
} | |
return (struct DicEntryT **)mem; | |
} | |
void defineDic(struct DicEntryT dic[], int sizeDic) | |
{ | |
} | |
size_dic_t getSizeDic() | |
{ | |
return sizeDic; | |
} | |
inline size_t sizeDicEntry() | |
{ | |
return sizeof(struct DicEntryT); | |
} | |
inline size_t sizeDicEntryMem() | |
{ | |
return sizeof(struct DicEntryT *); | |
} | |
inline size_dic_t memSizeAllocDic(size_dic_t l_sizeDic) | |
{ | |
if (!l_sizeDic) | |
l_sizeDic = getSizeDic(); | |
if (!l_sizeDic) | |
l_sizeDic = 1; | |
return l_sizeDic * sizeDicEntry(); | |
} | |
size_dic_t getDic(struct DicEntryT **toDic) | |
{ | |
memcpy(toDic, (const void *)dic, memSizeAllocDic()); | |
return getSizeDic(); | |
} | |
static struct DicEntryT findDicEntry(const char t) | |
{ | |
for (size_dic_t i = 0; i < sizeDic; i++) | |
{ | |
char *et = getDicEntry(i).tokenP; | |
while (*et) | |
{ | |
Serial.print(et); | |
if ((*et) == t) | |
return getDicEntry(i); | |
et = et+1; | |
} | |
} | |
} | |
bool compactLZ(const char *str, char *strCompact) | |
{ | |
size_dic_t len = strlen(str); | |
size_t bufferEntryPSize = sizeDicEntry() * len; | |
struct DicEntryT *bufferEntryP = (struct DicEntryT *) malloc(bufferEntryPSize); | |
size_t i = 0; | |
for (; i < len; i++) | |
{ | |
int countnt = 1; | |
char t = str[i]; | |
struct DicEntryT de = findDicEntry(t); | |
if (de.index) | |
{ | |
bufferEntryP[i] = de; | |
} | |
else | |
{ | |
struct DicEntryT nde; | |
char *nt = (char*)malloc(countnt); | |
nde.tokenP = nt; | |
nde = (*addEntryDic(nde)); | |
bufferEntryP[i] = nde; | |
} | |
} | |
bufferEntryP[i] = nullEntry; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef _LZD_LIB_ | |
#define _LZD_LIB_ | |
#include <stdlib.h> | |
#include <stdint.h> | |
#include <stdbool.h> | |
#include <string.h> | |
/** tipo que contem o tamanho do dicionário */ | |
typedef uint16_t size_dic_t; | |
/** | |
* Estrutura que armazena tanto entrada no dicionario como | |
* metadado da string compactada | |
*/ | |
struct DicEntryT { | |
uint8_t np ; | |
char *tokenP; | |
size_dic_t index; | |
}; | |
#define DIC_CUSTOM 0 | |
#define DIC_ASCII 1 | |
#define DIC_NUM 2 | |
/** | |
* Usa o Dicionário ASCII | |
*/ | |
void useASCIIDic(); | |
/** | |
* Usa o Dicionário Numérico | |
*/ | |
void useNumDic(); | |
/** | |
* Cria o ponteiro par ao dicionário | |
* | |
* Se sizeDic não for informado usa o tamanho do dicionário atual; | |
* | |
*/ | |
struct DicEntryT ** createDic(size_dic_t sizeDic=0); | |
/** | |
* Retorna o tamanho em bytes do espaço de memória necessário | |
* a ser alocado para dicionário. | |
* | |
*/ | |
inline size_dic_t memSizeAllocDic(size_dic_t sizeDic = 0); | |
/** | |
* retorna o Entrada do Dicionário | |
* Informar posição no dicionário, e não index dda entrada | |
*/ | |
inline struct DicEntryT getDicEntry(size_dic_t indexEntry); | |
/** | |
Define o dicionário a ser usado; | |
Deve ser informado o tamanho do dicionário. | |
Quando se define um dicionário este passa a | |
ser do tipo Custom, anulando as funcionalidades | |
relativas a economia de processamento para | |
ASCII e NUM. | |
Para adicionar entradas a um dicionário ASCII ou NUM | |
use a função addEntry(dicEntryT); | |
*/ | |
void defineDic(struct DicEntryT dic[], size_dic_t sizeDic); | |
/** | |
Obtem o tamanho atual do Dicionário | |
Caso o dicionário não seja do tipo Custom, | |
retorna o tamanho do dic de novas entradas | |
identificadas. | |
*/ | |
size_dic_t getSizeDic(); | |
/** | |
* Limpa do dicionário. | |
* | |
* Se informado outro dicionário libera memória alocada; | |
* Se informado outro dicionário deve informar seu tamanho | |
* | |
* Retorna quantos registros foram limpos. | |
*/ | |
size_dic_t clearDic(struct DicEntryT ** dic = NULL,size_dic_t l_size_dic = getSizeDic()); | |
/** | |
Popula o ponteiro com o ponteiro para o dic atual. | |
Se o dicionário não for custom, apenas faz referência as | |
novas entradas, é importante consultar o tipo de dicionário | |
que está sendo usado. | |
Retorna o tamanho do dicionário. | |
*/ | |
size_dic_t getDic(struct DicEntryT **dic); | |
/** | |
Retorna o tipo de dicionário que está sendo usado; | |
*/ | |
uint_fast8_t getTypeDic(); | |
/** | |
Adiciona uma nova entrada ao dicionário | |
e retorna o ponteiro para a nova entrada | |
Adiciona novas entradas ao dicionário independente do tipo; | |
*/ | |
struct DicEntryT * addEntryDic(struct DicEntryT entry); | |
/** | |
Compacta a string conforme o dicionario cadastrado. | |
Informar o ponteiro da string a ser compactada | |
e o ponteiro onde será armazenada a nova string. | |
Para cadastrar/ativar o dicionário use uma das seguintes funções: | |
-> defineDic(DicEntryT[]) | |
-> addEntry(DicEntryT[]) | |
-> defineAsciiDic() | |
-> defineNumericDic() | |
Retorna true se houve compactação | |
*/ | |
bool compactLZ(const char *str, char *strCompact); | |
/** | |
* Retona o tamanho do struct DicEntryT | |
*/ | |
inline size_t sizeDicEntry(); | |
/** | |
* Retorna o tamanho do ponteiro para struct DicEntryT | |
*/ | |
inline size_t sizeDicEntryMem(); | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment