Exemplo n.º 1
0
void
d_sort(distance **d, int size)
{
  int i=0, j=0;
  rep(i,size)
    tper(j,size-1,i)
      if ( d_compare(d[j],d[j-1]) > 0 ) d_swap(d[j-1],d[j]);
}
Exemplo n.º 2
0
int main(int argc, char *argv[]) {
  char *prgm = *argv;
  argv++; argc--;

  dictionary_t dictionary;

  if(argc < 1) {
    printf("Usage: %s [options]\n",prgm);
    printf("Use any combination of the following:\n");
    printf("  -i: Initialize the dictionary\n");
    printf("  -a [key] [value]: Add a key/value to the dictionary\n");
    printf("  -p [string]: Parse a string into the dictionary\n");
    printf("  -g [key]: Get a value by its key from the dictionary\n");
    printf("  -r [key]: Remove a key from the dictionary\n");
    printf("  -d: Destroy the dictionary\n");
  }

  char *key, *value, *string;

  while(argc > 0) {
    if(strcmp(*argv,"-i") == 0) { //Initialize
      printf("Running init()\n");
      dictionary_init(&dictionary);
      printf("  completed\n");
    } else if(strcmp(*argv,"-a") == 0) {
      if(argc < 3) {
	printf("Not enough parameters to -a\n");
	return 2;
      }
      key = mstrcpy(argv[1]);
      value = mstrcpy(argv[2]);
      printf("Running add(\"%s\",\"%s\")\n",argv[1],argv[2]);
      d_retint(dictionary_add(&dictionary, key, value));
      d_compare("key",argv[1],key);
      d_compare("value",argv[2],value);
      argv += 2; argc -= 2;
    } else if(strcmp(*argv,"-p") == 0) {
      if(argc < 2) {
	printf("Not enough parameters to -p\n");
	return 2;
      }
      string = mstrcpy(argv[1]);
      printf("Running parse(\"%s\")\n",argv[1]);
      d_retint(dictionary_parse(&dictionary, string));
      d_compare("string",argv[1],string);
      argv += 1; argc -= 1;
    } else if(strcmp(*argv,"-g") == 0) {
      if(argc < 2) {
	printf("Not enough parameters to -g\n");
	return 2;
      }
      key = mstrcpy(argv[1]);
      printf("Running get(\"%s\")\n",argv[1]);
      d_retstr(dictionary_get(&dictionary, key));
      d_compare("key",argv[1],key);
      argv += 1; argc -= 1;
    } else if(strcmp(*argv,"-r") == 0) {
      if(argc < 2) {
	printf("Not enough parameters to -r\n");
	return 2;
      }
      key = mstrcpy(argv[1]);
      printf("Running remove(\"%s\")\n",argv[1]);
      d_retint(dictionary_remove(&dictionary, key));
      d_compare("remove",argv[1],key);
      argv += 1; argc -= 1;
    } else if(strcmp(*argv,"-d") == 0) {
      printf("Running destroy()\n");
      dictionary_destroy(&dictionary);
      printf("  completed\n");
    } else {
      printf("Did not understand parameter: %s\n",*argv);
      return 3;
    }
    argv++; argc--;
  }
}
Exemplo n.º 3
0
extern int p_dictionary_compare_hooker(void *left, void *right) {
	struct o_object *object_left = (struct o_object *)left, *object_right = (struct o_object *)right;
	return d_compare(object_left, object_right);
}