DO NOT REPLY TO THIS MESSAGE.  INSTEAD, POST ANY RESPONSES TO THE LINK BELOW.

[STR New]

Link: http://www.fltk.org/str.php?L2494
Version: 1.3-feature


This patch adds simple macro capability to the extra code lines in fluid:

[email protected]_data = @name;

If an extra code line starts with "=" then the "=" will be removed and any
occurrence of "@name" will be replaced by the widget name entered and every
occurrence of "@class" will be replaced by the classname (default or user
entered).

Supposing the above line on an "Fl_Input" with a name of "age" will be
converted to:

db_vat_rates.Fl_Input.user_data = age;


Link: http://www.fltk.org/str.php?L2494
Version: 1.3-feature
Index: Fl_Widget_Type.cxx
===================================================================
--- Fl_Widget_Type.cxx  (revision 8095)
+++ Fl_Widget_Type.cxx  (working copy)
@@ -2365,10 +2365,69 @@
   }
 }
 
+char * replace(
+    char const * const original,
+    char const * const pattern,
+    char const * const replacement
+) {
+  size_t const replen = strlen(replacement);
+  size_t const patlen = strlen(pattern);
+  size_t const orilen = strlen(original);
+
+  size_t patcnt = 0;
+  const char * oriptr;
+  const char * patloc;
+
+  // find how many times the pattern occurs in the original string
+  for (oriptr = original; (patloc = strstr(oriptr, pattern)); oriptr = patloc 
+ patlen)
+  {
+    patcnt++;
+  }
+
+  {
+    // allocate memory for the new string
+    size_t const retlen = orilen + patcnt * (replen - patlen);
+    char * const returned = (char *) malloc( sizeof(char) * (retlen + 1) );
+
+    if (returned != NULL)
+    {
+      // copy the original string,
+      // replacing all the instances of the pattern
+      char * retptr = returned;
+      for (oriptr = original; (patloc = strstr(oriptr, pattern)); oriptr = 
patloc + patlen)
+      {
+        size_t const skplen = patloc - oriptr;
+        // copy the section until the occurence of the pattern
+        strncpy(retptr, oriptr, skplen);
+        retptr += skplen;
+        // copy the replacement
+        strncpy(retptr, replacement, replen);
+        retptr += replen;
+      }
+      // copy the rest of the string.
+      strcpy(retptr, oriptr);
+    }
+    return returned;
+  }
+}
+
 void Fl_Widget_Type::write_extra_code() {
-  for (int n=0; n < NUM_EXTRA_CODE; n++)
-    if (extra_code(n) && !isdeclare(extra_code(n)))
-      write_c("%s%s\n", indent(), extra_code(n));
+  for (int n=0; n < NUM_EXTRA_CODE; n++){
+    char *pc, *ec = (char*)extra_code(n);
+    if (ec && !isdeclare(ec)){
+      if(ec[0] == '='){ //do macro substitutions
+          pc = ++ec; //skip "=" 
+          const char *nm = name();
+          const char *nk = subclassname(this);
+          if(nm) ec = replace(ec, "@name", nm);
+          if(nk) pc = replace(ec, "@class", nk);
+          write_c("%s%s\n", indent(), pc);
+          if(nm) free(ec);
+          if(nk) free(pc);
+      }
+      else write_c("%s%s\n", indent(), ec);
+    }
+  }
 }
 
 void Fl_Widget_Type::write_block_close() {
_______________________________________________
fltk-dev mailing list
[email protected]
http://lists.easysw.com/mailman/listinfo/fltk-dev

Reply via email to