等字符串产生不同的哈希索引

时间:2017-09-09 11:46:52

标签: c string function pointers

我这里有一个程序可以复制一个内存文件系统(还没有完成),它必须从文件中读取它的命令,它们在这里非常自我解释:

create /foo
create /foo/bar
create /foo/baz
create /foo/baz/qux
write  /foo/bar "test"
read   /foo/bar
read   /foo/baz/qux
read   /foo/baz/quux
create /foo/bar
create /dir
create /bar
create /dir/bar
find bar
delete /foo/bar
find wat
find foo
read   /foo/bar
create /foo/bar
read   /foo/bar
delete_r /foo
exit

然后我有一个函数给定它操作它的字符串以在数组字符串中插入文件夹名称,命令是命令字符串,而fullPath字符串由另一个使用先前创建的字符串数组合的函数给出一个新的。这是结构和操作结构:

typedef struct _command {
    unsigned char command[10];
    unsigned char path[255][255];
    unsigned char* fullPath;
    int pathLevels;
} command;

这是实现树状结构的节点结构:

typedef struct _node {
    int isRoot;
    int isDir;
    char* message;
    int childNumber;
    struct _node* childNodes[1024];
    unsigned char fullPath[MAX_LEN_PATH];
    unsigned char resName[255];
} node;

操作字符串的函数:

command* createCommandMul(unsigned char* str) {
    unsigned char* c = str;
    command* commandPointer = (command*) malloc(sizeof(command));
    //commandPointer->path[0][0] = '/';
    //commandPointer->path[0][1] = '\0';
    int commandIndex = 0;
    int pathLevel = 0;
    int pathIndex = 0;
    /* Parte Comando */
    while(*c != ' ' && commandIndex < 10) {
        commandPointer->command[commandIndex] = *c++;
        commandIndex++;
    }
    while(commandIndex<10) {
        commandPointer->command[commandIndex] = '\0';
        commandIndex++;
    }
    while(*c == ' ' || *c == '/') c++; 
    /* Parte Path*/
    while(*c != '\0') {
        if (*c == '/') {
            commandPointer->path[pathLevel][pathIndex] = '\0';
            pathLevel++;
            pathIndex = 0;
            c++;
        } else {
            commandPointer->path[pathLevel][pathIndex] = *c++;
            pathIndex++;
        }
    }
    commandPointer->path[pathLevel][pathIndex] = '\0';
    commandPointer->pathLevels = pathLevel;
    return commandPointer;
}

我有一个createDir函数,它检查传递给函数的节点*是dir还是root(想象一下它有一棵树); 如果它是它创建节点。

int createDir(node* fatherOfChildToCreate, unsigned char* fullPath, command* currentCommand) {
    if ((fatherOfChildToCreate->isRoot == 1 || fatherOfChildToCreate->isDir == 1) && fatherOfChildToCreate->childNumber < 1024) {
        node* dirToCreate = (node*) malloc(sizeof(node));
        command* comando = (command*) currentCommand;
        dirToCreate->isDir = 1;
        dirToCreate->isRoot = 0;
        dirToCreate->message = NULL;
        dirToCreate->childNumber = 0;
        strcmp(dirToCreate->fullPath, fullPath);
        for (int i = 0; i < 1024; i++) dirToCreate->childNodes[i] = NULL;
        int index = (int) hashCalc(comando->path[comando->pathLevels]);
        printf("Hash di %s = %d", comando->path[comando->pathLevels], index);
        fatherOfChildToCreate->childNodes[index] = dirToCreate;
        fatherOfChildToCreate->childNumber += 1;
        return 1;
    } else return 0;
}

请注意,创建此createDir函数的目的是创建node* fatherOfChildToCreate的直接subDir,因此基本上文本文件的第一个命令使用此函数创建/foo,因为它只有parentDir是根{1},它是在main()中创建的。 第二个命令将在下面使用此函数搜索/foo目录,因为它是/foo/bar的父目录,指针将被传递给createDir函数,该函数将创建childNode/foo目录。

