Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
M
mpd
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Registry
Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Иван Мажукин
mpd
Commits
287ef181
Commit
287ef181
authored
Jun 10, 2016
by
Max Kellermann
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
util/MimeType: add ParseMimeTypeParameters()
parent
bc63810e
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
53 additions
and
0 deletions
+53
-0
MimeType.cxx
src/util/MimeType.cxx
+22
-0
MimeType.hxx
src/util/MimeType.hxx
+10
-0
MimeTypeTest.hxx
test/MimeTypeTest.hxx
+21
-0
No files found.
src/util/MimeType.cxx
View file @
287ef181
...
@@ -18,6 +18,7 @@
...
@@ -18,6 +18,7 @@
*/
*/
#include "MimeType.hxx"
#include "MimeType.hxx"
#include "SplitString.hxx"
#include <string.h>
#include <string.h>
...
@@ -29,3 +30,24 @@ GetMimeTypeBase(const char *s)
...
@@ -29,3 +30,24 @@ GetMimeTypeBase(const char *s)
?
std
::
string
(
s
,
semicolon
)
?
std
::
string
(
s
,
semicolon
)
:
std
::
string
(
s
);
:
std
::
string
(
s
);
}
}
std
::
map
<
std
::
string
,
std
::
string
>
ParseMimeTypeParameters
(
const
char
*
s
)
{
std
::
map
<
std
::
string
,
std
::
string
>
result
;
auto
l
=
SplitString
(
s
,
';'
,
true
);
if
(
!
l
.
empty
())
l
.
pop_front
();
for
(
const
auto
&
i
:
l
)
{
const
auto
eq
=
i
.
find
(
'='
);
if
(
eq
==
i
.
npos
)
continue
;
result
.
insert
(
std
::
make_pair
(
std
::
string
(
&
i
.
front
(),
eq
),
std
::
string
(
&
i
[
eq
+
1
])));
}
return
result
;
}
src/util/MimeType.hxx
View file @
287ef181
...
@@ -21,6 +21,7 @@
...
@@ -21,6 +21,7 @@
#define MPD_MIME_TYPE_HXX
#define MPD_MIME_TYPE_HXX
#include <string>
#include <string>
#include <map>
/**
/**
* Extract the part of the MIME type before the parameters, i.e. the
* Extract the part of the MIME type before the parameters, i.e. the
...
@@ -30,4 +31,13 @@
...
@@ -30,4 +31,13 @@
std
::
string
std
::
string
GetMimeTypeBase
(
const
char
*
s
);
GetMimeTypeBase
(
const
char
*
s
);
/**
* Parse the parameters from a MIME type string. Parameters are
* separated by semicolon. Example:
*
* "foo/bar; param1=value1; param2=value2"
*/
std
::
map
<
std
::
string
,
std
::
string
>
ParseMimeTypeParameters
(
const
char
*
s
);
#endif
#endif
test/MimeTypeTest.hxx
View file @
287ef181
...
@@ -13,6 +13,7 @@
...
@@ -13,6 +13,7 @@
class
MimeTypeTest
:
public
CppUnit
::
TestFixture
{
class
MimeTypeTest
:
public
CppUnit
::
TestFixture
{
CPPUNIT_TEST_SUITE
(
MimeTypeTest
);
CPPUNIT_TEST_SUITE
(
MimeTypeTest
);
CPPUNIT_TEST
(
TestBase
);
CPPUNIT_TEST
(
TestBase
);
CPPUNIT_TEST
(
TestParameters
);
CPPUNIT_TEST_SUITE_END
();
CPPUNIT_TEST_SUITE_END
();
public
:
public
:
...
@@ -25,4 +26,24 @@ public:
...
@@ -25,4 +26,24 @@ public:
CPPUNIT_ASSERT
(
"foo/bar"
==
GetMimeTypeBase
(
"foo/bar; x=y"
));
CPPUNIT_ASSERT
(
"foo/bar"
==
GetMimeTypeBase
(
"foo/bar; x=y"
));
CPPUNIT_ASSERT
(
"foo/bar"
==
GetMimeTypeBase
(
"foo/bar;x=y"
));
CPPUNIT_ASSERT
(
"foo/bar"
==
GetMimeTypeBase
(
"foo/bar;x=y"
));
}
}
void
TestParameters
()
{
CPPUNIT_ASSERT
(
ParseMimeTypeParameters
(
""
).
empty
());
CPPUNIT_ASSERT
(
ParseMimeTypeParameters
(
"foo/bar"
).
empty
());
CPPUNIT_ASSERT
(
ParseMimeTypeParameters
(
"foo/bar;"
).
empty
());
CPPUNIT_ASSERT
(
ParseMimeTypeParameters
(
"foo/bar;garbage"
).
empty
());
CPPUNIT_ASSERT
(
ParseMimeTypeParameters
(
"foo/bar; garbage"
).
empty
());
auto
p
=
ParseMimeTypeParameters
(
"foo/bar;a=b"
);
CPPUNIT_ASSERT
(
!
p
.
empty
());
CPPUNIT_ASSERT
(
p
[
"a"
]
==
"b"
);
CPPUNIT_ASSERT
(
p
.
size
()
==
1
);
p
=
ParseMimeTypeParameters
(
"foo/bar; a=b;c;d=e ; f=g "
);
CPPUNIT_ASSERT
(
!
p
.
empty
());
CPPUNIT_ASSERT
(
p
[
"a"
]
==
"b"
);
CPPUNIT_ASSERT
(
p
[
"d"
]
==
"e"
);
CPPUNIT_ASSERT
(
p
[
"f"
]
==
"g"
);
CPPUNIT_ASSERT
(
p
.
size
()
==
3
);
}
};
};
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment