aboutsummaryrefslogtreecommitdiffstats
path: root/libqpdf/QPDF_Name.cc
blob: ce2184f8e7147e3dedf175be6470943826bd958f (plain) (blame)
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
#include <qpdf/QPDF_Name.hh>

#include <string.h>
#include <stdio.h>

QPDF_Name::QPDF_Name(std::string const& name) :
    name(name)
{
}

QPDF_Name::~QPDF_Name()
{
}

std::string
QPDF_Name::normalizeName(std::string const& name)
{
    std::string result;
    char num[4];
    result += name[0];
    for (unsigned int i = 1; i < name.length(); ++i)
    {
	char ch = name[i];
	// Don't use locale/ctype here; follow PDF spec guidelines.
	if (strchr("#()<>[]{}/%", ch) || (ch < 33) || (ch > 126))
	{
	    sprintf(num, "#%02x", (unsigned char) ch);
	    result += num;
	}
	else
	{
	    result += ch;
	}
    }
    return result;
}

std::string
QPDF_Name::unparse()
{
    return normalizeName(this->name);
}

std::string
QPDF_Name::getName() const
{
    return this->name;
}