some remake about stringHash4MCU_: modified to test with parameter and argument

This commit is contained in:
Edward Emelianov
2023-02-04 12:01:02 +03:00
parent 338b9cb4e2
commit 5260ddb3bb
22 changed files with 366 additions and 1186 deletions

View File

@@ -1,6 +1,7 @@
hashtest.c allows to test different hash functions on your dictionary
hashtest.c allows to test different hash functions on your dictionary (test En_words for example)
hashgen.c will generate two files by dictionary: source and header
use test.c to test generated files
another files used for test: use script ./run for it
Compile: gcc -lusefull_macro file.c -o file
gcc -lusefull_macro test.c hash.c -o test

View File

@@ -169,21 +169,39 @@ static char *fnname(const char *cmd){
}
static const char *fhdr =
"int parsecmd(char *cmd, char *args){\n\
if(!cmd || !args) return 0;\n\
uint32_t h = hashf(cmd);\n\
switch(h){\n"
"int parsecmd(const char *str){\n\
char cmd[CMD_MAXLEN + 1];\n\
if(!str || !*str) return RET_CMDNOTFOUND;\n\
int i = 0;\n\
while(*str > '@' && i < CMD_MAXLEN){ cmd[i++] = *str++; }\n\
cmd[i] = 0;\n\
if(*str){\n\
while(*str <= ' ') ++str;\n\
}\n\
char *args = (char*) str;\n\
uint32_t h = hashf(cmd);\n\
switch(h){\n\n"
;
static const char *ffooter =
" default: return 0;\n\
" default: break;\n\
}\n\
return 0;\n\
return RET_CMDNOTFOUND;\n\
}\n\n"
;
static const char *fns =
"int fn_%s(_U_ uint32_t hash, _U_ char *args) WAL; // \"%s\" (%u)\n\n"
;
static const char *fproto = "int parsecmd(char *cmdwargs, char *args);\n\n";
static const char *headercontent = "#ifndef _U_\n\
#define _U_ __attribute__((__unused__))\n\
#endif\n\n\
#define CMD_MAXLEN (32)\n\n\
enum{\n\
RET_CMDNOTFOUND = -2,\n\
RET_WRONGCMD = -1,\n\
RET_BAD = 0,\n\
RET_GOOD = 1\n\
};\n\n\
int parsecmd(const char *cmdwargs);\n\n";
static const char *sw =
" case CMD_%s:\n\
return fn_%s(h, args);\n\
@@ -192,9 +210,6 @@ static const char *srchdr =
"#include <stdint.h>\n\
#include <stddef.h>\n\
#include \"%s\"\n\n\
#ifndef _U_\n\
#define _U_ __attribute__((__unused__))\n\
#endif\n\n\
#ifndef WAL\n\
#define WAL __attribute__ ((weak, alias (\"__f1\")))\n\
#endif\n\nstatic int __f1(_U_ uint32_t h, _U_ char *a){return 1;}\n\n"
@@ -220,7 +235,7 @@ static void build(strhash *H, int hno, int hlen){
fprintf(source, fns, H[i].fname, H[i].str, H[i].hash);
}
}
fprintf(header, "%s", fproto);
fprintf(header, "%s", headercontent);
fprintf(source, "%s\n", hashsources[hno]);
fprintf(source, "%s", fhdr);
for(int i = 0; i < hlen; ++i){

10
stringHash4MCU_/run Executable file
View File

@@ -0,0 +1,10 @@
#!/bin/bash
gcc hashgen.c -o hashgen -lusefull_macros
./hashgen -d testdic -H hdr.h -S hdr.c -F
gcc hdr.c test.c strfunc.c -o test
./test "time 54 = some"
./test "voltage12 more"
./test "esw45 = some text"
./test "goto 55 = "
./test "stop 3256"

256
stringHash4MCU_/strfunc.c Normal file
View File

@@ -0,0 +1,256 @@
#include "strfunc.h"
/**
* @brief hexdump - dump hex array by 16 bytes in string
* @param sendfun - function to send data
* @param arr - array to dump
* @param len - length of `arr`
*/
void hexdump(int (*sendfun)(const char *s), uint8_t *arr, uint16_t len){
char buf[52], *bptr = buf;
for(uint16_t l = 0; l < len; ++l, ++arr){
for(int16_t j = 1; j > -1; --j){
register uint8_t half = (*arr >> (4*j)) & 0x0f;
if(half < 10) *bptr++ = half + '0';
else *bptr++ = half - 10 + 'a';
}
if(l % 16 == 15){
*bptr++ = '\n';
*bptr = 0;
sendfun(buf);
bptr = buf;
}else *bptr++ = ' ';
}
if(bptr != buf){
*bptr++ = '\n';
*bptr = 0;
sendfun(buf);
}
}
/**
* @brief _2str - convert value into string buffer
* @param val - |value|
* @param minus - ==0 if value > 0
* @return buffer with number
*/
static char *_2str(uint32_t val, uint8_t minus){
static char strbuf[12];
char *bufptr = &strbuf[11];
*bufptr = 0;
if(!val){
*(--bufptr) = '0';
}else{
while(val){
uint32_t x = val / 10;
*(--bufptr) = (val - 10*x) + '0';
val = x;
//*(--bufptr) = val % 10 + '0';
//val /= 10;
}
}
if(minus) *(--bufptr) = '-';
return bufptr;
}
// return string with number `val`
char *u2str(uint32_t val){
return _2str(val, 0);
}
char *i2str(int32_t i){
uint8_t minus = 0;
uint32_t val;
if(i < 0){
minus = 1;
val = -i;
}else val = i;
return _2str(val, minus);
}
/**
* @brief uhex2str - print 32bit unsigned int as hex
* @param val - value
* @return string with number
*/
char *uhex2str(uint32_t val){
static char buf[12] = "0x";
int npos = 2;
uint8_t *ptr = (uint8_t*)&val + 3;
int8_t i, j, z=1;
for(i = 0; i < 4; ++i, --ptr){
if(*ptr == 0){ // omit leading zeros
if(i == 3) z = 0;
if(z) continue;
}
else z = 0;
for(j = 1; j > -1; --j){
uint8_t half = (*ptr >> (4*j)) & 0x0f;
if(half < 10) buf[npos++] = half + '0';
else buf[npos++] = half - 10 + 'a';
}
}
buf[npos] = 0;
return buf;
}
/**
* @brief omit_spaces - eliminate leading spaces and other trash in string
* @param buf - string
* @return - pointer to first character in `buf` > ' '
*/
const char *omit_spaces(const char *buf){
while(*buf){
if(*buf > ' ') break;
++buf;
}
return buf;
}
/**
* @brief getdec - read decimal number & return pointer to next non-number symbol
* @param buf - string
* @param N - number read
* @return Next non-number symbol. In case of overflow return `buf` and N==0xffffffff
*/
static const char *getdec(const char *buf, uint32_t *N){
char *start = (char*)buf;
uint32_t num = 0;
while(*buf){
char c = *buf;
if(c < '0' || c > '9'){
break;
}
if(num > 429496729 || (num == 429496729 && c > '5')){ // overflow
*N = 0xffffff;
return start;
}
num *= 10;
num += c - '0';
++buf;
}
*N = num;
return buf;
}
// read hexadecimal number (without 0x prefix!)
static const char *gethex(const char *buf, uint32_t *N){
const char *start = buf;
uint32_t num = 0;
while(*buf){
char c = *buf;
uint8_t M = 0;
if(c >= '0' && c <= '9'){
M = '0';
}else if(c >= 'A' && c <= 'F'){
M = 'A' - 10;
}else if(c >= 'a' && c <= 'f'){
M = 'a' - 10;
}
if(M){
if(num & 0xf0000000){ // overflow
*N = 0xffffff;
return start;
}
num <<= 4;
num += c - M;
}else{
break;
}
++buf;
}
*N = num;
return buf;
}
// read octal number (without 0 prefix!)
static const char *getoct(const char *buf, uint32_t *N){
const char *start = (char*)buf;
uint32_t num = 0;
while(*buf){
char c = *buf;
if(c < '0' || c > '7'){
break;
}
if(num & 0xe0000000){ // overflow
*N = 0xffffff;
return start;
}
num <<= 3;
num += c - '0';
++buf;
}
*N = num;
return buf;
}
// read binary number (without b prefix!)
static const char *getbin(const char *buf, uint32_t *N){
const char *start = (char*)buf;
uint32_t num = 0;
while(*buf){
char c = *buf;
if(c < '0' || c > '1'){
break;
}
if(num & 0x80000000){ // overflow
*N = 0xffffff;
return start;
}
num <<= 1;
if(c == '1') num |= 1;
++buf;
}
*N = num;
return buf;
}
/**
* @brief getnum - read uint32_t from string (dec, hex or bin: 127, 0x7f, 0b1111111)
* @param buf - buffer with number and so on
* @param N - the number read
* @return pointer to first non-number symbol in buf
* (if it is == buf, there's no number or if *N==0xffffffff there was overflow)
*/
const char *getnum(const char *txt, uint32_t *N){
const char *nxt = NULL;
const char *s = omit_spaces(txt);
if(*s == '0'){ // hex, oct or 0
if(s[1] == 'x' || s[1] == 'X'){ // hex
nxt = gethex(s+2, N);
if(nxt == s+2) nxt = (char*)txt;
}else if(s[1] > '0'-1 && s[1] < '8'){ // oct
nxt = getoct(s+1, N);
if(nxt == s+1) nxt = (char*)txt;
}else{ // 0
nxt = s+1;
*N = 0;
}
}else if(*s == 'b' || *s == 'B'){
nxt = getbin(s+1, N);
if(nxt == s+1) nxt = (char*)txt;
}else{
nxt = getdec(s, N);
if(nxt == s) nxt = (char*)txt;
}
return nxt;
}
// get signed integer
const char *getint(const char *txt, int32_t *I){
const char *s = omit_spaces(txt);
int32_t sign = 1;
uint32_t U;
if(*s == '-'){
sign = -1;
++s;
}
const char *nxt = getnum(s, &U);
if(nxt == s) return txt;
if(U & 0x80000000) return txt; // overfull
*I = sign * (int32_t)U;
return nxt;
}
/*
void mymemcpy(char *dest, const char *src, int len){
if(len < 1) return;
while(len--) *dest++ = *src++;
}
*/

13
stringHash4MCU_/strfunc.h Normal file
View File

@@ -0,0 +1,13 @@
#pragma once
#include <stdint.h>
#include <string.h>
void hexdump(int (*sendfun)(const char *s), uint8_t *arr, uint16_t len);
char *u2str(uint32_t val);
char *i2str(int32_t i);
char *uhex2str(uint32_t val);
const char *getnum(const char *txt, uint32_t *N);
const char *omit_spaces(const char *buf);
const char *getint(const char *txt, int32_t *I);
//void mymemcpy(char *dest, const char *src, int len);

View File

@@ -2,22 +2,61 @@
#include <string.h>
#include <stdint.h>
#include "hash.h"
#include "hdr.h"
#include "strfunc.h"
int fn_hello(uint32_t hash, char *args){
printf("HELLO! Hash=%u, param=%s\n", hash, args);
static int noargs(uint32_t hash){
switch(hash){
case CMD_REBOOT: printf("REBOOT\n"); break;
case CMD_TIME: printf("TIME!\n"); break;
case CMD_TEMP: printf("Temp\n"); break;
default: printf("Unknown hash 0x%x\n", hash); return 0;
}
return 1;
}
int fn_world(uint32_t hash, char *args){
printf("WORLD: %u - %s\n", hash, args);
return 1;
static int withparno(uint32_t hash, char *args){
uint32_t N;
//printf("args=%s\n", args);
const char *nxt = getnum((const char*)args, &N);
if(nxt == args){ printf("need arg\n"); return RET_WRONGCMD; }
args = (char*)omit_spaces(nxt);
if(*args){
if(*args != '='){ printf("need nothing or '=' after par\n"); return RET_WRONGCMD; }
args = (char*)omit_spaces(args+1);
// we can check in next functions what if `args` will be an empty string
}
else args = NULL;
const char *fname = NULL;
switch(hash){
case CMD_ESW: fname = "ESW"; break;
case CMD_GOTO: fname = "GOTO"; break;
case CMD_POS: fname = "POS"; break;
case CMD_STOP: fname = "STOP"; break;
default: fname = "unknown";
}
if(!args) printf("We want a getter '%s' with par %u\n", fname, N);
else printf("We want a setter '%s' with par %u and arg '%s'\n", fname, N, args);
if(N > 255){ printf("N should be 0..255\n"); return RET_BAD;}
return RET_GOOD;
}
// these functions should be global to substitute weak aliases
int fn_esw(uint32_t hash, char *args){return withparno(hash, args);}
int fn_goto(uint32_t hash, char *args){return withparno(hash, args);}
int fn_pos(uint32_t hash, char *args){return withparno(hash, args);}
int fn_stop(uint32_t hash, char *args){return withparno(hash, args);}
int fn_voltage(uint32_t hash, char *args){return withparno(hash, args);}
int fn_reboot(uint32_t hash, _U_ char *args){return noargs(hash);}
int fn_time(uint32_t hash, _U_ char *args){return noargs(hash);}
int fn_temp(uint32_t hash, _U_ char *args){return noargs(hash);}
int main(int argc, char **argv){
if(argc < 2) return 1;
char *args = "";
if(argc > 2) args = argv[2];
if(!parsecmd(argv[1], args)) printf("%s not found\n", argv[1]);
else printf("All OK\n");
if(argc != 2) return 1;
int ret = parsecmd(argv[1]);
switch(ret){
case RET_CMDNOTFOUND: printf("'%s' not found\n", argv[1]); break;
case RET_WRONGCMD: printf("Wrong sintax of '%s'\n", argv[1]); break;
case RET_GOOD: printf("'%s' - OK\n", argv[1]); break;
default: printf("'%s' - bad\n", argv[1]);
}
return 0;
}

View File

@@ -1,13 +1,8 @@
hello
world
what
put
sim key
change ip
change param
set
clear
reset
get
out
in
pos
temp
voltage
time
reboot
stop
goto
esw