File: misc.cc

package info (click to toggle)
yapet 0.6-3
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 6,012 kB
  • ctags: 2,913
  • sloc: ansic: 13,661; cpp: 11,384; sh: 4,814; makefile: 847; yacc: 291; sed: 16
file content (129 lines) | stat: -rw-r--r-- 3,228 bytes parent folder | download
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
// $Id: misc.cc 2719 2009-08-20 01:30:25Z rafi $
//
// Copyright (C) 2008, 2009  Rafael Ostertag
//
// This file is part of YAPET.
//
// YAPET is free software: you can redistribute it and/or modify it under the
// terms of the GNU General Public License as published by the Free Software
// Foundation, either version 3 of the License, or (at your option) any later
// version.
//
// YAPET is distributed in the hope that it will be useful, but WITHOUT ANY
// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
// details.
//
// You should have received a copy of the GNU General Public License along with
// YAPET.  If not, see <http://www.gnu.org/licenses/>.
//

#include "misc.h"

#ifdef HAVE_NCURSES_H
# include <ncurses.h>
#else // HAVE_NCURSES_H
# ifdef HAVE_CURSES_H
#  include <curses.h>
# else
#  error "Neither curses.h nor ncurses.h available"
# endif // HAVE_CURSES_H
#endif // HAVE_NCURSES_H
#include "curswa.h" // Leave this here. It depends on the above includes.

#ifdef HAVE_STDIO_H
#include <stdio.h>
#else
# error "Sorry, stdio.h needed"
#endif

#ifdef HAVE_STRING_H
#include <string.h>
#endif
#ifdef HAVE_STRINGS_H
#include <strings.h>
#endif

// For convenience
#if defined(HAVE_TERMINALTITLE) && defined(HAVE_TERMNAME)
#define CANSETTITLE
#endif

/**
 * @brief The terminals we know
 *
 * Holds the terminals we know that can have set the title.
 */
static const char* xterms[] = {
    "xterm",
    "dtterm",
    NULL
};

/**
 * @brief Wrapper function
 *
 * Wrapper function for strncasecmp, strncmp, strcmp.
 *
 * @param s1 String one
 * @param s2 String two
 */
inline static int mystrcmp (const char* s1, const char* s2) {
#ifdef HAVE_STRNCASECMP
    return strncasecmp (s1, s2, (strlen (s1) < strlen (s2) ? strlen (s1) : strlen (s2) ) );
#elif HAVE_STRNCMP
    return strncmp (s1, s2, (strlen (s1) < strlen (s2) ? strlen (s1) : strlen (s2) ) );
#elif HAVE_STRCMP
    return strcmp (s1, s2);
#else
# error "Sorry, strncasecmp, strncmp, or strcmp needed"
#endif
}



/**
 * Indicates whether or not the terminal is an X Terminal. It uses the
 * \c termname() function to determine the type of terminal. It compares the
 * return value of \c termname() with the values stored in \c xterms.
 *
 * The function is always available, but always returns \c false if \c termname
 * is not supported
 *
 * @retval \c true if the terminal is an X Terminal, \c false otherwise.
 */
#ifdef HAVE_TERMNAME
bool isXTerm() {
    const char** tmp = xterms;
    char* tn = termname();

    while (*tmp != NULL) {
        if (mystrcmp (tn, *tmp) == 0) return true;

        tmp++;
    }

    return false;
}
#else
bool isXTerm() {
    return false;
}
#endif // HAVE_TERMNAME

/**
 * If supported, set the title on a terminal. The function is always available,
 * but might not have an effect.
 *
 * @param title the title to set
 */
#ifdef CANSETTITLE
void setTerminalTitle (const std::string& title) {
    if (isXTerm() ) {
        fprintf (stdout, "%c]0;%s%c", '\033', title.c_str(), '\007');
        fflush (stdout);
    }
}
#else // CANSETTITLE
void setTerminalTitle (const std::string& title) {}
#endif // CANSETTITLE