~ubuntu-branches/ubuntu/vivid/bino/vivid-proposed

« back to all changes in this revision

Viewing changes to src/base/str.h

  • Committer: Bazaar Package Importer
  • Author(s): Daniel Schaal
  • Date: 2011-09-09 14:23:54 UTC
  • Revision ID: james.westby@ubuntu.com-20110909142354-m1a9a4523i7ddb02
Tags: upstream-1.2.0
ImportĀ upstreamĀ versionĀ 1.2.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * This file is part of bino, a 3D video player.
 
3
 *
 
4
 * Copyright (C) 2009-2011
 
5
 * Martin Lambers <marlam@marlam.de>
 
6
 *
 
7
 * This program is free software; you can redistribute it and/or modify
 
8
 * it under the terms of the GNU General Public License as published by
 
9
 * the Free Software Foundation; either version 3 of the License, or
 
10
 * (at your option) any later version.
 
11
 *
 
12
 * This program is distributed in the hope that it will be useful,
 
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
15
 * GNU General Public License for more details.
 
16
 *
 
17
 * You should have received a copy of the GNU General Public License
 
18
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
19
 */
 
20
 
 
21
/**
 
22
 * \file str.h
 
23
 *
 
24
 * Tiny tools for strings.
 
25
 */
 
26
 
 
27
#ifndef STR_H
 
28
#define STR_H
 
29
 
 
30
#include <string>
 
31
#include <cstdarg>
 
32
#include <cerrno>
 
33
#include <stdint.h>
 
34
 
 
35
#include "exc.h"
 
36
 
 
37
#ifdef __GNUC__
 
38
# define STR_AFP(a, b) __attribute__ ((format (printf, a, b)))
 
39
#else
 
40
# define STR_AFP(a, b) /* empty */
 
41
#endif
 
42
 
 
43
namespace str
 
44
{
 
45
    /* Sanitize a string (replace control characters with '?') */
 
46
    std::string sanitize(const std::string &s);
 
47
 
 
48
    /* Trim a string (remove whitespace from both ends) */
 
49
    std::string trim(const std::string &s);
 
50
 
 
51
    /* Create std::strings from all basic data types */
 
52
    std::string from(bool x);
 
53
    std::string from(signed char x);
 
54
    std::string from(unsigned char x);
 
55
    std::string from(short x);
 
56
    std::string from(unsigned short x);
 
57
    std::string from(int x);
 
58
    std::string from(unsigned int x);
 
59
    std::string from(long x);
 
60
    std::string from(unsigned long x);
 
61
    std::string from(long long x);
 
62
    std::string from(unsigned long long x);
 
63
    std::string from(float x);
 
64
    std::string from(double x);
 
65
    std::string from(long double x);
 
66
 
 
67
    /* Convert a string to one of the basic data types */
 
68
    template<typename T> T to(const std::string &s);
 
69
    template<> bool to<bool>(const std::string &s);
 
70
    template<> signed char to<signed char>(const std::string &s);
 
71
    template<> unsigned char to<unsigned char>(const std::string &s);
 
72
    template<> short to<short>(const std::string &s);
 
73
    template<> unsigned short to<unsigned short>(const std::string &s);
 
74
    template<> int to<int>(const std::string &s);
 
75
    template<> unsigned int to<unsigned int>(const std::string &s);
 
76
    template<> long to<long>(const std::string &s);
 
77
    template<> unsigned long to<unsigned long>(const std::string &s);
 
78
    template<> long long to<long long>(const std::string &s);
 
79
    template<> unsigned long long to<unsigned long long>(const std::string &s);
 
80
    template<> float to<float>(const std::string &s);
 
81
    template<> double to<double>(const std::string &s);
 
82
    template<> long double to<long double>(const std::string &s);
 
83
 
 
84
    /* Create std::strings printf-like */
 
85
    std::string vasprintf(const char *format, va_list args) STR_AFP(1, 0);
 
86
    std::string asprintf(const char *format, ...) STR_AFP(1, 2);
 
87
 
 
88
    /* Replace all instances of s with r in str */
 
89
    std::string &replace(std::string &str, const std::string &s, const std::string &r);
 
90
 
 
91
    /* Create a hex string from binary data */
 
92
    std::string hex(const std::string &s, bool uppercase = false);
 
93
    std::string hex(const void *buf, size_t n, bool uppercase = false);
 
94
 
 
95
    /* Convert various values to human readable strings */
 
96
    std::string human_readable_memsize(const uintmax_t size);
 
97
    std::string human_readable_length(const double length);
 
98
    std::string human_readable_time(int64_t microseconds);
 
99
 
 
100
    /* Convert a string from one character set to another */
 
101
    std::string convert(const std::string &src, const std::string &from_charset, const std::string &to_charset);
 
102
}
 
103
 
 
104
#endif