Code Cleanup: style and redundant casts
[blender.git] / source / blender / blenkernel / intern / suggestions.c
index e1e872c42b6107d3f5d9626ce5c62312ecf018d3..d46fa591885f2481a582c9094f1c8bf1c89188ec 100644 (file)
@@ -47,11 +47,12 @@ static SuggList suggestions = {NULL, NULL, NULL, NULL, NULL};
 static char *documentation = NULL;
 //static int doc_lines = 0;
 
+/* TODO, replace with  BLI_strncasecmp() */
 static int txttl_cmp(const char *first, const char *second, int len)
 {
        int cmp, i;
-       for (cmp=0, i=0; i<len; i++) {
-               if ( (cmp= toupper(first[i])-toupper(second[i])) ) {
+       for (cmp = 0, i = 0; i < len; i++) {
+               if ((cmp = toupper(first[i]) - toupper(second[i]))) {
                        break;
                }
        }
@@ -61,7 +62,7 @@ static int txttl_cmp(const char *first, const char *second, int len)
 static void txttl_free_suggest(void)
 {
        SuggItem *item, *prev;
-       for (item = suggestions.last; item; item=prev) {
+       for (item = suggestions.last; item; item = prev) {
                prev = item->prev;
                MEM_freeN(item);
        }
@@ -104,7 +105,7 @@ void texttool_text_clear(void)
 
 short texttool_text_is_active(Text *text)
 {
-       return activeToolText==text ? 1 : 0;
+       return activeToolText == text ? 1 : 0;
 }
 
 /***************************/
@@ -113,28 +114,28 @@ short texttool_text_is_active(Text *text)
 
 void texttool_suggest_add(const char *name, char type)
 {
+       const int len = strlen(name);
+       int cmp;
        SuggItem *newitem, *item;
-       int len, cmp;
 
-       newitem = MEM_mallocN(sizeof(SuggItem) + strlen(name) + 1, "SuggestionItem");
+       newitem = MEM_mallocN(sizeof(SuggItem) + len + 1, "SuggItem");
        if (!newitem) {
                printf("Failed to allocate memory for suggestion.\n");
                return;
        }
 
        newitem->name = (char *) (newitem + 1);
-       len = strlen(name);
-       strncpy(newitem->name, name, len);
-       newitem->name[len] = '\0';
+       memcpy(newitem->name, name, len + 1);
        newitem->type = type;
        newitem->prev = newitem->next = NULL;
 
        /* Perform simple linear search for ordered storage */
        if (!suggestions.first || !suggestions.last) {
                suggestions.first = suggestions.last = newitem;
-       } else {
+       }
+       else {
                cmp = -1;
-               for (item=suggestions.last; item; item=item->prev) {
+               for (item = suggestions.last; item; item = item->prev) {
                        cmp = txttl_cmp(name, item->name, len);
 
                        /* Newitem comes after this item, insert here */
@@ -159,30 +160,31 @@ void texttool_suggest_add(const char *name, char type)
                }
        }
        suggestions.firstmatch = suggestions.lastmatch = suggestions.selected = NULL;
-       suggestions.top= 0;
+       suggestions.top = 0;
 }
 
-void texttool_suggest_prefix(const char *prefix)
+void texttool_suggest_prefix(const char *prefix, const int prefix_len)
 {
        SuggItem *match, *first, *last;
-       int cmp, len = strlen(prefix), top = 0;
+       int cmp, top = 0;
 
        if (!suggestions.first) return;
-       if (len==0) {
+       if (prefix_len == 0) {
                suggestions.selected = suggestions.firstmatch = suggestions.first;
                suggestions.lastmatch = suggestions.last;
                return;
        }
        
        first = last = NULL;
-       for (match=suggestions.first; match; match=match->next) {
-               cmp = txttl_cmp(prefix, match->name, len);
-               if (cmp==0) {
+       for (match = suggestions.first; match; match = match->next) {
+               cmp = txttl_cmp(prefix, match->name, prefix_len);
+               if (cmp == 0) {
                        if (!first) {
                                first = match;
                                suggestions.top = top;
                        }
-               } else if (cmp<0) {
+               }
+               else if (cmp < 0) {
                        if (!last) {
                                last = match->prev;
                                break;
@@ -195,7 +197,8 @@ void texttool_suggest_prefix(const char *prefix)
                suggestions.firstmatch = first;
                suggestions.lastmatch = last;
                suggestions.selected = first;
-       } else {
+       }
+       else {
                suggestions.firstmatch = NULL;
                suggestions.lastmatch = NULL;
                suggestions.selected = NULL;
@@ -251,12 +254,13 @@ void texttool_docs_show(const char *docs)
        }
 
        /* Ensure documentation ends with a '\n' */
-       if (docs[len-1] != '\n') {
-               documentation = MEM_mallocN(len+2, "Documentation");
+       if (docs[len - 1] != '\n') {
+               documentation = MEM_mallocN(len + 2, "Documentation");
                strncpy(documentation, docs, len);
                documentation[len++] = '\n';
-       } else {
-               documentation = MEM_mallocN(len+1, "Documentation");
+       }
+       else {
+               documentation = MEM_mallocN(len + 1, "Documentation");
                strncpy(documentation, docs, len);
        }
        documentation[len] = '\0';