1/*
2 * Copyright 2012, The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *     http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "Parameters.h"
18
19#include <media/stagefright/MediaErrors.h>
20
21namespace android {
22
23// static
24sp<Parameters> Parameters::Parse(const char *data, size_t size) {
25    sp<Parameters> params = new Parameters;
26    status_t err = params->parse(data, size);
27
28    if (err != OK) {
29        return NULL;
30    }
31
32    return params;
33}
34
35Parameters::Parameters() {}
36
37Parameters::~Parameters() {}
38
39status_t Parameters::parse(const char *data, size_t size) {
40    size_t i = 0;
41    while (i < size) {
42        size_t nameStart = i;
43        while (i < size && data[i] != ':') {
44            ++i;
45        }
46
47        if (i == size || i == nameStart) {
48            return ERROR_MALFORMED;
49        }
50
51        AString name(&data[nameStart], i - nameStart);
52        name.trim();
53        name.tolower();
54
55        ++i;
56
57        size_t valueStart = i;
58
59        while (i + 1 < size && (data[i] != '\r' || data[i + 1] != '\n')) {
60            ++i;
61        }
62
63        AString value(&data[valueStart], i - valueStart);
64        value.trim();
65
66        mDict.add(name, value);
67
68        i += 2;
69    }
70
71    return OK;
72}
73
74bool Parameters::findParameter(const char *name, AString *value) const {
75    AString key = name;
76    key.tolower();
77
78    ssize_t index = mDict.indexOfKey(key);
79
80    if (index < 0) {
81        value->clear();
82
83        return false;
84    }
85
86    *value = mDict.valueAt(index);
87    return true;
88}
89
90}  // namespace android
91