mirror of
https://github.com/eddyem/eddys_snippets.git
synced 2026-03-20 08:41:02 +03:00
fixed, tested
This commit is contained in:
@@ -20,6 +20,8 @@
|
||||
#include <usefull_macros.h>
|
||||
#include <ctype.h>
|
||||
|
||||
// maximal string length (n*4) without terminating '\0' - for buffers
|
||||
#define MAXCMDLEN (128)
|
||||
#define ALLOCSZ (5000)
|
||||
|
||||
typedef struct{
|
||||
@@ -51,15 +53,25 @@ static void parse_args(int argc, char **argv){
|
||||
}
|
||||
}
|
||||
|
||||
#define HASHFNO (3)
|
||||
#define HASHFNO (4)
|
||||
static uint32_t my(const char *str){
|
||||
char buf[MAXCMDLEN+1];
|
||||
uint32_t hash = 0, *c = (uint32_t*)buf;
|
||||
bzero(buf, sizeof(buf));
|
||||
int len = strlen(str);
|
||||
memcpy(buf, str, len);
|
||||
len = (len + 3) / 4;
|
||||
for(int i = 0; i < len; ++i)
|
||||
hash = (hash << 7) | c[i];
|
||||
DBG("Hash for '%s' is %u", str, hash);
|
||||
return hash;
|
||||
}
|
||||
// djb2 & sdbm: http://www.cse.yorku.ca/~oz/hash.html
|
||||
static uint32_t djb2(const char *str){
|
||||
uint32_t hash = 5381;
|
||||
uint32_t c;
|
||||
while((c = (uint32_t)*str++))
|
||||
hash = ((hash << 7) + hash) + c;
|
||||
//hash = hash * 31 + c;
|
||||
//hash = hash * 33 + c;
|
||||
return hash;
|
||||
}
|
||||
static uint32_t sdbm(const char *str){
|
||||
@@ -84,6 +96,17 @@ static uint32_t jenkins(const char *str){
|
||||
}
|
||||
|
||||
static const char *hashsources[HASHFNO] = {
|
||||
"static uint32_t hashf(const char *str){\n\
|
||||
char buf[CMD_MAXLEN+1];\n\
|
||||
uint32_t hash = 0, *c = (uint32_t*)buf;\n\
|
||||
bzero(buf, sizeof(buf));\n\
|
||||
int len = strlen(str);\n\
|
||||
memcpy(buf, str, len);\n\
|
||||
len = (len + 3) / 4;\n\
|
||||
for(int i = 0; i < len; ++i)\n\
|
||||
hash = (hash << 7) | c[i];\n\
|
||||
return hash;\n\
|
||||
}\n",
|
||||
"static uint32_t hashf(const char *str){\n\
|
||||
uint32_t hash = 5381;\n\
|
||||
uint32_t c;\n\
|
||||
@@ -112,13 +135,14 @@ static const char *hashsources[HASHFNO] = {
|
||||
}\n"
|
||||
};
|
||||
|
||||
static uint32_t (*hash[HASHFNO])(const char *str) = {djb2, sdbm, jenkins};
|
||||
static const char *hashnames[HASHFNO] = {"DJB2", "SDBM", "Jenkins"};
|
||||
static uint32_t (*hash[HASHFNO])(const char *str) = {my, djb2, sdbm, jenkins};
|
||||
static const char *hashnames[HASHFNO] = {"My", "DJB2", "SDBM", "Jenkins"};
|
||||
|
||||
typedef struct{
|
||||
char str[32]; // string command
|
||||
char fname[32]; // function namee
|
||||
uint32_t hash; // command hash
|
||||
char str[MAXCMDLEN+1]; // string command define (capital letters)
|
||||
char fname[MAXCMDLEN+1]; // function name
|
||||
char macroname[MAXCMDLEN+1];// macro name
|
||||
uint32_t hash; // command hash
|
||||
} strhash;
|
||||
|
||||
static int sorthashesH(const void *a, const void *b){ // sort by hash
|
||||
@@ -143,11 +167,11 @@ static FILE *openoutp(const char *name){
|
||||
}
|
||||
|
||||
static char *macroname(const char *cmd){
|
||||
static char macro[32];
|
||||
static char macro[MAXCMDLEN+1];
|
||||
int i = 0;
|
||||
while(i < 31 && *cmd){
|
||||
while(i < MAXCMDLEN && *cmd){
|
||||
char c = *cmd++;
|
||||
if(!isalnum(c)) c = '_';
|
||||
if(!isalpha(c)) c = '_';
|
||||
if(islower(c)) c = toupper(c);
|
||||
macro[i++] = c;
|
||||
}
|
||||
@@ -156,9 +180,9 @@ static char *macroname(const char *cmd){
|
||||
}
|
||||
|
||||
static char *fnname(const char *cmd){
|
||||
static char fn[32];
|
||||
static char fn[MAXCMDLEN+1];
|
||||
int i = 0;
|
||||
while(i < 31 && *cmd){
|
||||
while(i < MAXCMDLEN && *cmd){
|
||||
char c = *cmd++;
|
||||
if(!isalpha(c)) c = '_';
|
||||
if(isupper(c)) c = tolower(c);
|
||||
@@ -170,14 +194,14 @@ static char *fnname(const char *cmd){
|
||||
|
||||
static const char *fhdr =
|
||||
"int parsecmd(const char *str){\n\
|
||||
if(!str || !*str) return RET_CMDNOTFOUND;\n\
|
||||
int i = 0;\n\
|
||||
while(*str > '@' && i < CMD_MAXLEN){ lastcmd[i++] = *str++; }\n\
|
||||
lastcmd[i] = 0;\n\
|
||||
while(*str && *str <= ' ') ++str;\n\
|
||||
char *args = (char*) str;\n\
|
||||
uint32_t h = hashf(lastcmd);\n\
|
||||
switch(h){\n\n"
|
||||
if(!str || !*str) return RET_CMDNOTFOUND;\n\
|
||||
bzero(lastcmd, sizeof(lastcmd));\n\
|
||||
int i = 0;\n\
|
||||
while(*str > '@' && i < CMD_MAXLEN){ lastcmd[i++] = *str++; }\n\
|
||||
while(*str && *str <= ' ') ++str;\n\
|
||||
char *args = (char*) str;\n\
|
||||
uint32_t h = hashf(lastcmd);\n\
|
||||
switch(h){\n"
|
||||
;
|
||||
static const char *ffooter =
|
||||
" default: break;\n\
|
||||
@@ -186,7 +210,10 @@ static const char *ffooter =
|
||||
}\n\n"
|
||||
;
|
||||
static const char *fns =
|
||||
"int fn_%s(uint32_t _U_ hash, char _U_ *args) WAL; // \"%s\" (%u)\n\n"
|
||||
"int fn_%s(uint32_t _U_ hash, char _U_ *args) WAL; // \"%s\" (%u)\n"
|
||||
;
|
||||
static const char *fnsh =
|
||||
"int fn_%s(uint32_t, char*); // \"%s\" (%u)\n"
|
||||
;
|
||||
static const char *headercontent =
|
||||
"// Generated by HASHGEN (https://github.com/eddyem/eddys_snippets/tree/master/stringHash4MCU_)\n\
|
||||
@@ -195,7 +222,7 @@ static const char *headercontent =
|
||||
#ifndef _U_\n\
|
||||
#define _U_ __attribute__((__unused__))\n\
|
||||
#endif\n\n\
|
||||
#define CMD_MAXLEN (32)\n\n\
|
||||
#define CMD_MAXLEN (%d)\n\n\
|
||||
enum{\n\
|
||||
RET_HELP = -3,\n\
|
||||
RET_CMDNOTFOUND = -2,\n\
|
||||
@@ -215,10 +242,11 @@ static const char *srchdr =
|
||||
// Licensed by GPLv3\n\
|
||||
#include <stdint.h>\n\
|
||||
#include <stddef.h>\n\
|
||||
#include <string.h>\n\
|
||||
#include \"%s\"\n\n\
|
||||
#ifndef WAL\n\
|
||||
#define WAL __attribute__ ((weak, alias (\"__f1\")))\n\
|
||||
#endif\n\nstatic int __f1(uint32_t _U_ h, char _U_ *a){return 1;}\n\n\
|
||||
#endif\n\nstatic int __f1(uint32_t _U_ h, char _U_ *a){return RET_BAD;}\n\n\
|
||||
char lastcmd[CMD_MAXLEN + 1];\n\n"
|
||||
;
|
||||
|
||||
@@ -226,13 +254,14 @@ static void build(strhash *H, int hno, int hlen){
|
||||
green("Generate files for hash function '%s'\n", hashnames[hno]);
|
||||
int lmax = 1;
|
||||
for(int i = 0; i < hlen; ++i){
|
||||
strcpy(H[i].macroname, macroname(H[i].str));
|
||||
int l = strlen(H[i].str);
|
||||
if(l > lmax){
|
||||
lmax = l;
|
||||
}
|
||||
}
|
||||
lmax = (lmax + 3)/4;
|
||||
lmax *= 4;
|
||||
lmax *= 4; // round by 4 bytes
|
||||
// resort H by strings
|
||||
qsort(H, hlen, sizeof(strhash), sorthashesS);
|
||||
FILE *source = openoutp(G.sourcefile), *header = openoutp(G.headerfile);
|
||||
@@ -242,22 +271,26 @@ static void build(strhash *H, int hno, int hlen){
|
||||
fprintf(source, fns, H[i].fname, H[i].str, H[i].hash);
|
||||
}
|
||||
}
|
||||
fprintf(header, "%s", headercontent);
|
||||
fprintf(source, "%s\n", hashsources[hno]);
|
||||
fprintf(header, headercontent, lmax);
|
||||
fprintf(source, "\n%s\n", hashsources[hno]);
|
||||
fprintf(source, "%s", fhdr);
|
||||
for(int i = 0; i < hlen; ++i){
|
||||
char *m = macroname(H[i].str);
|
||||
fprintf(source, sw, m, H[i].fname);
|
||||
fprintf(header, "#define CMD_%-*s (%u)\n", lmax, m, H[i].hash);
|
||||
fprintf(source, sw, H[i].macroname, H[i].fname);
|
||||
fprintf(header, "#define CMD_%-*s (%u)\n", lmax, H[i].macroname, H[i].hash);
|
||||
}
|
||||
fprintf(source, "%s", ffooter);
|
||||
fclose(source);
|
||||
fprintf(header, "\n\n");
|
||||
fprintf(header, "\n");
|
||||
for(int i = 0; i < hlen; ++i){
|
||||
char *m = macroname(H[i].str);
|
||||
fprintf(header, "#define STR_%-*s \"%s\"\n", lmax, m, H[i].str);
|
||||
fprintf(header, "#define STR_%-*s \"%s\"\n", lmax, H[i].macroname, H[i].str);
|
||||
}
|
||||
|
||||
if(G.genfunc){
|
||||
fprintf(header, "\n");
|
||||
for(int i = 0; i < hlen; ++i){
|
||||
fprintf(header, fnsh, H[i].fname, H[i].str, H[i].hash);
|
||||
}
|
||||
}
|
||||
|
||||
fclose(header);
|
||||
}
|
||||
|
||||
@@ -280,19 +313,26 @@ int main(int argc, char **argv){
|
||||
}
|
||||
while(*word && *word < 33) ++word;
|
||||
if(!*word) break;
|
||||
char *nxt = strchr(word, '\n');
|
||||
char *eol = strchr(word, '\n');
|
||||
char *sp = strchr(word, ' ');
|
||||
char *nxt = eol;
|
||||
if(sp){ // got space after command - truncate
|
||||
if(eol){
|
||||
if(eol > sp) nxt = sp; // space before strend
|
||||
}else nxt = sp; // no strend, but have space
|
||||
};
|
||||
if(nxt){
|
||||
int len = nxt - word;
|
||||
if(len > 31) len = 31;
|
||||
if(len > MAXCMDLEN) len = MAXCMDLEN;
|
||||
strncpy(H[idx].str, word, len);
|
||||
H[idx].str[len] = 0;
|
||||
}else{
|
||||
snprintf(H[idx].str, 31, "%s", word);
|
||||
snprintf(H[idx].str, MAXCMDLEN, "%s", word);
|
||||
}
|
||||
snprintf(H[idx].fname, 31, "%s", fnname(H[idx].str));
|
||||
snprintf(H[idx].fname, MAXCMDLEN, "%s", fnname(H[idx].str));
|
||||
++idx;
|
||||
if(!nxt) break;
|
||||
word = nxt + 1;
|
||||
if(!eol) break;
|
||||
word = eol + 1;
|
||||
}
|
||||
// test fname matches
|
||||
qsort(H, idx, sizeof(strhash), sorthashesF);
|
||||
|
||||
Reference in New Issue
Block a user