⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 testsfx.c

📁 后缀数存储算法
💻 C
字号:
/* * example using the suffix tree library * * this example shows both how to store a new entry and how to retrieve it * usage: testsfx -i file.sft INDEX data *        to store a new entry (file.sft will be created or appended if *                              already existing) *        INDEX is the key and has to be upper case here as the sfxdisk.h *        only has upper case characters in the SThashtable * *        testsfx -l file.sft INDEX *        to retrieve the data * *        testsfx -d file.sfx INDEX data *        to delete the INDEX/data pair */#include <stdio.h>#include <stdlib.h>#include "sfxdisk.h"enum{  ACT_INSERT, ACT_READ, ACT_DELETE};int main(int argc, char *argv[]){  STFile *fichier;  int action;  offset_t pos;  char *data = NULL;  int tdata;  if (argc < 3)  {    fprintf(stderr, "syntax: %s [-i][-l][-d] datafile.sft key [data]\n",            argv[0]);    return(1);  }  if (strcmp(argv[1], "-l") == 0)    action = ACT_READ;  else if (strcmp(argv[1], "-i") == 0)    action = ACT_INSERT;  else if (strcmp(argv[1], "-d") == 0)    action = ACT_DELETE;  else  {    fprintf(stderr, "unknown command\n");    return(1);  }  switch (action)  {    case ACT_READ:      if ((fichier = STopen(argv[2], ST_RDONLY)) == NULL)      {        perror(argv[2]);        return(1);      }      /*       * first step now that we have the file descriptor is to look for       * the key with the STlookup() function, argv[3] being our key.       * we retrieve the starting position, that will be necessary to read       * our data with STreaddata, in pos       * if STlookup() fails, it returns 0       */      if (!STlookup(fichier, argv[3], &pos))      {        return(2);      }      /*       * each data node is linked, STreaddata() returns the position to the       * next node, or -1 is at the end       * data will be malloc()'d (realloc()'d actually) and tdata contains       * the size of the retrieved data. data must be set to NULL before       * calling the function (realloc() is used).       */      do      {	pos = STreaddata(fichier, pos, (void *)&data, &tdata);        printf("%s\n", data);      }      while (pos != -1);      break;    case ACT_INSERT:      if ((fichier = STopen(argv[2], ST_RDWRITE)) == NULL)      {	perror(argv[2]);	return(1);      }      /*       * now we insert with argv[3] as the key, argv[4] as data and the length       * of the string as 4th argument       */      STinsert(fichier, argv[3], argv[4], strlen(argv[4]) + 1);      /*       * as the library uses a custom cache, this function has to be called       * otherwise the library will be corrupted       */      STclose(fichier);      break;    case ACT_DELETE:      if ((fichier = STopen(argv[2], ST_RDWRITE)) == NULL)      {	perror(argv[2]);	return(1);      }      if (!STlookup(fichier, argv[3], &pos))      {        return(2);      }      do      {	pos = STreaddata(fichier, pos, (void *)&data, &tdata);        if (strcmp(data, argv[4]) == 0)          STdelete(fichier);      }      while (pos != -1);      STclose(fichier);      break;  }  return(0);}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -