summaryrefslogtreecommitdiff
path: root/support/xpdfopen/utils.c
blob: 5201b2dba5847aceced85e5340341c5fbddd9b5a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
/*
 * utils.c: Functions common to pdfopen and pdfclose.
 *
 * Copyright (C) 2010--2014 Jim Diamond <jim.diamond@acadiau.ca>
 * You may freely use, modify and/or distribute this file.
 */

#include    <stdio.h>
#include    <stdlib.h>
#include    <string.h>
#include    "utils.h"
#include    "xpdfopen.h"
#include    "externs.h"

/*
 * Allocate memory and create a window name.
 * Return a pointer to the new string or NULL on failure.
 * 
 * The caller is responsible for free()ing the memory allocated here.
 *
 * Note: acroread (at least AR 7, 8, 9) uses only the basename in the
 *       window title.
 *	 xpdf uses the whole file name.
 *	 Other PDF viewers may need other treatment.
 */

char *
make_window_name(const char * fmt, const char * filename)
{
    char * window_name;
    const char * title_name;

    title_name = filename;
    if (strrchr(title_name, '/') != NULL
	&& strncasecmp(fmt, XPDF_WIN_NAME, strlen(XPDF_WIN_NAME)))
	title_name = strrchr(title_name, '/') + 1;

    window_name = malloc(strlen(fmt) + strlen(title_name) + 1);
    if (window_name != NULL)
	sprintf(window_name, fmt, title_name);
    else
	fprintf(stderr, "%s: out of memory\n", progname);

    return window_name;
}