Пример #1
0
static void testExport() {
	Preferences * preferences;
	HashTable * hashTable;
	DataValue * value;
	
	preferences = Preferences_create();
	
	Preferences_set(preferences, "a", valueCreateBoolean(false));
	Preferences_set(preferences, "b", valueCreateInt8(1));
	
	hashTable = Preferences_export(preferences);
	TestCase_assert(hashTable != NULL, "Expected non-NULL but got NULL");
	TestCase_assert(hashTable->count == 2, "Expected 2 but got " SIZE_T_FORMAT, hashTable->count);
	
	value = hashGet(hashTable, "a");
	TestCase_assert(value != NULL, "Expected non-NULL but got NULL");
	TestCase_assert(value->type == DATA_TYPE_BOOLEAN, "Expected %d but got %d", DATA_TYPE_BOOLEAN, value->type);
	TestCase_assert(!value->value.boolean, "Expected false but got true");
	
	value = hashGet(hashTable, "b");
	TestCase_assert(value != NULL, "Expected non-NULL but got NULL");
	TestCase_assert(value->type == DATA_TYPE_INT8, "Expected %d but got %d", DATA_TYPE_INT8, value->type);
	TestCase_assert(value->value.int8 == 1, "Expected 1 but got %d", value->value.int8);
	hashDispose(hashTable);
	
	Preferences_set(preferences, "b", valueCreateBoolean(true));
	Preferences_set(preferences, "c", valueCreateUInt8(3));
	
	hashTable = Preferences_export(preferences);
	TestCase_assert(hashTable != NULL, "Expected non-NULL but got NULL");
	TestCase_assert(hashTable->count == 3, "Expected 3 but got " SIZE_T_FORMAT, hashTable->count);
	
	value = hashGet(hashTable, "a");
	TestCase_assert(value != NULL, "Expected non-NULL but got NULL");
	TestCase_assert(value->type == DATA_TYPE_BOOLEAN, "Expected %d but got %d", DATA_TYPE_BOOLEAN, value->type);
	TestCase_assert(!value->value.boolean, "Expected false but got true");
	
	value = hashGet(hashTable, "b");
	TestCase_assert(value != NULL, "Expected non-NULL but got NULL");
	TestCase_assert(value->type == DATA_TYPE_BOOLEAN, "Expected %d but got %d", DATA_TYPE_BOOLEAN, value->type);
	TestCase_assert(value->value.boolean, "Expected true but got false");
	
	value = hashGet(hashTable, "c");
	TestCase_assert(value != NULL, "Expected non-NULL but got NULL");
	TestCase_assert(value->type == DATA_TYPE_UINT8, "Expected %d but got %d", DATA_TYPE_UINT8, value->type);
	TestCase_assert(value->value.uint8 == 3, "Expected 3 but got %u", value->value.uint8);
	hashDispose(hashTable);
	
	Preferences_dispose(preferences);
}
Пример #2
0
void extenderDoMenu(SOCKET_DATA *sock, OLC_EXTENDER *ext, void *data) {
  LIST           *keys = hashCollect(ext->opt_hash);
  char            *key = NULL;
  OLC_EXT_DATA  *edata = NULL;

  // display each menu item alphabetically
  listSortWith(keys, strcasecmp);
  LIST_ITERATOR *key_i = newListIterator(keys);
  ITERATE_LIST(key, key_i) {
    // display the menu option
    send_to_socket(sock, "{g%s) ", key);

    // then display the information
    edata = hashGet(ext->opt_hash, key);
    if(edata->type == OLCEXT_C)
      edata->menu(sock, data);
    else if(ext->borrow_py != NULL) {
      PyObject *ret = PyObject_CallFunction(edata->pymenu, "OO", 
					    socketGetPyFormBorrowed(sock), 
					    ext->borrow_py(data));
      if(ret == NULL)
	log_pyerr("Error running Python OLC exention menu function: %s", key);
      Py_XDECREF(ret);
    }
  } deleteListIterator(key_i);
Пример #3
0
void Preferences_import(Preferences * self, HashTable * hashTable) {
	const char ** keys;
	size_t keyCount = 0, keyIndex;
	
	keys = hashGetKeys(hashTable, &keyCount);
	for (keyIndex = 0; keyIndex < keyCount; keyIndex++) {
		Preferences_set(self, keys[keyIndex], *hashGet(hashTable, keys[keyIndex]));
	}
	free(keys);
}
Пример #4
0
void *findInTrie(char *key, triePo trie) {
  if (trie == NULL)
    return NULL;
  else if ((*key) == '\0')
    return trie->value;
  else if (trie->follows != NULL) {
    long K = *key;
    triePo next = (triePo) hashGet(trie->follows, (void *) K);
    if (next != NULL)
      return findInTrie(key + 1, next);
    else
      return NULL;
  } else
    return NULL;
}
Пример #5
0
static PXMLNode xmlCreateChild(void* Pool,PXMLNode x,pchar Name)
{
  PXMLNode c;
  PHashLink l;
  if(x->hChildren)
  {
    c=hashGet(x->hChildren,Name);
    if(c)return c;
  }
  c=xmlNewChild(Pool,x,xmlEmpty);
  c->pNext=x->pChildren;
  x->pChildren=c;
  if(x->hChildren==NULL)x->hChildren=hashNew(Pool);
  l=hashAdd(x->hChildren,Name,c);
  c->szName=l->szKey;
  return c;
}
Пример #6
0
void Preferences_set(Preferences * self, const char * key, DataValue value) {
	struct PreferencesEvent event;
	DataValue * previousValue;
	
	event.key = key;
	event.value = value;
	previousValue = hashGet(self->private_ivar(hashTable), key);
	if (previousValue == NULL) {
		memset(&event.previousValue, 0, sizeof(event.previousValue));
		event.previousValue.type = value.type;
	} else {
		event.previousValue = *previousValue;
	}
	EventDispatcher_dispatchEvent(self->eventDispatcher, ATOM(PREFERENCES_EVENT_VALUE_CHANGED), &event);
	
	hashSet(self->private_ivar(hashTable), key, valueCopy(&event.value));
}
Пример #7
0
static void addToTr(char *full, char *key, void *value, triePo trie) {
  if (*key == '\0') {
    trie->value = value;
  } else {
    if (trie->follows == NULL) {
      trie->follows = NewHash(6, charHash, charComp, NULL);
    }
    long K = *key;
    triePo follows = (triePo) hashGet(trie->follows, (void *) K);
    if (follows == NULL) {
      char *prefix = (char *) calloc((key - full + 2), sizeof(byte));
      for (int ix = 0; ix < key - full + 1; ix++)
        prefix[ix] = full[ix];
      follows = emptyTr(strdup(prefix));
      hashPut(trie->follows, (void *) K, follows);
    }
    addToTr(full, key + 1, value, follows);
  }
}
Пример #8
0
PXMLNode xmlGetItem(PXMLNode x,pchar szPath)
{
  pchar s;
  if(!x)return NULL;
  if(*szPath=='/')
  {
    while(x->pParent)x=x->pParent;
    szPath++;
  }
  for(;;)
  {
    s=xstrchr(szPath,'/');
    if(!s)return hashGet(x->hChildren,szPath);
    //*s=0;
    x=hashGetEx(x->hChildren,szPath,s-szPath);
    //*s='/';
    if(!x)return NULL;
    szPath=s+1;
  }
}
Пример #9
0
PXMLNode xmlGetItem(PXMLNode x,const wchar_t *szPath)
{
  const wchar_t *s;
  if(!x)return NULL;
  if(*szPath==L'/')
  {
    while(x->pParent)x=x->pParent;
    szPath++;
  }
  for(;;)
  {
    s=xstrchr(szPath,L'/');
    if(!s)return (PXMLNode)hashGet(x->hChildren,szPath);
    //*s=0;
    x=(PXMLNode)hashGetEx(x->hChildren,szPath,(int)(s-szPath));
    //*s='/';
    if(!x)return NULL;
    szPath=s+1;
  }
}
Пример #10
0
int parser(char* str)
{
    char *cmdstr;
    char *newcmdstr;
    PFI  cmdfunc;

    cmdstr = strtok(str," ");
    cmdfunc = (PFI) hashGet(cmdtbl,cmdstr);
    if (cmdfunc != (PFI) -1L)
    {
	/* skip over the cmdstr, and pass this along 
           as the argument to the called function, this allows strtok_r
	   to work if used  */
	newcmdstr = cmdstr + strlen(cmdstr) + 1;
       (*cmdfunc)(newcmdstr);   /* was (*cmdfunc)(cmdstr); */
    }
    else
    {
       errLogRet(LOGOPT,debugInfo,
	  "parser: Cmd Str: '%s' is an unknown command\n",cmdstr);
       return(-1);
    }
    return(0);
}
void handleQuery(char *commandName, unsigned int key, unsigned int value, unsigned long responseAddress, unsigned int responsePort) {	 //, struct hashnode *table
	if(isSetQuery(commandName)) {						// -- process set cmd
		if(hashSet(table, key, value) == -1){
			strcpy(commandName, "err");
			// key = value = 0;
		}else{
			strcpy(commandName, "ok!");
		}
	} else if(isGetQuery(commandName)) {				// -- process get cmd
		int val = hashGet(table, key);

		if(val == -1){
			strcpy(commandName, "nof");
			// key = value = 0;
		}else{
			strcpy(commandName, "val");
			value = val;
		}
	} else if(isDelQuery(commandName)) {				// -- process del cmd
		if(hashDel(table, key) == -1){
			strcpy(commandName, "nof");
			// key = value = 0;
		}else{
			strcpy(commandName, "ok!");
		}
	} else {
		strcpy(commandName, "err");
		// key = value = 0;
	}
	
	printf("Table modification complete.\n");
	
	forwardQuery(commandName, key, value, responseAddress, responsePort);
	
	return;
}
Пример #12
0
int main(int argc, char const *argv[]) {

	// default to a table of 1,000 elements.
	size_t size = 1000;

	if (argc > 1) {
		size = atoi(argv[1]);
		if (!size) {
			// couldn't convert argv[1] to a number
			fprintf(stderr, USAGE);
			exit(EXIT_FAILURE);
		}
	}

	// create a new hash table
	Hash *h = hashCreate(size);

	if (!h) {
		// failed to create hash table
		fprintf(stderr, "failed to create hash table\n");
		exit(EXIT_FAILURE);
	} else {
		printf("Successfully created a hash table of size %zu.\n",size);
		FILE *opt;
		if ((opt = fopen(OPTIONS_FILE,"r"))) {
			char c;
			while((c = fgetc(opt)) != EOF) {
				putchar(c);
			}
			fclose(opt);
		} else {
			fprintf(stderr, "failed to open %s\n", OPTIONS_FILE);
			exit(EXIT_FAILURE);
		}
	}

	// create some variables
	char *command, *key, *obj, *res;
	
	bool hasSet,hasGet,onOwn;
	hasSet = hasGet = onOwn = false;

	bool keepGoing = true;

	while(keepGoing) {
		// initialize the variables and print the prompt
		command = key = obj = res = NULL;

		// encourage the user to try out the different commands
		if (!hasSet) {
			printf("%% (type 'set') ");
		} else if (!hasGet) {
			printf("%% (type 'get') ");
		} else if (!onOwn) {
			onOwn = true;
			printf("You're on your own now... available commands: set, get, delete, load\n");
			printf("%% ");
		} else {
			printf("%% ");
		}

		// read the command
		command = readString();

		// process a set command
		if (!strcasecmp(command,"set")) {
			hasSet = true;
			printf("\tkey: ");
			key = readString();

			printf("\tvalue: ");
			obj = readString();

			printf("\tresult: ");
			printf(hashSet(h,key,obj) ? "SUCCESS\n" : "FAILURE\n");

		// process a get command
		} else if (!strcasecmp(command,"get")) {
			hasGet = true;
			printf("\tkey: ");
			key = readString();
			if ((res = (char *) hashGet(h,key))){
				printf("\tresult: %s\n", res);
			} else {
				printf("\tresult: NULL\n");
			}

		// process a delete command
		} else if (!strcasecmp(command,"delete")) {
			printf("\tkey: ");
			key = readString();
			if ((res = (char *) hashDelete(h,key))){
				printf("\tresult: %s\n", res);
				free(res);
			} else {
				printf("\tresult: NULL\n");
			}

		// process a load command
		} else if (!strcasecmp(command,"load")) {
			printf("\tresult: %f\n", hashLoad(h));

		// let the user exit
		} else if (!strcasecmp(command,"exit") || !strcasecmp(command,"quit")) {
			keepGoing = false;			

		// invalid command
		} else if (strlen(command) > 0) {
			printf("invalid command\n");
		} 

		// free the command and key, as necessary
		if (command) { free(command); }
		if (key) { free(key); }

	}

	hashDestroy(h,destroy);
	return EXIT_SUCCESS;
}
Пример #13
0
static void testCreate() {
	DataValue value;
	char * string1 = "abc", * string2 = "foo";
	char blob1[] = {0x00, 0x01}, blob2[] = {0x03, 0x05, 0x07};
	HashTable * hashTable1, * hashTable2;
	DataArray * array1, * array2;
	AssociativeArray * assArray1, * assArray2;
	
	value = valueCreateBoolean(false);
	TestCase_assert(value.type == DATA_TYPE_BOOLEAN, "Expected %d but got %d", DATA_TYPE_BOOLEAN, value.type);
	TestCase_assert(!value.value.boolean, "Expected false but got true");
	valueDispose(&value);
	value = valueCreateBoolean(true);
	TestCase_assert(value.type == DATA_TYPE_BOOLEAN, "Expected %d but got %d", DATA_TYPE_BOOLEAN, value.type);
	TestCase_assert(value.value.boolean, "Expected true but got false");
	valueDispose(&value);
	
	value = valueCreateInt8(0);
	TestCase_assert(value.type == DATA_TYPE_INT8, "Expected %d but got %d", DATA_TYPE_INT8, value.type);
	TestCase_assert(value.value.int8 == 0, "Expected 0 but got %d", value.value.int8);
	valueDispose(&value);
	value = valueCreateInt8(1);
	TestCase_assert(value.type == DATA_TYPE_INT8, "Expected %d but got %d", DATA_TYPE_INT8, value.type);
	TestCase_assert(value.value.int8 == 1, "Expected 1 but got %d", value.value.int8);
	valueDispose(&value);
	
	value = valueCreateUInt8(0);
	TestCase_assert(value.type == DATA_TYPE_UINT8, "Expected %d but got %d", DATA_TYPE_UINT8, value.type);
	TestCase_assert(value.value.uint8 == 0, "Expected 0 but got %d", value.value.uint8);
	valueDispose(&value);
	value = valueCreateUInt8(1);
	TestCase_assert(value.type == DATA_TYPE_UINT8, "Expected %d but got %d", DATA_TYPE_UINT8, value.type);
	TestCase_assert(value.value.uint8 == 1, "Expected 1 but got %d", value.value.uint8);
	valueDispose(&value);
	
	value = valueCreateInt16(0);
	TestCase_assert(value.type == DATA_TYPE_INT16, "Expected %d but got %d", DATA_TYPE_INT16, value.type);
	TestCase_assert(value.value.int16 == 0, "Expected 0 but got %d", value.value.int16);
	valueDispose(&value);
	value = valueCreateInt16(1);
	TestCase_assert(value.type == DATA_TYPE_INT16, "Expected %d but got %d", DATA_TYPE_INT16, value.type);
	TestCase_assert(value.value.int16 == 1, "Expected 1 but got %d", value.value.int16);
	valueDispose(&value);
	
	value = valueCreateUInt16(0);
	TestCase_assert(value.type == DATA_TYPE_UINT16, "Expected %d but got %d", DATA_TYPE_UINT16, value.type);
	TestCase_assert(value.value.uint16 == 0, "Expected 0 but got %d", value.value.uint16);
	valueDispose(&value);
	value = valueCreateUInt16(1);
	TestCase_assert(value.type == DATA_TYPE_UINT16, "Expected %d but got %d", DATA_TYPE_UINT16, value.type);
	TestCase_assert(value.value.uint16 == 1, "Expected 1 but got %d", value.value.uint16);
	valueDispose(&value);
	
	value = valueCreateInt32(0);
	TestCase_assert(value.type == DATA_TYPE_INT32, "Expected %d but got %d", DATA_TYPE_INT32, value.type);
	TestCase_assert(value.value.int32 == 0, "Expected 0 but got %d", value.value.int32);
	valueDispose(&value);
	value = valueCreateInt32(1);
	TestCase_assert(value.type == DATA_TYPE_INT32, "Expected %d but got %d", DATA_TYPE_INT32, value.type);
	TestCase_assert(value.value.int32 == 1, "Expected 1 but got %d", value.value.int32);
	valueDispose(&value);
	
	value = valueCreateUInt32(0);
	TestCase_assert(value.type == DATA_TYPE_UINT32, "Expected %d but got %d", DATA_TYPE_UINT32, value.type);
	TestCase_assert(value.value.uint32 == 0, "Expected 0 but got %d", value.value.uint32);
	valueDispose(&value);
	value = valueCreateUInt32(1);
	TestCase_assert(value.type == DATA_TYPE_UINT32, "Expected %d but got %d", DATA_TYPE_UINT32, value.type);
	TestCase_assert(value.value.uint32 == 1, "Expected 1 but got %d", value.value.uint32);
	valueDispose(&value);
	
	value = valueCreateInt64(0);
	TestCase_assert(value.type == DATA_TYPE_INT64, "Expected %d but got %d", DATA_TYPE_INT64, value.type);
	TestCase_assert(value.value.int64 == 0, "Expected 0 but got " INT64_FORMAT, value.value.int64);
	valueDispose(&value);
	value = valueCreateInt64(1);
	TestCase_assert(value.type == DATA_TYPE_INT64, "Expected %d but got %d", DATA_TYPE_INT64, value.type);
	TestCase_assert(value.value.int64 == 1, "Expected 1 but got " INT64_FORMAT, value.value.int64);
	valueDispose(&value);
	
	value = valueCreateUInt64(0);
	TestCase_assert(value.type == DATA_TYPE_UINT64, "Expected %d but got %d", DATA_TYPE_UINT64, value.type);
	TestCase_assert(value.value.uint64 == 0, "Expected 0 but got " UINT64_FORMAT, value.value.uint64);
	valueDispose(&value);
	value = valueCreateUInt64(1);
	TestCase_assert(value.type == DATA_TYPE_UINT64, "Expected %d but got %d", DATA_TYPE_UINT64, value.type);
	TestCase_assert(value.value.uint64 == 1, "Expected 1 but got " UINT64_FORMAT, value.value.uint64);
	valueDispose(&value);
	
	value = valueCreateFloat(0.0f);
	TestCase_assert(value.type == DATA_TYPE_FLOAT, "Expected %d but got %d", DATA_TYPE_FLOAT, value.type);
	TestCase_assert(value.value.float32 == 0.0f, "Expected 0.0 but got %f", value.value.float32);
	valueDispose(&value);
	value = valueCreateFloat(1.0f);
	TestCase_assert(value.type == DATA_TYPE_FLOAT, "Expected %d but got %d", DATA_TYPE_FLOAT, value.type);
	TestCase_assert(value.value.float32 == 1.0f, "Expected 1.0 but got %f", value.value.float32);
	valueDispose(&value);
	
	value = valueCreateDouble(0.0);
	TestCase_assert(value.type == DATA_TYPE_DOUBLE, "Expected %d but got %d", DATA_TYPE_DOUBLE, value.type);
	TestCase_assert(value.value.float64 == 0.0, "Expected 0.0 but got %f", value.value.float64);
	valueDispose(&value);
	value = valueCreateDouble(1.0);
	TestCase_assert(value.type == DATA_TYPE_DOUBLE, "Expected %d but got %d", DATA_TYPE_DOUBLE, value.type);
	TestCase_assert(value.value.float64 == 1.0, "Expected 1.0 but got %f", value.value.float64);
	valueDispose(&value);
	
	value = valueCreateFixed16_16(0x00000);
	TestCase_assert(value.type == DATA_TYPE_FIXED_16_16, "Expected %d but got %d", DATA_TYPE_FIXED_16_16, value.type);
	TestCase_assert(value.value.fixed == 0x00000, "Expected 0x00000 but got 0x%05X", value.value.fixed);
	valueDispose(&value);
	value = valueCreateFixed16_16(0x10000);
	TestCase_assert(value.type == DATA_TYPE_FIXED_16_16, "Expected %d but got %d", DATA_TYPE_FIXED_16_16, value.type);
	TestCase_assert(value.value.fixed == 0x10000, "Expected 0x10000 but got 0x%05X", value.value.fixed);
	valueDispose(&value);
	
	value = valueCreatePointer((void *) 0x0);
	TestCase_assert(value.type == DATA_TYPE_POINTER, "Expected %d but got %d", DATA_TYPE_POINTER, value.type);
	TestCase_assert(value.value.pointer == (void *) 0x0, "Expected 0x0 but got %p", value.value.pointer);
	valueDispose(&value);
	value = valueCreatePointer((void *) 0x1);
	TestCase_assert(value.type == DATA_TYPE_POINTER, "Expected %d but got %d", DATA_TYPE_POINTER, value.type);
	TestCase_assert(value.value.pointer == (void *) 0x1, "Expected 0x1 but got %p", value.value.pointer);
	valueDispose(&value);
	
	value = valueCreateString(string1, 2, true, true);
	TestCase_assert(value.type == DATA_TYPE_STRING, "Expected %d but got %d", DATA_TYPE_STRING, value.type);
	TestCase_assert(!strcmp(value.value.string, "ab"), "Expected \"ab\" but got \"%s\"", value.value.string);
	TestCase_assert(value.value.string != string1, "Expected differing pointers, but both are %p", string1);
	valueDispose(&value);
	value = valueCreateString(string2, DATA_USE_STRLEN, false, false);
	TestCase_assert(value.type == DATA_TYPE_STRING, "Expected %d but got %d", DATA_TYPE_STRING, value.type);
	TestCase_assert(value.value.string == string2, "Expected %p but got %p", string2, value.value.string);
	valueDispose(&value);
	
	value = valueCreateBlob(blob1, sizeof(blob1), true, true);
	TestCase_assert(value.type == DATA_TYPE_BLOB, "Expected %d but got %d", DATA_TYPE_BLOB, value.type);
	TestCase_assert(value.value.blob.length == 2, "Expected 2 but got " SIZE_T_FORMAT, value.value.blob.length);
	TestCase_assert(!memcmp(value.value.blob.bytes, blob1, sizeof(blob1)), "Expected {0x%02X, 0x%02X} but got {0x%02X, 0x%02X}", blob1[0], blob1[1], ((char *) value.value.blob.bytes)[0], ((char *) value.value.blob.bytes)[1]);
	TestCase_assert(value.value.blob.bytes != blob1, "Expected differing pointers, but both are %p", blob1);
	valueDispose(&value);
	value = valueCreateBlob(blob2, sizeof(blob2), false, false);
	TestCase_assert(value.type == DATA_TYPE_BLOB, "Expected %d but got %d", DATA_TYPE_BLOB, value.type);
	TestCase_assert(value.value.blob.length == 3, "Expected 3 but got " SIZE_T_FORMAT, value.value.blob.length);
	TestCase_assert(value.value.blob.bytes == blob2, "Expected %p but got %p", blob2, value.value.blob.bytes);
	valueDispose(&value);
	
	hashTable1 = hashCreate();
	hashSet(hashTable1, "a", valueCreateBoolean(false));
	hashTable2 = hashCreate();
	value = valueCreateHashTable(hashTable1, true, true);
	TestCase_assert(value.type == DATA_TYPE_HASH_TABLE, "Expected %d but got %d", DATA_TYPE_HASH_TABLE, value.type);
	TestCase_assert(value.value.hashTable != hashTable1, "Expected differing pointers, but both are %p", hashTable1);
	TestCase_assert(value.value.hashTable->count == hashTable1->count, "Expected " SIZE_T_FORMAT " but got " SIZE_T_FORMAT, hashTable1->count, value.value.hashTable->count);
	TestCase_assert(hashGet(value.value.hashTable, "a") != NULL, "Expected non-NULL but got NULL");
	TestCase_assert(hashGet(value.value.hashTable, "a")->type == DATA_TYPE_BOOLEAN, "Expected %d but got %d", DATA_TYPE_BOOLEAN, hashGet(value.value.hashTable, "a")->type);
	TestCase_assert(!hashGet(value.value.hashTable, "a")->value.boolean, "Expected false but got true");
	valueDispose(&value);
	value = valueCreateHashTable(hashTable2, true, false);
	TestCase_assert(value.type == DATA_TYPE_HASH_TABLE, "Expected %d but got %d", DATA_TYPE_HASH_TABLE, value.type);
	TestCase_assert(value.value.hashTable == hashTable2, "Expected %p but got %p", hashTable2, value.value.hashTable);
	valueDispose(&value);
	
	array1 = arrayCreate();
	arrayAppend(array1, valueCreateBoolean(false));
	array2 = arrayCreate();
	value = valueCreateArray(array1, true, true);
	TestCase_assert(value.type == DATA_TYPE_ARRAY, "Expected %d but got %d", DATA_TYPE_ARRAY, value.type);
	TestCase_assert(value.value.array != array1, "Expected differing pointers, but both are %p", array1);
	TestCase_assert(value.value.array->count == array1->count, "Expected " SIZE_T_FORMAT " but got " SIZE_T_FORMAT, array1->count, value.value.array->count);
	TestCase_assert(arrayGet(value.value.array, 0) != NULL, "Expected non-NULL but got NULL");
	TestCase_assert(arrayGet(value.value.array, 0)->type == DATA_TYPE_BOOLEAN, "Expected %d but got %d", DATA_TYPE_BOOLEAN, arrayGet(value.value.array, 0)->type);
	TestCase_assert(!arrayGet(value.value.array, 0)->value.boolean, "Expected false but got true");
	valueDispose(&value);
	value = valueCreateArray(array2, true, false);
	TestCase_assert(value.type == DATA_TYPE_ARRAY, "Expected %d but got %d", DATA_TYPE_ARRAY, value.type);
	TestCase_assert(value.value.array == array2, "Expected %p but got %p", array2, value.value.array);
	valueDispose(&value);
	
	assArray1 = associativeArrayCreate();
	associativeArrayAppend(assArray1, "a", valueCreateBoolean(false));
	assArray2 = associativeArrayCreate();
	value = valueCreateAssociativeArray(assArray1, true, true);
	TestCase_assert(value.type == DATA_TYPE_ASSOCIATIVE_ARRAY, "Expected %d but got %d", DATA_TYPE_ASSOCIATIVE_ARRAY, value.type);
	TestCase_assert(value.value.associativeArray != assArray1, "Expected differing pointers, but both are %p", assArray1);
	TestCase_assert(value.value.associativeArray->count == assArray1->count, "Expected " SIZE_T_FORMAT " but got " SIZE_T_FORMAT, assArray1->count, value.value.associativeArray->count);
	TestCase_assert(associativeArrayGetValueAtIndex(value.value.associativeArray, 0) != NULL, "Expected non-NULL but got NULL");
	TestCase_assert(associativeArrayGetValueAtIndex(value.value.associativeArray, 0)->type == DATA_TYPE_BOOLEAN, "Expected %d but got %d", DATA_TYPE_BOOLEAN, associativeArrayGetValueAtIndex(value.value.associativeArray, 0)->type);
	TestCase_assert(!associativeArrayGetValueAtIndex(value.value.associativeArray, 0)->value.boolean, "Expected false but got true");
	TestCase_assert(!strcmp(associativeArrayGetKeyAtIndex(value.value.associativeArray, 0), "a"), "Expected \"a\" but got \"%s\"", associativeArrayGetKeyAtIndex(value.value.associativeArray, 0));
	valueDispose(&value);
	value = valueCreateAssociativeArray(assArray2, true, false);
	TestCase_assert(value.type == DATA_TYPE_ASSOCIATIVE_ARRAY, "Expected %d but got %d", DATA_TYPE_ASSOCIATIVE_ARRAY, value.type);
	TestCase_assert(value.value.associativeArray == assArray2, "Expected %p but got %p", assArray2, value.value.associativeArray);
	valueDispose(&value);
}
Пример #14
0
const char *raceGetAbbrev(const char *name) {
  RACE_DATA *data = hashGet(race_table, name);
  return (data ? data->abbrev : NULL);
}
Пример #15
0
bool raceIsForPC(const char *name) {
  RACE_DATA *data = hashGet(race_table, name);
  return (data ? data->pc_ok : FALSE);
}
Пример #16
0
BODY_DATA *raceCreateBody(const char *name) {
  RACE_DATA *data = hashGet(race_table, name);
  return (data ? bodyCopy(data->body) : NULL);
}
Пример #17
0
bool isRace(const char *name) {
  return (hashGet(race_table, name) != NULL);
}
/**
 * A client has requested the given url using the given method
 * (#MHD_HTTP_METHOD_GET, #MHD_HTTP_METHOD_PUT,
 * #MHD_HTTP_METHOD_DELETE, #MHD_HTTP_METHOD_POST, etc).  The callback
 * must call MHD callbacks to provide content to give back to the
 * client and return an HTTP status code (i.e. #MHD_HTTP_OK,
 * #MHD_HTTP_NOT_FOUND, etc.).
 *
 * @param cls argument given together with the function
 *        pointer when the handler was registered with MHD
 * @param url the requested url
 * @param method the HTTP method used (#MHD_HTTP_METHOD_GET,
 *        #MHD_HTTP_METHOD_PUT, etc.)
 * @param version the HTTP version string (i.e.
 *        #MHD_HTTP_VERSION_1_1)
 * @param upload_data the data being uploaded (excluding HEADERS,
 *        for a POST that fits into memory and that is encoded
 *        with a supported encoding, the POST data will NOT be
 *        given in upload_data and is instead available as
 *        part of #MHD_get_connection_values; very large POST
 *        data *will* be made available incrementally in
 *        @a upload_data)
 * @param upload_data_size set initially to the size of the
 *        @a upload_data provided; the method must update this
 *        value to the number of bytes NOT processed;
 * @param con_cls pointer that the callback can set to some
 *        address and that will be preserved by MHD for future
 *        calls for this request; since the access handler may
 *        be called many times (i.e., for a PUT/POST operation
 *        with plenty of upload data) this allows the application
 *        to easily associate some request-specific state.
 *        If necessary, this state can be cleaned up in the
 *        global #MHD_RequestCompletedCallback (which
 *        can be set with the #MHD_OPTION_NOTIFY_COMPLETED).
 *        Initially, `*con_cls` will be NULL.
 * @return #MHD_YES if the connection was handled successfully,
 *         #MHD_NO if the socket must be closed due to a serios
 *         error while handling the request
 */
static int
answer_to_connection(void *cls, struct MHD_Connection *connection,
                     const char *url, const char *method, const char *version, const char *upload_data, size_t * upload_data_size, void **con_cls)
{
    if (*con_cls == NULL) {
        printf("CON_CLS is NULL. Request-type: %s\n", method);

        struct connection_info_struct *con_info;

        con_info = malloc(sizeof(struct connection_info_struct));
        if (con_info == NULL) {
            return MHD_NO;
        }
        con_info->answerstring = NULL;

        if (strcmp(method, "POST") == 0) {
            con_info->postprocessor = MHD_create_post_processor(connection, POSTBUFFERSIZE, iterate_post, (void *) con_info);

            if (con_info->postprocessor == NULL) {
                free(con_info);
                return MHD_NO;
            }

            con_info->connectiontype = POST;
        } else if (strcmp(method, "DELETE") == 0) {
            con_info->connectiontype = DELETE;
        } else {
            con_info->connectiontype = GET;
        }

        *con_cls = (void *) con_info;

        return MHD_YES;
    }

    printf("CON_CLS is NOT NULL.\n");

    if (0 == strcmp(method, "GET")) {
        char modifiableUrl [strlen(url) + 1];
        strncpy(modifiableUrl, url, sizeof modifiableUrl);
        modifiableUrl[strlen(url)] = '\0';
        char * argument = strtok(modifiableUrl, "/"); // remove api name
        argument = strtok(NULL, "/");
        int parsedArgument = atoi(argument);

        int responseValue = hashGet(parsedArgument);

        if (responseValue >= 0) {
          char buffer[100]; // totally arbitrary but pretty big
          sprintf(buffer, "200 - OK\nValue: %d\n", responseValue);
          return send_page(connection, MHD_HTTP_OK, buffer);
        } else if (responseValue == NOT_FOUND) {
          return send_page(connection, 404, "404 - Not found\nThere are no entries matching your key.\n");
        } else {
          return send_page(connection, 500, "500 - Internal server error\n");
        }
    }

    if (0 == strcmp(method, "DELETE")) {
        printf("DELETE REQUEST to %s >>>\n", url);
        char modifiableUrl [strlen(url) + 1];
        strncpy(modifiableUrl, url, sizeof modifiableUrl);
        modifiableUrl[strlen(url)] = '\0';
        char * argument = strtok(modifiableUrl, "/"); // remove api name
        argument = strtok(NULL, "/");
        int parsedArgument = atoi(argument);
        printf("Parsed as %d from %s\n", parsedArgument, argument);

        int responseValue = hashDel(parsedArgument);
        printf("Result: %d\n", responseValue);
        if (responseValue >= 0) {
          return send_page(connection, 200, "200 - OK\n");
        } else if (responseValue == NOT_FOUND) {
          return send_page(connection, 404, "404 - Not found\nThere are no entries matching your key.\n");
        } else {
          return send_page(connection, 500, "500 - Internal server error\n");
        }
    }

    if (0 == strcmp(method, "POST")) {
        struct connection_info_struct *con_info = *con_cls;

        if (*upload_data_size != 0) {
            MHD_post_process(con_info->postprocessor, upload_data, *upload_data_size);

            *upload_data_size = 0;

            return MHD_YES;
        } else if (keySet && valueSet) {
            keySet = 0;
            valueSet = 0;

            printf("Set %d:%d!\n", key, value);
            if (hashSet(key, value)) {
              const char *responseText = "201 - Created\n";

              struct MHD_Response *response = MHD_create_response_from_buffer(strlen(responseText), (void*) responseText, MHD_RESPMEM_PERSISTENT);

              char buffer[100]; // totally arbitrary but pretty big
              sprintf(buffer, "http://localhost:%d%s/%d", port, url, key);

              MHD_add_response_header (response, "Location", buffer);
              int ret = MHD_queue_response (connection, 201, response);
              MHD_destroy_response(response);

              return MHD_YES;
            } else {
              return send_page(connection, 500, "500 - Internal server error\n");
            }
        } else {
            return send_page(connection, MHD_HTTP_BAD_REQUEST, "400 - Malformed request\n");
        }
    }

    return send_page(connection, MHD_HTTP_NOT_FOUND, "404 - Not found\n");
}
Пример #19
0
DataValue * Preferences_get(Preferences * self, const char * key) {
	return hashGet(self->private_ivar(hashTable), key);
}
Пример #20
0
Файл: mmgs1.c Проект: XL64/mmg
/* analyze triangles and split if needed */
static int anaelt(pMesh mesh,pSol met,char typchk) {
    pTria    pt;
    pPoint   ppt,p1,p2;
    Hash     hash;
    Bezier   pb;
    pGeom    go;
    double   s,o[3],no[3],to[3],dd,len;
    int      vx[3],i,j,ip,ip1,ip2,ier,k,ns,nc,nt;
    char     i1,i2;
    static double uv[3][2] = { {0.5,0.5}, {0.,0.5}, {0.5,0.} };

    hashNew(&hash,mesh->np);
    ns = 0;
    s  = 0.5;
    for (k=1; k<=mesh->nt; k++) {
        pt = &mesh->tria[k];
        if ( !MS_EOK(pt) || pt->ref < 0 )  continue;
        if ( MS_SIN(pt->tag[0]) || MS_SIN(pt->tag[1]) || MS_SIN(pt->tag[2]) )  continue;

        /* check element cut */
        pt->flag = 0;
        if ( typchk == 1 ) {
            if ( !chkedg(mesh,k) )  continue;
        }
        else if ( typchk == 2 ) {
            for (i=0; i<3; i++) {
                i1 = inxt[i];
                i2 = iprv[i];
                len = lenedg(mesh,met,pt->v[i1],pt->v[i2],0);
                if ( len > LLONG )  MS_SET(pt->flag,i);
            }
            if ( !pt->flag )  continue;
        }
        ns++;

        /* geometric support */
        ier = bezierCP(mesh,k,&pb);
        assert(ier);

        /* scan edges to split */
        for (i=0; i<3; i++) {
            if ( !MS_GET(pt->flag,i) )  continue;
            i1  = inxt[i];
            i2  = iprv[i];
            ip1 = pt->v[i1];
            ip2 = pt->v[i2];
            ip = hashGet(&hash,ip1,ip2);
            if ( !MS_EDG(pt->tag[i]) && ip > 0 )  continue;

            /* new point along edge */
            ier = bezierInt(&pb,uv[i],o,no,to);
            if ( !ip ) {
                ip = newPt(mesh,o,MS_EDG(pt->tag[i]) ? to : no);
                assert(ip);
                hashEdge(&hash,ip1,ip2,ip);
                p1  = &mesh->point[ip1];
                p2  = &mesh->point[ip2];
                ppt = &mesh->point[ip];

                if ( MS_EDG(pt->tag[i]) ) {
                    ++mesh->ng;
                    assert(mesh->ng < mesh->ngmax);
                    ppt->tag = pt->tag[i];
                    if ( p1->ref == pt->edg[i] || p2->ref == pt->edg[i] )
                        ppt->ref = pt->edg[i];
                    ppt->ig  = mesh->ng;
                    go = &mesh->geom[mesh->ng];
                    memcpy(go->n1,no,3*sizeof(double));

                    dd = go->n1[0]*ppt->n[0] + go->n1[1]*ppt->n[1] + go->n1[2]*ppt->n[2];
                    ppt->n[0] -= dd*go->n1[0];
                    ppt->n[1] -= dd*go->n1[1];
                    ppt->n[2] -= dd*go->n1[2];
                    dd = ppt->n[0]*ppt->n[0] + ppt->n[1]*ppt->n[1] + ppt->n[2]*ppt->n[2];
                    if ( dd > EPSD2 ) {
                        dd = 1.0 / sqrt(dd);
                        ppt->n[0] *= dd;
                        ppt->n[1] *= dd;
                        ppt->n[2] *= dd;
                    }
                }
                if ( met->m ) {
                    if ( typchk == 1 )
                        intmet33(mesh,met,ip1,ip2,ip,s);
                    else
                        intmet(mesh,met,k,i,ip,s);
                }
            }
            else if ( pt->tag[i] & MS_GEO ) {
                ppt = &mesh->point[ip];
                go  = &mesh->geom[ppt->ig];
                memcpy(go->n2,no,3*sizeof(double));

                /* a computation of the tangent with respect to these two normals is possible */
                ppt->n[0] = go->n1[1]*go->n2[2] - go->n1[2]*go->n2[1];
                ppt->n[1] = go->n1[2]*go->n2[0] - go->n1[0]*go->n2[2];
                ppt->n[2] = go->n1[0]*go->n2[1] - go->n1[1]*go->n2[0];
                dd = ppt->n[0]*ppt->n[0] + ppt->n[1]*ppt->n[1] + ppt->n[2]*ppt->n[2];
                if ( dd > EPSD2 ) {
                    dd = 1.0 / sqrt(dd);
                    ppt->n[0] *= dd;
                    ppt->n[1] *= dd;
                    ppt->n[2] *= dd;
                }
            }
        }
    }
    if ( !ns ) {
        free(hash.item);
        return(ns);
    }

    /* step 2. checking if split by adjacent */
    for (k=1; k<=mesh->nt; k++) {
        pt = &mesh->tria[k];
        if ( !MS_EOK(pt) || pt->ref < 0 )  continue;
        else if ( pt->flag == 7 )  continue;

        /* geometric support */
        ier = bezierCP(mesh,k,&pb);
        assert(ier);
        nc = 0;

        for (i=0; i<3; i++) {
            i1 = inxt[i];
            i2 = inxt[i1];
            if ( !MS_GET(pt->flag,i) && !MS_SIN(pt->tag[i]) ) {
                ip = hashGet(&hash,pt->v[i1],pt->v[i2]);
                if ( ip > 0 ) {
                    MS_SET(pt->flag,i);
                    nc++;
                    if ( pt->tag[i] & MS_GEO ) {
                        /* new point along edge */
                        ier = bezierInt(&pb,uv[i],o,no,to);
                        assert(ier);

                        ppt = &mesh->point[ip];
                        go  = &mesh->geom[ppt->ig];
                        memcpy(go->n2,no,3*sizeof(double));

                        /* a computation of the tangent with respect to these two normals is possible */
                        ppt->n[0] = go->n1[1]*go->n2[2] - go->n1[2]*go->n2[1];
                        ppt->n[1] = go->n1[2]*go->n2[0] - go->n1[0]*go->n2[2];
                        ppt->n[2] = go->n1[0]*go->n2[1] - go->n1[1]*go->n2[0];
                        dd = ppt->n[0]*ppt->n[0] + ppt->n[1]*ppt->n[1] + ppt->n[2]*ppt->n[2];
                        if ( dd > EPSD2 ) {
                            dd = 1.0 / sqrt(dd);
                            ppt->n[0] *= dd;
                            ppt->n[1] *= dd;
                            ppt->n[2] *= dd;
                        }
                    }
                }
            }
        }
        if ( nc > 0 )  ++ns;
    }
    if ( info.ddebug && ns ) {
        fprintf(stdout,"     %d analyzed  %d proposed\n",mesh->nt,ns);
        fflush(stdout);
    }

    /* step 3. splitting */
    ns = 0;
    nt = mesh->nt;
    for (k=1; k<=nt; k++) {
        pt = &mesh->tria[k];
        if ( !MS_EOK(pt) || pt->ref < 0 )  continue;
        else if ( pt->flag == 0 )  continue;

        j  = -1;
        vx[0] = vx[1] = vx[2] = 0;
        for (i=0; i<3; i++) {
            i1 = inxt[i];
            i2 = inxt[i1];
            if ( MS_GET(pt->flag,i) ) {
                vx[i] = hashGet(&hash,pt->v[i1],pt->v[i2]);
                assert(vx[i]);
                j = i;
            }
        }
        if ( pt->flag == 1 || pt->flag == 2 || pt->flag == 4 ) {
            ier = split1(mesh,met,k,j,vx);
            assert(ier);
            ns++;
        }
        else if ( pt->flag == 7 ) {
            ier = split3(mesh,met,k,vx);
            assert(ier);
            ns++;
        }
        else {
            ier = split2(mesh,met,k,vx);
            assert(ier);
            ns++;
        }
    }
    if ( (info.ddebug || abs(info.imprim) > 5) && ns > 0 )
        fprintf(stdout,"     %7d splitted\n",ns);
    free(hash.item);

    return(ns);
}
Пример #21
0
packagePo loadedPackage(const char *package) {
  return (packagePo) hashGet(packages, (void*)package);
}