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);
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;
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,
};
}
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;
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. */
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. */
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;
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;
char *
token_to_string (const struct token *token)
{
- const char *name;
-
switch (token->type)
{
case T_POS_NUM:
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));
}
}
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;
&& !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;
#include "memcasecmp.h"
#include "xstrndup.h"
#include "xvasprintf.h"
+
+#include "gl/xalloc.h"
\f
/* Miscellaneous. */
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. */
return ss;
}
+static inline char *
+xstrdup_if_nonnull (const char *s)
+{
+ return s ? xstrdup (s) : NULL;
+}
+
#endif /* str_h */
#include "libpspp/cast.h"
#include "libpspp/compiler.h"
+#include "libpspp/str.h"
#include "output/driver.h"
#include "output/output-item-provider.h"
*item = (struct chart_item) {
.output_item = OUTPUT_ITEM_INITIALIZER (&chart_item_class),
.class = class,
- .title = title ? xstrdup (title) : NULL
+ .title = xstrdup_if_nonnull (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
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))
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)
#include <stdlib.h>
#include "libpspp/compiler.h"
+#include "libpspp/str.h"
#include "output/driver.h"
#include "output/output-item-provider.h"
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 *
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;
}
#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.
{ \
.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 */
#include "libpspp/assertion.h"
#include "libpspp/cast.h"
+#include "libpspp/str.h"
#include "gl/xalloc.h"
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.
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)
{
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;
: 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);
}
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;
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.
#include <stdbool.h>
#include <stdio.h>
+#include "libpspp/str.h"
#if HAVE_READLINE
#include <readline/readline.h>
if (state == 0)
cmd = NULL;
name = cmd_complete (text, &cmd);
- return name ? xstrdup (name) : NULL;
+ return xstrdup_if_nonnull (name);
}
#else /* !HAVE_READLINE */
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. */
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. */