Return-Path: Delivered-To: apmail-incubator-stdcxx-commits-archive@www.apache.org Received: (qmail 20881 invoked from network); 18 Sep 2005 23:58:40 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (209.237.227.199) by minotaur.apache.org with SMTP; 18 Sep 2005 23:58:40 -0000 Received: (qmail 46995 invoked by uid 500); 18 Sep 2005 23:58:40 -0000 Delivered-To: apmail-incubator-stdcxx-commits-archive@incubator.apache.org Received: (qmail 46976 invoked by uid 500); 18 Sep 2005 23:58:40 -0000 Mailing-List: contact stdcxx-commits-help@incubator.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: stdcxx-dev@incubator.apache.org Delivered-To: mailing list stdcxx-commits@incubator.apache.org Received: (qmail 46962 invoked by uid 500); 18 Sep 2005 23:58:39 -0000 Delivered-To: apmail-incubator-stdcxx-cvs@incubator.apache.org Received: (qmail 46958 invoked by uid 99); 18 Sep 2005 23:58:39 -0000 X-ASF-Spam-Status: No, hits=-9.8 required=10.0 tests=ALL_TRUSTED,NO_REAL_NAME X-Spam-Check-By: apache.org Received: from [209.237.227.194] (HELO minotaur.apache.org) (209.237.227.194) by apache.org (qpsmtpd/0.29) with SMTP; Sun, 18 Sep 2005 16:58:37 -0700 Received: (qmail 20868 invoked by uid 65534); 18 Sep 2005 23:58:36 -0000 Message-ID: <20050918235836.20867.qmail@minotaur.apache.org> Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r290020 - in /incubator/stdcxx/trunk/tests: include/file.h src/file.cpp Date: Sun, 18 Sep 2005 23:58:35 -0000 To: stdcxx-cvs@incubator.apache.org From: sebor@apache.org X-Mailer: svnmailer-1.0.5 X-Virus-Checked: Checked by ClamAV on apache.org X-Spam-Rating: minotaur.apache.org 1.6.2 0/1000/N Author: sebor Date: Sun Sep 18 16:58:30 2005 New Revision: 290020 URL: http://svn.apache.org/viewcvs?rev=290020&view=rev Log: 2005-09-18 Martin Sebor * file.h: New. Declarations of testsuite file I/O helper functions. * file.cpp: New. Implementation of the same. Added: incubator/stdcxx/trunk/tests/include/file.h (with props) incubator/stdcxx/trunk/tests/src/file.cpp (with props) Added: incubator/stdcxx/trunk/tests/include/file.h URL: http://svn.apache.org/viewcvs/incubator/stdcxx/trunk/tests/include/file.h?rev=290020&view=auto ============================================================================== --- incubator/stdcxx/trunk/tests/include/file.h (added) +++ incubator/stdcxx/trunk/tests/include/file.h Sun Sep 18 16:58:30 2005 @@ -0,0 +1,96 @@ +/************************************************************************ + * + * file.h - common file I/O definitions + * + * $Id$ + * + *************************************************************************** + * + * Copyright (c) 1994-2005 Quovadx, Inc., acting through its Rogue Wave + * Software division. Licensed under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * http://www.apache.org/licenses/LICENSE-2.0. Unless required by + * applicable law or agreed to in writing, software distributed under + * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. See the License + * for the specific language governing permissions and limitations under + * the License. + * + **************************************************************************/ + +#ifndef RW_FILE_H_INCLUDED +#define RW_FILE_H_INCLUDED + + +#include // for test config macros + + +#ifndef _MSC_VER + // POSIX special files: + // http://www.opengroup.org/onlinepubs/007904975/basedefs/xbd_chap10.html +# define DEV_CONSOLE "/dev/console" +# define DEV_NULL "/dev/null" +# define DEV_TTY "/dev/tty" +#else // if defined (_MSC_VER) +# define DEV_CONSOLE "CON:" +# define DEV_NULL "NUL:" +# define DEV_TTY "CON:" +#endif // _MSC_VER + + +#if _RWSTD_PATH_SEP == '/' +# define SLASH "/" +# define SHELL_MV "mv " +# define SHELL_RM_F "rm -f " +# define SHELL_RM_RF "rm -rf " +#else +# define SLASH "\\" +# define SHELL_MV "move /Y " +# define SHELL_RM_F "del /F " +# define SHELL_RM_RF "rmdir /Q /S " +#endif + + +// writes chars using symbolic names from the Portable Character Set (PCS) +// or using the notations for narrow characters outside that set +// if teh second argument is 0, writes out the CHARMAP section of the locale +// definition file for the Portable Character Set (in POSIX-compliant format) +_TEST_EXPORT void +pcs_write (void*, const char*); + + +// creates a unique temporary file name as if by calling tmpnam() +// but avoiding various platform-specific quirks (such as HP-UX +// failure when _REENTRANT is #defined or GNU glibc warnings) +_TEST_EXPORT +const char* rw_tmpnam (char*); + +// tries to open file named by the first argument and, if successful, +// allocates a block of storage sufficiently large to hold the file's +// entire contents, as determined by the stat() function; it then reads +// the contents of the file into the block of storage, returning a pointer +// to the block; if the second argument is non-0, sets the pointed-to value +// to the number of bytes read +// as a special case, when the first argument is 0 and the second is not, +// the function takes the third argument as a pointer to the buffer that +// it will use to read the contents of files into in subsequent calls, +// provided the buffer is large enough +_TEST_EXPORT void* +rw_fread (const char*, + _RWSTD_SIZE_T* = 0 /* size in bytes */, + const char* = "r" /* stdio open mode */); + +// if the second argument is non-0, writes N bytes starting at that +// location into the file named by the first argument; N is taken +// from the value pointed to by the third argument, if non-0, or +// as the result of calling strlen() on the buffer pointed to by +// the second argument; if the second argument is 0, the function +// removes the named file; returns the number of bytes written +_TEST_EXPORT _RWSTD_SIZE_T +rw_fwrite (const char*, + const void*, + _RWSTD_SIZE_T = ~0 /* size in bytes */, + const char* = "w" /* stdio open mode */ ); + +#endif // RW_FILE_H_INCLUDED Propchange: incubator/stdcxx/trunk/tests/include/file.h ------------------------------------------------------------------------------ svn:eol-style = native Propchange: incubator/stdcxx/trunk/tests/include/file.h ------------------------------------------------------------------------------ svn:keywords = Id Added: incubator/stdcxx/trunk/tests/src/file.cpp URL: http://svn.apache.org/viewcvs/incubator/stdcxx/trunk/tests/src/file.cpp?rev=290020&view=auto ============================================================================== --- incubator/stdcxx/trunk/tests/src/file.cpp (added) +++ incubator/stdcxx/trunk/tests/src/file.cpp Sun Sep 18 16:58:30 2005 @@ -0,0 +1,453 @@ +/************************************************************************ + * + * file.cpp - definitions of testsuite file I/O helpers + * + * $Id$ + * + ************************************************************************ + * + * Copyright (c) 1994-2005 Quovadx, Inc., acting through its Rogue Wave + * Software division. Licensed under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * http://www.apache.org/licenses/LICENSE-2.0. Unless required by + * applicable law or agreed to in writing, software distributed under + * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. See the License + * for the specific language governing permissions and limitations under + * the License. + * + **************************************************************************/ + +// expand _TEST_EXPORT macros +#define _RWSTD_TEST_SRC + +#include + +#if defined __linux__ + // on Linux define _XOPEN_SOURCE to get CODESET defined in +# define _XOPEN_SOURCE 500 /* Single Unix conformance */ + // bring __int32_t into scope (otherwise fails to compile) +# include +#endif // __linux__ + +#include +#include + +#if !defined (_WIN32) && !defined (_WIN64) +# include // for CODESET +# include +#else +# include +#endif + +#include // for assert +#include // for errno +#include // for LC_XXX macros +#include // for sprintf, ... +#include // for free, malloc, realloc +#include // for strcat, strcpy, strlen, ... +#include +#include // for wcslen, ... + + +#ifndef PATH_MAX +# define PATH_MAX 1024 +#endif + +#ifndef P_tmpdir +# define P_tmpdir "/tmp/" +#endif + +#ifndef _RWSTD_NO_PURE_C_HEADERS + +extern "C" int mkstemp (char*); + +#endif // _RWSTD_NO_PURE_C_HEADERS + +// write `str' using symbolic names from the Portable Character Set (PCS) +// or using the notations for narrow characters outside that set +// if (0 == str), writes out the CHARMAP section of the locale definition +// file for the Portable Character Set (in POSIX-compliant format) +_TEST_EXPORT void pcs_write (void *fpv, const char *str) +{ + FILE* const fp = _RWSTD_STATIC_CAST (FILE*, fpv); + + // ASCII (ISO-646) character map definition + static const char* charmap[] = { + "", "", "", "", "", "", "", "", + "", + "", + "", + "", + "", + "", + "", "", "", "", "", "", "", "", + "","", "", "", "", "", "", "", + "", "", + "", + /* ! */ "", + /* " */ "", + /* # */ "", + /* $ */ "", + /* % */ "", + /* & */ "", + /* ' */ "", + /* ( */ "", + /* ) */ "", + /* * */ "", + /* + */ "", + /* , */ "", + /* - */ "", + /* . */ "", + /* / */ "", + /* 0 */ "", + /* 1 */ "", + /* 2 */ "", + /* 3 */ "", + /* 4 */ "", + /* 5 */ "", + /* 6 */ "", + /* 7 */ "", + /* 8 */ "", + /* 9 */ "", + /* : */ "", + /* ; */ "", + /* < */ "", + /* = */ "", + /* > */ "", + /* ? */ "", + /* @ */ "", + "", "", "", "", "", "", "", "", "", "", + "", "", "", "", "", "

