/** * \brief Close the Var component * \private */ void Var_close(void) { List* keys; int n; if(initialized) { /* Free readonly variable cache */ keys = Dictionary_getKeys(ro_cache); n = List_getSize(keys); for(int i = 0; i < n; i++) { free(Dictionary_get(ro_cache, List_get(keys, i))); } List_destroy(keys); Dictionary_destroy(ro_cache); /* Free subscriptions */ keys = Dictionary_getKeys(subscriptions); n = List_getSize(keys); for(int i = 0; i < n; i++) { free(Dictionary_get(subscriptions, List_get(keys, i))); } List_destroy(keys); Dictionary_destroy(subscriptions); initialized = false; } }
/** *** \brief Stores a value in the dictionary with the specified key, maintaining the *** keys in alphabetical order. *** *** If a value with that key already exists in the *** dictionary, it will be replaced with the new value *** \note This is a potential memory leak! **/ bool Dict_putSorted(Dict *d, const char *key, void *value) { bool retVal = false; bool added = false; size_t idx = 0; Pair *p = NULL; Pair *pp = NULL; size_t ii; if (keyToIndex(d, key, &idx)) { p = (Pair *)List_get((List *)d, idx); p->value = value; retVal = true; } else { p = new_Pair(key, value); added = false; if (List_length((List *)d) > 0) { for (ii = 0; ii < List_length((List *)d); ++ii) { pp = (Pair *)List_get((List *)d, ii); if (strcmp(key, pp->key) < 0) { List_insert((List *)d, ii, p); added = true; break; } } } if (!added) retVal = List_append((List *)d, p); } Logging_tracef("++++ Added pointer 0x%x to Dict 0x%x", (unsigned int)value, (unsigned int)d); return retVal; }
void printList(List_t * self){ for(int i = 0;i<List_getSize(self);i++){ book_t * tmpBook = List_get(self,i,NULL); printf("\nBook number: %i\nBook name: %s\ncurDaysUsed: %i\n",i+1,tmpBook->name,tmpBook->curDaysUsed); if(tmpBook->status == 1){ user_t * tmpUser = List_get(tmpBook->users,List_getSize(tmpBook->users)-1,NULL); printf("CurrentUser: %s\n",tmpUser->name); } } puts(""); }
END_TEST START_TEST (test_List_findIf) { List_t *list; const char *foo = "foo"; const char *bar = "bar"; const char *baz = "baz"; const char *bop = "bop"; List_add(L, (void *) foo); List_add(L, (void *) bop); list = List_findIf(L, myPredicate); fail_unless( list != NULL ); fail_unless( List_size(list) == 0 ); List_free(list); List_add(L, (void *) foo); List_add(L, (void *) bar); List_add(L, (void *) baz); List_add(L, (void *) bop); list = List_findIf(L, myPredicate); fail_unless( list != NULL ); fail_unless( List_size(list) == 2 ); fail_unless( List_get(list, 0) == bar ); fail_unless( List_get(list, 1) == baz ); List_free(list); List_add(L, (void *) baz); list = List_findIf(L, myPredicate); fail_unless( list != NULL ); fail_unless( List_size(list) == 3 ); fail_unless( List_get(list, 0) == bar ); fail_unless( List_get(list, 1) == baz ); fail_unless( List_get(list, 2) == baz ); List_free(list); }
void addBookReader(library_t * self,const char * name,const char * readerName){ for(int i = 0;i<List_getSize(self->book);i++){ book_t * tmpBook = List_get(self->book,i,NULL); if(strcmp(tmpBook->name,name)==0 && tmpBook->status == 0){ user_t * tmpUser = newUser(readerName); //List_add(tmpBook->users,List_getSize(tmpBook->users)-1,tmpUser); List_t tmpList = tmpBook->users; List_add(tmpList,List_getSize(tmpList),tmpUser); //user_t * tmpUser1 = List_get(tmpList,List_getSize(tmpList)-1,NULL); //puts(tmpUser1->name); book_t * nB = newBookchangeParam(name,1,tmpBook->curDaysUsed,"day",tmpList); List_remove(self->book,i,NULL); List_add(self->book,i,nB); return; } } }
/** Returns the Section with the specified ID */ Section *Section_findById(Section *start, int sectionId) { Section *result = NULL; char *sectionIdValue = NULL; int currentSectionId; int ii; // Check if current section is the one if (Vars_defined(start->variables, "section_id")) { sectionIdValue = Vars_get(start->variables, "section_id"); if (Type_isNumeric(sectionIdValue)) { currentSectionId = atoi(sectionIdValue); if (currentSectionId == sectionId) { result = start; } } } if (result == NULL) { // Check subsections for (ii = 0; ii < List_length(start->sections); ++ii) { result = Section_findById( (Section *)List_get(start->sections, ii), sectionId ); if (result != NULL) { break; } } } return result; }
END_TEST START_TEST (test_List_get) { List_add(L, "foo"); List_add(L, "bar"); fail_unless(List_size(L) == 2); fail_unless( !strcmp(List_get(L, 0), "foo") ); fail_unless( !strcmp(List_get(L, 1), "bar") ); fail_unless(List_get(L, -1) == NULL); fail_unless(List_get(L, 2) == NULL); }
/** Logs the contents of the dictionary to the logger, with the string "prefix" *** prepended to each line. This function assumes all values in the dictionary *** are strings. **/ void Dict_dump(Dict *d, char *prefix) { size_t ii; List *l = NULL; Pair *p = NULL; char *v = NULL; Buffer *b = NULL; char n[] = "(null)"; if (d != NULL) { l = (List *)d; b = new_Buffer(0); for (ii = 0; ii < List_length(l); ++ii) { p = (Pair *)List_get(l, ii); v = (char *)p->value; if (prefix != NULL) Buffer_appendString(b, prefix); Buffer_appendString(b, p->key); Buffer_appendString(b, " = "); if (v == NULL) { Buffer_appendString(b, n); } else { Buffer_appendChar(b, '\"'); Buffer_appendString(b, v); Buffer_appendChar(b, '\"'); } Logging_debugf("%s", b->data); Buffer_reset(b); } delete_Buffer(b); } else { Logging_warnf("%s: NULL argument", __FUNCTION__); } }
int main() { Post_t post = Post_new(0, 0); Post_addWantedCar(post, "007"); Person_t carOwner = Person_new("Nikita", "Silchenko"); Car_t car = Car_new("007"); Car_setOwner(car, carOwner); char buff[128]; Transist_t transist = Transist_new(car, TRANSIST_DEST_TO); Post_subscribeWantedCarTransist(post, carOwner, wantedCarPassed_Person, NULL ); Post_pass(post, transist); List_t carsTo = Post_getCars(post, TRANSIST_DEST_TO); printf("%d\n", Post_getTransistsNum(post)); int lSize = List_getSize(carsTo); for(int i = 0; i < lSize; i++){ char buff[128]; puts(Person_toString(Car_getOwner(List_get(carsTo, i, NULL)), buff)); } Post_delete(post); Transist_delete(transist); Person_delete(carOwner); Car_delete(car); return 0; }
char * DataHandler_convertToXml(DataHandler_t self, char * buff) { xmlDoc * doc = NULL; xmlNode * root = NULL; xmlChar * str = NULL; doc = xmlNewDoc("1.0"); root = xmlNewNode(NULL, "patients"); xmlDocSetRootElement(doc, root); int numberOfPatients = List_getSize(self->patients); for(int i = 0; i < numberOfPatients; i++) { patient_convertToXml(List_get(self->patients, i, NULL), doc, root); } xmlDocDumpMemory(doc, &str, NULL); strcpy(buff, str); free(str); xmlFreeDoc(doc); return buff; }
END_TEST START_TEST (test_ModelHistory_addCreator) { ModelCreator_t * newMC; ModelHistory_t * history = ModelHistory_create(); fail_unless(ModelHistory_getNumCreators(history) == 0); fail_unless(history != NULL); ModelCreator_t * mc = ModelCreator_create(); fail_unless(mc != NULL); ModelCreator_setFamilyName(mc, "Keating"); ModelCreator_setGivenName(mc, "Sarah"); ModelCreator_setEmail(mc, "*****@*****.**"); ModelCreator_setOrganisation(mc, "UH"); ModelHistory_addCreator(history, mc); fail_unless(ModelHistory_getNumCreators(history) == 1); ModelCreator_free(mc); newMC = (ModelCreator_t*) List_get(ModelHistory_getListCreators(history), 0); fail_unless(newMC != NULL); fail_unless(!strcmp(ModelCreator_getFamilyName(newMC), "Keating")); fail_unless(!strcmp(ModelCreator_getGivenName(newMC), "Sarah")); fail_unless(!strcmp(ModelCreator_getEmail(newMC), "*****@*****.**")); fail_unless(!strcmp(ModelCreator_getOrganisation(newMC), "UH")); ModelHistory_free(history); }
List_t Post_getTransists(Post_t self){ int lSize = List_getSize(self->transists); List_t retList = List_new(); for(int i = 0; i < lSize; i++){ List_append(retList, List_get(self->transists, i, NULL)); } return retList; }
List_t Post_getWantedCars(Post_t self){ int lSize = List_getSize(self->wantedCars); List_t retList = List_new(); for(int i = 0; i < lSize; i++){ List_append(retList, List_get(self->wantedCars, i, NULL)); } return retList; }
/** Removes "." and ".." from a path */ char *getCanonicalPath(const char *path){ char *prefix = NULL; char *rest = NULL; char *tmp = NULL; size_t offset = 0; char **src = NULL; List *dst = NULL; size_t ii = 0; Buffer *buf = NULL; char *result = NULL; if (path != NULL && strlen(path) > 0) { tmp = strxreplace(astrcpy(path), '\\', '/'); if (isDosPath(tmp) || isUncPath(tmp)) { if (isDosPath(tmp)) { prefix = getPathPart(tmp, PATH_DRIVE); offset = 0; } else if (isUncPath(tmp)) { prefix = getPathPart(tmp, PATH_HOST); offset = 2; } rest = astrcpy(strchr(&tmp[offset], '/')); } else { rest = astrcpy(tmp); } src = astrtok(rest, "/"); dst = new_List(); while (src[ii] != NULL) { if (strxequals(src[ii], "..")) { List_remove(dst, -1, false); } else if (!strxequals(src[ii], ".")) { List_append(dst, src[ii]); } ii++; } buf = new_Buffer(0); if (prefix != NULL) { Buffer_appendString(buf, prefix); } for (ii = 0; ii < List_length(dst); ++ii) { Buffer_appendString(buf, List_get(dst, ii)); if (ii != (List_length(dst) - 1)) { Buffer_appendChar(buf, '/'); } } result = astrcpy(buf->data); delete_Buffer(buf); delete_List(dst, false); astrtokfree(src); mu_free(prefix); mu_free(rest); mu_free(tmp); } return result; }
void *Dict_get(Dict *d, const char *key) { void *retVal = NULL; size_t idx = 0; if (keyToIndex(d, key, &idx)) { retVal = ((Pair *)List_get((List *)d, idx))->value; } return retVal; }
void deleteBook(library_t * self,const char * name){ for(int i = 0;i<List_getSize(self->book);i++){ book_t * tmpBook = List_get(self->book,i,NULL); if(strcmp(tmpBook->name,name)==0){ List_remove(self->book,i,NULL); return; } } }
bool Post_carIsWanted(Post_t self, const char * carId){ int lSize = List_getSize(self->wantedCars); for(int i = 0; i < lSize; i++){ if(!strcmp(List_get(self->wantedCars, i, NULL), carId)){ return true; } } return false; }
void Post_unsubscribeWantedCarTransist(Post_t self, void * receiver, PostEvent_CB cb){ int lSize = List_getSize(self->wantedCarEvents); for(int i = 0; i < lSize; i++){ Event_t ev = List_get(self->wantedCarEvents, i, NULL); if(ev->receiver == receiver && ev->cb == cb){ List_remove(self->wantedCarEvents, i, NULL); } } }
List_t Post_getLastNCars(Post_t self, unsigned int N){ int lSize = List_getSize(self->transists); if (N >= lSize) printf("Post: invalid index %d", N); List_t retList = List_new(); for(int i = lSize - N; i < lSize; i++){ List_append(retList, Transist_getCar(List_get(self->transists, i, NULL))); } return retList; }
/** * \brief Free a sources list * * Free a sources list as returned by SVR_getSourcesList * * \param sources_list List to free */ void SVR_freeSourcesList(List* sources_list) { char* source_name; for(int i = 0; (source_name = List_get(sources_list, i)) != NULL; i++) { free(source_name); } List_destroy(sources_list); }
List_t * getOldBooks(library_t * self,int days){ List_t neededList = List_new(); for(int i = 0;i<List_getSize(self->book);i++){ book_t * tmpBook = List_get(self->book,i,NULL); if(tmpBook->curDaysUsed >= days){ List_add(neededList,List_getSize(neededList),tmpBook); } } return neededList; }
/* * ======== MessageQ_get ======== */ Int MessageQ_get(MessageQ_Handle handle, MessageQ_Msg *msg, UInt timeout) { Int status; List_Handle highList; List_Handle normalList; ti_sdo_ipc_MessageQ_Object *obj = (ti_sdo_ipc_MessageQ_Object *)handle; /* Get the list */ normalList = ti_sdo_ipc_MessageQ_Instance_State_normalList(obj); highList = ti_sdo_ipc_MessageQ_Instance_State_highList(obj); /* Keep looping while there are no elements on either list */ *msg = (MessageQ_Msg)List_get(highList); while (*msg == NULL) { *msg = (MessageQ_Msg)List_get(normalList); if (*msg == NULL) { /* Block until notified. */ status = ISync_wait(obj->synchronizer, timeout, NULL); if (status == ISync_WaitStatus_TIMEOUT) { return (MessageQ_E_TIMEOUT); } else if (status < 0) { return (MessageQ_E_FAIL); } if (obj->unblocked) { /* *(msg) may be NULL */ return (MessageQ_E_UNBLOCKED); } *msg = (MessageQ_Msg)List_get(highList); } } if ((ti_sdo_ipc_MessageQ_traceFlag == TRUE) || (((*msg)->flags & ti_sdo_ipc_MessageQ_TRACEMASK) != 0)) { Log_write4(ti_sdo_ipc_MessageQ_LM_get, (UArg)(*msg), (UArg)((*msg)->seqNum), (UArg)((*msg)->srcProc), (UArg)(obj)); } return (MessageQ_S_SUCCESS); }
/** *** \brief Destroys the specified Dict, freeing the memory associated with it *** \param d The Dict to destroy *** \param freeData True if the storage for the items in the Dict is to be freed *** also **/ void delete_Dict(Dict *d, bool freeData) { List *l = (List *)d; Pair *p = NULL; while (List_length(l) > 0) { p = (Pair *)List_get(l, 0); mu_free(p->key); if (freeData) mu_free(p->value); List_remove(l, 0, true); } }
List_t Post_getCars(Post_t self, TRANSIST_DEST dest){ int lSize = List_getSize(self->transists); List_t retList = List_new(); for(int i = 0; i < lSize; i++){ Transist_t transist = List_get(self->transists, i, NULL); if(dest == transist->dest){ List_append(retList, Transist_getCar(transist)); } } return retList; }
END_TEST START_TEST (test_ModelHistory_addModifiedDate) { ModelHistory_t * history = ModelHistory_create(); fail_unless(history != NULL); fail_unless(ModelHistory_isSetModifiedDate(history) == 0); fail_unless(ModelHistory_getNumModifiedDates(history) == 0); Date_t * date = Date_createFromValues(2005, 12, 30, 12, 15, 45, 1, 2, 0); ModelHistory_addModifiedDate(history, date); Date_free(date); fail_unless(ModelHistory_getNumModifiedDates(history) == 1); fail_unless(ModelHistory_isSetModifiedDate(history) == 1); Date_t * newdate = (Date_t *) List_get(ModelHistory_getListModifiedDates(history), 0); fail_unless(Date_getYear(newdate) == 2005); fail_unless(Date_getMonth(newdate) == 12); fail_unless(Date_getDay(newdate) == 30); fail_unless(Date_getHour(newdate) == 12); fail_unless(Date_getMinute(newdate) == 15); fail_unless(Date_getSecond(newdate) == 45); fail_unless(Date_getSignOffset(newdate) == 1); fail_unless(Date_getHoursOffset(newdate) == 2); fail_unless(Date_getMinutesOffset(newdate) == 0); Date_t * date1 = Date_createFromValues(2008, 11, 2, 16, 42, 40, 1, 2, 0); ModelHistory_addModifiedDate(history, date1); Date_free(date1); fail_unless(ModelHistory_getNumModifiedDates(history) == 2); fail_unless(ModelHistory_isSetModifiedDate(history) == 1); Date_t * newdate1 = ModelHistory_getModifiedDateFromList(history, 1); fail_unless(Date_getYear(newdate1) == 2008); fail_unless(Date_getMonth(newdate1) == 11); fail_unless(Date_getDay(newdate1) == 2); fail_unless(Date_getHour(newdate1) == 16); fail_unless(Date_getMinute(newdate1) == 42); fail_unless(Date_getSecond(newdate1) == 40); fail_unless(Date_getSignOffset(newdate1) == 1); fail_unless(Date_getHoursOffset(newdate1) == 2); fail_unless(Date_getMinutesOffset(newdate1) == 0); ModelHistory_free(history); }
END_TEST START_TEST (test_List_remove_4) { List_add(L, "foo"); List_add(L, "bar"); List_add(L, "baz"); fail_unless( !strcmp( List_remove(L, 2), "baz" ) ); fail_unless(List_size(L) == 2); fail_unless( !strcmp( List_get(L, 0), "foo" ) ); fail_unless( !strcmp( List_get(L, 1), "bar" ) ); /* fail_unless(L->head != L->tail); fail_unless(L->tail->next == NULL ); */ }
/* * ======== DriverAdapter_reclaim ======== */ Void DriverAdapter_reclaim(DriverAdapter_Object *obj, DriverTypes_Packet **packet, Error_Block *eb) { List_Handle fromDriver; fromDriver = DriverAdapter_Instance_State_fromDriver(obj); /* Should never ever get this assert */ Assert_isTrue(!List_empty(fromDriver), DriverAdapter_A_noReadyPacket); *packet = (DriverTypes_Packet *)List_get(fromDriver); }
bool Dict_remove(Dict *d, const char *key, bool freeData) { bool retVal = false; size_t idx = 0; Pair *p = NULL; if (keyToIndex(d, key, &idx)) { p = (Pair *)List_get((List *)d, idx); if(freeData) mu_free(p->value); mu_free(p->key); retVal = List_remove((List *)d, idx, true); } return retVal; }
void Post_pass(Post_t self, Transist_t transist){ if(Post_carIsWanted(self, transist->car->id)){ int lSize = List_getSize(self->wantedCarEvents); for(int i = 0; i < lSize; i++){ Event_t ev = List_get(self->wantedCarEvents, i, NULL); if(ev->cb != NULL){ PostEvent_CB fn = ev->cb; fn(ev->receiver, self, ev->data); break; } } } List_append(self->transists, transist); }
List_t * getNeededBooks(library_t * self,int status){ List_t neededList = List_new(); for(int i = 0;i<List_getSize(self->book);i++){ book_t * tmpBook = List_get(self->book,i,NULL); if(tmpBook->status == status){ List_add(neededList,List_getSize(neededList),tmpBook); } } return neededList; }