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; } } }
int main() { List *list = NULL; char c; int i; while ((c = getchar()) != 'q') switch (c) { case 'a': { scanf("%i", &i); if (List_add(&list, i) == 1) printf("Error\n"); break; } case 'r': { scanf("%i", &i); if (List_remove(&list, i) == 1) printf("Not found =(\n"); break; } case 'p': { List_print(&list); break; } } List_delList(&list); return 0; }
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); }
//Update the clipping rectangles to only include those areas within both the //existing clipping region AND the passed Rect void Context_intersect_clip_rect(Context* context, Rect* rect) { int i; List* output_rects; Rect* current_rect; Rect* intersect_rect; context->clipping_on = 1; if(!(output_rects = List_new())) return; for(i = 0; i < context->clip_rects->count; i++) { current_rect = (Rect*)List_get_at(context->clip_rects, i); intersect_rect = Rect_intersect(current_rect, rect); if(intersect_rect) List_add(output_rects, (Object*)intersect_rect); } //Delete the original rectangle list Object_delete((Object*)context->clip_rects); //And re-point it to the new one we built above context->clip_rects = output_rects; //Free the input rect Object_delete((Object*)rect); }
void List_addonce(List *l, void *data) { if(List_contains(l, data)) return; List_add(l, data); }
void Context_add_clip_rect(Context* context, Rect* added_rect) { Context_subtract_clip_rect(context, added_rect); //Now that we have made sure none of the existing rectangles overlap //with the new rectangle, we can finally insert it List_add(context->clip_rects, (Object*)added_rect); }
END_TEST START_TEST (test_List_add_2) { List_add(L, "foo"); List_add(L, "bar"); fail_unless(List_size(L) == 2); /* fail_unless( !strcmp(L->head->item , "foo") ); fail_unless( !strcmp(L->head->next->item, "bar") ); fail_unless(L->head->next == L->tail); fail_unless(L->tail->next == NULL ); */ }
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; }
END_TEST START_TEST (test_List_remove_2) { List_add(L, "foo"); List_add(L, "bar"); fail_unless( !strcmp(List_remove(L, 1), "bar") ); fail_unless(List_size(L) == 1); fail_unless( !strcmp( List_get(L, 0), "foo" ) ); /* fail_unless(L->head == L->tail); fail_unless(L->head->next == NULL ); */ }
/* ------------------------------------------------------------ FindAnnot internal routine to optimize searching for an annotation on the list of annotations. ------------------------------------------------------------ */ static AnnotMeta* FindAnnot( List** listP, char* annot_url, ThotBool create ) { AnnotMeta *annot = find_last_annot; int url_length; char *ptr_annot_url; if (!IsW3Path (annot_url)) { ptr_annot_url = FixFileURL (annot_url); if (!ptr_annot_url) /* ignoring weird URLs @@ RRS: this was a temporary bug */ return NULL; } else ptr_annot_url = annot_url; /* uri = HTLocalToWWW (file_name, "file:"); */ if (!find_last_annotURL || strcmp(find_last_annotURL, ptr_annot_url)) { /* search for annotation in list */ annot = AnnotList_searchAnnot (*listP, ptr_annot_url, AM_ANNOT_URL); if (!annot) { if (create) { annot = AnnotMeta_new (); annot->annot_url = TtaStrdup (ptr_annot_url); List_add (listP, (void *) annot); } else { if (ptr_annot_url != annot_url) TtaFreeMemory (ptr_annot_url); return NULL; /* if lookup fails, allow create next time */ } } url_length = strlen(ptr_annot_url) + 1; if (find_last_length < url_length) { if (find_last_annotURL) TtaFreeMemory (find_last_annotURL); find_last_length = 2*url_length; find_last_annotURL = (char *)TtaGetMemory (find_last_length); } strcpy(find_last_annotURL, ptr_annot_url); find_last_annot = annot; } if (ptr_annot_url != annot_url) TtaFreeMemory (ptr_annot_url); return annot; }
END_TEST START_TEST (test_List_freeItems) { List_add(L, safe_strdup("foo")); List_add(L, safe_strdup("bar")); List_add(L, safe_strdup("baz")); fail_unless(List_size(L) == 3); List_freeItems(L, safe_free, void); fail_unless(List_size(L) == 0); /* fail_unless(L->head == NULL); fail_unless(L->tail == NULL); */ }
int Object_add_triangle(Object* object, Vertex *v1, Vertex *v2, Vertex *v3, color24 c) { Triangle* triangle; if(!(triangle = Triangle_new(v1, v2, v3, c))) return 0; if(!List_add(object->triangles, (void*)triangle)) { Triangle_delete(triangle); return 0; } }
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; }
END_TEST START_TEST (test_List_remove_1) { List_add(L, "foo"); fail_unless( !strcmp(List_remove(L, 0), "foo") ); fail_unless(List_size(L) == 0); /* fail_unless(L->head == NULL); fail_unless(L->tail == NULL); */ }
int Menu_Callback(void *pData,int cols,char **colvalu,char **colname) { pList pfirst; pfirst = (pList)pData; MenuBar *pMenu; if (pfirst->pNext == NULL) { c = 0; } pMenu = new MenuBar(NULL,3,16,1,c,colvalu[0],colvalu[1],1); pMenu->AddAction(colvalu[0]);// 添加一级菜单拥有的二级菜单,colvalu[0]为一级菜单ID List_add(pfirst,pMenu); c = c + 16; return 0; }
//split all existing clip rectangles against the passed rect void Context_subtract_clip_rect(Context* context, Rect* subtracted_rect) { //Check each item already in the list to see if it overlaps with //the new rectangle int i, j; Rect* cur_rect; List* split_rects; context->clipping_on = 1; for(i = 0; i < context->clip_rects->count; ) { cur_rect = (Rect*)List_get_at(context->clip_rects, i); //Standard rect intersect test (if no intersect, skip to next) //see here for an example of why this works: //http://stackoverflow.com/questions/306316/determine-if-two-rectangles-overlap-each-other#tab-top if(!(cur_rect->left <= subtracted_rect->right && cur_rect->right >= subtracted_rect->left && cur_rect->top <= subtracted_rect->bottom && cur_rect->bottom >= subtracted_rect->top)) { i++; continue; } //If this rectangle does intersect with the new rectangle, //we need to split it List_remove_at(context->clip_rects, i); //Original will be replaced w/splits split_rects = Rect_split(cur_rect, subtracted_rect); //Do the split Object_delete((Object*)cur_rect); //We can throw this away now, we're done with it //Copy the split, non-overlapping result rectangles into the list while(split_rects->count) { cur_rect = (Rect*)List_remove_at(split_rects, 0); List_add(context->clip_rects, (Object*)cur_rect); } //Free the empty split_rect list Object_delete((Object*)split_rects); //Since we removed an item from the list, we need to start counting over again //In this way, we'll only exit this loop once nothing in the list overlaps i = 0; } }
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); }
int MenuBar_Callback(void *pData,int cols,char **colvalu,char **colname) { pList psecond; MenuItem *pMenu; int menu_sid; static int a; psecond = (pList)pData; if (psecond->pNext == NULL) { a = 4; } pMenu = new MenuItem(NULL,3,16,a,c,colvalu[1],1); sscanf(colvalu[0],"%d",&menu_sid); pMenu->menu_sid = menu_sid;// 类里存有二级菜单的菜单ID List_add(psecond,pMenu); a = a + 3; return 0; }
//Given a parent, allocates, attaches and returns a new child TokenTree* TokenTree_add_child(TokenTree* root, char* new_type, char* new_value) { TokenTree* new_tree = TokenTree_new(new_type, new_value); if(!new_tree) return new_tree; //Insert the new tree into the parent's children list if(!List_add(root->children, (void*)new_tree)) { TokenTree_delete(new_tree); return (TokenTree*)0; } //Set the parent of the new tree to the supplied root new_tree->parent = root; return new_tree; }
void bookDeleteReader(library_t * self,const char * name){ // printf("ListSize: %i",List_getSize(self)); 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){ if(tmpBook->status == 1){ book_t * nB = newBookchangeParam(name,0,tmpBook->curDaysUsed+1,"",tmpBook->users); List_remove(self->book,i,NULL); List_add(self->book,i,nB); return; }else{ return; } } } }
END_TEST START_TEST (test_List_accessWithNULL) { // test null arguments List_add (NULL, NULL); fail_unless( List_countIf (NULL, NULL) == 0 ); fail_unless( List_find (NULL, NULL, NULL) == NULL ); fail_unless( List_findIf (NULL, NULL) == NULL ); List_free(NULL); fail_unless( List_get (NULL, 0) == NULL ); List_prepend(NULL, NULL); fail_unless( List_remove (NULL, 0) == NULL ); fail_unless( List_size (NULL) == 0 ); }
void List_append(List_t self, void * element){ List_add(self, self->size, element); }
/*----------------------------------------------------------------------- LINK_CreateMeta -----------------------------------------------------------------------*/ AnnotMeta *LINK_CreateMeta (Document source_doc, Document annot_doc, AnnotMode mode) { AnnotMeta *annot; char *annot_user; char *source_doc_url; /* the url we will use to refer to the source doc */ /* ** Make a new annotation entry, add it to annotlist, and initialize it. */ #ifdef ANNOT_ON_ANNOT if (DocumentTypes[source_doc] == docAnnot && AnnotMetaData[source_doc].annot_url) { /* we reply to an annotation (using it's metadata). However, we annotate the body of an annotation, not its metadata */ if (mode & ANNOT_isReplyTo) source_doc_url = AnnotMetaData[source_doc].annot_url; else source_doc_url = DocumentURLs[source_doc]; } else #endif /* ANNOT_ON_ANNOT */ source_doc_url = DocumentURLs[source_doc]; /* download the local annotations, if they do exist, but mark them invisible */ if (!IsW3Path (DocumentURLs[source_doc]) && (!AnnotMetaData[source_doc].annotations && !AnnotMetaData[source_doc].local_annot_loaded)) { char *annotIndex = NULL; annotIndex = LINK_GetAnnotationIndexFile (source_doc_url); LINK_LoadAnnotationIndex (source_doc, annotIndex, FALSE); AnnotMetaData[source_doc].local_annot_loaded = TRUE; } annot = AnnotMeta_new (); if (source_doc_url && (IsW3Path (source_doc_url) || IsFilePath(source_doc_url))) annot->source_url = TtaStrdup (source_doc_url); else annot->source_url = ANNOT_MakeFileURL (source_doc_url); /* get the current date... cdate = mdate at this stage */ annot->cdate = StrdupDate (); annot->mdate = TtaStrdup (annot->cdate); /* Annotation type -- will be reassigned below for Reply */ annot->type = DEFAULT_ANNOTATION_TYPE; /* Annotation XPointer */ /* annot->xptr = XPointer_build (source_doc, 1, useDocRoot);*/ annot_user = GetAnnotUser (); annot->author = TtaStrdup (annot_user); annot->content_type = TtaStrdup (AM_XHTML_MIME_TYPE); if (!IsW3Path (DocumentURLs[annot_doc]) && !IsFilePath (DocumentURLs[annot_doc])) { annot->body_url = ANNOT_MakeFileURL (DocumentURLs[annot_doc]); } else annot->body_url = TtaStrdup (DocumentURLs[annot_doc]); /* the annotation is visible to the user */ annot->is_visible = TRUE; /* display the annotation */ annot->show = TRUE; /* it's not an orphan */ annot->is_orphan = FALSE; /* create the reverse link name */ LINK_CreateAName (annot); #ifdef ANNOT_ON_ANNOT /* initialize the annot_url we will use to reference this annotation*/ AnnotMetaData[annot_doc].annot_url = TtaStrdup (annot->body_url); if (mode & ANNOT_isReplyTo) { char *source_annot_url; annot->type = DEFAULT_REPLY_TYPE; source_annot_url = AnnotMetaData[source_doc].annot_url; /* initialize the thread info */ if (!AnnotMetaData[source_doc].thread) { AnnotMetaData[source_doc].thread = &AnnotThread[source_doc]; AnnotThread[source_doc].rootOfThread = TtaStrdup (source_annot_url); } AnnotMetaData[annot_doc].thread = AnnotMetaData[source_doc].thread; annot->thread = AnnotMetaData[source_doc].thread; AnnotMetaData[source_doc].thread->references++; annot->inReplyTo = TtaStrdup (source_annot_url); /* maybe useless */ annot->rootOfThread = TtaStrdup (AnnotMetaData[source_doc].thread->rootOfThread); /* and add the annotation to the list of threads */ List_add (&(AnnotMetaData[source_doc].thread->annotations), (void *) annot); } else #endif /* ANNOT_ON_ANNOT */ { /* add the annotation to the list of annotations */ List_add (&(AnnotMetaData[source_doc].annotations), (void *) annot); } return annot; }
/*----------------------------------------------------------------------- LINK_LoadAnnotationIndex ----------------------------------------------------------------------- Searches for an annotation index related to the document. If it exists, it parses it and then, if the variable mark_visible is set true, adds links to them from the source document. -----------------------------------------------------------------------*/ void LINK_LoadAnnotationIndex (Document doc, char *annotIndex, ThotBool mark_visible) { View view; Element body; List *annot_list, *list_ptr; AnnotMeta *annot; AnnotMeta *old_annot; DisplayMode dispMode; /* counts the number of orphan annotations in the document */ int orphan_count = 0; #ifdef ANNOT_ON_ANNOT Document doc_thread; AnnotThreadList *thread; #endif /* ANNOT_ON_ANNOT */ if (!annotIndex || !(TtaFileExist (annotIndex))) /* there are no annotations */ return; annot_list = RDF_parseFile (annotIndex, &AnnotMetaData[doc].rdf_model); TtaFreeMemory (annotIndex); annotIndex = NULL; if (!annot_list) /* we didn't read any annotation */ return; /* avoid refreshing the document while adding the annotation links */ dispMode = TtaGetDisplayMode (doc); if (dispMode == DisplayImmediately) TtaSetDisplayMode (doc, DeferredDisplay); /* Insert the annotations in the body */ view = TtaGetViewFromName (doc, "Formatted_view"); body = SearchElementInDoc (doc, HTML_EL_BODY); list_ptr = annot_list; while (list_ptr) { char *url; AnnotMetaDataSearch searchType; annot = (AnnotMeta *) list_ptr->object; /* don't add an annotation if it's already on the list */ /* @@ JK: later, Ralph will add code to delete the old one. We should remove the old annotations and preserve the newer ones. Take into account that an anotation window may be open and that we'll have to close it without saving... or just update the metadata... */ if (IsW3Path (annot->annot_url)) { url = annot->annot_url; searchType = AM_ANNOT_URL; } else { url = annot->body_url; searchType = AM_BODY_URL; } old_annot = AnnotList_searchAnnot (AnnotMetaData[doc].annotations, url, searchType); #ifdef ANNOT_ON_ANNOT /* do the same thing to avoid duplicating the thread items */ if (!old_annot && AnnotThread[doc].annotations) old_annot = AnnotList_searchAnnot (AnnotThread[doc].annotations, url, searchType); #endif /* ANNOT_ON_ANNOT */ if (!old_annot) { if (mark_visible) { /* create the reverse link name */ LINK_CreateAName (annot); if (! LINK_AddLinkToSource (doc, annot)) orphan_count++; annot->is_visible = TRUE; annot->show = TRUE; } else annot->is_visible = FALSE; #ifdef ANNOT_ON_ANNOT if (annot->inReplyTo) { doc_thread = AnnotThread_searchRoot (annot->rootOfThread); /* if there's no other thread, then use the source doc as the start of the thread */ if (doc_thread == 0) { thread = NULL; doc_thread = doc; } else thread = &AnnotThread[doc_thread]; /* there was no thread. Create a new one if it's the same rootOfThread document */ if (!thread && Annot_isSameURL (AnnotMetaData[doc].annot_url, annot->rootOfThread)) { /* add the root of thread (used by load index later on) */ AnnotThread[doc].rootOfThread = TtaStrdup (annot->rootOfThread); AnnotThread[doc].references = 1; thread = &AnnotThread[doc_thread]; } /* add and show the thread item */ if (thread) { List_add (&(thread->annotations), (void *) annot); if (!AnnotMetaData[doc].thread) AnnotMetaData[doc].thread = thread; annot->thread = thread; /* ANNOT_AddThreadItem (doc_thread, annot); */ } else Annot_free (annot); } else /* not a reply */ #endif /* ANNOT_ON_ANNOT */ /* Verify that the annotates property really does point to this document and discard if not or if there is no annotates property. */ if (Annot_isSameURL (annot->source_url, DocumentURLs[doc])) List_add (&AnnotMetaData[doc].annotations, (void *) annot); else Annot_free (annot); } else Annot_free (annot); List_delFirst (&list_ptr); } #ifdef ANNOT_ON_ANNOT /* erase and redisplay the thread items */ ANNOT_DeleteThread (doc); if (AnnotThread[doc].annotations) ANNOT_BuildThread (doc); #endif /* ANNOT_ON_ANNOT */ /* show the document */ if (dispMode == DisplayImmediately) TtaSetDisplayMode (doc, dispMode); if (orphan_count) { /* warn the user there were some orphan annotations */ InitInfo ("Annotation load", "There were some orphan annotations. You may See them with the Links view."); } }
List* splitRect(Rect* rdest, Rect* rknife) { Rect baserect; List* outrect; Rect* new_rect; baserect.top = rdest->top; baserect.left = rdest->left; baserect.bottom = rdest->bottom; baserect.right = rdest->right; /* cons_prints("Splitting rect ("); cons_printDecimal(rdest->top); cons_prints(", "); cons_printDecimal(rdest->left); cons_prints(", "); cons_printDecimal(rdest->bottom); cons_prints(", "); cons_printDecimal(rdest->right); cons_prints(") with ("); cons_printDecimal(rknife->top); cons_prints(", "); cons_printDecimal(rknife->left); cons_prints(", "); cons_printDecimal(rknife->bottom); cons_prints(", "); cons_printDecimal(rknife->right); cons_prints(")\n"); */ #ifdef RECT_TEST //printf("splitting (%u, %u, %u, %u)", baserect.top, baserect.left, baserect.bottom, baserect.right); //printf("against (%u, %u, %u, %u)\n", rknife.top, rknife.left, rknife.bottom, rknife.right); #endif //RECT_TEST //prints("Allocating space for "); //printDecimal(sizeof(rect)*rect_count); //prints(" rect bytes\n"); outrect = List_new(); if(!outrect) { prints("Couldn't allocate rect space\n"); return outrect; } // cons_prints("Doing left edge split\n"); //Split by left edge if(rknife->left >= baserect.left && rknife->left <= baserect.right) { new_rect = Rect_new(baserect.top, baserect.left, baserect.bottom, rknife->left - 1); if(!new_rect) { List_delete(outrect, Rect_deleter); return (List*)0; } if(!List_add(outrect, new_rect)) { free((void*)new_rect); List_delete(outrect, Rect_deleter); return (List*)0; } baserect.left = rknife->left; } // cons_prints("Doing top edge split\n"); //Split by top edge if(rknife->top <= baserect.bottom && rknife->top >= baserect.top) { new_rect = Rect_new(baserect.top, baserect.left, rknife->top - 1, baserect.right); if(!new_rect) { List_delete(outrect, Rect_deleter); return (List*)0; } if(!List_add(outrect, new_rect)) { free((void*)new_rect); List_delete(outrect, Rect_deleter); return (List*)0; } baserect.top = rknife->top; } // cons_prints("Doing right edge split\n"); //Split by right edge if(rknife->right >= baserect.left && rknife->right <= baserect.right) { new_rect = Rect_new(baserect.top, rknife->right + 1, baserect.bottom, baserect.right); if(!new_rect) { List_delete(outrect, Rect_deleter); return (List*)0; } if(!List_add(outrect, new_rect)) { free((void*)new_rect); List_delete(outrect, Rect_deleter); return (List*)0; } baserect.right = rknife->right; } // cons_prints("Doing bottom edge split\n"); //Split by bottom edge if(rknife->bottom >= baserect.top && rknife->bottom <= baserect.bottom) { new_rect = Rect_new(rknife->bottom + 1, baserect.left, baserect.bottom, baserect.right); if(!new_rect) { List_delete(outrect, Rect_deleter); return (List*)0; } if(!List_add(outrect, new_rect)) { free((void*)new_rect); List_delete(outrect, Rect_deleter); return (List*)0; } baserect.bottom = rknife->bottom; } /* cons_prints("Result: \n"); List_for_each(outrect, new_rect, Rect*) { cons_prints(" "); cons_printDecimal(new_rect->top); cons_prints(", "); cons_printDecimal(new_rect->left); cons_prints(", "); cons_printDecimal(new_rect->bottom); cons_prints(", "); cons_printDecimal(new_rect->right); cons_prints("\n"); } scans(10, inbuf); */ return outrect; }
void drawOccluded(Window* win, Rect* baserect, List* splitrect_list) { if (!splitrect_list) return; int split_count = 0; int total_count = 1; int working_total = 0; List* out_rects; Rect* working_rects = (Rect*)0; int i, j, k; Rect *new_rect, *rect, *split_rect, *out_rect; //If there's nothing occluding us, just render the bitmap and get out of here if (!splitrect_list->count) { drawBmpRect(win, baserect); return; } out_rects = List_new(); if (!out_rects) { return; } rect = Rect_new(baserect->top, baserect->left, baserect->bottom, baserect->right); if (!rect) { List_delete(out_rects, Rect_deleter); return; } if (!List_add(out_rects, (void*)rect)) { free((void*)rect); List_delete(out_rects, Rect_deleter); return; } //For each splitting rect, split each rect in out_rects, delete the rectangle that was split, and add the resultant split rectangles List_for_each(splitrect_list, split_rect, Rect*) { List_for_each(out_rects, out_rect, Rect*) { if ((split_rect->left <= out_rect->right && split_rect->right >= out_rect->left && split_rect->top <= out_rect->bottom && split_rect->bottom >= out_rect->top)) { List* clip_list = splitRect(out_rect, split_rect); if (!clip_list) { List_delete(out_rects, Rect_deleter); return; } //If nothing was returned, we actually want to clip a rectangle in its entirety if (!clip_list->count) { List_remove(out_rects, (void*)out_rect, Rect_deleter); //If we deleted the last output rectangle, we are completely //occluded and can return early if (out_rects->count == 0) { List_delete(clip_list, Rect_deleter); List_delete(out_rects, Rect_deleter); return; } //Otherwise, go back to the top of the loop and test the next out_rect continue; } //Replace the rectangle that got split with the first result rectangle rect = (Rect*)List_get_at(clip_list, 0); out_rect->top = rect->top; out_rect->left = rect->left; out_rect->bottom = rect->bottom; out_rect->right = rect->right; //Append the rest of the result rectangles to the output collection List_for_each_skip(clip_list, rect, Rect*, 1) { new_rect = Rect_new(rect->top, rect->left, rect->bottom, rect->right); if (!new_rect) { List_delete(clip_list, Rect_deleter); List_delete(out_rects, Rect_deleter); return; } if (!List_add(out_rects, (void*)new_rect)){ free((void*)new_rect); List_delete(clip_list, Rect_deleter); List_delete(out_rects, Rect_deleter); return; } } //Free the space that was used for the split List_delete(clip_list, Rect_deleter); //Restart the list List_rewind(out_rects); } }
Window* newWindow(unsigned int width, unsigned int height, unsigned char flags, unsigned int pid) { static int next_handle = 1; Window *new_window, *temp_window; unsigned int i, bufsz; if (!(new_window = (Window*)malloc(sizeof(window)))) { printf("Coudln't allocate a new window structure"); return 0; } new_window->active = 1; new_window->pid = pid; new_window->flags = flags; new_window->x = 0; new_window->y = 0; new_window->w = width; new_window->h = height; new_window->title = (unsigned char*)0; new_window->frame_needs_redraw = 1; //Create a drawing context for the new window if (!(new_window->context = newBitmap(new_window->w, new_window->h))) { free((void*)new_window); printf("Couldn't allocate bitmap area for new window"); return (window*)0; } bufsz = new_window->w * new_window->h; //Clear new window to white for (i = 0; i < bufsz; i++) new_window->context->data[i] = RGB(255, 255, 255); new_window->handle = next_handle++; if (mouse_window) List_pop(window_list, (void*)mouse_window); //De-activate the old active window if (temp_window = (window*)List_get_at(window_list, window_list->count - (mouse_window ? 2 : 1))) { temp_window->active = 0; } if (!List_add(window_list, (void*)new_window)){ freeBitmap(new_window->context); free((void*)new_window); //re-activate the old active window if (temp_window) temp_window->active = 1; return (window*)0; } //Give the new window its initial decoration if (!(new_window->flags & WIN_UNDECORATED)) drawFrame(new_window); drawWindow(new_window, 0); //Update the titlebar on the old active window if (temp_window) drawTitlebar(temp_window, 1); if (mouse_window) { List_add(window_list, mouse_window); drawWindow(mouse_window, 0); } return new_window; }
List* Rect_split(Rect subject_rect, Rect cutting_rect) { //Allocate the list of result rectangles List* output_rects; if(!(output_rects = List_new())) return output_rects; Rect subject_copy = subject_rect; //We need a rectangle to hold new rectangles before //they get pushed into the output list Rect* temp_rect; //Begin splitting //1 -Split by left edge if that edge is between the subject's left and right edges if(rect_min_x(cutting_rect) > rect_min_x(subject_copy) && rect_min_x(cutting_rect) <= rect_max_x(subject_copy)) { //Try to make a new rectangle spanning from the subject rectangle's left and stopping before //the cutting rectangle's left if(!(temp_rect = Rect_new(rect_min_y(subject_copy), rect_min_x(subject_copy), rect_max_y(subject_copy), rect_min_x(cutting_rect) - 1))) { //If the object creation failed, we need to delete the list and exit failed kfree(output_rects); return (List*)0; } //Add the new rectangle to the output list List_add(output_rects, temp_rect); //Shrink the subject rectangle to exclude the split portion int diff = rect_min_x(cutting_rect) - rect_min_x(subject_copy); rect_min_x(subject_copy) += diff; subject_copy.size.width -= diff; } //2 -Split by top edge if that edge is between the subject's top and bottom edges if(rect_min_y(cutting_rect) > rect_min_y(subject_copy) && rect_min_y(cutting_rect) <= rect_max_y(subject_copy)) { //Try to make a new rectangle spanning from the subject rectangle's top and stopping before //the cutting rectangle's top if(!(temp_rect = Rect_new(rect_min_y(subject_copy), rect_min_x(subject_copy), rect_min_y(cutting_rect) - 1, rect_max_x(subject_copy)))) { //If the object creation failed, we need to delete the list and exit failed //This time, also delete any previously allocated rectangles for(; output_rects->count; temp_rect = List_remove_at(output_rects, 0)) kfree(temp_rect); kfree(output_rects); return (List*)0; } //Add the new rectangle to the output list List_add(output_rects, temp_rect); //Shrink the subject rectangle to exclude the split portion int diff = rect_min_y(cutting_rect) - rect_min_y(subject_copy); rect_min_y(subject_copy) += diff; subject_copy.size.height -= diff; } //3 -Split by right edge if that edge is between the subject's left and right edges if(rect_max_x(cutting_rect) >= rect_min_x(subject_copy) && rect_max_x(cutting_rect) < rect_max_x(subject_copy)) { //Try to make a new rectangle spanning from the subject rectangle's right and stopping before //the cutting rectangle's right if(!(temp_rect = Rect_new(rect_min_y(subject_copy), rect_max_x(cutting_rect) + 1, rect_max_y(subject_copy), rect_max_x(subject_copy)))) { //Free on fail for(; output_rects->count; temp_rect = List_remove_at(output_rects, 0)) kfree(temp_rect); kfree(output_rects); return (List*)0; } //Add the new rectangle to the output list List_add(output_rects, temp_rect); //Shrink the subject rectangle to exclude the split portion int shrink_amount = rect_max_x(subject_copy) - rect_max_x(cutting_rect); subject_copy.size.width -= shrink_amount; } //4 -Split by bottom edge if that edge is between the subject's top and bottom edges if(rect_max_y(cutting_rect) >= rect_min_y(subject_copy) && rect_max_y(cutting_rect) < rect_max_y(subject_copy)) { //Try to make a new rectangle spanning from the subject rectangle's bottom and stopping before //the cutting rectangle's bottom if(!(temp_rect = Rect_new(rect_max_y(cutting_rect) + 1, rect_min_x(subject_copy), rect_max_y(subject_copy), rect_max_x(subject_copy)))) { //Free on fail for(; output_rects->count; temp_rect = List_remove_at(output_rects, 0)) kfree(temp_rect); kfree(output_rects); return (List*)0; } //Add the new rectangle to the output list List_add(output_rects, temp_rect); //Shrink the subject rectangle to exclude the split portion int shrink_amount = rect_max_y(subject_copy) - rect_max_y(cutting_rect); subject_copy.size.height -= shrink_amount; } //Finally, after all that, we can return the output rectangles return output_rects; }
void PlatformWrapper_install_audio_handler(AudioHandler* audio_handler) { List_add(global_win->ah_list, (Object*)audio_handler); }
SentenceList_ErrorStatus SentenceList_add(SentenceList_t list, unsigned int index, Sentence_t sentence){ if (!Sentence_is_sentence(sentence)) return SENTENCE_LIST_NOT_SENTENCE; return (SentenceList_ErrorStatus)List_add(list, index, sentence); }