summaryrefslogtreecommitdiff
path: root/misc.c
diff options
context:
space:
mode:
authorJohn Millaway <john43@users.sourceforge.net>2002-08-14 00:46:59 +0000
committerJohn Millaway <john43@users.sourceforge.net>2002-08-14 00:46:59 +0000
commit318787eade383c5b8ed3a65ff7adb212a88d3aa9 (patch)
tree66331dfcafc8a11e85665d0d6e4df4519e062baa /misc.c
parent2de61eb3df07519dfa02fbc44ab244603d1a901e (diff)
Start condition prefixes attempts to adjust to user preferences.
Diffstat (limited to 'misc.c')
-rw-r--r--misc.c102
1 files changed, 102 insertions, 0 deletions
diff --git a/misc.c b/misc.c
index 5a86a23..2d5c9e7 100644
--- a/misc.c
+++ b/misc.c
@@ -933,3 +933,105 @@ char* chomp(str)
*p-- = 0;
return str;
}
+
+/* Converts str to lowercase in place. returns str*/
+char * strlower (str)
+ char* str;
+{
+ char * s = str;
+ if (str)
+ for (s=str; *s; s++)
+ *s = tolower(*s);
+ return str;
+}
+
+/* Converts str to uppercase in place. returns str*/
+char * strupper (str)
+ char* str;
+{
+ char * s;
+ if (str)
+ for (s=str; *s; s++)
+ *s = toupper(*s);
+ return str;
+}
+
+/* return < 0 if prefers lowercase
+ * return > 0 if prefers uppercase
+ * return 0 if no preference or it looks like first-letter-capitalization
+ * Similar to strcmp(), the absolute value of the return value is larger
+ * the more characters are upper or lowercase.
+ */
+int case_preference (str)
+ const char* str;
+{
+ int nup=0, nlow=0, first_up=0;
+ const char* s;
+ if (!str)
+ return 0;
+
+ /* find the first upper or lowercase letter */
+ for (s=str; *s; s++){
+ if (!isupper(*s) && !islower(*s))
+ continue;
+ first_up = isupper(*s);
+ break;
+ }
+
+ for (s=str; *s; s++){
+ if (isupper(*s))
+ nup++;
+ if (islower(*s))
+ nlow++;
+ }
+
+ return (first_up && nlow > nup) ? 0 : nup - nlow;
+}
+
+/* Creates a name-space friendly start condition name, safe for
+ * use in the generated header.
+ * buf should be large enough to contain name and prefix.
+ * returns buf
+ */
+char * fix_scname (buf, name)
+ char * buf;
+ const char * name;
+{
+ char * pre;
+ int cn,cp;
+
+ buf[0]= '\0';
+ if (!name)
+ return buf;
+
+ /* we do comparisons in lowercase */
+ pre = copy_string(prefix);
+ strlower(pre);
+ strcpy(buf,name);
+ strlower(buf);
+
+ /* If the user has already prefixed name, then we leave it alone. */
+ if (strncmp(buf,pre,strlen(pre))==0 && strlen(name) > strlen(pre)){
+ strcpy(buf,name);
+ free(pre);
+ return buf;
+ }
+
+ /* Build the new name */
+ sprintf(buf,"%ssc_%s", prefix, name);
+
+ /* Decide whether the user prefers upper or lowercase or neither */
+ cp = case_preference(prefix);
+ cn = case_preference(name);
+
+ if (cn != 0) {
+ /* If it's mostly lowercase... */
+ if (cn < 0 && (0 - cn) > strlen(name)/2)
+ strlower(buf);
+ else if (cn > 0 && cn > strlen(name)/2)
+ strupper(buf);
+ }
+
+ free(pre);
+ return buf;
+}