-
Notifications
You must be signed in to change notification settings - Fork 91
/
Copy pathform_data.cpp
86 lines (76 loc) · 2.17 KB
/
form_data.cpp
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
/*
* .============.
* // M A K E / \
* // C++ DEV / \
* // E A S Y / \/ \
* ++ ----------. \/\ .
* \\ \ \ /\ /
* \\ \ \ /
* \\ \ \ /
* -============'
*
* Copyright (c) 2025 Hevake and contributors, all rights reserved.
*
* This file is part of cpp-tbox (https://github.com/cpp-main/cpp-tbox)
* Use of this source code is governed by MIT license that can be found
* in the LICENSE file in the root of the source tree. All contributing
* project authors may be found in the CONTRIBUTORS.md file in the root
* of the source tree.
*/
#include "form_data.h"
namespace tbox {
namespace http {
namespace server {
void FormData::addItem(const FormItem& item) {
items_.push_back(item);
size_t index = items_.size() - 1;
name_index_[item.name].push_back(index);
}
const std::vector<FormItem>& FormData::items() const {
return items_;
}
std::vector<FormItem> FormData::getItems(const std::string& name) const {
std::vector<FormItem> result;
auto it = name_index_.find(name);
if (it != name_index_.end()) {
for (auto index : it->second) {
result.push_back(items_[index]);
}
}
return result;
}
bool FormData::getItem(const std::string& name, FormItem& item) const {
auto it = name_index_.find(name);
if (it != name_index_.end() && !it->second.empty()) {
item = items_[it->second.front()];
return true;
}
return false;
}
bool FormData::getField(const std::string& name, std::string& value) const {
FormItem item;
if (getItem(name, item) && item.type == FormItem::Type::kField) {
value = item.value;
return true;
}
return false;
}
bool FormData::getFile(const std::string& name, std::string& filename, std::string& content) const {
FormItem item;
if (getItem(name, item) && item.type == FormItem::Type::kFile) {
filename = item.filename;
content = item.value;
return true;
}
return false;
}
bool FormData::contains(const std::string& name) const {
return name_index_.find(name) != name_index_.end();
}
void FormData::clear() {
items_.clear();
name_index_.clear();
}
}
}
}