CPPMyth
Library to interoperate with MythTV server
mythtypes.cpp
1 /*
2  * Copyright (C) 2014 Jean-Luc Barriere
3  *
4  * This Program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2, or (at your option)
7  * any later version.
8  *
9  * This Program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; see the file COPYING. If not, write to
16  * the Free Software Foundation, 51 Franklin Street, Fifth Floor, Boston,
17  * MA 02110-1301 USA
18  * http://www.gnu.org/copyleft/gpl.html
19  *
20  */
21 
22 #include "mythtypes.h"
23 #include "private/builtin.h"
24 
25 using namespace Myth;
26 
31 
32 uint32_t Myth::StringToId(const std::string& str)
33 {
34  uint32_t id = 0;
35  string_to_uint32(str.c_str(), &id);
36  return id;
37 }
38 
39 std::string Myth::IdToString(uint32_t id)
40 {
41  char buf[32];
42  *buf = '\0';
43  uint32_to_string(id, buf);
44  return std::string(buf);
45 }
46 
47 time_t Myth::StringToTime(const std::string& isotime)
48 {
49  time_t time = INVALID_TIME;
50  string_to_time(isotime.c_str(), &time);
51  return time;
52 }
53 
54 std::string Myth::TimeToString(time_t time, bool utc)
55 {
56  if (utc)
57  {
58  char buf[TIMESTAMP_UTC_LEN + 1];
59  *buf = '\0';
60  time_to_iso8601utc(time, buf);
61  return std::string(buf);
62  }
63  else
64  {
65  char buf[TIMESTAMP_LEN + 1];
66  *buf = '\0';
67  time_to_iso8601(time, buf);
68  return std::string(buf);
69  }
70 }
71 
72 int Myth::StringToInt(const std::string& str)
73 {
74  int32_t i = 0;
75  string_to_int32(str.c_str(), &i);
76  return (int)i;
77 }
78 
79 std::string Myth::IntToString(int i)
80 {
81  char buf[32];
82  *buf = '\0';
83  int32_to_string(i, buf);
84  return std::string(buf);
85 }
86 
91 
92 typedef struct
93 {
94  unsigned protoVer;
95  int tVal;
96  int iVal;
97  const char *sVal;
98 } protoref_t;
99 
100 static int __tValFromString(protoref_t *map, unsigned sz, unsigned proto, const std::string& sVal, int unk)
101 {
102  for (unsigned i = 0; i < sz; ++i)
103  {
104  if (proto >= map[i].protoVer && sVal.compare(map[i].sVal) == 0)
105  return map[i].tVal;
106  }
107  return unk;
108 }
109 
110 static int __tValFromNum(protoref_t *map, unsigned sz, unsigned proto, int iVal, int unk)
111 {
112  for (unsigned i = 0; i < sz; ++i)
113  {
114  if (proto >= map[i].protoVer && iVal == map[i].iVal)
115  return map[i].tVal;
116  }
117  return unk;
118 }
119 
120 static const char *__tValToString(protoref_t *map, unsigned sz, unsigned proto, int tVal, const char *unk)
121 {
122  for (unsigned i = 0; i < sz; ++i)
123  {
124  if (proto >= map[i].protoVer && tVal == map[i].tVal)
125  return map[i].sVal;
126  }
127  return unk;
128 }
129 
130 static int __tValToNum(protoref_t *map, unsigned sz, unsigned proto, int tVal, int unk)
131 {
132  for (unsigned i = 0; i < sz; ++i)
133  {
134  if (proto >= map[i].protoVer && tVal == map[i].tVal)
135  return map[i].iVal;
136  }
137  return unk;
138 }
139 
144 
145 static protoref_t ruleType[] =
146 {
147  { 79, RT_TemplateRecord, 11, "Recording Template" },
148  { 79, RT_NotRecording, 0, "Not Recording" },
149  { 76, RT_OneRecord, 6, "Record One" },
150  { 75, RT_SingleRecord, 1, "Single Record" },
151  { 75, RT_DailyRecord, 2, "Record Daily" },
152  { 75, RT_ChannelRecord, 3, "Channel Record" },
153  { 75, RT_AllRecord, 4, "Record All" },
154  { 75, RT_WeeklyRecord, 5, "Record Weekly" },
155  { 75, RT_OneRecord, 6, "Find One" },
156  { 75, RT_OverrideRecord, 7, "Override Recording" },
157  { 75, RT_DontRecord, 8, "Do not Record" },
158  { 75, RT_FindDailyRecord, 9, "Find Daily" },
159  { 75, RT_FindWeeklyRecord, 10, "Find Weekly" },
160  { 75, RT_TemplateRecord, 11, "Not Recording" },
161  { 75, RT_NotRecording, 0, "Not Recording" },
162 };
163 
164 RT_t Myth::RuleTypeFromString(unsigned proto, const std::string& type)
165 {
166  static unsigned sz = sizeof(ruleType) / sizeof(protoref_t);
167  return (RT_t)__tValFromString(ruleType, sz, proto, type, (int)RT_UNKNOWN);
168 }
169 
170 RT_t Myth::RuleTypeFromNum(unsigned proto, int type)
171 {
172  static unsigned sz = sizeof(ruleType) / sizeof(protoref_t);
173  return (RT_t)__tValFromNum(ruleType, sz, proto, type, (int)RT_UNKNOWN);
174 }
175 
176 const char *Myth::RuleTypeToString(unsigned proto, RT_t type)
177 {
178  static unsigned sz = sizeof(ruleType) / sizeof(protoref_t);
179  return __tValToString(ruleType, sz, proto, (int)type, "");
180 }
181 
182 int Myth::RuleTypeToNum(unsigned proto, RT_t type)
183 {
184  static unsigned sz = sizeof(ruleType) / sizeof(protoref_t);
185  return __tValToNum(ruleType, sz, proto, (int)type, 0);
186 }
187 
192 
193 static protoref_t dupIn[] =
194 {
195  { 75, DI_InRecorded, 0x01, "Current Recordings" },
196  { 75, DI_InOldRecorded, 0x02, "Previous Recordings" },
197  { 75, DI_InAll, 0x0F, "All Recordings" },
198  { 75, DI_NewEpi, 0x10, "New Episodes Only" },
199 };
200 
201 DI_t Myth::DupInFromString(unsigned proto, const std::string& type)
202 {
203  static unsigned sz = sizeof(dupIn) / sizeof(protoref_t);
204  return (DI_t)__tValFromString(dupIn, sz, proto, type, (int)DI_UNKNOWN);
205 }
206 
207 DI_t Myth::DupInFromNum(unsigned proto, int type)
208 {
209  static unsigned sz = sizeof(dupIn) / sizeof(protoref_t);
210  return (DI_t)__tValFromNum(dupIn, sz, proto, type, (int)DI_UNKNOWN);
211 }
212 
213 const char *Myth::DupInToString(unsigned proto, DI_t type)
214 {
215  static unsigned sz = sizeof(dupIn) / sizeof(protoref_t);
216  return __tValToString(dupIn, sz, proto, (int)type, "");
217 }
218 
219 int Myth::DupInToNum(unsigned proto, DI_t type)
220 {
221  static unsigned sz = sizeof(dupIn) / sizeof(protoref_t);
222  return __tValToNum(dupIn, sz, proto, (int)type, 0);
223 }
224 
229 
230 static protoref_t dupMethod[] =
231 {
232  { 75, DM_CheckNone, 0x01, "None" },
233  { 75, DM_CheckSubtitle, 0x02, "Subtitle" },
234  { 75, DM_CheckDescription, 0x04, "Description" },
235  { 75, DM_CheckSubtitleAndDescription, 0x06, "Subtitle and Description" },
236  { 75, DM_CheckSubtitleThenDescription, 0x08, "Subtitle then Description" },
237 };
238 
239 DM_t Myth::DupMethodFromString(unsigned proto, const std::string& type)
240 {
241  static unsigned sz = sizeof(dupMethod) / sizeof(protoref_t);
242  return (DM_t)__tValFromString(dupMethod, sz, proto, type, (int)DM_UNKNOWN);
243 }
244 
245 DM_t Myth::DupMethodFromNum(unsigned proto, int type)
246 {
247  static unsigned sz = sizeof(dupMethod) / sizeof(protoref_t);
248  return (DM_t)__tValFromNum(dupMethod, sz, proto, type, (int)DM_UNKNOWN);
249 }
250 
251 const char *Myth::DupMethodToString(unsigned proto, DM_t type)
252 {
253  static unsigned sz = sizeof(dupMethod) / sizeof(protoref_t);
254  return __tValToString(dupMethod, sz, proto, (int)type, "");
255 }
256 
257 int Myth::DupMethodToNum(unsigned proto, DM_t type)
258 {
259  static unsigned sz = sizeof(dupMethod) / sizeof(protoref_t);
260  return __tValToNum(dupMethod, sz, proto, (int)type, 0);
261 }
262 
267 
268 static protoref_t searchType[] =
269 {
270  { 75, ST_NoSearch, 0, "None" },
271  { 75, ST_PowerSearch, 1, "Power Search" },
272  { 75, ST_TitleSearch, 2, "Title Search" },
273  { 75, ST_KeywordSearch, 3, "Keyword Search" },
274  { 75, ST_PeopleSearch, 4, "People Search" },
275  { 75, ST_ManualSearch, 5, "Manual Search" },
276 };
277 
278 ST_t Myth::SearchTypeFromString(unsigned proto, const std::string& type)
279 {
280  static unsigned sz = sizeof(searchType) / sizeof(protoref_t);
281  return (ST_t)__tValFromString(searchType, sz, proto, type, (int)ST_UNKNOWN);
282 }
283 
284 ST_t Myth::SearchTypeFromNum(unsigned proto, int type)
285 {
286  static unsigned sz = sizeof(searchType) / sizeof(protoref_t);
287  return (ST_t)__tValFromNum(searchType, sz, proto, type, (int)ST_UNKNOWN);
288 }
289 
290 const char *Myth::SearchTypeToString(unsigned proto, ST_t type)
291 {
292  static unsigned sz = sizeof(searchType) / sizeof(protoref_t);
293  return __tValToString(searchType, sz, proto, (int)type, "");
294 }
295 
296 int Myth::SearchTypeToNum(unsigned proto, ST_t type)
297 {
298  static unsigned sz = sizeof(searchType) / sizeof(protoref_t);
299  return __tValToNum(searchType, sz, proto, (int)type, 0);
300 }
301 
306 
307 static protoref_t categoryType[] =
308 {
309  { 79, CATT_CategoryNone, 0, "" },
310  { 79, CATT_CategoryMovie, 1, "movie" },
311  { 79, CATT_CategorySeries, 2, "series" },
312  { 79, CATT_CategorySports, 3, "sports" },
313  { 79, CATT_CategoryTVShow, 4, "tvshow" },
314 };
315 
316 CATT_t Myth::CategoryTypeFromString(unsigned proto, const std::string& type)
317 {
318  static unsigned sz = sizeof(categoryType) / sizeof(protoref_t);
319  if (type.empty())
320  return CATT_CategoryNone;
321  return (CATT_t)__tValFromString(categoryType, sz, proto, type, (int)CATT_UNKNOWN);
322 }
323 
324 CATT_t Myth::CategoryTypeFromNum(unsigned proto, int type)
325 {
326  static unsigned sz = sizeof(categoryType) / sizeof(protoref_t);
327  return (CATT_t)__tValFromNum(categoryType, sz, proto, type, (int)CATT_UNKNOWN);
328 }
329 
330 const char *Myth::CategoryTypeToString(unsigned proto, CATT_t type)
331 {
332  static unsigned sz = sizeof(categoryType) / sizeof(protoref_t);
333  return __tValToString(categoryType, sz, proto, (int)type, "");
334 }
335 
336 int Myth::CategoryTypeToNum(unsigned proto, CATT_t type)
337 {
338  static unsigned sz = sizeof(categoryType) / sizeof(protoref_t);
339  return __tValToNum(categoryType, sz, proto, (int)type, 0);
340 }
341 
346 
347 static protoref_t recStatus[] =
348 {
349  { 75, RS_TUNING, -10, "Tuning" },
350  { 75, RS_FAILED, -9, "Failed" },
351  { 75, RS_TUNER_BUSY, -8, "Tuner busy" },
352  { 75, RS_LOW_DISKSPACE, -7, "Low disk space" },
353  { 75, RS_CANCELLED, -6, "Cancelled" },
354  { 75, RS_MISSED, -5, "Missed" },
355  { 75, RS_ABORTED, -4, "Aborted" },
356  { 75, RS_RECORDED, -3, "Recorded" },
357  { 75, RS_RECORDING, -2, "Recording" },
358  { 75, RS_WILL_RECORD, -1, "Will record" },
359  { 75, RS_UNKNOWN, 0, "Unknown" },
360  { 75, RS_DONT_RECORD, 1, "Don't record" },
361  { 75, RS_PREVIOUS_RECORDING, 2, "Previous recording" },
362  { 75, RS_CURRENT_RECORDING, 3, "Current recording" },
363  { 75, RS_EARLIER_RECORDING, 4, "Earlier recording" },
364  { 75, RS_TOO_MANY_RECORDINGS, 5, "Too many recordings" },
365  { 75, RS_NOT_LISTED, 6, "Not listed" },
366  { 75, RS_CONFLICT, 7, "Conflict" },
367  { 75, RS_LATER_SHOWING, 8, "Later showing" },
368  { 75, RS_REPEAT, 9, "Repeat" },
369  { 75, RS_INACTIVE, 10, "Inactive" },
370  { 75, RS_NEVER_RECORD, 11, "Never record" },
371  { 75, RS_OFFLINE, 12, "Offline" },
372  { 75, RS_OTHER_SHOWING, 13, "Other showing" },
373 };
374 
375 RS_t Myth::RecStatusFromString(unsigned proto, const std::string& type)
376 {
377  static unsigned sz = sizeof(recStatus) / sizeof(protoref_t);
378  return (RS_t)__tValFromString(recStatus, sz, proto, type, (int)RT_UNKNOWN);
379 }
380 
381 RS_t Myth::RecStatusFromNum(unsigned proto, int type)
382 {
383  static unsigned sz = sizeof(recStatus) / sizeof(protoref_t);
384  return (RS_t)__tValFromNum(recStatus, sz, proto, type, (int)RT_UNKNOWN);
385 }
386 
387 const char *Myth::RecStatusToString(unsigned proto, RS_t type)
388 {
389  static unsigned sz = sizeof(recStatus) / sizeof(protoref_t);
390  return __tValToString(recStatus, sz, proto, (int)type, "");
391 }
392 
393 int Myth::RecStatusToNum(unsigned proto, RS_t type)
394 {
395  static unsigned sz = sizeof(recStatus) / sizeof(protoref_t);
396  return __tValToNum(recStatus, sz, proto, (int)type, 0);
397 }
This is the main namespace that encloses all public classes.
Definition: mythcontrol.h:29