xaizek / hstr (License: Apachev2) (since 2018-12-07)
Bash and Zsh shell history suggest box - easily view, navigate, search and manage your command history.
Commit f530fe0dba2703936760015cf429f9ea8b2d3591

Unique favorites & hash/hh memory cleanup implemented - no obvious problem present.
Author: Martin Dvorak
Author date (UTC): 2014-05-16 16:51
Committer name: Martin Dvorak
Committer date (UTC): 2014-05-16 16:51
Parent(s): f4e012e48d4206f574fce4848faed708186b5d68
Signing key:
Tree: acba8259e80ddba49bbcc1087f68a8b1fab9b9ff
File Lines added Lines deleted
dist/1-dist.sh 1 0
dist/debian-make-distro.sh 1 1
src/hashset.c 52 28
src/hstr.c 9 30
src/hstr_favorites.c 10 6
src/hstr_history.c 1 0
src/hstr_regexp.c 1 1
src/include/hashset.h 2 1
tests/src/test_hashset.c 12 2
File dist/1-dist.sh changed (mode: 100755) (index c730637..56ec294)
... ... rm -rvf autom*te.cache
11 11
12 12 if [ -d /usr/share/automake-1.11 ] if [ -d /usr/share/automake-1.11 ]
13 13 then then
14 rm -vf depcomp install-sh missing
14 15 cp -vf /usr/share/automake-1.11/depcomp . cp -vf /usr/share/automake-1.11/depcomp .
15 16 cp -vf /usr/share/automake-1.11/install-sh . cp -vf /usr/share/automake-1.11/install-sh .
16 17 cp -vf /usr/share/automake-1.11/missing . cp -vf /usr/share/automake-1.11/missing .
File dist/debian-make-distro.sh changed (mode: 100755) (index 3d02240..53b0ee3)
1 1 #!/bin/bash #!/bin/bash
2 2
3 . /home/dvorka/p/hstr/github/hstr/dist/debian-env.sh
3 . ~/p/hstr/github/hstr/dist/debian-env.sh
4 4
5 5 rm -rvf *.*~ ./debian rm -rvf *.*~ ./debian
6 6
File src/hashset.c changed (mode: 100644) (index b3f837b..bf32eec)
8 8 */ */
9 9
10 10 #include "include/hashset.h" #include "include/hashset.h"
11 #include "include/hstr_utils.h"
11 12
12 13 unsigned int hashmap_hash(const char *str) unsigned int hashmap_hash(const char *str)
13 14 { {
14 15 int i; int i;
15 unsigned int result = 5381;
16 unsigned int result=5381;
16 17
17 for( i = 0; str[ i ] != '\0'; i++ )
18 result = result * 33 + str[ i ];
19
20 result = result ^ (result >> 16);
18 for(i=0; str[i]!='\0'; i++) {
19 result=result*33+str[i];
20 }
21 result = result^(result>>16);
21 22
22 return result % HASH_MAP_SIZE;
23 return result%HASH_MAP_SIZE;
23 24 } }
24 25
25 26 void hashset_init(HashSet * hs) void hashset_init(HashSet * hs)
 
