Code Cleanup: style and redundant casts
[blender.git] / source / blender / blenkernel / intern / suggestions.c
index dee3365633264051cccd0751af3b7d17482fd08a..d46fa591885f2481a582c9094f1c8bf1c89188ec 100644 (file)
@@ -1,6 +1,4 @@
-/**
- * $Id$
- *
+/*
  * ***** BEGIN GPL LICENSE BLOCK *****
  *
  * This program is free software; you can redistribute it and/or
@@ -15,7 +13,7 @@
  *
  * You should have received a copy of the GNU General Public License
  * along with this program; if not, write to the Free Software Foundation,
- * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  *
  * The Original Code is Copyright (C) 2008, Blender Foundation
  * All rights reserved.
  * ***** END GPL LICENSE BLOCK *****
  */
 
+/** \file blender/blenkernel/intern/suggestions.c
+ *  \ingroup bke
+ */
+
+
 #include <stdlib.h>
 #include <string.h>
 #include <ctype.h>
 
 #include "MEM_guardedalloc.h"
-#include "BLI_blenlib.h"
 #include "DNA_text_types.h"
-#include "BKE_text.h"
 #include "BKE_suggestions.h"
 
 /**********************/
@@ -46,19 +47,22 @@ static SuggList suggestions = {NULL, NULL, NULL, NULL, NULL};
 static char *documentation = NULL;
 //static int doc_lines = 0;
 
-static int txttl_cmp(const char *first, const char *second, int len) { 
+/* 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;
                }
        }
        return cmp;
 }
 
-static void txttl_free_suggest() {
+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);
        }
@@ -68,7 +72,8 @@ static void txttl_free_suggest() {
        suggestions.top = 0;
 }
 
-static void txttl_free_docs() {
+static void txttl_free_docs(void)
+{
        if (documentation) {
                MEM_freeN(documentation);
                documentation = NULL;
@@ -79,53 +84,58 @@ static void txttl_free_docs() {
 /* General tool functions */
 /**************************/
 
-void free_texttools() {
+void free_texttools(void)
+{
        txttl_free_suggest();
        txttl_free_docs();
 }
 
-void texttool_text_set_active(Text *text) {
+void texttool_text_set_active(Text *text)
+{
        if (activeToolText == text) return;
        texttool_text_clear();
        activeToolText = text;
 }
 
-void texttool_text_clear() {
+void texttool_text_clear(void)
+{
        free_texttools();
        activeToolText = NULL;
 }
 
-short texttool_text_is_active(Text *text) {
-       return activeToolText==text ? 1 : 0;
+short texttool_text_is_active(Text *text)
+{
+       return activeToolText == text ? 1 : 0;
 }
 
 /***************************/
 /* Suggestion list methods */
 /***************************/
 
-void texttool_suggest_add(const char *name, char type) {
+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 */
@@ -150,29 +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;
@@ -185,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;
@@ -193,27 +206,33 @@ void texttool_suggest_prefix(const char *prefix) {
        }
 }
 
-void texttool_suggest_clear() {
+void texttool_suggest_clear(void)
+{
        txttl_free_suggest();
 }
 
-SuggItem *texttool_suggest_first() {
+SuggItem *texttool_suggest_first(void)
+{
        return suggestions.firstmatch;
 }
 
-SuggItem *texttool_suggest_last() {
+SuggItem *texttool_suggest_last(void)
+{
        return suggestions.lastmatch;
 }
 
-void texttool_suggest_select(SuggItem *sel) {
+void texttool_suggest_select(SuggItem *sel)
+{
        suggestions.selected = sel;
 }
 
-SuggItem *texttool_suggest_selected() {
+SuggItem *texttool_suggest_selected(void)
+{
        return suggestions.selected;
 }
 
-int *texttool_suggest_top() {
+int *texttool_suggest_top(void)
+{
        return &suggestions.top;
 }
 
@@ -221,7 +240,8 @@ int *texttool_suggest_top() {
 /* Documentation methods */
 /*************************/
 
-void texttool_docs_show(const char *docs) {
+void texttool_docs_show(const char *docs)
+{
        int len;
 
        if (!docs) return;
@@ -234,21 +254,24 @@ 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';
 }
 
-char *texttool_docs_get() {
+char *texttool_docs_get(void)
+{
        return documentation;
 }
 
-void texttool_docs_clear() {
+void texttool_docs_clear(void)
+{
        txttl_free_docs();
 }