BRL-CAD
bu_basename.c
Go to the documentation of this file.
1 /* T E S T _ B A S E N A M E . C
2  * BRL-CAD
3  *
4  * Copyright (c) 2011-2014 United States Government as represented by
5  * the U.S. Army Research Laboratory.
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public License
9  * version 2.1 as published by the Free Software Foundation.
10  *
11  * This library is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this file; see the file named COPYING for more
18  * information.
19  */
20 
21 #include "common.h"
22 
23 #include <stdlib.h>
24 #include <stdio.h>
25 #include <string.h>
26 #ifdef HAVE_LIBGEN_H /* for basename */
27 # include <libgen.h>
28 #endif
29 
30 #include "bu.h"
31 
32 #ifdef HAVE_BASENAME
33 /* These two functions wrap the system and bu implementations to
34  * standardize the memory behavior. The input string is unmodified,
35  * the output string is dynamically allocated and must be freed by the
36  * caller.
37  */
38 
39 char *
40 get_system_output(const char *input)
41 {
42  char *in = input ? bu_strdup(input) : NULL;
43  char *out = bu_strdup(basename(in));
44 
45  if (in) {
46  bu_free(in, "input copy");
47  }
48  return out;
49 }
50 
51 char *
52 get_bu_output(const char *input)
53 {
54  /* basename should return "." when given a NULL string */
55  size_t null_result_chars = sizeof(".") / sizeof(char);
56  size_t max_result_chars = input ? strlen(input) + 1 : null_result_chars;
57 
58  char *out = (char *)bu_calloc(max_result_chars, sizeof(char), "bu output");
59 
60  bu_basename(out, input);
61 
62  return out;
63 }
64 #endif
65 
66 void
68 {
69 #ifdef HAVE_BASENAME
70  char *sys_out = get_system_output(input);
71  char *bu_out = get_bu_output(input);
72 
73  if (BU_STR_EQUAL(sys_out, bu_out)) {
74  printf("%24s -> %24s [PASSED]\n", input, bu_out);
75  bu_free(bu_out, "bu output");
76  bu_free(sys_out, "system output");
77  } else {
78  bu_log("%24s -> %24s (should be: %s) [FAIL]\n", input, bu_out, sys_out);
79  bu_free(bu_out, "bu output");
80  bu_free(sys_out, "system output");
81  bu_exit(EXIT_FAILURE, "");
82  }
83 #else
84  bu_exit(EXIT_FAILURE, "BASENAME not available on this platform\n");
85 #endif
86 }
87 
88 
89 int
90 main(int argc, char *argv[])
91 {
92  /* If we don't have any args at all, test NULL */
93  if (argc == 1) {
95  }
96 
97  /* If we have something, print it and test it */
98  if (argc > 1) {
99  printf("Testing string \"%s\"\n", argv[1]);
101  }
102 
103  return 0;
104 }
105 
106 
107 /*
108  * Local Variables:
109  * mode: C
110  * tab-width: 8
111  * indent-tabs-mode: t
112  * c-file-style: "stroustrup"
113  * End:
114  * ex: shiftwidth=4 tabstop=8
115  */
void bu_log(const char *,...) _BU_ATTR_PRINTF12
Definition: log.c:176
Header file for the BRL-CAD common definitions.
void bu_exit(int status, const char *fmt,...) _BU_ATTR_NORETURN _BU_ATTR_PRINTF23
Definition: bomb.c:195
void * bu_calloc(size_t nelem, size_t elsize, const char *str)
Definition: malloc.c:321
goto out
Definition: nmg_mod.c:3846
int main(int argc, char *argv[])
Definition: bu_basename.c:90
void bu_basename(char *basename, const char *path)
Definition: basename.c:30
void bu_free(void *ptr, const char *str)
Definition: malloc.c:328
#define bu_strdup(s)
Definition: str.h:71
void compare_bu_to_system_basename(const char *input)
Definition: bu_basename.c:67
#define BU_STR_EQUAL(s1, s2)
Definition: str.h:126