... ... void hashset_stat(const HashSet *hs)
84 85 { {
85 86 int i; int i;
86 87 struct HashSetNode *ptr; struct HashSetNode *ptr;
87
88 for( i = 0; i < HASH_MAP_SIZE; i++ )
89 for( ptr = hs->lists[ i ]; ptr != NULL; ptr = ptr->next )
90 printf( "%s\n", ptr->key );
88 for(i=0; i<HASH_MAP_SIZE; i++) {
89 for(ptr=hs->lists[i]; ptr!=NULL; ptr=ptr->next) {
90 printf("%s\n",ptr->key);
91 }
92 }
91 93 } }
92 94
93 95 char** hashset_keys(const HashSet *hs) char** hashset_keys(const HashSet *hs)
94 96 { {
95 // TODO to be implemented
96
97 char **result=malloc(sizeof(char*) * hs->currentSize);
98 int i=0, j=0;
99 for(i=0; i<HASH_MAP_SIZE; i++) {
100 if(hs->lists[i]) {
101 while(hs->lists[i]->next) {
102 printf(">>> %s",hs->lists[i]->key);
103 result[j++]=hs->lists[i]->key;
104 }
105 result[j++]=hs->lists[i]->key;
106 }
107 }
108 return NULL;
97 if(hs->currentSize) {
98 char **result=malloc(sizeof(char*) * hs->currentSize);
99 int i=0, j=0;
100 struct HashSetNode *p;
101 for(i=0; i<HASH_MAP_SIZE; i++) {
102 p=hs->lists[i];
103 while(p && p->next) {
104 result[j++]=hstr_strdup(p->key);
105 p=p->next;
106 }
107 if(p) {
108 result[j++]=hstr_strdup(p->key);
109 }
110 }
111 return result;
112 } else {
113 return NULL;
114 }
109 115 } }
110 116
111 void hashset_destroy(HashSet *hs)
117 void hashset_destroy(HashSet *hs, const bool freeValues)
112 118 { {
113 // TODO to be implemented
114 if(hs) {
115 free(hs);
119 // only hashset keys (and possibly values) are freed - caller must free hashset itself
120 if(hs && hs->currentSize) {
121 int i=0;
122 struct HashSetNode *p, *pp;
123 for(i=0; i<HASH_MAP_SIZE; i++) {
124 p=hs->lists[i];
125 while(p && p->next) {
126 if(p->key) {
127 free(p->key);
128 if(freeValues && p->value) free(p->value);
129 }
130 pp=p;
131 p=p->next;
132 free(pp);
133 }
134 if(p && p->key) {
135 free(p->key);
136 if(freeValues && p->value) free(p->value);
137 free(p);
138 }
139 }
116 140 } }
117 141 } }
File src/hstr.c changed (mode: 100644) (index 0edd472..6e1a46d)
... ... void hstr_getopt(int argc, char **argv, Hstr *hstr)
1007 1007
1008 1008 int main(int argc, char *argv[]) int main(int argc, char *argv[])
1009 1009 { {
1010 const char* commandBlacklist[] = { };
1011 HashSet blacklist;
1012 int i;
1013 hashset_init(&blacklist);
1014 for (i = 0; i < 5; i++) {
1015 hashset_add(&blacklist, commandBlacklist[i]);
1016 }
1017 for (i = 0; i < 5; i++) {
1018 printf("match %d\n", hashset_contains(&blacklist, hstr_strdup(commandBlacklist[i])));
1019 }
1020
1021 char **keys=hashset_keys(&blacklist);
1022 if(keys) {
1023 for(i=0; i<hashset_size(&blacklist); i++) {
1024 printf("\nKey: %s", keys[i]);
1025 }
1026 }
1027
1010 hstr=malloc(sizeof(Hstr));
1028 1011
1012 hstr_init(hstr);
1013 hstr_get_env_configuration(hstr);
1014 hstr_getopt(argc, argv, hstr);
1015 hstr_init_favorites(hstr);
1016 hstr_main(hstr);
1029 1017
1018 favorites_destroy(hstr->favorites);
1019 free(hstr);
1030 1020
1031 // hstr=malloc(sizeof(Hstr));
1032 //
1033 // hstr_init(hstr);
1034 // hstr_get_env_configuration(hstr);
1035 // hstr_getopt(argc, argv, hstr);
1036 // hstr_init_favorites(hstr);
1037 // hstr_main(hstr);
1038 //
1039 // favorites_destroy(hstr->favorites);
1040 // free(hstr);
1041 //
1042 // return EXIT_SUCCESS;
1021 return EXIT_SUCCESS;
1043 1022 } }
File src/hstr_favorites.c changed (mode: 100644) (index 8d06d21..0526ce6)
... ... void favorites_get(FavoriteItems *favorites)
74 74 } }
75 75
76 76 favorites->items = malloc(sizeof(char*) * favorites->count); favorites->items = malloc(sizeof(char*) * favorites->count);
77 int i = 0;
78 char *pb=file_contents, *pe;
77 favorites->count=0;
78 char *pb=file_contents, *pe, *s;
79 79 pe=strchr(file_contents, '\n'); pe=strchr(file_contents, '\n');
80 HashSet set;
81 hashset_init(&set);
80 82 while(pe!=NULL) { while(pe!=NULL) {
81 favorites->items[i]=pb;
82 83 *pe=0; *pe=0;
83 favorites->items[i]=hstr_strdup(pb);
84 if(!hashset_contains(&set,pb)) {
85 s=hstr_strdup(pb);
86 favorites->items[favorites->count++]=s;
87 hashset_add(&set,s);
88 }
84 89 pb=pe+1; pb=pe+1;
85 90 pe=strchr(pb, '\n'); pe=strchr(pb, '\n');
86 i++;
87 91 } }
88 92 free(file_contents); free(file_contents);
89 93 } }
 
