peterGraf / pbl

PBL is an MIT License open source C-language library of functions that can be used in a C or C++ project. PBL is highly portable and compiles warning free on Linux gcc, MAC OS X and Windows Microsoft Visual C++ 2010 Express Edition.
http://www.mission-base.com/peter/source/
MIT License
40 stars 12 forks source link

two different lines be regarded as same element #2

Closed goog closed 8 years ago

goog commented 8 years ago
// create new set
        PblSet *the_fresh = pblSetNewHashSet();

        while(fgets(path, PATH_MAX, fp)  != NULL)
        {

            if(regex_match(&reg_ssh_conn, path))
            {
                printf("-------------------\n");
                printf("we matched. %s and the path length %ld.\n", path, strlen(path));

                ret = pblSetAdd(the_fresh, path);
                printf("pblSetAdd return value %d.\n", ret);

            }

        }

        pclose(fp);
        // setprint
        pblSetPrint(stdout, the_fresh);

below is the log

-------------------
we matched. tcp        0      0 127.0.0.1:22            127.0.0.1:55648         ESTABLISHED -               
 and the path length 97.
pblSetAdd return value 1.
-------------------
we matched. tcp        0      0 127.0.0.1:55648         127.0.0.1:22            ESTABLISHED 2751/ssh        
 and the path length 97.
pblSetAdd return value 0.
# size 1
# capacity 8
# step size 3
# i 0, hashval 0, tcp6       1      0 ::1:33055               ::1:631                 CLOSE_WAIT  - 

Set element does not support the long string like "0 0 127.0.0.1:22 127.0.0.1:55648 ESTABLISHED -"?

peterGraf commented 8 years ago

You are adding the address of path twice to the set, therefore you will only have one element after the two additions. If you want to add char* strings to a hash set, you have to copy them to the heap yourself, pbl will not do that for you. Plus you should also set a char* string specific compare function to the hash set.

goog commented 8 years ago

thx