Back to index

tetex-bin  3.0
log.c
Go to the documentation of this file.
00001 /* Log file output.
00002    Copyright (C) 2003 Free Software Foundation, Inc.
00003 
00004    This program is free software; you can redistribute it and/or modify it
00005    under the terms of the GNU Library General Public License as published
00006    by the Free Software Foundation; either version 2, or (at your option)
00007    any later version.
00008 
00009    This program is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012    Library General Public License for more details.
00013 
00014    You should have received a copy of the GNU Library General Public
00015    License along with this program; if not, write to the Free Software
00016    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
00017    USA.  */
00018 
00019 /* Written by Bruno Haible <bruno@clisp.org>.  */
00020 
00021 #ifdef HAVE_CONFIG_H
00022 # include <config.h>
00023 #endif
00024 
00025 #include <stdio.h>
00026 #include <stdlib.h>
00027 #include <string.h>
00028 
00029 /* Print an ASCII string with quotes and escape sequences where needed.  */
00030 static void
00031 print_escaped (FILE *stream, const char *str)
00032 {
00033   putc ('"', stream);
00034   for (; *str != '\0'; str++)
00035     if (*str == '\n')
00036       {
00037        fputs ("\\n\"", stream);
00038        if (str[1] == '\0')
00039          return;
00040        fputs ("\n\"", stream);
00041       }
00042     else
00043       {
00044        if (*str == '"' || *str == '\\')
00045          putc ('\\', stream);
00046        putc (*str, stream);
00047       }
00048   putc ('"', stream);
00049 }
00050 
00051 /* Add to the log file an entry denoting a failed translation.  */
00052 void
00053 _nl_log_untranslated (const char *logfilename, const char *domainname,
00054                     const char *msgid1, const char *msgid2, int plural)
00055 {
00056   static char *last_logfilename = NULL;
00057   static FILE *last_logfile = NULL;
00058   FILE *logfile;
00059 
00060   /* Can we reuse the last opened logfile?  */
00061   if (last_logfilename == NULL || strcmp (logfilename, last_logfilename) != 0)
00062     {
00063       /* Close the last used logfile.  */
00064       if (last_logfilename != NULL)
00065        {
00066          if (last_logfile != NULL)
00067            {
00068              fclose (last_logfile);
00069              last_logfile = NULL;
00070            }
00071          free (last_logfilename);
00072          last_logfilename = NULL;
00073        }
00074       /* Open the logfile.  */
00075       last_logfilename = (char *) malloc (strlen (logfilename) + 1);
00076       if (last_logfilename == NULL)
00077        return;
00078       strcpy (last_logfilename, logfilename);
00079       last_logfile = fopen (logfilename, "a");
00080       if (last_logfile == NULL)
00081        return;
00082     }
00083   logfile = last_logfile;
00084 
00085   fprintf (logfile, "domain ");
00086   print_escaped (logfile, domainname);
00087   fprintf (logfile, "\nmsgid ");
00088   print_escaped (logfile, msgid1);
00089   if (plural)
00090     {
00091       fprintf (logfile, "\nmsgid_plural ");
00092       print_escaped (logfile, msgid2);
00093       fprintf (logfile, "\nmsgstr[0] \"\"\n");
00094     }
00095   else
00096     fprintf (logfile, "\nmsgstr \"\"\n");
00097   putc ('\n', logfile);
00098 }