Author: sebor
Date: Fri Aug 18 14:11:40 2006
New Revision: 432725
URL: http://svn.apache.org/viewvc?rev=432725&view=rev
Log:
2006-08-18 Martin Sebor <[EMAIL PROTECTED]>
* messages.cpp: Constified locals and sanitized formatting.
(_RWSTD_NO_V3_LOCALE): Removed dead macro.
(_V3_LOCALE): Replaced with _STD.
Modified:
incubator/stdcxx/trunk/src/messages.cpp
Modified: incubator/stdcxx/trunk/src/messages.cpp
URL:
http://svn.apache.org/viewvc/incubator/stdcxx/trunk/src/messages.cpp?rev=432725&r1=432724&r2=432725&view=diff
==============================================================================
--- incubator/stdcxx/trunk/src/messages.cpp (original)
+++ incubator/stdcxx/trunk/src/messages.cpp Fri Aug 18 14:11:40 2006
@@ -1,35 +1,37 @@
/***************************************************************************
*
- * rw_messages.cpp - Source for the Standard Library messages locale classes.
+ * messages.cpp
*
- * $Id: //stdlib/dev/source/stdlib/messages.cpp#28 $
+ * $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.
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed
+ * with this work for additional information regarding copyright
+ * ownership. The ASF licenses this file to you 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.
+ *
+ * Copyright 2000-2006 Rogue Wave Software.
*
**************************************************************************/
#define _RWSTD_LIB_SRC
-
#include <rw/_defs.h>
+#include <new> // for placement new
-#ifndef _RWSTD_NO_V3_LOCALE
-
-
-#include <new> // for placement new
-
-#include <string.h> // for memcpy
+#include <string.h> // for memcpy, size_t
#include <loc/_locale.h>
#include <loc/_messages.h>
@@ -44,10 +46,10 @@
#endif // !_RWSTD_NO_NL_TYPES_H && !_RWSTD_NO_CATOPEN_CATGETS
-_RWSTD_NAMESPACE (__rw) {
+#define _RWSTD_BAD_CATD ((nl_catd)-1)
-#define _RWSTD_BAD_CATD ((nl_catd)-1)
+_RWSTD_NAMESPACE (__rw) {
// Implementation structure private to this module -- __rw_open_cat_data.
@@ -63,8 +65,8 @@
{
nl_catd catd;
union {
- void * _C_align;
- char _C_data[ sizeof(_V3_LOCALE::locale)];
+ void *_C_align;
+ char _C_data [sizeof (_STD::locale)];
} loc;
};
@@ -91,21 +93,21 @@
// a per-process array of catalog data structs
static __rw_open_cat_data catalog_buf [8];
static __rw_open_cat_data* catalogs = catalog_buf;
- static _RWSTD_SIZE_T n_catalogs = 0;
- static _RWSTD_SIZE_T catalog_bufsize =
- sizeof catalog_buf / sizeof *catalog_buf;
- static _RWSTD_SIZE_T largest_cat = 0;
- static int init = 0;
+
+ static size_t n_catalogs = 0;
+ static size_t catalog_bufsize = sizeof catalog_buf / sizeof *catalog_buf;
+ static size_t largest_cat = 0;
+ static int init = 0;
- if (!init) {
- for (_RWSTD_SIZE_T i = 0; i < catalog_bufsize; i++) {
- catalogs[i].catd = _RWSTD_BAD_CATD;
+ if (0 == init) {
+ for (size_t i = 0; i < catalog_bufsize; ++i) {
+ catalogs [i].catd = _RWSTD_BAD_CATD;
}
init = 1;
}
- if (cat == -1) {
+ if (-1 == cat) {
_RWSTD_ASSERT (0 != pcat_data);
// append the locale and return a struct with the associated catalog
@@ -117,78 +119,90 @@
__rw_open_cat_data* const tmp =
new __rw_open_cat_data[n_catalogs * 2];
- ::memcpy (tmp, catalogs, n_catalogs * sizeof
(__rw_open_cat_data));
+ memcpy (tmp, catalogs, n_catalogs * sizeof *tmp);
if (catalogs != catalog_buf)
delete[] catalogs;
catalogs = tmp;
catalog_bufsize *= 2;
- for (_RWSTD_SIZE_T i = n_catalogs; i < catalog_bufsize; i++) {
- catalogs[i].catd = _RWSTD_BAD_CATD;
+
+ for (size_t i = n_catalogs; i < catalog_bufsize; ++i) {
+ catalogs [i].catd = _RWSTD_BAD_CATD;
}
+
cat = int (n_catalogs);
- ::memcpy(&catalogs[cat].loc, &pcat_data->loc,
- sizeof(_V3_LOCALE::locale));
- catalogs[cat].catd = pcat_data->catd;
- if (_RWSTD_STATIC_CAST (_RWSTD_SIZE_T, cat) > largest_cat)
- largest_cat = _RWSTD_STATIC_CAST (_RWSTD_SIZE_T, cat);
- n_catalogs++;
+ memcpy (&catalogs [cat].loc, &pcat_data->loc,
+ sizeof (_STD::locale));
+
+ catalogs [cat].catd = pcat_data->catd;
+
+ if (size_t (cat) > largest_cat)
+ largest_cat = size_t (cat);
+
+ ++n_catalogs;
}
else {
// find the first open slot and use it.
cat = 0;
- while (catalogs[cat].catd != _RWSTD_BAD_CATD) {
- (cat)++;
+ while (catalogs [cat].catd != _RWSTD_BAD_CATD) {
+ ++cat;
}
- if (_RWSTD_STATIC_CAST (_RWSTD_SIZE_T, cat) > largest_cat)
- largest_cat = _RWSTD_STATIC_CAST (_RWSTD_SIZE_T, cat);
- ::memcpy(&catalogs[cat].loc, &pcat_data->loc,
- sizeof(_V3_LOCALE::locale));
- catalogs[cat].catd = pcat_data->catd;
- n_catalogs++;
+
+ if (size_t (cat) > largest_cat)
+ largest_cat = size_t (cat);
+
+ memcpy (&catalogs [cat].loc, &pcat_data->loc,
+ sizeof (_STD::locale));
+
+ catalogs [cat].catd = pcat_data->catd;
+ ++n_catalogs;
}
- return pcat_data;
}
}
else {
- if (!pcat_data) {
+ if (0 == pcat_data) {
// find struct and return it
- if ((_RWSTD_SIZE_T)cat < catalog_bufsize)
- return &catalogs[cat];
- return NULL;
+ if (size_t (cat) < catalog_bufsize)
+ return catalogs + cat;
+
+ return 0;
}
- else {
- // initialize the struct to an invalid state
- --n_catalogs;
- catalogs[cat].catd = _RWSTD_BAD_CATD;
- if ((_RWSTD_SIZE_T)cat == largest_cat) {
- // find the next smallest valid slot
- for (int i = cat; i >= 0; i--) {
- if (catalogs[i].catd != _RWSTD_BAD_CATD) {
- largest_cat = _RWSTD_STATIC_CAST (_RWSTD_SIZE_T, i);
- break;
- }
- }
- static const _RWSTD_SIZE_T bufsize =
- sizeof catalog_buf / sizeof *catalog_buf;
- if ((largest_cat < bufsize / 2) && (catalogs != catalog_buf)) {
-
- // when there are no more open catalogs indexed beyond
- // second half of the statically allocated repository, copy
- // the open catalogs back into the statically allocated
- // repository.
+
+ // initialize the struct to an invalid state
+ --n_catalogs;
+ catalogs [cat].catd = _RWSTD_BAD_CATD;
+ if (size_t (cat) == largest_cat) {
+
+ // find the next smallest valid slot
+ for (int i = cat; i >= 0; --i) {
+ if (catalogs [i].catd != _RWSTD_BAD_CATD) {
+ largest_cat = size_t (i);
+ break;
+ }
+ }
+
+ static const size_t bufsize =
+ sizeof catalog_buf / sizeof *catalog_buf;
+
+ if ((largest_cat < bufsize / 2) && (catalogs != catalog_buf)) {
+
+ // when there are no more open catalogs indexed beyond
+ // second half of the statically allocated repository, copy
+ // the open catalogs back into the statically allocated
+ // repository.
- catalog_bufsize = bufsize;
+ catalog_bufsize = bufsize;
- ::memcpy (catalog_buf, catalogs,
- catalog_bufsize * sizeof (*catalogs));
- delete[] catalogs;
- catalogs = catalog_buf;
- }
- }
- }
+ memcpy (catalog_buf, catalogs,
+ catalog_bufsize * sizeof (*catalogs));
+
+ delete[] catalogs;
+ catalogs = catalog_buf;
+ }
+ }
}
+
return pcat_data;
}
@@ -197,26 +211,30 @@
// Open a message catalog and assign and return a handle for it.
int
-__rw_cat_open (const _STD::string &cat_name, const _V3_LOCALE::locale &loc)
+__rw_cat_open (const _STD::string &cat_name, const _STD::locale &loc)
{
-
_RWSTD_MT_STATIC_GUARD (__rw_open_cat_data);
- nl_catd catd = catopen (cat_name.c_str (), NL_CAT_LOCALE);
+ const nl_catd catd = catopen (cat_name.c_str (), NL_CAT_LOCALE);
if (_RWSTD_BAD_CATD == catd)
return -1;
- int cat = -1;
__rw_open_cat_data cat_data;
cat_data.catd = catd;
- new (&cat_data.loc) _V3_LOCALE::locale(loc);
- __rw_manage_cat_data(cat, &cat_data);
+ new (&cat_data.loc) _STD::locale (loc);
+
+ int cat = -1;
+ __rw_manage_cat_data (cat, &cat_data);
+
+ _RWSTD_ASSERT (0 <= cat);
+
return cat;
}
// Get message text from catalog.
-const char *__rw_get_message (int cat, int set_num, int msg_num)
+const char*
+__rw_get_message (int cat, int set_num, int msg_num)
{
if (cat < 0)
return 0;
@@ -224,14 +242,14 @@
_RWSTD_MT_STATIC_GUARD (__rw_open_cat_data);
__rw_open_cat_data *const pcat_data = __rw_manage_cat_data (cat, 0);
- if (pcat_data) {
- // verify 22.2.7.1.2, p3: `catalog' must be valid
- if (_RWSTD_BAD_CATD == pcat_data->catd)
- return 0;
- const char* const dflt = "";
+ if (pcat_data && _RWSTD_BAD_CATD != pcat_data->catd) {
+ // 22.2.7.1.2, p3: `catalog' must be valid
+
+ const char dflt[] = "";
+
+ const nl_catd catd = pcat_data->catd;
- nl_catd catd = pcat_data->catd;
const char* const text = catgets (catd, set_num, msg_num, dflt);
if (text != dflt)
@@ -243,50 +261,46 @@
// Get locale to be used for character translation for this message catalog.
-
-const _V3_LOCALE::locale&
+const _STD::locale&
__rw_get_locale (int cat)
{
_RWSTD_MT_STATIC_GUARD (__rw_open_cat_data);
+
_RWSTD_ASSERT (0 <= cat);
- __rw_open_cat_data* pcat_data = __rw_manage_cat_data(cat, NULL);
+ __rw_open_cat_data* const pcat_data = __rw_manage_cat_data (cat, 0);
_RWSTD_ASSERT (0 != pcat_data);
- return *(_RWSTD_REINTERPRET_CAST (_V3_LOCALE::locale*, &(pcat_data->loc)));
+
+ return *(_RWSTD_REINTERPRET_CAST (_STD::locale*, &(pcat_data->loc)));
}
-// Close a catalog and release its handle.
-void __rw_cat_close (int cat)
+// Close a catalog and release its handle.
+void
+__rw_cat_close (int cat)
{
- nl_catd catd;
-
_RWSTD_MT_STATIC_GUARD (__rw_open_cat_data);
- // verify 22.2.7.1.2, p5: `catalog' must be valid
- _RWSTD_REQUIRES (cat >= 0, (_RWSTD_ERROR_INVALID_ARGUMENT,
- _RWSTD_FUNC ("__rw_cat_close (int cat)")));
-
- __rw_open_cat_data *pcat_data;
- pcat_data = __rw_manage_cat_data (cat, NULL);
- if (pcat_data) {
- // verify 22.2.7.1.2, p5: `catalog' must be valid
- _RWSTD_REQUIRES (pcat_data->catd != _RWSTD_BAD_CATD,
(_RWSTD_ERROR_INVALID_ARGUMENT,
- _RWSTD_FUNC
("__rw_cat_close (int cat)")));
- catd = pcat_data->catd;
- catclose (catd);
- _V3_LOCALE::locale *ploc = _RWSTD_REINTERPRET_CAST
(_V3_LOCALE::locale*, &pcat_data->loc);
+ __rw_open_cat_data* const pcat_data =
+ cat < 0 ? 0 : __rw_manage_cat_data (cat, 0);
+
+ // 22.2.7.1.2, p5: `catalog' must be valid
+ if (pcat_data && pcat_data->catd != _RWSTD_BAD_CATD) {
+
+ catclose (pcat_data->catd);
+
+ _STD::locale* const ploc =
+ _RWSTD_REINTERPRET_CAST (_STD::locale*, &pcat_data->loc);
+
ploc->~locale ();
- __rw_manage_cat_data(cat, pcat_data);
+
+ __rw_manage_cat_data (cat, pcat_data);
}
else {
- // verify 22.2.7.1.2, p5: `catalog' must be valid
- _RWSTD_REQUIRES (0, (_RWSTD_ERROR_INVALID_ARGUMENT, _RWSTD_FUNC
("__rw_cat_close (int cat)")));
+ _RWSTD_REQUIRES (0, (_RWSTD_ERROR_INVALID_ARGUMENT,
+ _RWSTD_FUNC ("__rw_cat_close (int cat)")));
}
}
} // namespace __rw
-
-
-#endif // _RWSTD_NO_V3_LOCALE