lookup goes both ways now

This commit is contained in:
stacksmith 2022-03-07 17:04:22 -05:00
parent 6648a467e6
commit 16fef45617
4 changed files with 46 additions and 9 deletions

15
db.c
View File

@ -121,6 +121,21 @@ void sys_close(){
Given a URL, return an idx of the interned URL.
******************************************************************************/
U32 URL_check(char* str){
U32 idx = string_hash(str);
// Linear-probe
char buf[256];
U32 off;
while((off=map[idx])){ // as long as a map exists for hash,
log_read(off,buf); // look at what it refers to, and
if(!strcmp(str,buf)) // see if it is our needle. If so,
return idx; // we already have it, return it.
idx = (idx+1) & IDX_MASK; // otherwise, linear-probe next.
}
return 0;
}
U32 URL_idx(char* str){
U32 idx = string_hash(str);

1
db.h
View File

@ -3,4 +3,5 @@ void sys_close();
void idx_sigil(U32 idx,char*sigil);
U32 sigil_idx(char* sigil);
U32 URL_idx(char* str);
U32 URL_check(char* str);
U32 idx_URL(U32 idx,char* buf);

View File

@ -91,7 +91,8 @@ render_head - Render a header line, # ## or ###
******************************************************************************/
void render_head(FILE* out, char*p){
int level;
for(level=0;*p=='#';p++,level++);
char *q = p;
for(level=0;*q=='#';q++,level++);
switch(level){
case 1: fprintf(out,"\033[93m");break;
case 2: fprintf(out,"\033[94;1m");break;

View File

@ -5,28 +5,48 @@
FILE* errlog;
#undef ERRLOG
int doit(char* req){
int sigil(char* req){
char buf[1024];
U32 idx = sigil_idx(req);
int ret = idx_URL(idx,buf);
if(ret) {
printf("[%s](@%X)%s\n",req,idx*4,buf);
printf("[%s](@%X) %s\n",req,idx*4,buf);
return 0;
} else {
printf("Not Found\n");
return 1;
}
}
int main(int argc,char*argv[]){
if(strlen(argv[1])!=4) {
printf("Usage: lookup XXXX<enter>, where XXXX is a 4-char sigil\n");
int url(char* url){
printf("[%s]\n",url);
U32 idx = URL_check(url);
if(idx){
char sigil[5];
idx_sigil(idx,sigil);
printf("%s\n",sigil);
return 0;
} else {
printf("Not Found\n");
return 1;
}
}
int main(int argc,char*argv[]){
if(argc>2) {
printf("Usage: lookup XXXX<enter>, where XXXX is a 4-char sigil\n");
printf(" or lookup <url><enter>\n");
return 1;
}
int ret;
sys_open();
int ret = doit(argv[1]);
if(4==strlen(argv[1])) {
ret = sigil(argv[1]);
} else {
ret = url(argv[1]);
}
sys_close();
return ret;
}