DataSpace.c

Go to the documentation of this file.
00001 #define _DATASPACE_C_
00002 
00003 #include <stdlib.h>
00004 #include <string.h>
00005 #include "DataSpace.h"
00006 #include "TaskData.h"
00007 #include "../constants.h"
00008 
00009 DataSpace *createDataSpace() {
00010         DataSpace *dataSpace = (DataSpace *) malloc(sizeof(DataSpace));
00011         dataSpace->dataHash = hashStrVoidCreate(INITIAL_CAPACITY);
00012 
00013         setStrVoidSerializer(dataSpace->dataHash, &writeTaskData, &readTaskData);
00014                 
00015         return dataSpace;
00016 }
00017 
00018 void destroyDataSpace(DataSpace * space) {
00019         hashStrVoidDestroy(space->dataHash);
00020 }
00021 
00022 void *getData(DataSpace *space, char *key, int *valSz) {
00023         void *ret = NULL;
00024         PosHandlerStrVoid pos = hashStrVoidGet(space->dataHash, key);
00025         if(pos == NULL){
00026                 return NULL;
00027         }
00028 
00029         TaskData *taskData = posGetValue(pos);
00030         *valSz = taskData->valSz;
00031         if (taskData->valSz > 0) {
00032                 ret = malloc(taskData->valSz);
00033         }
00034         memcpy(ret, taskData->val, taskData->valSz);
00035 
00036         return ret;
00037 }
00038 
00039 int putData(DataSpace * space, char * key, void * val, int valSz) {
00040         PosHandlerStrVoid pos = NULL;
00041         TaskData *taskData = (TaskData *) malloc(sizeof(TaskData));
00042         
00043         if(space->dataHash == NULL){
00044                 space->dataHash = hashStrVoidCreate(INITIAL_CAPACITY);
00045         }
00046         
00047         // Get the old val for posterior destruction
00048         pos = hashStrVoidAdd(space->dataHash, key);
00049         TaskData *oldTaskData = posGetValue(pos);
00050         
00051         taskData->valSz = valSz;
00052         if (valSz > 0) {
00053                 taskData->val = (char *) malloc(sizeof(char)*valSz);
00054                 memcpy(taskData->val, val, valSz);
00055         } else {
00056                 taskData->val = NULL;           
00057         }
00058         posSetValue(pos, taskData);
00059         
00060         // destroy oldTaskData here, because it can be val 
00061         if (oldTaskData != NULL) {
00062                 // Destroy the old task data, we will overwrite this
00063                 destroyTaskData(oldTaskData);
00064         }       
00065         return 0;
00066 }
00067 
00068 int removeData(DataSpace *space, char *key) {   
00069         if(space->dataHash == NULL){
00070                 printf("Error: key = %s dont have saved data\n", key);
00071                 return -1;
00072         }
00073 
00074         PosHandlerStrVoid pos = hashStrVoidGet(space->dataHash, key);
00075         if(pos == NULL){
00076                 return -1;
00077         }
00078 
00079         TaskData *taskData = posGetValue(pos);
00080         if (taskData != NULL) {
00081                 destroyTaskData(taskData);
00082         }
00083         
00084         // get the handler to intern Hash where key==dataId
00085         return hashStrVoidRemove(space->dataHash, key);
00086 }
00087 
00088 int readDataSpace(FILE *inputFile, DataSpace *space) {
00089         hashStrVoidDeserialize(inputFile, space->dataHash);
00090         
00091         return 1;
00092 }
00093 
00094 int  writeDataSpace (FILE *outputSpace, DataSpace *space) {
00095         hashStrVoidSerialize(outputSpace, space->dataHash);
00096         
00097         return 1;
00098 }
00099 

Generated on Tue Jan 17 19:18:37 2006 for Void by  doxygen 1.4.6