File: ObjectFormat.cpp

package info (click to toggle)
android-file-transfer 4.5-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,496 kB
  • sloc: cpp: 12,909; python: 140; lex: 47; xml: 26; sh: 13; makefile: 6
file content (216 lines) | stat: -rw-r--r-- 5,794 bytes parent folder | download | duplicates (2)
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
/*
    This file is part of Android File Transfer For Linux.
    Copyright (C) 2015-2020  Vladimir Menshakov

    This library is free software; you can redistribute it and/or modify it
    under the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation; either version 2.1 of the License,
    or (at your option) any later version.

    This library is distributed in the hope that it will be useful, but
    WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
    Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with this library; if not, write to the Free Software Foundation,
    Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/

#include <mtp/ptp/ObjectFormat.h>
#include <mtp/log.h>
#include <algorithm>
#include <ctype.h>
#include <map>

#ifdef HAVE_LIBMAGIC
#	include <magic.h>
#endif

namespace mtp
{
	namespace
	{
		std::string GetExtension(const std::string &filename)
		{
			size_t extPos = filename.rfind('.');
			std::string ext = (extPos != filename.npos)? filename.substr(extPos + 1): std::string();
			std::transform(ext.begin(), ext.end(), ext.begin(), tolower);
			return ext;
		}
	}

#ifdef HAVE_LIBMAGIC
	namespace
	{
		class Magic
		{
			magic_t								_magic;
			std::map<std::string, ObjectFormat>	_types;

		public:
			Magic(): _magic(magic_open(MAGIC_MIME_TYPE | MAGIC_SYMLINK | MAGIC_ERROR))
			{
#define MAP_TYPE(name, format) _types[name] = (format)
				magic_load(_magic, NULL);
				MAP_TYPE("inode/directory",		ObjectFormat::Association);
				MAP_TYPE("audio/mpeg",			ObjectFormat::Mp3);
				MAP_TYPE("text/plain",			ObjectFormat::Text);
				MAP_TYPE("image/jpeg",			ObjectFormat::ExifJpeg);
				MAP_TYPE("image/gif",			ObjectFormat::Gif);
				MAP_TYPE("image/x-ms-bmp",		ObjectFormat::Bmp);
				MAP_TYPE("image/png",			ObjectFormat::Png);
				MAP_TYPE("audio/x-ms-wma",		ObjectFormat::Wma);
				MAP_TYPE("audio/ogg",			ObjectFormat::Ogg);
				MAP_TYPE("audio/x-flac",		ObjectFormat::Flac);
				MAP_TYPE("audio/x-m4a",			ObjectFormat::Aac);
				MAP_TYPE("audio/audio/x-wav",	ObjectFormat::Aiff);
				MAP_TYPE("audio/mp4",			ObjectFormat::Mp4);
				MAP_TYPE("application/x-mpegurl", ObjectFormat::M3uPlaylist);
#undef MAP_TYPE
			}

			ObjectFormat GetType(const std::string &path)
			{
				const char *type = _magic? magic_file(_magic, path.c_str()): NULL;
				if (!type)
					return ObjectFormat::Undefined;

				//debug("MAGIC MIME: ", type);
				auto it = _types.find(type);
				return it != _types.end()? it->second: ObjectFormat::Undefined;
			}

			~Magic()
			{ if (_magic) magic_close(_magic); }
		};
	}
#else
	namespace
	{
		struct Magic
		{
			const ObjectFormat GetType(const std::string &) { return ObjectFormat::Undefined; }
		};
	}
#endif

	ObjectFormat ObjectFormatFromFilename(const std::string &filename)
	{
		//libmagic missing mime type for m3u files
		auto ext = GetExtension(filename);
		if (ext == "m3u")
			return mtp::ObjectFormat::M3uPlaylist;

		static Magic magic;
		{
			ObjectFormat magicType = magic.GetType(filename);
			if (magicType != ObjectFormat::Undefined)
				return magicType;
		}

		if (ext == "mp3")
			return mtp::ObjectFormat::Mp3;
		else if (ext == "txt")
			return mtp::ObjectFormat::Text;
		else if (ext == "jpeg" || ext == "jpg")
			return mtp::ObjectFormat::ExifJpeg;
		else if (ext == "gif")
			return mtp::ObjectFormat::Gif;
		else if (ext == "bmp")
			return mtp::ObjectFormat::Bmp;
		else if (ext == "png")
			return mtp::ObjectFormat::Png;
		else if (ext == "wma")
			return mtp::ObjectFormat::Wma;
		else if (ext == "ogg")
			return mtp::ObjectFormat::Ogg;
		else if (ext == "flac")
			return mtp::ObjectFormat::Flac;
		else if (ext == "aac")
			return mtp::ObjectFormat::Aac;
		else if (ext == "wav")
			return mtp::ObjectFormat::Aiff;
		else if (ext == "wmv")
			return mtp::ObjectFormat::Wmv;
		else if (ext == "mp4")
			return mtp::ObjectFormat::Mp4;
		else if (ext == "m4a")
			return mtp::ObjectFormat::M4a;
		else if (ext == "3gp")
			return mtp::ObjectFormat::_3gp;
		else if (ext == "asf")
			return mtp::ObjectFormat::Asf;
		else
			return ObjectFormat::Undefined;
	}

	bool IsAudioFormat(ObjectFormat format)
	{
		switch(format)
		{
			case ObjectFormat::Aiff:
			case ObjectFormat::Wav:
			case ObjectFormat::Mp3:
			case ObjectFormat::M4a:
			case ObjectFormat::UndefinedAudio:
			case ObjectFormat::Wma:
			case ObjectFormat::Ogg:
			case ObjectFormat::Aac:
			case ObjectFormat::Audible:
			case ObjectFormat::Flac:
				return true;
			default:
				return false;
		}
	}

	bool IsImageFormat(ObjectFormat format)
	{
		u16 type = static_cast<u16>(format) >> 8;
		return type == 0x38;
	}


	time_t ConvertDateTime(const std::string &timespec)
	{
		struct tm time = {};
		time.tm_isdst = -1;
		char *end = strptime(timespec.c_str(), "%Y%m%dT%H%M%S", &time);
		if (!end)
			return 0;
		return mktime(&time);
	}

	std::string ConvertDateTime(time_t time)
	{
		struct tm bdt = {};
		if (!gmtime_r(&time, &bdt))
			throw std::runtime_error("gmtime_r failed");
		char buf[64];
		size_t r = strftime(buf, sizeof(buf), "%Y%m%dT%H%M%SZ", &bdt);
		return std::string(buf, r);
	}

	std::string ConvertYear(int year)
	{
		struct tm bdt = {};
		bdt.tm_mday = 1;
		bdt.tm_year = year - 1900;
		auto ts = mktime(&bdt);
		if (ts == (time_t) -1)
			throw std::runtime_error("mktime failed");
		return ConvertDateTime(ts);
	}

	std::string ToString(ObjectFormat property)
	{
		switch(property)
		{
#			define ENUM_VALUE(NAME, VALUE) ENUM_VALUE_TO_STRING(ObjectFormat, NAME, VALUE)
#			include <mtp/ptp/ObjectFormat.values.h>
			ENUM_VALUE_TO_STRING_DEFAULT(ObjectFormat, property, 4);
		}
	}

}