-
Notifications
You must be signed in to change notification settings - Fork 1
/
file-type.cpp
97 lines (83 loc) · 2.1 KB
/
file-type.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
87
88
89
90
91
92
93
94
95
96
97
/*
* Copyright (c) 2013-2019 amded workers, All rights reserved.
* Terms for redistribution and use can be found in LICENCE.
*/
/**
* @file file-type.h
* @brief Transparent file-type id vs. label handling
*
* Internally, it's convenient to use intergers to identify different types of
* a thing. When interfacing with humans, strings are preferable for obvious
* reasons. Having to manually convert from one to another is bothersome. Just
* tracking both at the same time and keeping both at sync at the same time
* lifts that burden.
*/
#include <map>
#include <string>
#include "file-spec.h"
#include "file-type.h"
static std::map < enum file_type, std::string >
file_type_map = {
{ FILE_T_FLAC, "flac" },
{ FILE_T_OGG_VORBIS, "ogg-vorbis" },
{ FILE_T_MP3, "mp3" },
{ FILE_T_M4A, "m4a" },
{ FILE_T_OPUS, "opus" },
{ FILE_T_INVALID, "invalid" }
};
namespace Amded {
FileType::FileType()
{
label = "invalid";
id = FILE_T_INVALID;
}
FileType::~FileType() = default;
FileType::FileType(const std::string &l)
{
for (auto &iter : file_type_map) {
if (iter.second == l) {
label = l;
id = iter.first;
return;
}
}
id = FILE_T_INVALID;
label = "invalid";
}
FileType::FileType(enum file_type t)
{
label = file_type_map[t];
id = t;
}
FileType&
FileType::operator=(const std::string &l)
{
for (auto &iter : file_type_map) {
if (iter.second == l) {
label = l;
id = iter.first;
return *this;
}
}
id = FILE_T_INVALID;
label = "invalid";
return *this;
}
FileType&
FileType::operator=(enum file_type t)
{
label = file_type_map[t];
id = t;
return *this;
}
std::string
FileType::get_label(void) const
{
return label;
}
enum file_type
FileType::get_id(void) const
{
return id;
}
} /* namespace Amded */