", "", "", "", "", + "", "", "", "", "", "", + /* { */ "", + /* | */ "", + /* } */ "", + /* ~ */ "", + "" + }; + + if (str) { + // write out `str' using the charmap above + for (; *str; ++str) { + const unsigned char uc = _RWSTD_STATIC_CAST (unsigned char, *str); + + if (uc < sizeof charmap / sizeof *charmap) + fprintf (fp, "%s", charmap [uc]); + else + fprintf (fp, "", uc); + } + } + else { + +#if !defined (_WIN32) && !defined (_WIN64) + const char* const codeset = nl_langinfo (CODESET); +#else + // FIXME: determine the current code page + const char* const codeset = "UTF-8"; +#endif // _WIN{32,64} + + fprintf (fp, " \"%s\"\n", codeset); + fprintf (fp, " 1\n"); + fprintf (fp, " 1\n"); + + fprintf (fp, "CHARMAP\n"); + + // write out the charmap above + for (unsigned i = 0; i != sizeof charmap / sizeof *charmap; ++i) { + fprintf (fp, "%s \\x%02x\n", charmap [i], i); + } + + // write out duplicate symbolic names to prevent warnings + fprintf (fp, " \\x%02x\n", '\a'); + fprintf (fp, " \\x%02x\n", '-'); + fprintf (fp, " \\x%02x\n", '.'); + fprintf (fp, " \\x%02x\n", '/'); + fprintf (fp, " \\x%02x\n", '\\'); + fprintf (fp, " \\x%02x\n", '^'); + fprintf (fp, " \\x%02x\n", '_'); + fprintf (fp, " \\x%02x\n", '_'); + fprintf (fp, " \\x%02x\n", '{'); + fprintf (fp, " \\x%02x\n", '}'); + + fprintf (fp, "END CHARMAP\n"); + } +} + + +_TEST_EXPORT +const char* rw_tmpnam (char *buf) +{ +#ifndef _RWSTD_NO_MKSTEMP +# define TMP_TEMPLATE "tmpfile-XXXXXX" + + if (!buf) { + static char fname_buf [sizeof (P_tmpdir) + sizeof (TMP_TEMPLATE)]; + + buf = fname_buf; + *buf = '\0'; + } + + if ('\0' == *buf) { + // copy the template to the buffer; make sure there is exactly + // one path separator character between P_tmpdir and the file + // name template (it doesn't really matter how many there are + // as long as it's at least one, but one looks better than two + // in diagnostic messages) + size_t len = sizeof (P_tmpdir) - 1; + + memcpy (buf, P_tmpdir, len); + if (_RWSTD_PATH_SEP != buf [len - 1]) + buf [len++] = _RWSTD_PATH_SEP; + + memcpy (buf + len, TMP_TEMPLATE, sizeof TMP_TEMPLATE); + } + + // prevent annoying glibc warnings (issued by the linker): + // the use of `tmpnam' is dangerous, better use `mkstemp' + + const int fd = mkstemp (buf); + + if (-1 == fd) { + fprintf (stderr, "%s:%d: mkstemp(\"%s\") failed: %s\n", + __FILE__, __LINE__, buf, strerror (errno)); + return 0; + } + + close (fd); + + const char* const fname = buf; + +# undef TMP_TEMPLATE +#else // if defined (_RWSTD_NO_MKSTEMP) + +# if defined (_WIN32) || defined (_WIN64) + + // create a temporary file name + char* fname = tempnam (P_tmpdir, ".rwtest-tmp"); + + if (fname) { + + static char tmpbuf [256]; + + if (0 == buf) + buf = tmpbuf; + + _RWSTD_ASSERT (strlen (fname) < sizeof (tmpbuf)); + + // copy the generated temporary file name to the provided buffer + strcpy (buf, fname); + + // free the storage allocated by tempnam() + free (fname); + fname = buf; + } + else { + fprintf (stderr, "%s:%d: tempnam(\"%s\", \"%s\") failed: %s\n", + __FILE__, __LINE__, + P_tmpdir, ".rwtest-tmp", strerror (errno)); + } + +# else +# if defined (__hpux) && defined (_RWSTD_REENTRANT) + + // on HP-UX, in reentrant mode, tmpnam(0) fails by design + + if (!buf) { + static char tmpbuf [L_tmpnam]; + buf = tmpbuf; + *buf = '\0'; + } + +# endif // __hpux && _REENTRANT + + const char* const fname = tmpnam (buf); + + if (!fname) + fprintf (stderr, "%s:%d: tmpnam(\"%s\") failed: %s\n", + __FILE__, __LINE__, buf, strerror (errno)); + +# endif // _WIN{32,64} +#endif // _RWSTD_NO_MKSTEMP + + return fname; +} + + +_TEST_EXPORT +size_t rw_fsize (const char *fname) +{ +#if defined (_WIN32) || defined (_WIN64) + + // note: both method of obtaining the size of a file + // just written by a process may fail (i.e., the size + // will be 0) + +# if 1 + + struct _stat sb; + + if (-1 == _stat (fname, &sb)) + return _RWSTD_SIZE_MAX; + + return sb.st_size; + +# else + + // #include for CreateFile() and GetFileSize() + const HANDLE hfile = + CreateFile (fname, + GENERIC_READ, + 0, // dwShareMode, + 0, // lpSecurityAttributes, + OPEN_EXISTING, // dwCreationDisposition, + FILE_ATTRIBUTE_NORMAL, // dwFlagsAndAttributes, + 0); // hTemplateFile + + if (INVALID_HANDLE_VALUE == hfile) + return _RWSTD_SIZE_MAX; + + const size_t size = GetFileSize (hfile, 0); + + CloseHandle (hfile); + + return size; + +# endif // 0/1 + +#else // if !defined (_WIN{32,64}) + + struct stat sb; + + if (stat (fname, &sb) == -1) + return _RWSTD_SIZE_MAX; + + return sb.st_size; + +#endif // _WIN{32,64} + +} + + +_TEST_EXPORT +void* rw_fread (const char *fname, + size_t *size /* = 0 */, + const char *mode /* = "r" */) +{ + // buffer and size supplied by the user + static char* usrbuf = 0; + static size_t usrsize = 0; + + // when called with 0 file name and non-0 size, set the static + // local buffer for the functions to use in subsequent calls + // with non-0 `fname' instead of dynamically allocating a new + // buffer + if (!fname && size) { + + char* const oldbuf = usrbuf; + + usrbuf = _RWSTD_CONST_CAST (char*, mode); + usrsize = usrbuf ? *size : 0; + + return oldbuf; + } + + static char buffer [1024]; + static char* buf = usrbuf ? usrbuf : buffer; + static size_t bufsize = usrbuf ? usrsize : sizeof buffer; + + // open the file in the specified mode + FILE* const fp = fopen (fname, mode); + + if (!fp) + return 0; + + for (char *bufend = buf; ; ) { + // compute the total number of bytes read from the file so far + // and the number of bytes that are still available in the buffer + const size_t bytes_read = size_t (bufend - buf); + const size_t bytes_avail = bufsize - bytes_read; + + // try to read the contents of the file into the buffer + const size_t nbytes = fread (bufend, 1, bytes_avail, fp); + + if (0 == nbytes) { + *bufend = '\0'; + + // store the number of bytes read + if (size) + *size = bytes_read; + + break; + } + + if (nbytes == bytes_avail) { + + // do not grow user-specified buffer + if (buf == usrbuf) + break; + + const size_t newsize = (bufsize + 1) * 2; + + // increase the size of the buffer and continue reading + char *tmp = new char [newsize]; + memcpy (tmp, buf, bufsize); + + // deallocate buffer only if it's been + // previously dynamically allocated + if (buf != buffer) + delete[] buf; + + bufsize = newsize; + bufend = tmp + bytes_read; + buf = tmp; + } + + bufend += nbytes; + } + + fclose (fp); + + return buf; +} + + +_TEST_EXPORT +size_t rw_fwrite (const char *fname, + const void *buf, + size_t size /* = -1 */, + const char *mode /* = "w" */) +{ + FILE *fp = 0; + + if (buf) + fp = fopen (fname, mode); + else { + remove (fname); + return 0; + } + + if (!fp) + return size_t (-1); + + if (size_t (-1) == size) + size = strlen (_RWSTD_STATIC_CAST (const char*, buf)); + + // fwrite() returns the number of elements successfully written + // set it up so that the number of elements == the number of bytes + const size_t nbytes = fwrite (buf, 1 /* byte */, size, fp); + + fclose (fp); + + return nbytes; +} Propchange: incubator/stdcxx/trunk/tests/src/file.cpp ------------------------------------------------------------------------------ svn:eol-style = native Propchange: incubator/stdcxx/trunk/tests/src/file.cpp ------------------------------------------------------------------------------ svn:keywords = Id

", "", "", "", "", + "", "", "", "", "", "", + /* [ */ "", + /* \ */ "", + /* ] */ "", + /* ^ */ "", + /* _ */ "", + /* ` */ "", + "", "", "", "", "", "", "", "", "", "", + "", "", "", "", "", "