node* linearSearchUpper(node* rootNode, unsigned char* upperPath, command* currentCommand) {
    command* comandoSearch = (command*) currentCommand;
    node* curr = (node*) rootNode;
    int counter = comandoSearch->pathLevels;
    int index;
    unsigned char* upperName = comandoSearch->path[comandoSearch->pathLevels - 1];
    for (int i = 0; i < counter; i++) {
        index = (int) hashCalc(comandoSearch->path[i]);
        printf("Hash di %s = %d", comandoSearch->path[i], index);
        if (curr->childNodes[index] == NULL) return NULL;
        else curr = curr->childNodes[index];
    }
    if (strcmp(upperPath, curr->fullPath) == 1) return curr;
}

在所有这些中,我使用这个哈希函数来搜索parentDir并在node->childNodes[]数组中插入一个新元素

unsigned long hashCalc(unsigned char* str) {
    unsigned long hash = 5381;
    int c;
    while (c = *str++)
        hash = ((hash << 5) + hash) + c; /* hash * 33 + c */
    return hash % 1024;
}

现在,我将粘贴main(),这是最后一个要审核的功能。

int main() {
    node* rootNode = (node*) createRoot();
    command* comando = (command*) malloc(sizeof(command));
    unsigned char* upPath = NULL;
    unsigned char* allPath = NULL;
    unsigned char* line = NULL;
    FILE* fp;
    size_t len = 0;
    ssize_t read;

    fp = fopen("/Users/mattiarighetti/Downloads/semplice.txt", "r");
    if (fp == NULL)
        exit(EXIT_FAILURE);
    while ((read = getline(&line, &len, fp)) != -1) {
        if (*line == 'f') {
            //comandoFind = createCommandFind(line);
        } if (*line == 'w') {
            //comandoWrite = createCommandWrite(line);
        } if (*line == 'c') {
            comando = createCommandMul(line);
            upPath = upperPath(comando);
            allPath = fullPath(comando);
            if (comando->pathLevels == 0) {
                if (createDir(rootNode, allPath, comando) == 1) printf("ok\n\n");
                else printf("no\n\n");
            } else {
                node* upperNode = (node*) linearSearchUpper(rootNode, upPath, comando);
                if (upperNode == NULL) {
                    printf("no\n\n");
                }
                else {
                    if (createDir(upperNode, allPath, comando) == 1) printf("ok\n\n");
                    else printf("no\n\n");
                }
            }
        }
    }
    fclose(fp);
    if (line)
        free(line);
    return 0;
}

所以,这样做是从文件中逐行读取,创建并填充命令结构,然后创建一个upPath,它是父(要发现)和fullPath。我得到的问题是该程序使用createDir作为此文本文件的第一行,这没关系,但由于某些奇怪的原因在foo中读取comando->path[I],哈希函数给了我179这是不正确的。继续,第二行使用linearSearchUpper()来搜索父文件夹/foo,因此它给出了comando-&gt; path [I],这又是foo但这次是hashCalc给了我905,这应该是正确的答案,所以最终linearSearchUpper找不到/ foo文件夹,因为它在索引905中不存在。每次我使用create命令或create_dir文件夹时都会发生这种情况是root用户的孩子,所以像/ foo,/ dir,/ bar这样的dirs会给我一个奇怪的哈希索引。

你知道为什么会这样吗?

1 个答案:

答案 0 :(得分:1)

我没有尝试理解你的整个程序,但是你得到不同哈希的字符串确实是不同的:其中一个字符串最后保留了新行字符,可能来自fgets。< / p>

ASCII中换行符的numerc值为10,所以:

hash("foo") == 905;
hash("foo\n") == (33 * hash("foo") + '\n') % 1024
              == (33 * 905 + 10) % 1024
              == 179

解决方案是从fgets收到的字符串中删除尾随空格,或者使用更好的标记,这样可以保证您的标记没有前导空格或尾随空格。