1/* -*- c -*- */
2/*****************************************************************************/
3/*  LibreDWG - free implementation of the DWG file format                    */
4/*                                                                           */
5/*  Copyright (C) 2010 Thien-Thi Nguyen                                      */
6/*  Copyright (C) 2014, 2018 Free Software Foundation, Inc.                  */
7/*                                                                           */
8/*  This library is free software, licensed under the terms of the GNU       */
9/*  General Public License as published by the Free Software Foundation,     */
10/*  either version 3 of the License, or (at your option) any later version.  */
11/*  You should have received a copy of the GNU General Public License        */
12/*  along with this program.  If not, see <http://www.gnu.org/licenses/>.    */
13/*****************************************************************************/
14
15/*
16 * suffix.c: avoid hardcoded input/output filenames
17 * written by Thien-Thi Nguyen
18 * modified by Reini Urban
19 */
20
21#include "config.h"
22#ifdef __STDC_ALLOC_LIB__ /* for strdup */
23# define __STDC_WANT_LIB_EXT2__ 1 /* for strdup */
24#else
25# define _USE_BSD 1
26#endif
27#ifndef _XOPEN_SOURCE /* for strdup, snprintf */
28# define _XOPEN_SOURCE 700
29#endif
30#include <stdlib.h>
31#include <stdio.h>
32#include <libgen.h>
33#include <string.h>
34
35char *
36suffix (const char *filename, const char *ext);
37
38/* Return a newly `malloc'ed string made from "re-suffixing" FILENAME with
39   ".EXT" (note dot).  That is, when FILENAME has the form "STEM.dwg" the
40   value is "STEM.EXT", otherwise the value is "FILENAME.EXT".
41
42   Caller should `free' the returned string when done using it.  */
43char *
44suffix (const char *filename, const char *ext)
45{
46  char *copy = strdup (filename ? filename : "");
47#ifdef HAVE_BASENAME
48  char *base = basename (copy);
49#else
50  char *base = copy;
51#endif
52  int len = (base ? strlen (base) : 0) + 1 + strlen (ext) + 1;
53  char *rv = malloc (len);
54  char *dot;
55
56  if (!base) // basename may fail
57    {
58      base = (char *)"";
59    }
60  else
61    {
62      if ((dot = strrchr (base, '.'))
63          && dot + 4 < base + len
64          //valid input extensions:
65          && (!strncmp (1 + dot, "dwg", 3) || // strip known extensions
66              !strncmp (1 + dot, "DWG", 3) ||
67              !strncmp (1 + dot, "dxf", 3) || // TODO xml
68              !strncmp (1 + dot, "DXF", 3) || // TODO xml
69              !strncmp (1 + dot, "json", 4) ||
70              !strncmp (1 + dot, "JSON", 4)))
71        *dot = '\0';
72    }
73  if (strchr(ext, '.'))
74    snprintf (rv, len, "%s%s", base, ext);
75  else
76    snprintf (rv, len, "%s.%s", base, ext);
77  free (copy);
78  return rv;
79}
80
81#define REQUIRE_INPUT_FILE_ARG(argc)            \
82  do                                            \
83    {                                           \
84      if (1 == argc)                            \
85        {                                       \
86          puts ("No input file specified");     \
87          return 1;                             \
88        }                                       \
89    }                                           \
90  while (0)
91
92/* suffix.c ends here */
93