]> git.ipfire.org Git - thirdparty/cups.git/blobdiff - cups/string.c
Clean up error messages.
[thirdparty/cups.git] / cups / string.c
index 7a862f6617882d7147aefc9d41eef61432e2bbdf..54f7bd0cf8e4c7928c36e0a6fafb201e4115f4b8 100644 (file)
@@ -1,35 +1,11 @@
 /*
- * "$Id$"
+ * String functions for CUPS.
  *
- *   String functions for CUPS.
+ * Copyright © 2007-2019 by Apple Inc.
+ * Copyright © 1997-2007 by Easy Software Products.
  *
- *   Copyright 2007-2012 by Apple Inc.
- *   Copyright 1997-2007 by Easy Software Products.
- *
- *   These coded instructions, statements, and computer programs are the
- *   property of Apple Inc. and are protected by Federal copyright
- *   law.  Distribution and use rights are outlined in the file "LICENSE.txt"
- *   which should have been included with this file.  If this file is
- *   file is missing or damaged, see the license at "http://www.cups.org/".
- *
- *   This file is subject to the Apple OS-Developed Software exception.
- *
- * Contents:
- *
- *   _cupsStrAlloc()      - Allocate/reference a string.
- *   _cupsStrFlush()      - Flush the string pool.
- *   _cupsStrFormatd()    - Format a floating-point number.
- *   _cupsStrFree()       - Free/dereference a string.
- *   _cupsStrRetain()     - Increment the reference count of a string.
- *   _cupsStrScand()      - Scan a string for a floating-point number.
- *   _cupsStrStatistics() - Return allocation statistics for string pool.
- *   _cups_strcpy()       - Copy a string allowing for overlapping strings.
- *   _cups_strdup()       - Duplicate a string.
- *   _cups_strcasecmp()   - Do a case-insensitive comparison.
- *   _cups_strncasecmp()  - Do a case-insensitive comparison on up to N chars.
- *   _cups_strlcat()      - Safely concatenate two strings.
- *   _cups_strlcpy()      - Safely copy two strings.
- *   compare_sp_items()   - Compare two string pool items...
+ * Licensed under Apache License v2.0.  See the file "LICENSE" for more
+ * information.
  */
 
 /*
  */
 
 #define _CUPS_STRING_C_
-#include "string-private.h"
-#include "debug-private.h"
-#include "thread-private.h"
-#include "array.h"
+#include "cups-private.h"
+#include "debug-internal.h"
 #include <stddef.h>
 #include <limits.h>
 
@@ -161,6 +135,39 @@ _cupsStrAlloc(const char *s)               /* I - String */
 }
 
 
+/*
+ * '_cupsStrDate()' - Return a localized date for a given time value.
+ *
+ * This function works around the locale encoding issues of strftime...
+ */
+
+char *                                 /* O - Buffer */
+_cupsStrDate(char   *buf,              /* I - Buffer */
+             size_t bufsize,           /* I - Size of buffer */
+            time_t timeval)            /* I - Time value */
+{
+  struct tm    *dateval;               /* Local date/time */
+  char         temp[1024];             /* Temporary buffer */
+  _cups_globals_t *cg = _cupsGlobals();        /* Per-thread globals */
+
+
+  if (!cg->lang_default)
+    cg->lang_default = cupsLangDefault();
+
+  dateval = localtime(&timeval);
+
+  if (cg->lang_default->encoding != CUPS_UTF8)
+  {
+    strftime(temp, sizeof(temp), "%c", dateval);
+    cupsCharsetToUTF8((cups_utf8_t *)buf, temp, (int)bufsize, cg->lang_default->encoding);
+  }
+  else
+    strftime(buf, bufsize, "%c", dateval);
+
+  return (buf);
+}
+
+
 /*
  * '_cupsStrFlush()' - Flush the string pool.
  */
@@ -260,7 +267,7 @@ _cupsStrFormatd(char         *buf,  /* I - String */
   }
   else
   {
-    strlcpy(buf, temp, bufend - buf + 1);
+    strlcpy(buf, temp, (size_t)(bufend - buf + 1));
     bufptr = buf + strlen(buf);
   }
 
@@ -305,15 +312,6 @@ _cupsStrFree(const char *s)                /* I - String to free */
 
   key = (_cups_sp_item_t *)(s - offsetof(_cups_sp_item_t, str));
 
-#ifdef DEBUG_GUARDS
-  if (key->guard != _CUPS_STR_GUARD)
-  {
-    DEBUG_printf(("5_cupsStrFree: Freeing string %p(%s), guard=%08x, "
-                  "ref_count=%d", key, key->str, key->guard, key->ref_count));
-    abort();
-  }
-#endif /* DEBUG_GUARDS */
-
   if ((item = (_cups_sp_item_t *)cupsArrayFind(stringpool, key)) != NULL &&
       item == key)
   {
@@ -321,6 +319,14 @@ _cupsStrFree(const char *s)                /* I - String to free */
     * Found it, dereference...
     */
 
+#ifdef DEBUG_GUARDS
+    if (key->guard != _CUPS_STR_GUARD)
+    {
+      DEBUG_printf(("5_cupsStrFree: Freeing string %p(%s), guard=%08x, ref_count=%d", key, key->str, key->guard, key->ref_count));
+      abort();
+    }
+#endif /* DEBUG_GUARDS */
+
     item->ref_count --;
 
     if (!item->ref_count)
@@ -436,7 +442,7 @@ _cupsStrScand(const char   *buf,    /* I - Pointer to number */
 
     if (loc && loc->decimal_point)
     {
-      strlcpy(tempptr, loc->decimal_point, sizeof(temp) - (tempptr - temp));
+      strlcpy(tempptr, loc->decimal_point, sizeof(temp) - (size_t)(tempptr - temp));
       tempptr += strlen(tempptr);
     }
     else if (tempptr < (temp + sizeof(temp) - 1))
@@ -546,7 +552,7 @@ _cupsStrStatistics(size_t *alloc_bytes,     /* O - Allocated bytes */
     */
 
     count  += item->ref_count;
-    len    = (strlen(item->str) + 8) & ~7;
+    len    = (strlen(item->str) + 8) & (size_t)~7;
     abytes += sizeof(_cups_sp_item_t) + len;
     tbytes += item->ref_count * len;
   }
@@ -684,10 +690,11 @@ _cups_strlcat(char       *dst,            /* O - Destination string */
   */
 
   dstlen = strlen(dst);
-  size   -= dstlen + 1;
 
-  if (!size)
-    return (dstlen);           /* No room, return immediately... */
+  if (size < (dstlen + 1))
+    return (dstlen);                   /* No room, return immediately... */
+
+  size -= dstlen + 1;
 
  /*
   * Figure out how much room is needed...
@@ -702,7 +709,7 @@ _cups_strlcat(char       *dst,              /* O - Destination string */
   if (srclen > size)
     srclen = size;
 
-  memcpy(dst + dstlen, src, srclen);
+  memmove(dst + dstlen, src, srclen);
   dst[dstlen + srclen] = '\0';
 
   return (dstlen + srclen);
@@ -738,7 +745,7 @@ _cups_strlcpy(char       *dst,              /* O - Destination string */
   if (srclen > size)
     srclen = size;
 
-  memcpy(dst, src, srclen);
+  memmove(dst, src, srclen);
   dst[srclen] = '\0';
 
   return (srclen);
@@ -756,8 +763,3 @@ compare_sp_items(_cups_sp_item_t *a,        /* I - First item */
 {
   return (strcmp(a->str, b->str));
 }
-
-
-/*
- * End of "$Id$".
- */