On Wednesday 27 November 2002 3:24 pm, Angus Leeming wrote:
> Which is preferable, more elegant generated C++ code or simpler code used
> to generate it? The attached fdfix.diff is used to create the diff below.
>
> Should I commit fdfix.diff or leave things as they are?
> Angus

What it suggests is that a single pass is unnecessarily compilicated. Two 
passes gives the elegant C++ code whilst keeping the sed clean too. I'll 
apply this, I think.

Angus
? fdfix.diff
Index: c_str.sed
===================================================================
RCS file: c_str.sed
diff -N c_str.sed
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ c_str.sed	27 Nov 2002 16:30:50 -0000
@@ -0,0 +1,39 @@
+# file c_str.sed
+# This file is part of LyX, the document processor.
+# Licence details can be found in the file COPYING.
+#
+# author Angus Leeming
+#
+# Full author contact details are available in file CREDITS
+
+# This sed script is run on the .C file after the main fdfixc.sed has done
+# its stuff. It ensures that any c_str variables inserted by fdfixc.sed
+# are declared at the top of the appropriate function.
+
+# We use a two-pass algorithm like this because a single pass results in
+# convoluted sed.
+
+# Initialise the hold space at the start of the function
+/ \* build_/ {
+h; d
+}
+
+# For all lines within the function...
+/^{$/,/^}$/ {
+
+# If it isn't the last line, append it to the hold space.
+/^}$/!{
+H; d;
+}
+
+# If it is the last line, paste the contents of the hold space above it,
+# seach for the string "c_str" and, if found, add its declaration to the top
+# of the function.
+/^}$/ {
+x; G
+
+/c_str/s/\(	FL_OBJECT \*\)/	char const * c_str;\
+\1/
+}
+
+}
Index: fdfix.sh
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/frontends/xforms/forms/fdfix.sh,v
retrieving revision 1.22
diff -u -p -r1.22 fdfix.sh
--- fdfix.sh	26 Nov 2002 19:29:39 -0000	1.22
+++ fdfix.sh	27 Nov 2002 16:30:50 -0000
@@ -117,31 +117,29 @@ CPATCH=${DIRNAME}/${BASENAME}.C.patch
 COUT=${BASENAME}.cpp
 FINAL_COUT=${BASENAME}.C
 
-FDFIXC=${DIRNAME}/fdfixc.sed
+# We use a two pass algorithm to generate elegant C++ code whilst
+# keeping the sed clean also.
 
-OUTPUT_FILE=${COUT}; INTRO_MESSAGE
+# Pass 1. The bulk of the clean-up
+FDFIXC=${DIRNAME}/fdfixc.sed
+TMP=tmp
+OUTPUT_FILE=${TMP}; INTRO_MESSAGE
 
-# This "c_str" is potentially used many times in many functions
-# so add it to the top of the generated file.
-grep -E 'fl_add.*".*[|].*"' ${CIN} > /dev/null &&
-	cat - >> ${COUT} <<EOF
-namespace {
-char const * c_str;
-} // namespace anon
-
-
-EOF
-
-echo "#include <config.h>" >> ${COUT}
-echo "#include \"forms_gettext.h\"" >> ${COUT}
-echo "#include \"gettext.h\"" >> ${COUT}
+echo "#include <config.h>" >> ${TMP}
+echo "#include \"forms_gettext.h\"" >> ${TMP}
+echo "#include \"gettext.h\"" >> ${TMP}
 
 grep bmtable ${CIN} > /dev/null &&
-	echo "#include \"bmtable.h\"" >> ${COUT}
+	echo "#include \"bmtable.h\"" >> ${TMP}
+
+sed -f ${FDFIXC} < ${CIN} >> ${TMP}
 
-sed -f ${FDFIXC} < ${CIN} >> ${COUT}
+# Pass 2. Ensure that any c_str variables inserted by fdfixc.sed
+# are declared at the top of the appropriate function.
+FDFIXC=${DIRNAME}/c_str.sed
+sed -f ${FDFIXC} < ${TMP} > ${COUT}
+rm -f ${TMP}
 
-# Patch the .C file if a patch exists
 if [ -f "${CPATCH}" ] ; then
 	echo "Patching ${COUT} with ${CPATCH}"
 	patch -s ${COUT} < ${CPATCH}

Reply via email to