testXmlPrint.main.c
/******************************************** -*-c-*- ***********************************************/
/*                                                                                                  */
/* testXmlPrint.main.c                                                                              */
/*                                                                                                  */
/****************************************************************************************************/

/****************************************************************************************************/
/*                                                                                                  */
/*     Copyright (C) 2004 Joerg Kunze                                                               */
/*                                                                                                  */
/*     This file is part of siliconBrainLib.                                                        */
/*                                                                                                  */
/*     siliconBrainLib is free software; you can redistribute it and/or modify                      */
/*     it under the terms of the GNU General Public License as published by                         */
/*     the Free Software Foundation; either version 2 of the License, or                            */
/*     (at your option) any later version.                                                          */
/*                                                                                                  */
/*     siliconBrainLib is distributed in the hope that it will be useful,                           */
/*     but WITHOUT ANY WARRANTY; without even the implied warranty of                               */
/*     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                                */
/*     GNU General Public License for more details.                                                 */
/*                                                                                                  */
/*     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                    */
/*                                                                                                  */
/****************************************************************************************************/
char *siliconBrainRelease       = "$siliconBrainRelease: 0.0.4 $";
char *siliconBrainRcsIdentifier = "$Id: testXmlPrint.main.c,v 1.15 2004/12/14 23:09:01 joerg Exp $";
char *siliconBrainSaveStamp     = "$siliconBrainSaveStamp: 2004/12/14 22:31:17, Joerg Kunze$";

/****************************************************************************************************/
/*                                                                                                  */
/* include:                                                                                         */
/*                                                                                                  */
/****************************************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/stat.h>

#define siliconBrainPrintShortNames

#include "siliconBrainLib"

/****************************************************************************************************/
/*                                                                                                  */
/* generatedXml:                                                                                    */
/*                                                                                                  */
/****************************************************************************************************/
static void generateXml( const char *generatedName, int argc, const char *argv[] ) {

   if( !fork() ) {
      FILE *generated = freopen( generatedName, "w", stdout );

      SiliconBrainPrinter siliconBrainPrinter;
      siliconBrainPrinterInit( &siliconBrainPrinter, argc, argv );

      tag( "joerg" );
         tag( "command" );
            attribute( "name", "myCommand" );
            attribute( "nameNull", 0 );
            tag( "command2" );
            end();
            comment( "Next tag has an attribute with a ' in its value" );
            comment( 0 );
            tag( "command2" );
                attribute( "name", "myCommand'2" );
            end();
            comment( "This comment\nhas a newline in it" );
            tag( "description" );
               text(
               "first line\n"
               "bla bla\n"
               "long line:fd fdsf asdfasd <well/> dasfasdf asdf asdf asdf sadf sadf asdf asdfsdafsad fasdf asdf asdf asdfasdf asdf asdf asdfasd f adsf\n"
               "fo fooo\n"
               "last line\n"
               );
               text( "And yet another line" );
               text( 0 );
            end();
         end();
      end();

      tag( "siliconBrain" );
         tag( "ftpHost" ); text( "siliconbrain.com" ); end();
         tag( "ftpDir" ); text( "pub/ftp" ); end();
         tag( "foo" ); text( "bar" ); end();
         tag( "ute" ); text( "joerg" ); end();
         key( "ute2", "tim" );
         key( "ute32", "florian" );
         key( "ute432", "katja" );
         key( "ute4321", 0 );
         keyBool( "boolTrue" , true  );
         keyBool( "boolFalse", false );
      end();

      destroy();

      fclose( generated );

      exit( 0 );
   }
}

/****************************************************************************************************/
/*                                                                                                  */
/* expectedOutput:                                                                                  */
/*                                                                                                  */
/****************************************************************************************************/
static void expectedOutput( const char *expectedName, const char *outputString ) {
   if( !fork() ) {
      FILE *expected = fopen( expectedName, "w" );

      fprintf( expected, outputString );
      fclose( expected );

      exit( 0 );
   }
}

