Re: [pgsql-hackers-win32] [HACKERS] %2$, %1$ gettext placeholder replacement is not working under Win32

2005-01-23 Thread Nicolai Tufar
Greetings,

I would like to submit a new version of src/port/snprintf.c 
It passes regression tests on Linux and Win32 and
prints all of %n$ messages finely.

I added printf() function too because --help usage-type 
output is printed with printf().

I have no experience with autoconf so I would ask you
for help on what changes to do to include libpgport to 
src/Makefile.global and  src/Makefile.shlib.

PostgreSQL uses snprintf() in more places than I expected.
So these changes need a through testing. I have no 64-bit
to able to run regression test on it would be nice to run
it on a 64-bit platform too.

Best regards,
Nicolai Tufar
/*
 * Copyright (c) 1983, 1995, 1996 Eric P. Allman
 * Copyright (c) 1988, 1993
 *  The Regents of the University of California.  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *notice, this list of conditions and the following disclaimer in the
 *documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *must display the following acknowledgement:
 *  This product includes software developed by the University of
 *  California, Berkeley and its contributors.
 * 4. Neither the name of the University nor the names of its contributors
 *may be used to endorse or promote products derived from this software
 *without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

/* might be in either frontend or backend */
#include postgres_fe.h

#ifdef ENABLE_THREAD_SAFETY
#error  The replacement snprintf() is not thread-safe.  \
Your platform must have a thread-safe snprintf() to compile with threads.
#endif

#if !defined(WIN32)  !defined(__CYGWIN__)
#include sys/ioctl.h
#endif
#include sys/param.h


/*
 * We do all internal arithmetic in the widest available integer type,
 * here called long_long (or ulong_long for unsigned).
 */
#ifdef HAVE_LONG_LONG_INT_64
typedef long long long_long;
typedef unsigned long long ulong_long;

#else
typedef long long_long;
typedef unsigned long ulong_long;
#endif

#ifndef NL_ARGMAX
#define NL_ARGMAX 4096
#endif

/*
**  SNPRINTF, VSNPRINT -- counted versions of printf
**
**  These versions have been grabbed off the net.  They have been
**  cleaned up to compile properly and support for .precision and
**  %lx has been added.
*/

/**
 * Original:
 * Patrick Powell Tue Apr 11 09:48:21 PDT 1995
 * A bombproof version of doprnt (dopr) included.
 * Sigh.  This sort of thing is always nasty do deal with.  Note that
 * the version here does not include floating point. (now it does ... tgl)
 *
 * snprintf() is used instead of sprintf() as it does limit checks
 * for string length.  This covers a nasty loophole.
 *
 * The other functions are there to prevent NULL pointers from
 * causing nast effects.
 **/

/*static char _id[] = $PostgreSQL: pgsql/src/port/snprintf.c,v 1.4 2004/08/29 
05:07:02 momjian Exp $;*/
static char *end;
static int  SnprfOverflow;

int snprintf(char *str, size_t count, const char *fmt,...);
int vsnprintf(char *str, size_t count, const char *fmt, 
va_list args);
int printf(const char *format, ...);
static void dopr(char *buffer, const char *format, va_list args);

int
printf(const char *fmt,...)
{
int len;
va_list args;
static char*buffer[4096];
char*   p;

va_start(args, fmt);
len = vsnprintf((char*)buffer, (size_t)4096, fmt, args);
va_end(args);
p = (char*)buffer;
for(;*p;p++)
putchar(*p);
return len;
}

int
snprintf(char *str, size_t count, const char 

Re: [pgsql-hackers-win32] [HACKERS] %2$, %1$ gettext placeholder replacement is not working under Win32

2005-01-22 Thread Tom Lane
Nicolai Tufar [EMAIL PROTECTED] writes:
 On Sat, 22 Jan 2005 15:31:39 +0100, Peter Eisentraut [EMAIL PROTECTED] 
 wrote:
 Nicolai Tufar wrote:
 2. Why would we not just take FreeBSD's vfprintf() and use it
 instead?
 
 Try it.  It's painful.

 src/port/snprintf.c is not not a particularly pretty. And
 after my rework it got even uglier. I have looked at 
 FreeBDS's one and it is not *that* complicated.

If you can get it to work then it'd be a better bet than writing (and
having to maintain) our own.

regards, tom lane

---(end of broadcast)---
TIP 5: Have you checked our extensive FAQ?

   http://www.postgresql.org/docs/faq


Re: [pgsql-hackers-win32] [HACKERS] %2$, %1$ gettext placeholder replacement is not working under Win32

2005-01-22 Thread Nicolai Tufar
On Sat, 22 Jan 2005 17:05:22 -0500, Tom Lane [EMAIL PROTECTED] wrote:
 
  src/port/snprintf.c is not not a particularly pretty. And
  after my rework it got even uglier. I have looked at
  FreeBDS's one and it is not *that* complicated.
 
 If you can get it to work then it'd be a better bet than writing (and
 having to maintain) our own.

Very well, I am starting to work on it.
I will put everything necessary in on file.
So far it is 1500-odd lines and will probably
grow a little bit. Plus we would probably need to
include Double-to-ASCII package too:
http://cvsup.pt.freebsd.org/cgi-bin/cvsweb/cvsweb.cgi/src/contrib/gdtoa/
A daunting task indeed. I will do it but I am afraid
it would not be too portable. 

I was wandering if reimplementing print formatting
is the right thing to do.  The problem I have is 
in fmtnum() and probably in  fmtfloat() functions
which are very platform-dependent.

The code to shuffle xxprinf() arguments based on 
%n$ formatting stirngs is ready why don't shuffle
formatting placeholders too and call OS's vsnprintf()
with modified formatting formatting strings and
va_list?

Or, a similar solution, for every parameter
extract formatting placeholder and value,
call snprintf() and the combine the resulting string?

The first solution requires doing nasty manipulations 
with va_list, the second will be slower because of 
multiple calls to snprintf().

Which one is better?

Best regards,
Nicolai Tufar


 
 regards, tom lane


---(end of broadcast)---
TIP 5: Have you checked our extensive FAQ?

   http://www.postgresql.org/docs/faq