... ... void favorites_destroy(FavoriteItems *favorites)
187 191 for(i=0; i<favorites->count; i++) { for(i=0; i<favorites->count; i++) {
188 192 free(favorites->items[i]); free(favorites->items[i]);
189 193 } }
194 hashset_destroy(&favorites->set, false);
190 195 free(favorites); free(favorites);
191 hashset_destroy(favorites->set);
192 196 } }
193 197 } }
File src/hstr_history.c changed (mode: 100644) (index 079cdd4..35655db)
... ... HistoryItems *get_prioritized_history()
149 149 } }
150 150
151 151 radixsort_destroy(&rs); radixsort_destroy(&rs);
152 // TODO rankmap (?) and blacklist (?) to be destroyed
152 153
153 154 return prioritizedHistory; return prioritizedHistory;
154 155 } else { } else {
File src/hstr_regexp.c changed (mode: 100644) (index 5ac2bec..06e9029)
... ... bool hstr_regexp_match(
58 58
59 59 void hstr_regexp_destroy(HstrRegexp *hstrRegexp) void hstr_regexp_destroy(HstrRegexp *hstrRegexp)
60 60 { {
61 // TODO hashset_destroy();
61 hashset_destroy(&hstrRegexp->cache, true);
62 62 } }
File src/include/hashset.h changed (mode: 100644) (index e7e5715..f9a9717)
13 13 #include <stdlib.h> #include <stdlib.h>
14 14 #include <stdio.h> #include <stdio.h>
15 15 #include <string.h> #include <string.h>
16 #include <stdbool.h>
16 17
17 18 #define HASH_MAP_SIZE 10007 #define HASH_MAP_SIZE 10007
18 19
 
... ... int hashset_put(HashSet *hm, const char *key, void *value);
39 40 int hashset_remove(const HashSet *hm, const char *key); int hashset_remove(const HashSet *hm, const char *key);
40 41 void hashset_stat(const HashSet *hm); void hashset_stat(const HashSet *hm);
41 42
42 void hashset_destroy(HashSet *hs);
43 void hashset_destroy(HashSet *hs, const bool freeValues);
43 44
44 45 #endif #endif
File tests/src/test_hashset.c changed (mode: 100644) (index 6c14162..ec42edd)
11 11 #include "../../src/include/hstr_utils.h" #include "../../src/include/hstr_utils.h"
12 12
13 13 void testBlacklist() { void testBlacklist() {
14 const char* commandBlacklist[] = { };
14 const char* commandBlacklist[] = { "a","b","c","d","e" };
15 15 HashSet blacklist; HashSet blacklist;
16 16 int i; int i;
17 17 hashset_init(&blacklist); hashset_init(&blacklist);
 
... ... void testBlacklist() {
21 21 for (i = 0; i < 5; i++) { for (i = 0; i < 5; i++) {
22 22 printf("match %d\n", hashset_contains(&blacklist, hstr_strdup(commandBlacklist[i]))); printf("match %d\n", hashset_contains(&blacklist, hstr_strdup(commandBlacklist[i])));
23 23 } }
24 }
25
26 void testGetKeys() {
27 const char* commandBlacklist[] = { "a","b","c","d","e" };
28 HashSet blacklist;
29 int i;
30 hashset_init(&blacklist);
31 for (i = 0; i < 5; i++) {
32 hashset_add(&blacklist, commandBlacklist[i]);
33 }
24 34
25 35 char **keys=hashset_keys(&blacklist); char **keys=hashset_keys(&blacklist);
26 36 if(keys) { if(keys) {
 
... ... void testBlacklist() {
32 42
33 43 int main(int argc, char *argv[]) int main(int argc, char *argv[])
34 44 { {
35 testBlacklist();
45 testGetKeys();
36 46 } }
Hints

Before first commit, do not forget to setup your git environment:
git config --global user.name "your_name_here"
git config --global user.email "your@email_here"

Clone this repository using HTTP(S):
git clone https://code.reversed.top/user/xaizek/hstr

Clone this repository using ssh (do not forget to upload a key first):
git clone ssh://rocketgit@code.reversed.top/user/xaizek/hstr

You are allowed to anonymously push to this repository.
This means that your pushed commits will automatically be transformed into a pull request:
... clone the repository ...
... make some changes and some commits ...
git push origin master