str: Add function xstrdup_if_nonnull() and introduce many users.
authorBen Pfaff <blp@cs.stanford.edu>
Mon, 11 Jan 2021 01:29:38 +0000 (17:29 -0800)
committerBen Pfaff <blp@cs.stanford.edu>
Mon, 11 Jan 2021 01:53:54 +0000 (17:53 -0800)
23 files changed:
src/data/file-handle-def.c
src/data/format.c
src/data/mrset.c
src/data/sys-file-reader.c
src/language/lexer/lexer.c
src/language/lexer/token.c
src/language/stats/autorecode.c
src/libpspp/line-reader.c
src/libpspp/str.h
src/output/chart-item.c
src/output/driver.c
src/output/group-item.c
src/output/options.c
src/output/output-item-provider.h
src/output/output-item.c
src/output/pivot-table.c
src/output/spv/spv-legacy-decoder.c
src/output/spv/spv-output.c
src/output/spv/spv-table-look.c
src/output/text-item.c
src/ui/terminal/terminal-reader.c
tests/libpspp/stringi-map-test.c
tests/libpspp/stringi-set-test.c

index 716a029cc701a567d4c28c53ab0119fed66d003f..075f7b07901c79150e84b1b27014ca7202270d53 100644 (file)
@@ -224,7 +224,7 @@ create_handle (const char *id, char *handle_name, enum fh_referent referent,
   struct file_handle *handle = xzalloc (sizeof *handle);
 
   handle->ref_cnt = 1;
-  handle->id = id != NULL ? xstrdup (id) : NULL;
+  handle->id = xstrdup_if_nonnull (id);
   handle->name = handle_name;
   handle->referent = referent;
   handle->encoding = xstrdup (encoding);
@@ -261,7 +261,7 @@ fh_create_file (const char *id, const char *file_name, const char *file_name_enc
   handle_name = id != NULL ? xstrdup (id) : xasprintf ("`%s'", file_name);
   handle = create_handle (id, handle_name, FH_REF_FILE, properties->encoding);
   handle->file_name = xstrdup (file_name);
-  handle->file_name_encoding = file_name_encoding ? xstrdup (file_name_encoding) : NULL;
+  handle->file_name_encoding = xstrdup_if_nonnull (file_name_encoding);
   handle->mode = properties->mode;
   handle->line_ends = properties->line_ends;
   handle->record_width = properties->record_width;
index a25e08f62b4d381e7b9490bc74ffe1dc27ebec85..a4c52b788ebd811541b9e3ac7d0e25beab016d81 100644 (file)
@@ -1157,7 +1157,7 @@ static struct fmt_affix
 fmt_affix_clone (const struct fmt_affix *old)
 {
   return (struct fmt_affix) {
-    .s = old->s ? xstrdup (old->s) : NULL,
+    .s = xstrdup_if_nonnull (old->s),
     .width = old->width,
   };
 }
index 38b0ab2d5266e88192342166df828cd7a98e2bd9..63f4b3fe339cc03248c3631e71132cd3c80e1384 100644 (file)
@@ -40,7 +40,7 @@ mrset_clone (const struct mrset *old)
 
   new = xmalloc (sizeof *new);
   new->name = xstrdup (old->name);
-  new->label = old->label != NULL ? xstrdup (old->label) : NULL;
+  new->label = xstrdup_if_nonnull (old->label);
   new->type = old->type;
   new->vars = xmemdup (old->vars, old->n_vars * sizeof *old->vars);
   new->n_vars = old->n_vars;
index ec8ae3b32fd654c9f9ee0fcbbca007c0049fc075..3a54801c3b648b7a92854f6a75d22022e332e15c 100644 (file)
@@ -1999,7 +1999,7 @@ rename_var_and_save_short_names (struct sfm_reader *r, off_t pos,
   for (i = 0; i < n_short_names; i++)
     {
       const char *s = var_get_short_name (var, i);
-      short_names[i] = s != NULL ? xstrdup (s) : NULL;
+      short_names[i] = xstrdup_if_nonnull (s);
     }
 
   /* Set long name. */
index 820a48127175bd6decd0545d28d0f3f37f5533f7..ceeb153c3e596804a924ea90fffd6a474ff0db66 100644 (file)
@@ -141,7 +141,7 @@ void
 lex_reader_set_file_name (struct lex_reader *reader, const char *file_name)
 {
   free (reader->file_name);
-  reader->file_name = file_name != NULL ? xstrdup (file_name) : NULL;
+  reader->file_name = xstrdup_if_nonnull (file_name);
 }
 \f
 /* Creates and returns a new lexer. */
@@ -1610,7 +1610,7 @@ lex_reader_for_file (const char *file_name, const char *encoding,
   r->reader.syntax = syntax;
   r->reader.error = error;
   r->reader.file_name = xstrdup (file_name);
-  r->reader.encoding = encoding ? xstrdup (encoding) : NULL;
+  r->reader.encoding = xstrdup_if_nonnull (encoding);
   r->reader.line_number = 1;
   r->istream = istream;
 
@@ -1679,7 +1679,7 @@ lex_reader_for_substring_nocopy (struct substring s, const char *encoding)
   r = xmalloc (sizeof *r);
   lex_reader_init (&r->reader, &lex_string_reader_class);
   r->reader.syntax = LEX_SYNTAX_AUTO;
-  r->reader.encoding = encoding ? xstrdup (encoding) : NULL;
+  r->reader.encoding = xstrdup_if_nonnull (encoding);
   r->s = s;
   r->offset = 0;
 
index 80c6615c5a5015048ed7e4767ada3e135c26262f..9c5fef9991b20726811a684c047fb52d941c8af4 100644 (file)
@@ -135,8 +135,6 @@ string_representation (struct substring ss)
 char *
 token_to_string (const struct token *token)
 {
-  const char *name;
-
   switch (token->type)
     {
     case T_POS_NUM:
@@ -150,8 +148,7 @@ token_to_string (const struct token *token)
       return string_representation (token->string);
 
     default:
-      name = token_type_to_name (token->type);
-      return name != NULL ? xstrdup (name) : NULL;
+      return xstrdup_if_nonnull (token_type_to_name (token->type));
     }
 }
 
index 91fffcf27a07158b8f78ffa9bc1833fb2e428aba..3b51928fc64e4633008d0cc24b5e484d3312d618 100644 (file)
@@ -245,7 +245,7 @@ cmd_autorecode (struct lexer *lexer, struct dataset *ds)
       spec->format = *var_get_print_format (src_vars[i]);
 
       const char *label = var_get_label (src_vars[i]);
-      spec->label = label ? xstrdup (label) : NULL;
+      spec->label = xstrdup_if_nonnull (label);
 
       if (group && i > 0)
         spec->items = arc->specs[0].items;
index cb4bd014b93ef6b90bd4e75d8649a216be009e2b..c7d024b44a45706e925e2d45743c6e831f6beb9b 100644 (file)
@@ -125,7 +125,7 @@ line_reader_for_fd (const char *encoding, int fd)
       && !strcmp (r->encoding, "ASCII"))
     {
       r->state = S_AUTO;
-      r->auto_encoding = encoding ? xstrdup (encoding) : NULL;
+      r->auto_encoding = xstrdup_if_nonnull (encoding);
     }
   else
     r->state = r->encoding_info.unit == 1 ? S_UNIBYTE : S_MULTIBYTE;
index 66552a396486133a7642366152f7d98b72151bf1..7917d58a70dd3a62e305ff84c6884aa85e7061cb 100644 (file)
@@ -29,6 +29,8 @@
 #include "memcasecmp.h"
 #include "xstrndup.h"
 #include "xvasprintf.h"
+
+#include "gl/xalloc.h"
 \f
 /* Miscellaneous. */
 
@@ -50,6 +52,8 @@ void str_lowercase (char *);
 bool str_format_26adic (unsigned long int number, bool uppercase,
                         char buffer[], size_t);
 
+static inline char *xstrdup_if_nonnull (const char *);
+
 void *mempset (void *, int, size_t);
 \f
 /* Common character classes for use with substring and string functions. */
@@ -281,4 +285,10 @@ ss_buffer (const char *buffer, size_t cnt)
   return ss;
 }
 
+static inline char *
+xstrdup_if_nonnull (const char *s)
+{
+  return s ? xstrdup (s) : NULL;
+}
+
 #endif /* str_h */
index 3b3761372e185af855992f5c5d3a3b162e497722..cedfa37a60c6cb78c0a9d2f4a165def011606fc0 100644 (file)
@@ -24,6 +24,7 @@
 
 #include "libpspp/cast.h"
 #include "libpspp/compiler.h"
+#include "libpspp/str.h"
 #include "output/driver.h"
 #include "output/output-item-provider.h"
 
@@ -46,7 +47,7 @@ chart_item_init (struct chart_item *item, const struct chart_item_class *class,
   *item = (struct chart_item) {
     .output_item = OUTPUT_ITEM_INITIALIZER (&chart_item_class),
     .class = class,
-    .title = title ? xstrdup (title) : NULL
+    .title = xstrdup_if_nonnull (title)
   };
 }
 
@@ -67,7 +68,7 @@ chart_item_set_title (struct chart_item *item, const char *title)
 {
   assert (!chart_item_is_shared (item));
   free (item->title);
-  item->title = title != NULL ? xstrdup (title) : NULL;
+  item->title = xstrdup_if_nonnull (title);
 }
 
 /* Submits ITEM to the configured output drivers, and transfers ownership to
index a4718c939aaf79c04dbe504856a9fd72b0eeb17e..824a1ee2112a0810fe72fc83199cf8ca52015ed2 100644 (file)
@@ -227,9 +227,8 @@ output_submit (struct output_item *item)
       if (e->n_groups >= e->allocated_groups)
         e->groups = x2nrealloc (e->groups, &e->allocated_groups,
                                 sizeof *e->groups);
-      e->groups[e->n_groups] = (group_open_item->command_name
-                                ? xstrdup (group_open_item->command_name)
-                                : NULL);
+      e->groups[e->n_groups] = xstrdup_if_nonnull (
+        group_open_item->command_name);
       e->n_groups++;
     }
   else if (is_group_close_item (item))
@@ -298,7 +297,7 @@ static void
 output_set_title__ (struct output_engine *e, char **dst, const char *src)
 {
   free (*dst);
-  *dst = src ? xstrdup (src) : NULL;
+  *dst = xstrdup_if_nonnull (src);
 
   char *page_title
     = (e->title && e->subtitle ? xasprintf ("%s\n%s", e->title, e->subtitle)
index 3bfb44cd096146d9b97af5c9958d9741fc457296..1c8fce5e8b444412358061837d4d8a0a6dd87014 100644 (file)
@@ -21,6 +21,7 @@
 #include <stdlib.h>
 
 #include "libpspp/compiler.h"
+#include "libpspp/str.h"
 #include "output/driver.h"
 #include "output/output-item-provider.h"
 
@@ -33,8 +34,8 @@ struct group_open_item *
 group_open_item_create (const char *command_name, const char *label)
 {
   return group_open_item_create_nocopy (
-    command_name ? xstrdup (command_name) : NULL,
-    label ? xstrdup (label) : NULL);
+    xstrdup_if_nonnull (command_name),
+    xstrdup_if_nonnull (label));
 }
 
 struct group_open_item *
index f5ab6fbc87c523601a455044ccf21d05092cb8c0..a212d1c63ee5190c752a7a007087df6338c47087 100644 (file)
@@ -51,8 +51,8 @@ driver_option_create (const char *driver_name, const char *name,
   struct driver_option *o = xmalloc (sizeof *o);
   o->driver_name = xstrdup (driver_name);
   o->name = xstrdup (name);
-  o->value = value != NULL ? xstrdup (value) : NULL;
-  o->default_value = default_value ? xstrdup (default_value) : NULL;
+  o->value = xstrdup_if_nonnull (value);
+  o->default_value = xstrdup_if_nonnull (default_value);
   return o;
 }
 
index 7517996119112faa2d683dde6d6522d01b492595..552549cee9eaa54fee0288fd2b5dc9877f49c637 100644 (file)
@@ -17,6 +17,7 @@
 #ifndef OUTPUT_ITEM_PROVIDER_H
 #define OUTPUT_ITEM_PROVIDER_H 1
 
+#include "libpspp/str.h"
 #include "output/output-item.h"
 
 /* Class structure for an output item.
@@ -40,7 +41,7 @@ struct output_item_class
   {                                                             \
     .class = (SRC)->class,                                      \
     .ref_cnt = 1,                                               \
-    .label = (SRC)->label ? xstrdup ((SRC)->label) : NULL,      \
+    .label = xstrdup_if_nonnull ((SRC)->label),                 \
   }
 
 #endif /* output/output-item-provider.h */
index 860678b25f99932e3949fa23bd7cc404f3e2b164..cd37031b654b628711fc63dafc4faca3cf795e8b 100644 (file)
@@ -23,6 +23,7 @@
 
 #include "libpspp/assertion.h"
 #include "libpspp/cast.h"
+#include "libpspp/str.h"
 
 #include "gl/xalloc.h"
 
@@ -79,7 +80,7 @@ output_item_get_label (const struct output_item *item)
 void
 output_item_set_label (struct output_item *item, const char *label)
 {
-  output_item_set_label_nocopy (item, label ? xstrdup (label) : NULL);
+  output_item_set_label_nocopy (item, xstrdup_if_nonnull (label));
 }
 
 /* Sets the label for ITEM to LABEL, transferring ownership of LABEL to ITEM.
index 1b3014c629694900d0336007f6a19d8efb08429d..3698b78559037a955e949a2c56ce7201b03897da 100644 (file)
@@ -913,12 +913,6 @@ pivot_table_ref (const struct pivot_table *table_)
   return table;
 }
 
-static char *
-xstrdup_if_nonnull (const char *s)
-{
-  return s ? xstrdup (s) : NULL;
-}
-
 static struct pivot_table_sizing
 clone_sizing (const struct pivot_table_sizing *s)
 {
index 0b224405b7913a297f5236144d4ad804ae4e1edd..099d96cbcc8077ed63fd8db6fe77e7ad29460a86 100644 (file)
@@ -787,7 +787,7 @@ decode_spvdx_source_variable (const struct spvxml_node *node,
   struct spv_series *s = xzalloc (sizeof *s);
   s->name = xstrdup (node->id);
   s->xml = node;
-  s->label = sv->label ? xstrdup (sv->label) : NULL;
+  s->label = xstrdup_if_nonnull (sv->label);
   s->label_series = label_series;
   s->values = spv_data_values_clone (var->values, var->n_values);
   s->n_values = var->n_values;
index edca066ed2442f11211672a16bc82d79a2068672..49cc195341d8c77351939f8c7d9fa10204701f08 100644 (file)
@@ -33,6 +33,6 @@ spv_text_submit (const struct spv_item *in)
      : class == SPV_CLASS_PAGETITLE ? TEXT_ITEM_PAGE_TITLE
      : TEXT_ITEM_LOG),
     pivot_value_clone (spv_item_get_text (in)),
-    in->label ? xstrdup (in->label) : NULL);
+    xstrdup_if_nonnull (in->label));
   text_item_submit (item);
 }
index 3e5f962bd3b08be527cbe6f1f029bf1410fe9a57..75d5f97e82ab67c4b2ca2398b46805e85b8eeacc 100644 (file)
@@ -131,7 +131,7 @@ spv_table_look_decode (const struct spvsx_table_properties *in,
   struct pivot_table_look *out = pivot_table_look_new_builtin_default ();
   char *error = NULL;
 
-  out->name = in->name ? xstrdup (in->name) : NULL;
+  out->name = xstrdup_if_nonnull (in->name);
 
   const struct spvsx_general_properties *g = in->general_properties;
   out->omit_empty = g->hide_empty_rows != 0;
index 7bcf2aa10d67f8f72cf19fb23d9ed7c0f620d505..a0428acc82c44209e3c91d16e98ebb2a6a34a8c2 100644 (file)
@@ -75,7 +75,7 @@ text_item_create (enum text_item_type type, const char *text,
                   const char *label)
 {
   return text_item_create_nocopy (type, xstrdup (text),
-                                  label ? xstrdup (label) : NULL);
+                                  xstrdup_if_nonnull (label));
 }
 
 /* Creates and returns a new text item containing VALUE, TYPE, and LABEL.
index 071e435eca6f572809331c84d2cd2fe493c1b8c5..c7a9a311c2494adcecedf316474d910c404f2948 100644 (file)
@@ -22,6 +22,7 @@
 #include <stdbool.h>
 #include <stdio.h>
 
+#include "libpspp/str.h"
 
 #if HAVE_READLINE
 #include <readline/readline.h>
@@ -408,7 +409,7 @@ command_generator (const char *text, int state)
   if (state == 0)
     cmd = NULL;
   name = cmd_complete (text, &cmd);
-  return name ? xstrdup (name) : NULL;
+  return xstrdup_if_nonnull (name);
 }
 
 #else  /* !HAVE_READLINE */
index 20b8ce5c2c8e85039be225bae5b377be1d94e19b..a78fd56557f53a46be68ac9a32a346fe1f377acd 100644 (file)
@@ -69,58 +69,6 @@ check_func (bool ok, int line)
    terminates. */
 #define check(EXPR) check_func ((EXPR), __LINE__)
 
-/* Prints a message about memory exhaustion and exits with a
-   failure code. */
-static void
-xalloc_die (void)
-{
-  printf ("virtual memory exhausted\n");
-  exit (EXIT_FAILURE);
-}
-
-static void *xmalloc (size_t n) MALLOC_LIKE;
-static void *xnmalloc (size_t n, size_t m) MALLOC_LIKE;
-static void *xmemdup (const void *p, size_t n) MALLOC_LIKE;
-
-/* Allocates and returns N bytes of memory. */
-static void *
-xmalloc (size_t n)
-{
-  if (n != 0)
-    {
-      void *p = malloc (n);
-      if (p == NULL)
-        xalloc_die ();
-
-      return p;
-    }
-  else
-    return NULL;
-}
-
-static void *
-xmemdup (const void *p, size_t n)
-{
-  void *q = xmalloc (n);
-  memcpy (q, p, n);
-  return q;
-}
-
-/* Clone STRING.  */
-static char *
-xstrdup (const char *string)
-{
-  return xmemdup (string, strlen (string) + 1);
-}
-
-/* Allocates and returns N * M bytes of memory. */
-static void *
-xnmalloc (size_t n, size_t m)
-{
-  if ((size_t) -1 / m <= n)
-    xalloc_die ();
-  return xmalloc (n * m);
-}
 \f
 /* Support routines. */
 
index 2bcc760780c4bde4f78e718582d9fbcdf9f92f03..74f1ba11e62b6459057573f24d79dd89452b6357 100644 (file)
@@ -63,59 +63,6 @@ check_func (bool ok, int line)
    If not, prints a message citing the calling line number and
    terminates. */
 #define check(EXPR) check_func ((EXPR), __LINE__)
-
-/* Prints a message about memory exhaustion and exits with a
-   failure code. */
-static void
-xalloc_die (void)
-{
-  printf ("virtual memory exhausted\n");
-  exit (EXIT_FAILURE);
-}
-
-static void *xmalloc (size_t n) MALLOC_LIKE;
-static void *xnmalloc (size_t n, size_t m) MALLOC_LIKE;
-static void *xmemdup (const void *p, size_t n) MALLOC_LIKE;
-
-/* Allocates and returns N bytes of memory. */
-static void *
-xmalloc (size_t n)
-{
-  if (n != 0)
-    {
-      void *p = malloc (n);
-      if (p == NULL)
-        xalloc_die ();
-
-      return p;
-    }
-  else
-    return NULL;
-}
-
-static void *
-xmemdup (const void *p, size_t n)
-{
-  void *q = xmalloc (n);
-  memcpy (q, p, n);
-  return q;
-}
-
-/* Clone STRING.  */
-static char *
-xstrdup (const char *string)
-{
-  return xmemdup (string, strlen (string) + 1);
-}
-
-/* Allocates and returns N * M bytes of memory. */
-static void *
-xnmalloc (size_t n, size_t m)
-{
-  if ((size_t) -1 / m <= n)
-    xalloc_die ();
-  return xmalloc (n * m);
-}
 \f
 /* Support routines. */