[PATCH] (#2) Gettext save: be sure that comment lines start with #

Nicolas Goutte <[email protected]> Fri, 28 Oct 2005 22:07:22 +0200
Newsgroups gmane.comp.kde.devel.kbabel
Message-ID <[email protected]>
The attached patch is for kdesdk/kbabel/filters/gettext/gettextexport.cpp 

The change makes sure that, while saving Gettext, each comment line starts 
with a #, even if the user has not taken care about it.

Compared to the previous version of the patch, this one checks the white 
space.

(However I have seen that leading spaces and empty lines are removed anyway at 
thenext load by KBabel or by processing by Gettext tools.)

Have a nice day!

_______________________________________________
kbabel mailing list
[email protected]
https://mail.kde.org/mailman/listinfo/kbabel
gettext_comment2.diff (text/x-diff, 1.8 KB)
Index: gettextexport.cpp
===================================================================
--- gettextexport.cpp	(Revision 475265)
+++ gettextexport.cpp	(Arbeitskopie)
@@ -158,10 +158,48 @@
 	  }
 	  
 	  // write entry
-	  QString comment = catalog->comment(counter);
+	  const QString comment = catalog->comment(counter);
 	  if(!comment.isEmpty())
 	  {
-	      stream << comment << "\n";
+              // We must check that each comment line really starts with a #, to avoid syntax errors
+              int pos = 0;
+              for(;;)
+              {
+                  const int newpos = comment.find( '\n', pos, false );
+                  if ( newpos == pos )
+                  {
+                    ++pos;
+                    stream << "\n";
+                    continue;
+                  }
+                  const QString span ( ( newpos == -1 ) ? comment.mid( pos ) : comment.mid( pos, newpos-pos ) );
+                  
+                  const int len = span.length();
+                  for ( int i = 0 ; i < len ; ++i )
+                  {
+                      const QChar& ch = span[ i ];
+                      if ( ch == '#' )
+                      {
+                          stream << span.mid( i );
+                          break;
+                      }
+                      else if ( ch == ' ' || ch == '\t' )
+                      {
+                          stream << ch;
+                      }
+                      else
+                      {
+                          stream << "# " << span.mid( i );
+                          break;
+                      }
+                  }
+                  stream << "\n";
+
+                  if ( newpos == -1 )
+                      break;
+                  else
+                      pos = newpos + 1;
+              }
 	  }
 
 	  if(!catalog->msgctxt(counter).isEmpty())