/****************************************************************************************************/
/*                                                                                                  */
/* main:                                                                                            */
/*                                                                                                  */
/****************************************************************************************************/
int main( int argc, const char *argv[] ) {

   char generatedName[] = "temporary/testXmlPrint.generatedOutput";
   char expectedName [] = "temporary/testXmlPrint.expectedOutput";

   int returnCode = 0;

   /*------------------------------------------------------------------------------------------------*/
   puts( "testXmlPrint: Testing generated XML ..." );
   /*------------------------------------------------------------------------------------------------*/

   mkfifo( generatedName, 0600 );
   mkfifo( expectedName , 0600 );

   char *diffCommand;

   asprintf( &diffCommand, "diff %s %s", expectedName, generatedName );

   /*------------------------------------------------------------------------------------------------*/
   puts( "testXmlPrint: Testing non indented output ..." );
   /*------------------------------------------------------------------------------------------------*/
   generateXml( generatedName, 0, 0 );

   expectedOutput( expectedName,
      "<joerg>\n"
      "<command name=\"myCommand\">\n"
      "<command2/>\n"
      "<command2 name=\"myCommand&apos;2\"/>\n"
      "<description> first line\n"
      "bla bla\n"
      "long line:fd fdsf asdfasd &lt;well/&gt; dasfasdf asdf asdf asdf sadf sadf asdf asdfsdafsad fasdf asdf asdf asdfasdf asdf asdf asdfasd f adsf\n"
      "fo fooo\n"
      "last line\n"
      "And yet another line </description>\n"
      "</command>\n"
      "</joerg>\n"
      "<siliconBrain>\n"
      "<ftpHost> siliconbrain.com </ftpHost>\n"
      "<ftpDir> pub/ftp </ftpDir>\n"
      "<foo> bar </foo>\n"
      "<ute> joerg </ute>\n"
      "<ute2> tim </ute2>\n"
      "<ute32> florian </ute32>\n"
      "<ute432> katja </ute432>\n"
      "<boolTrue/>\n"
      "<boolFalse> false </boolFalse>\n"
      "</siliconBrain>\n"
   );

   if( returnCode = system( diffCommand ) ) {
      fprintf( stderr, "%s: %s: %d: Error %d in %s\n", __FILE__, __FUNCTION__, __LINE__, WEXITSTATUS( returnCode ), "print XML" );
      exit( 42 );
   }

   /*------------------------------------------------------------------------------------------------*/
   puts( "testXmlPrint: Testing indented output ..." );
   /*------------------------------------------------------------------------------------------------*/
   setenv( "siliconBrainLibPrintIndent", "true", true );

   generateXml( generatedName, 0, 0 );

   expectedOutput( expectedName,
      "<joerg>\n"
      "   <command name=\"myCommand\">\n"
      "      <command2/>\n"
      "\n"
      "      <!-- Next tag has an attribute with a ' in its value -->\n"
      "      <command2 name=\"myCommand&apos;2\"/>\n"
      "\n"
      "      <!-- This comment\n"
      "         has a newline in it -->\n"
      "      <description> first line\n"
      "         bla bla\n"
      "         long line:fd fdsf asdfasd &lt;well/&gt; dasfasdf asdf asdf asdf sadf sadf asdf asdfsdafsad fasdf asdf asdf asdfasdf asdf asdf asdfasd f adsf\n"
      "         fo fooo\n"
      "         last line\n"
      "         And yet another line </description>\n"
      "   </command>\n"
      "</joerg>\n"
      "<siliconBrain>\n"
      "   <ftpHost> siliconbrain.com </ftpHost>\n"
      "   <ftpDir> pub/ftp </ftpDir>\n"
      "   <foo> bar </foo>\n"
      "   <ute> joerg </ute>\n"
      "   <ute2> tim </ute2>\n"
      "   <ute32> florian </ute32>\n"
      "   <ute432> katja </ute432>\n"
      "   <boolTrue/>\n"
      "   <boolFalse> false </boolFalse>\n"
      "</siliconBrain>\n"
   );

   if( returnCode = system( diffCommand ) ) {
      fprintf( stderr, "%s: %s: %d: Error %d in %s\n", __FILE__, __FUNCTION__, __LINE__, WEXITSTATUS( returnCode ), "print XML" );
      exit( 42 );
   }

   /*------------------------------------------------------------------------------------------------*/
   puts( "testXmlPrint: Testing perl output ..." );
   /*------------------------------------------------------------------------------------------------*/
   const char *perlArgv[] = { "testXmlPrint", "--perl", 0 };
   int   perlArgc   = sizeof perlArgv / sizeof perlArgv[0];

   generateXml( generatedName, perlArgc, perlArgv );

   expectedOutput( expectedName,
      "\n"
      "# Usage: eval `<thisCommand> --perl`;\n"
      "$joerg='<configured>';\n"
      "   $command='<configured>';\n"
      "      $name='myCommand';\n"
      "      $command2='<configured>';\n"
      "\n"
      "      # Next tag has an attribute with a ' in its value\n"
      "      $command2='<configured>';\n"
      "         $name='myCommand\\'2';\n"
      "\n"
      "\n"
      "      # This comment\n"
      "      # has a newline in it\n"
      "      $description='first line\n"
      "         bla bla\n"
      "         long line:fd fdsf asdfasd <well/> dasfasdf asdf asdf asdf sadf sadf asdf asdfsdafsad fasdf asdf asdf asdfasdf asdf asdf asdfasd f adsf\n"
      "         fo fooo\n"
      "         last line\n"
      "         And yet another line';\n"
      "\n"
      "\n"
      "$siliconBrain='<configured>';\n"
      "   $ftpHost='siliconbrain.com';\n"
      "   $ftpDir='pub/ftp';\n"
      "   $foo='bar';\n"
      "   $ute='joerg';\n"
      "   $ute2='tim';\n"
      "   $ute32='florian';\n"
      "   $ute432='katja';\n"
      "   $boolTrue=1;\n"
      "   $boolFalse=0;\n"
      "\n"
   );

   if( returnCode = system( diffCommand ) ) {
      fprintf( stderr, "%s: %s: %d: Error %d in %s\n", __FILE__, __FUNCTION__, __LINE__, WEXITSTATUS( returnCode ), "print XML" );
      exit( 42 );
   }

   free( diffCommand );

   return 0;
}

/*
$Log: testXmlPrint.main.c,v $
Revision 1.15  2004/12/14 23:09:01  joerg
published for new release 0.0.4

Revision 1.14  2004/12/14 23:02:56  joerg
published for new release 0.0.3

Revision 1.13  2004/12/14 22:44:36  joerg
allFiles: all sources have a Log CVS keyword at the end now.

*/