aboutsummaryrefslogtreecommitdiffstats
path: root/searchsummary/src/vespa/juniper/propreader.cpp
blob: bd20c885f6c4833487db2e2d098465816fd3f542 (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
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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include "propreader.h"
#include <vespa/fastlib/io/bufferedfile.h>
#include "juniperdebug.h"
#include <vespa/log/log.h>
LOG_SETUP(".juniper.propreader");

PropReader::PropReader(const char* filename)
    : _keymap()
{
    Process(filename);
}

#define BUFLEN 1024


void PropReader::Process(const char* filename)
{
    Fast_BufferedFile propfile;
    propfile.ReadOpen(filename);
    if (!propfile.IsOpened())
    {
        LOG(warning, "Warning: Could not find property file '%s', using Juniper default values",
            filename);
        return;
    }
    char line[BUFLEN];
    char* linep;
    while ((linep = propfile.ReadLine(line, BUFLEN-1)) != NULL)
    {
        int i;
        char* key;
        if (line[0] == '#') continue; // skip comments

        // find key
        for (i = 0; !isspace(line[i]); i++) { }
        if (i == 0) continue; // Skip lines starting with blank
        line[i++] = 0;
        key = line;

        for (; isspace(line[i]); i++) { }  // Skip blanks

        // find value
        int offset = 0;
        char* value = &line[i];
        for (; !isspace(line[i]); i++)
        {
            if (line[i] == '\\')
            {
                offset++;
                if (line[++i] == 'x')
                {
                    unsigned char v = 0;
                    for (int s = 1; s <= 2; s++, v<<=4)
                    {
                        unsigned char c = static_cast<unsigned char>(line[i + s]);
                        if (isdigit(c))
                            v += (c - '0');
                        else if (c < 'a')
                            v += (c - 'A' + 10);
                        else
                            v += (c - 'a' + 10);
                        if (s == 2) break;
                    }
                    line[i - offset] = static_cast<char>(v);
                    i += 2;
                    offset += 2;
                }
                else
                    if (offset != 0) line[i - offset] = line[i];
            }
            else
                if (offset != 0) line[i - offset] = line[i];
        }
        line[i - offset] = 0;
        LOG(debug, "Parameter :%s: value :%s:", key, value);
        _keymap.Insert(key, value);
    }
}


const char* PropReader::GetProperty(const char* name, const char* def) const
{
    const char*  v = _keymap.Lookup(name, def);
    LOG(debug, "Parameter lookup :%s: value :%s:", name, v);
    return v;
}


void PropReader::UpdateProperty(const char* name, const char* value)
{
    _keymap.Insert(name, value);
}