Command commandContainerGetCommandByVersion(CommandContainer commandContainer,int version){ CommandContainerInternal* internal=(CommandContainerInternal*)commandContainer; Command temp; hashmapGet(internal->versionMap,version,(void**)&temp); return temp; }
int main(int argc, char *argv[]) { Hashmap *packages; if (!(packages = readPackages(PACKAGES))) return -1; //hashmapForEach(packages, dumpPackage, NULL); printf("Found packages: %u\n", (int) hashmapSize(packages)); initUsers(); if (argc == 2) { APK *package = (APK *) hashmapGet(packages, argv[1]); if (package) checkPackage(NULL, package, NULL); else printf("Package %s not found\n", argv[1]); } else hashmapForEach(packages, checkPackage, NULL); hashmapForEach(packages, freePackage, NULL); hashmapFree(packages); return 0; }
int propertiesGetInt(Properties properties, char *strigKey) { PropertiesInternal *internal = (PropertiesInternal *) properties; unsigned int key = stringTransformToInt(strigKey, strlen(strigKey)); Object value; int result = hashmapGet(internal->map, key, &value); char *stringValue = ""; if (result == VALID) { stringValue = (char *) value; } char *eptr = NULL; long intValue = strtol(stringValue, &eptr, 10); /* If the result is 0, test for an error */ if (intValue == 0) { /* If a conversion error occurred, display a message and exit */ if (errno == EINVAL) { printf("Conversion error occurred: %d\n", errno); exit(0); } /* If the value provided was out of range, display a warning message */ if (errno == ERANGE) printf("The value provided was out of range\n"); } }
void graphRemoveConnection(graph *ctx, size_t ID) { // Remove Connection from hashmap connection *conn = (connection*) hashmapRemove(ctx->CIDMap, ID); // Remove connection from Source and drain map/lists fastlist *drnList = (fastlist*) hashmapGet(ctx->srcMap, conn->srcID); fastlistRemoveByPointer(drnList, (void*)conn); fastlist *srcList = (fastlist*) hashmapGet(ctx->drnMap, conn->drnID); fastlistRemoveByPointer(srcList, (void*)conn); // Remove the Connection from main list fastlistRemoveByPointer(ctx->connections, conn); // Finally free the Connection free(conn); }
unsigned long graphAddConnection(graph *ctx, size_t src, size_t drn) { // get src and drn GLI* srcGli = (GLI*) hashmapGet(ctx->GIDMap, src); GLI* drnGli = (GLI*) hashmapGet(ctx->GIDMap, drn); //if the drn of this connection is an input gate no dice. if (drnGli->inputMode == INPUT) return -1; //fail //if the src of this connection is an output no dice. if (srcGli->inputMode == OUTPUT) return -1; // if the drn of this connection is in UNITY or output mode it can only accept 1 input if ((drnGli->inputMode == UNITY) || (drnGli->inputMode == OUTPUT) ) { // get the list of inputs to the drn. fastlist *drnInputs = (fastlist*) hashmapGet(ctx->drnMap, drn); // if the list is not currently empty then fail. if (fastlistSize(drnInputs)) return -1; } // TODO: safety this connection *conn = (connection*) malloc(sizeof(connection)); conn->ID = (size_t) conn; // We're using the pointer as a UUID, as we don't have a generator. // TODO: Generate Better IDs conn->srcEp = srcGli; conn->drnEp = drnGli; conn->srcID = src; conn->drnID = drn; // insert into Connection ID map; hashmapInsert(ctx->CIDMap, (void*)conn, conn->ID); fastlistAdd(ctx->connections, (void*)conn); // add the Connection to the srcList; fastlist *srcList = (fastlist*) hashmapGet(ctx->srcMap, src); fastlistAdd(srcList, (void*)conn); // add the Connection to the drnList; fastlist *drnList = (fastlist*) hashmapGet(ctx->drnMap, drn); fastlistAdd(drnList, (void*)conn); // done, cleanup return conn->ID; };
void printTopLeft() { for (int y = 1; y <= currentGame.mapSize && y <= 10; y++) { for (int x = 1; x <= currentGame.mapSize && x <= 10; x++) { printf("%c", getPawnSymbol(hashmapGet(currentGame.gameMap, x, y))); } printf("\n"); } printf("\n"); }
void graphRemoveGLI(graph *ctx, size_t ID) { //Unregister this GLI; GLI *gli = (GLI*) hashmapRemove(ctx->GIDMap, ID); fastlistRemoveByPointer(ctx->nodes, gli); // Get Connection lists fastlist *srcList = (fastlist*) hashmapGet(ctx->srcMap, ID); fastlist *drnList = (fastlist*) hashmapGet(ctx->drnMap, ID); // Remove src connections here. { size_t count = fastlistSize(srcList); for (size_t i = 0; i < count; i++) { // Get the connection. connection *conn = (connection *) fastlistGetIndex(srcList, i); size_t index = conn->ID; // Remove it graphRemoveConnection(ctx, index); } } // Remove drn connections here. { size_t count = fastlistSize(drnList); for (size_t i = 0; i < count; i++) { // Get the connection. connection *conn = (connection *) fastlistGetIndex(drnList, i); size_t index = conn->ID; // Remove it graphRemoveConnection(ctx, index); } } // Free the connection lists. hashmapRemove(ctx->srcMap, ID); hashmapRemove(ctx->drnMap, ID); fastlistDelete(srcList); fastlistDelete(drnList); // Done, cleanup free(gli); ctx->nodeCount--; };
void texture_cache_unload(struct texture_cache* self, const char* key) { struct texture* tex = hashmapGet(self->cache, (void*)key); if (!tex) { hashmapPut(self->cache, (void*)key, NULL); texture_unload(tex); } else { fprintf(stderr, "texture %s has already been removed", key); } }
int validationManagerGetLastVersion(ValidationManager validationManager,int nodeId){ ValidationManagerStruct* internal=(ValidationManagerStruct*)validationManager; ValidationDataStructure *data; hashmapGet(internal->entitiesMap,nodeId,(Object)&data); if(data==NULL){ return -1; }else{ return data->version; } }
static void next(void *c, void *v) { if (c) { struct ctx *ctx = c; CharBuffer *b = ctx->b; struct Schedule *s = (struct Schedule *) v; struct ScheduleEntry *entries = hashmapGet(timetable->scheduleEntry, &s->sid); // Loop and find our stanox. Do the entire schedule as a stanox can exist // multiple times on circular & long schedules. if (entries) for (int i = 0; i < s->numEntries; i++) { short tiploc = entries[i].time.tiploc; struct TTTiploc *tpl = hashmapGet(timetable->idTiploc, &tiploc); if (tpl && tpl->stanox == ctx->stanox && (!ctx->filterHour || ctx->hour == ((scheduleTime_getTime(&entries[i].time) / 3600) % 24)) ) appendEntry(ctx, s, entries, i); } } }
/* * Get node from registry. */ int registryGetNode(Registry registry,int nodeId, Node* result){ VALIDATE(validateIdFormat(nodeId)); VALIDATE(validateResultReference((Object*)result)); RegistryInternal* internal=(RegistryInternal*)registry; int valid=hashmapGet(internal->nodes,nodeId,(Object*)result); if(valid==MAP_MISSING){ *result=NULL; return MISSING_NODE; } return VALID; }
int validationManagerValidateEntityExistenceForLock(ValidationManager validationManager,int entityId,int sessionId,int administrator,int forceLock){ ValidationManagerStruct* internal=(ValidationManagerStruct*)validationManager; ValidationDataStructure * data; hashmapGet(internal->entitiesMap,entityId,(void**)&data); if(data==NULL){ return MISSING_NODE; }else if(data->lock==TRUE&&sessionId!=data->lockOwnerId&&(administrator!=TRUE||forceLock!=TRUE)){ return NOT_NODE_HOLDER; }else{ return VALID; } }
char *propertiesGet(Properties properties, char *strigKey) { PropertiesInternal *internal = (PropertiesInternal *) properties; unsigned int key = stringTransformToInt(strigKey, strlen(strigKey)); Object value; int result = hashmapGet(internal->map, key, &value); if (result == VALID) { return (char *) value; } else { return ""; } }
// TODO : export load from binary data in Lua. struct texture* texture_cache_load(struct texture_cache* self, const char* key) { struct texture* tex = hashmapGet(self->cache, (void*)key); if (!tex) { tex = texture_load_from_png(key); strncpy(tex->name, key, strlen(key)); printf("texture load key = %s, tex = %p\n", key, tex); hashmapPut(self->cache, (void*)tex->name, (void*)tex); } else { tex->ref++; } return tex; }
char *tboot_config_get(char *key) { if (!tboot_config) { pr_error("tboot config wasn't init.\n"); return NULL; } if (!hashmapContainsKey(tboot_config, (void *)key)) { pr_error("invalid tboot config keyword:%s\n", key); return NULL; } return hashmapGet(tboot_config, (void *)key); }
int validationManagerValidateRefferance(ValidationManager validationManager,int entityId){ ValidationManagerStruct* internal=(ValidationManagerStruct*)validationManager; ValidationDataStructure * data; HashMap chidrenMap; hashmapGet(internal->parentChildrenRelationEntities,entityId,&chidrenMap); if(chidrenMap==NULL){ chidrenMap = stringHashmapCreate(SMALL_CONTAINER_SIZE); hashmapPut(internal->parentChildrenRelationEntities,entityId,chidrenMap); } ArrayList parents; hashmapGet(internal->childrenParentRelationEntities,entityId,&parents); if(parents==NULL){ parents = arrayListCreate(); hashmapPut(internal->childrenParentRelationEntities,entityId,parents); } int parentSize=arrayListSize(parents); int childrenSize=stringHashmapLength(chidrenMap); if(parentSize>0||childrenSize>0){ return VALID; }else{ return NO_CHILD_PARENT_RELATION; } }
void commandContainerCleanup(CommandContainer commandContainer){ CommandContainerInternal* internal=(CommandContainerInternal*)commandContainer; lockLock(internal->lock); time_t currentTime; // Stores seconds elapsed since 01-01-1970 time(¤tTime); // Cleanup atomic commands if(linkedListSize(internal->atomicCommands)>0){ int length=linkedListSize(internal->atomicCommands); Command atomicCommand; while(length>0){ linkedListGetFirst(internal->atomicCommands,(Object*)&atomicCommand); int commandTime=commandGetInt(atomicCommand,SERVER_HANDLING_TIME); int distributed=commandGetInt(atomicCommand,DISTRIBUTED); if((currentTime-commandTime>MAX_TIME_TO_HOLD_COMMAND&&distributed==TRUE)||(currentTime-commandTime>MAX_TIME_TO_HOLD_UNCLEANED_COMMAND)){ commandFree(atomicCommand); linkedListRemoveFirst(internal->atomicCommands); }else{ break; } length=linkedListSize(internal->atomicCommands); } } // Cleanup commands int length=linkedListSize(internal->versions); int i; int* intBuffer; while(length>0){ linkedListGetFirst(internal->versions,(Object*)&intBuffer); Command temp; hashmapGet(internal->versionMap,*intBuffer,(Object*)&temp); int commandTime=commandGetInt(temp,SERVER_HANDLING_TIME); int persisted=commandGetInt(temp,PERSISTED); int distributed=commandGetInt(temp,DISTRIBUTED); if((currentTime-commandTime>MAX_TIME_TO_HOLD_COMMAND&&persisted==TRUE&&distributed==TRUE)||(currentTime-commandTime>MAX_TIME_TO_HOLD_UNCLEANED_COMMAND)){ linkedListRemoveFirst(internal->versions); hashmapRemove(internal->versionMap,*intBuffer); memoryFree(intBuffer); commandFree(temp); }else{ break; } length=linkedListSize(internal->versions); } lockUnlock(internal->lock); }
void commandContainerRevertCommand(CommandContainer commandContainer,int lastVersion){ CommandContainerInternal* internal=(CommandContainerInternal*)commandContainer; lockLock(internal->lock); int* intBuffer=NULL; linkedListGetLast(internal->versions,(Object*)&intBuffer); while(intBuffer!=NULL&&lastVersion<=*intBuffer){ linkedListRemoveLast(internal->versions); Command temp; hashmapGet(internal->versionMap,*intBuffer,(Object*)&temp); hashmapRemove(internal->versionMap,*intBuffer); linkedListGetLast(internal->versions,(Object*)&intBuffer); } lockUnlock(internal->lock); }
int validationManagerValidateParentOfChild(ValidationManager validationManager,int entityId,int childEntityId,char* arcName,int arcLength){ ValidationManagerStruct* internal=(ValidationManagerStruct*)validationManager; ValidationDataStructure * data; StringHashMap childrenMap; hashmapGet(internal->parentChildrenRelationEntities,entityId,&childrenMap); if(childrenMap==NULL){ childrenMap = stringHashmapCreate(SMALL_CONTAINER_SIZE); hashmapPut(internal->parentChildrenRelationEntities,entityId,childrenMap); } stringHashmapGet(childrenMap,arcName,arcLength,(Object*)&data); if(data==NULL||data->id!=childEntityId){ return NO_CHILD_PARENT_RELATION; }else{ return VALID; } }
extern OOTint MIOHashmap_Get (OOTint pmMapID, OOTstring key, OOTint *result) { hashmap *myMap = (hashmap*)MIO_IDGet (pmMapID, HASHMAP_ID); OOTint *retrieved; retrieved = (OOTint*)hashmapGet (myMap,key); if(retrieved == NULL) { *result = 0; return 0; // 0 if not found } else { *result = *retrieved; return 1; } }
int validationManagerIsLocked(ValidationManager validationManager,int entityId,int sessionId,int administrator,int foceUnlock){ ValidationManagerStruct* internal=(ValidationManagerStruct*)validationManager; if(administrator==TRUE&&foceUnlock==TRUE){ return VALID; } if(entityId>=0&&entityId<MIN_ENTITY_ID){ return VALID; }else{ ValidationDataStructure*data; hashmapGet(internal->entitiesMap,entityId,(Object)&data); if(data==NULL){ return VALID; }else if(data->lock==TRUE&&sessionId!=data->lockOwnerId){ return NOT_NODE_HOLDER; }else{ return VALID; } } }
/** * Handles a request to be connected to another peer. */ static void masterHandleConnectionRequest(PeerProxy* peerProxy, Header* header) { Peer* master = peerProxy->peer; pid_t targetPid = header->credentials.pid; if (!hashmapContainsKey(peerProxy->connections, &targetPid)) { // We haven't connected these peers yet. PeerProxy* targetPeer = (PeerProxy*) hashmapGet(master->peerProxies, &targetPid); if (targetPeer == NULL) { // Unknown process. masterReportConnectionError(peerProxy, header->credentials); } else { masterConnectPeers(peerProxy, targetPeer); } } // This packet is complete. Get ready for the next one. peerProxyExpectHeader(peerProxy); }
/** * The master told us this peer is dead. */ static void masterProxyHandleConnectionError(PeerProxy* masterProxy, Header* header) { Peer* peer = masterProxy->peer; // Look up the peer proxy. pid_t pid = header->credentials.pid; PeerProxy* peerProxy = NULL; peerLock(peer); peerProxy = hashmapGet(peer->peerProxies, &pid); peerUnlock(peer); if (peerProxy != NULL) { ALOGI("Couldn't connect to %d.", pid); peerProxyKill(peerProxy, false); } else { ALOGW("Peer proxy for %d not found. This shouldn't happen.", pid); } peerProxyExpectHeader(masterProxy); }
pNode pNodeSearch(hashmap *phmap,unsigned long aid) { pNode p; p = (pNode)hashmapGet(phmap,aid); if(p == NULL) { //printf("#3");fflush(NULL); pAlistAdd(aid); //printf("#4");fflush(NULL); p = (pNode)malloc(sizeof(aNode)); //printf("#5");fflush(NULL); p->len = 1; //printf("#6");fflush(NULL); p->aid[0] = aid; //printf("#7");fflush(NULL); hashmapInsert(phmap,p,aid); //printf("#8");fflush(NULL); } return p; }
unsigned long graphAddGLI(graph *ctx, gateInputType type, bool nin, size_t ID, unsigned int delay) { // if hashmapGet does not return null then there is already a gate with the desired ID. if ((GLI*) hashmapGet(ctx->GIDMap, ID)) return -1; //Give up. GLI *gli = (GLI*) malloc(sizeof(GLI)); gli->ID = ID; // set ID. // TODO: Generate Better IDs gli->state = DTKNOW; // Lets be honest about this. // Just copy this across. gli->delay = delay; gli->inputMode = type; gli->inputNegate = nin; gli->seen = false; gli->lastUpdated = -1; gli->updatedBy = -1; // Push gli into the map hashmapInsert(ctx->GIDMap, (void*)gli, gli->ID); fastlistAdd(ctx->nodes, (void*)gli); // Create Connection Lists for SRC and DRN for this gate fastlist * srcList = fastlistCreate(CONN_LIST_SIZE); //TODO: safety this; hashmapInsert(ctx->srcMap, (void*)srcList, gli->ID); fastlist *drnList = fastlistCreate(CONN_LIST_SIZE); //TODO: safety this; hashmapInsert(ctx->drnMap, (void*)drnList, gli->ID); //Construction complete, cleanup; ctx->nodeCount++; return gli->ID; // gli goes out of scope here. (psst, don't tell anyone the ID is a pointer) };
void ttref_add_schedule_activity(Hashmap *map, struct Schedule *s) { struct ScheduleEntry *entries = hashmapGet(timetable->scheduleEntry, &s->sid); if (entries) for (int i = 0; i < s->numEntries; i++) ttref_add_activity(map, entries[i].time.activity); }
int hashmap_test(void) { printf("Starting hashmap tests...\n\n"); hashmap* hmap = hashmapCreate(20); if (hashmapCount(hmap) != 0){ printf("\nTest failed. Hashmap does not initialize to empty."); return -1; } hashmapInsert(hmap, 1, 1); if (hashmapCount(hmap) != 1){ printf("\nTest failed. Hashmap does not have proper count of elements."); return -1; } int test = 0; test = (int *) hashmapGet(hmap, 1); if (test != 1){ printf("\nTest failed. Value found inside was actually %d ", test); return -1; } hashmapInsert(hmap, "Word", 2); char* word; word = (char*) hashmapGet(hmap, 2); if (strcmp(word, "Word") != 0){ printf("\nTest failed. The word found inside was actually %s", word); return -1; } test = (int*) hashmapRemove(hmap, 1); printf("\nTest 5: "); if (test == -1){ printf("\nTest failed. HASHMAPERROR returned."); return -1; } if (hashmapCount(hmap) != 1){ printf("\nTest failed. Hashmap value not actually removed"); return -1; } printf("\nTest 6: "); hashmapInsert(hmap, 2, 1); test = (int *) hashmapGet(hmap, 1); if (test != 2){ printf("\nTest failed. Value was not inserted."); return -1; } hashmapInsert(hmap, 7, 1); test = (int *) hashmapGet(hmap, 1); if (test != 7){ printf("\nTest failed. Value was not replaced."); return -1; } hashmapInsert(hmap, 'd', 13); char c = (char) hashmapGet(hmap, 13); if (c != 'd'){ printf("\nTest failed. Char 'd' was not properly inserted"); return -1; } hashmapInsert(hmap, 9, 4294967295); test = (int *) hashmapGet(hmap, 4294967295); if (test != 9){ printf("\nTest failed. 9 was not inserted"); return -1; } test = hashmapRemove(hmap, 11); if (test != -1){ printf("\nTest failed. ERROR should be returned."); return -1; } test = hashmapGet(hmap, 11); if (test != -1){ printf("\nTest failed. ERROR should be returned."); return -1; } hashmapDelete(hmap); hashmap* hmap2 = hashmapCreate(10); int x = 0; while(x < 200){ hashmapInsert(hmap2, x, x); int test2 = (int*) hashmapGet(hmap, x); if (test2 != x){ printf("\nTest failed. Improper value retrieved."); return -1; } x++; } x = 0; while (x< 200){ hashmapGet(hmap, 1); int test2 = (int*) hashmapGet(hmap, 1); if (test2 != 1){ printf("\nTest failed. Constant retrieval does not work."); return -1; } x++; } x = 0; while (x < 200){ int test2 = (int*) hashmapRemove(hmap, x); if (test2 == -1){ printf("\nTest failed. ERROR was thrown, when it shouldn't have been."); return -1; } x++; } printf("\n\nAll hashmap tests passed!\n"); hashmapDelete(hmap2); return 0; }
int main(int argc, char *argv[]) { extern char *optarg; extern int optind; char pcap_ebuf[PCAP_ERRBUF_SIZE]; char libnet_ebuf[LIBNET_ERRBUF_SIZE]; int c, netmask = 0, ifaddr = 0, nhosts = 0, i = 0; network_entry_t *entry; iface = NULL; gateway_ip = target_ip = 0; printf( "dSploit ArpSpoofer.\n\n" ); while( (c = getopt(argc, argv, "i:t:h?V")) != -1) { switch (c) { case 'i': iface = optarg; break; case 't': if ((target_ip = libnet_name2addr4(lnet, optarg, LIBNET_RESOLVE)) == -1) exit(1); break; default: return 1; } } argc -= optind; argv += optind; if (argc != 1) return 1; if( ( gateway_ip = libnet_name2addr4( lnet, argv[0], LIBNET_RESOLVE ) ) == -1 ) { printf( "[ERROR] Unable to resolve gateway ip.\n" ); return 1; } if( iface == NULL && (iface = pcap_lookupdev(pcap_ebuf)) == NULL ) { printf( "[ERROR] Unable to lookup network interface ( %s ).\n", pcap_ebuf ); return 1; } if( ( lnet = libnet_init(LIBNET_LINK, iface, libnet_ebuf) ) == NULL ) { printf( "[ERROR] Unable to initialize libnet ( %s ).\n", libnet_ebuf ); return 1; } signal( SIGHUP, cleanup ); signal( SIGINT, cleanup ); signal( SIGTERM, cleanup ); our_mac = ( struct ether_addr * )libnet_get_hwaddr( lnet ); if( our_mac == NULL ) { printf( "[ERROR] Unable to retrieve local hardware address libnet ( %s ).\n", libnet_geterror( lnet ) ); return 1; } if( net_get_details( iface, &netmask, &ifaddr, &nhosts ) != 0 ) exit( 1 ); printf( "netmask = %s\n", inet_ntoa( *( struct in_addr *)&netmask ) ); printf( "ifaddr = %s\n", inet_ntoa( *( struct in_addr *)&ifaddr ) ); printf( "gateway = %s\n", inet_ntoa( *( struct in_addr *)&gateway_ip ) ); printf( "hosts = %d\n", nhosts ); // force the arp cache to be populated net_wake( iface, nhosts, ifaddr, netmask, gateway_ip ); // if the target is the gateway itself, we switch to subnet mode, // otherwise if the target was set, we are in single ip spoofing mode. if( target_ip != 0 && target_ip != gateway_ip ) { if( target_ip != 0 && arp_lookup( target_ip, &target_mac, iface ) != 0 ) { printf( "[ERROR] Couldn't find a cached MAC address for %s, try to wait a little bit and then try again or restart the network discovery.\n", libnet_addr2name4(target_ip, LIBNET_DONT_RESOLVE) ); return 1; } printf( "\nSingle target mode.\n" ); while( killed == 0 ) { arp_send( lnet, ARPOP_REPLY, (unsigned char *)our_mac, gateway_ip, (unsigned char *)&target_mac, target_ip ); sleep(1); } } // whole network spoofing else { printf( "\nSubnet mode.\n" ); netmap = net_get_mapping( iface, nhosts, ifaddr, netmask, gateway_ip, &i ); if( i == 0 ) { printf( "[ERROR] No alive endpoints found.\n" ); return 1; } while( killed == 0 ) { for( i = 1; i <= nhosts && killed == 0; i++ ) { target_ip = ( ifaddr & netmask ) | htonl(i); entry = hashmapGet( netmap, (void *)target_ip ); if( entry && killed == 0 ) { arp_send( lnet, ARPOP_REPLY, (unsigned char *)our_mac, gateway_ip, (unsigned char *)&entry->mac, target_ip ); } } sleep(1); } } return 0; }
static int produceUnit(int x1, int y1, int x2, int y2, int unitId) { /// Validates initialization. if (!isValidField(currentGame.mapSize, x1, y1) || !isValidField(currentGame.mapSize, x2, y2)) { return ERROR; } if (currentGame.isInitialized == false) { return ERROR; } /// Validates distance. if (distMax(x1, y1, x2, y2) != 1) { return ERROR; } /// Picks pawn from board. pawn *currentPawn = hashmapGet(currentGame.gameMap, x1, y1); /// Checks if pawn was picked. if (currentPawn == NULL) { return ERROR; } /// Checks if pawn is able to produce. if (currentPawn->lastMove >= currentGame.currentRound - 2) { return ERROR; } /// Checks if pawn belongs to current player. if (currentGame.playerTurn != getPawnAdherence(currentPawn)) { return ERROR; } if (isPeasant(currentPawn)) { pawn *targetPawn = hashmapGet(currentGame.gameMap, x2, y2); if (getPawnId(targetPawn) == EMPTY_SPACE_ID) { pawn *createdPawn; currentPawn->lastMove = currentGame.currentRound; if (unitId == PEASANT_PRODUCE_ID) { int newPawnId = currentGame.playerTurn == PLAYER_A_TURN ? PEASANT_PLAYER_A_ID : PEASANT_PLAYER_B_ID; createdPawn = newPawn(x2, y2, currentGame.currentRound - 1, newPawnId); } else if (unitId == KNIGHT_PRODUCE_ID) { int newPawnId = currentGame.playerTurn == PLAYER_A_TURN ? KNIGHT_PLAYER_A_ID : KNIGHT_PLAYER_B_ID; createdPawn = newPawn(x2, y2, currentGame.currentRound - 1, newPawnId); } else { ///< Tried to produce wrong unit. return ERROR; } hashmapPut(currentGame.gameMap, createdPawn); return GAME_OK; } else { ///< Field was not empty. return ERROR; } } else { ///< Unit that tried to produce was not peasant. return ERROR; } }
void fileHelperConsume(FileHelper fileHelper, Command command) { FileHelperInternal *m = (FileHelperInternal *) fileHelper; PageManager pageManager = NULL; int schemaId = commandGetInt(command, SCHEMA_ID); int entityId = commandGetInt(command, ENTITY_ID); hashmapGet(m->managers, schemaId, &pageManager); char schemaIdString[10]; if (pageManager == NULL) { stringConvertInt(schemaIdString, schemaId); char *pageDir = (char *) memoryAlloc(strlen(m->filePath) + strlen(schemaIdString) + 1); strcpy(pageDir, m->filePath); strcat(pageDir, "/"); strcat(pageDir, schemaIdString); mkdir(pageDir, S_IRWXU | S_IRGRP | S_IXGRP); pageManager = pageManagerCreate(schemaId, pageDir); hashmapPut(m->managers, schemaId, pageManager); } Registry registry; hashmapGet(m->registryMap, schemaId, ®istry); if (registry == NULL) { Throw(Fatal, 1, "FileHelper unsupported state failed to find registry for schemaId "); } char *buf = NULL; int type = commandGetInt(command, COMMAND_TYPE); switch (type) { case CREATE_NEW_ENTITY: { int lenght = commandGetByteArray(command, SERIALIZED_NODE, &buf); pageManagerAdd(pageManager, entityId, buf, lenght); return; } case DELETE_ENTITY: { pageManagerDelete(pageManager, entityId); return; } case ADD_CHILD_ENTITY_TO_PARENT_ENTITY: { int lenght = commandGetByteArray(command, SERIALIZED_NODE, &buf); pageManagerUpdate(pageManager, entityId, buf, lenght); return; } case REMOVE_CHILD_ENTITY_FROM_PARENT_ENTITY: { int lenght = commandGetByteArray(command, SERIALIZED_NODE, &buf); pageManagerUpdate(pageManager, entityId, buf, lenght); return; } case UPDATE_ENTITY: { int lenght = commandGetByteArray(command, SERIALIZED_NODE, &buf); pageManagerUpdate(pageManager, entityId, buf, lenght); return; } case ADD_ATTRIBUTE: { int lenght = commandGetByteArray(command, SERIALIZED_NODE, &buf); pageManagerUpdate(pageManager, entityId, buf, lenght); return; } case REMOVE_ATTRIBUTE: { int lenght = commandGetByteArray(command, SERIALIZED_NODE, &buf); pageManagerUpdate(pageManager, entityId, buf, lenght); return; } } }