pl_entry.cpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. #include "main.h"
  2. #include "pl_entry.h"
  3. #include "plstring.h"
  4. #include <wchar.h>
  5. #include "../nu/strsafe.h"
  6. #include <iostream>
  7. static const wchar_t *_INFO_NAME_MEDIA_HASH = L"mediahash";
  8. static const wchar_t *_INFO_NAME_META_HASH = L"metahash";
  9. static const wchar_t *_INFO_NAME_CLOUD_ID = L"cloud_id";
  10. static const wchar_t *_INFO_NAME_CLOUD_STATUS = L"cloud_status";
  11. static const wchar_t *_INFO_NAME_CLOUD_DEVICES = L"cloud_devices";
  12. pl_entry::pl_entry( const wchar_t *p_filename, const wchar_t *p_title, int p_length_ms )
  13. {
  14. SetFilename( p_filename );
  15. SetTitle( p_title );
  16. SetLengthMilliseconds( p_length_ms );
  17. }
  18. pl_entry::pl_entry( const wchar_t *p_filename, const wchar_t *p_title, int p_length_ms, int p_size )
  19. {
  20. SetFilename( p_filename );
  21. SetTitle( p_title );
  22. SetLengthMilliseconds( p_length_ms );
  23. SetSizeBytes( p_size );
  24. }
  25. pl_entry::pl_entry( const wchar_t *p_filename, const wchar_t *p_title, int p_length_ms, ifc_plentryinfo *p_info )
  26. {
  27. SetFilename( p_filename );
  28. SetTitle( p_title );
  29. SetLengthMilliseconds( p_length_ms );
  30. if ( p_info )
  31. {
  32. SetMediahash( p_info->GetExtendedInfo( _INFO_NAME_MEDIA_HASH ) );
  33. SetMetahash( p_info->GetExtendedInfo( _INFO_NAME_META_HASH ) );
  34. SetCloudID( p_info->GetExtendedInfo( _INFO_NAME_CLOUD_ID ) );
  35. SetCloudStatus( p_info->GetExtendedInfo( _INFO_NAME_CLOUD_STATUS ) );
  36. SetCloudDevices( p_info->GetExtendedInfo( _INFO_NAME_CLOUD_DEVICES ) );
  37. const wchar_t *l_tvg_name = p_info->GetExtendedInfo( L"tvg-name" );
  38. if ( l_tvg_name && wcslen( l_tvg_name ) > 0 )
  39. {
  40. _extended_infos.emplace( L"tvg-name", l_tvg_name );
  41. const wchar_t *l_tvg_id = p_info->GetExtendedInfo( L"tvg-id" );
  42. if ( l_tvg_id && *l_tvg_id )
  43. _extended_infos.emplace( L"tvg-id", l_tvg_id );
  44. const wchar_t *l_tvg_logo = p_info->GetExtendedInfo( L"tvg-logo" );
  45. if ( l_tvg_logo && *l_tvg_logo )
  46. _extended_infos.emplace( L"tvg-logo", l_tvg_logo );
  47. const wchar_t *l_tvg_title = p_info->GetExtendedInfo( L"tvg-title" );
  48. if ( l_tvg_title && *l_tvg_title )
  49. _extended_infos.emplace( L"group-title", l_tvg_title );
  50. }
  51. const wchar_t *l_ext = p_info->GetExtendedInfo( L"ext" );
  52. if ( l_ext && wcslen( l_ext ) )
  53. _extended_infos.emplace( L"ext", l_ext );
  54. }
  55. }
  56. pl_entry::pl_entry( const wchar_t *p_filename, const wchar_t *p_title, int p_length_ms, int p_size, ifc_plentryinfo *p_info )
  57. {
  58. SetFilename( p_filename );
  59. SetTitle( p_title );
  60. SetLengthMilliseconds( p_length_ms );
  61. SetSizeBytes( p_size );
  62. if ( p_info )
  63. {
  64. SetMediahash( p_info->GetExtendedInfo( _INFO_NAME_MEDIA_HASH ) );
  65. SetMetahash( p_info->GetExtendedInfo( _INFO_NAME_META_HASH ) );
  66. SetCloudID( p_info->GetExtendedInfo( _INFO_NAME_CLOUD_ID ) );
  67. SetCloudStatus( p_info->GetExtendedInfo( _INFO_NAME_CLOUD_STATUS ) );
  68. SetCloudDevices( p_info->GetExtendedInfo( _INFO_NAME_CLOUD_DEVICES ) );
  69. const wchar_t *l_tvg_name = p_info->GetExtendedInfo( L"tvg-name" );
  70. if ( l_tvg_name && wcslen( l_tvg_name ) > 0 )
  71. {
  72. _extended_infos.emplace( L"tvg-id", p_info->GetExtendedInfo( L"tvg-id" ) );
  73. _extended_infos.emplace( L"tvg-name", l_tvg_name );
  74. _extended_infos.emplace( L"tvg-logo", p_info->GetExtendedInfo( L"tvg-logo" ) );
  75. _extended_infos.emplace( L"group-title", p_info->GetExtendedInfo( L"group-title" ) );
  76. }
  77. }
  78. }
  79. pl_entry::pl_entry( const wchar_t *p_filename, const wchar_t *p_title, int p_length_ms,
  80. const wchar_t *mediahash, const wchar_t *metahash,
  81. const wchar_t *cloud_id, const wchar_t *cloud_status,
  82. const wchar_t *cloud_devices )
  83. {
  84. SetFilename( p_filename );
  85. SetTitle( p_title );
  86. SetLengthMilliseconds( p_length_ms );
  87. SetMediahash( mediahash );
  88. SetMetahash( metahash );
  89. SetCloudID( cloud_id );
  90. SetCloudStatus( cloud_status );
  91. SetCloudDevices( cloud_devices );
  92. }
  93. pl_entry::~pl_entry()
  94. {
  95. plstring_release( filename );
  96. plstring_release( filetitle );
  97. plstring_release( mediahash );
  98. plstring_release( metahash );
  99. plstring_release( cloud_id );
  100. plstring_release( cloud_status );
  101. plstring_release( cloud_devices );
  102. }
  103. size_t pl_entry::GetFilename( wchar_t *p_filename, size_t filenameCch )
  104. {
  105. if ( !this->filename )
  106. return 0;
  107. if ( !p_filename )
  108. return wcslen( this->filename );
  109. if ( !this->filename[ 0 ] )
  110. return 0;
  111. StringCchCopyW( p_filename, filenameCch, this->filename );
  112. return 1;
  113. }
  114. size_t pl_entry::GetTitle( wchar_t *title, size_t titleCch )
  115. {
  116. if ( !this->filetitle )
  117. return 0;
  118. if ( !title )
  119. return wcslen( this->filetitle );
  120. if ( !this->filetitle[ 0 ] )
  121. return 0;
  122. StringCchCopyW( title, titleCch, this->filetitle );
  123. return 1;
  124. }
  125. int pl_entry::GetLengthInMilliseconds()
  126. {
  127. return this->length;
  128. }
  129. int pl_entry::GetSizeInBytes()
  130. {
  131. return this->size;
  132. }
  133. size_t pl_entry::GetExtendedInfo( const wchar_t *metadata, wchar_t *info, size_t infoCch )
  134. {
  135. if ( cloud_id )
  136. {
  137. if ( !_wcsnicmp( _INFO_NAME_MEDIA_HASH, metadata, 9 ) && mediahash )
  138. {
  139. lstrcpynW( info, mediahash, (int)infoCch );
  140. return 1;
  141. }
  142. else if ( !_wcsnicmp( _INFO_NAME_META_HASH, metadata, 8 ) && metahash )
  143. {
  144. lstrcpynW( info, metahash, (int)infoCch );
  145. return 1;
  146. }
  147. else if ( !_wcsnicmp( _INFO_NAME_CLOUD_ID, metadata, 8 ) && cloud_id )
  148. {
  149. lstrcpynW( info, cloud_id, (int)infoCch );
  150. return 1;
  151. }
  152. else if ( !_wcsnicmp( _INFO_NAME_CLOUD_STATUS, metadata, 12 ) && cloud_status )
  153. {
  154. lstrcpynW( info, cloud_status, (int)infoCch );
  155. return 1;
  156. }
  157. else if ( !_wcsnicmp( _INFO_NAME_CLOUD_DEVICES, metadata, 13 ) && cloud_devices )
  158. {
  159. lstrcpynW( info, cloud_devices, (int)infoCch );
  160. return 1;
  161. }
  162. else if ( !_wcsnicmp( metadata, L"cloud", 5 ) )
  163. {
  164. if ( _wtoi( cloud_id ) > 0 )
  165. {
  166. StringCchPrintfW( info, infoCch, L"#EXT-X-NS-CLOUD:mediahash=%s,metahash=%s,cloud_id=%s,cloud_status=%s,cloud_devices=%s",
  167. ( mediahash && *mediahash ? mediahash : L"" ),
  168. ( metahash && *metahash ? metahash : L"" ), cloud_id,
  169. ( cloud_status && *cloud_status ? cloud_status : L"" ),
  170. ( cloud_devices && *cloud_devices ? cloud_devices : L"" ) );
  171. return 1;
  172. }
  173. }
  174. else
  175. {
  176. auto l_extended_infos_iterator = _extended_infos.find( metadata );
  177. if ( l_extended_infos_iterator != _extended_infos.end() )
  178. {
  179. lstrcpynW( info, ( *l_extended_infos_iterator ).second.c_str(), (int)infoCch);
  180. return 1;
  181. }
  182. }
  183. }
  184. if ( !this->_extended_infos.empty() )
  185. {
  186. for ( std::map<std::wstring, std::wstring>::iterator l_extented_infos_iterator = _extended_infos.begin(); l_extented_infos_iterator != _extended_infos.end(); l_extented_infos_iterator++ )
  187. {
  188. const std::wstring &l_parameter_name = ( *l_extented_infos_iterator ).first;
  189. if ( l_parameter_name.compare( metadata ) == 0 )
  190. {
  191. lstrcpynW( info, ( *l_extented_infos_iterator ).second.c_str(), (int)infoCch);
  192. return 1;
  193. }
  194. }
  195. }
  196. return 0;
  197. }
  198. void pl_entry::SetFilename( const wchar_t *p_filename )
  199. {
  200. plstring_release( this->filename );
  201. if ( p_filename && p_filename[ 0 ] )
  202. {
  203. this->filename = plstring_wcsdup( p_filename );
  204. if ( wcslen( p_filename ) > 4 )
  205. _is_local_file = wcsncmp( this->filename, L"http", 4 ) != 0;
  206. }
  207. else
  208. this->filename = 0;
  209. }
  210. void pl_entry::SetTitle( const wchar_t *title )
  211. {
  212. plstring_release( this->filetitle );
  213. if ( title && title[ 0 ] )
  214. {
  215. const wchar_t *t = L" \t\n\r\f\v";
  216. std::wstring l_title( title );
  217. l_title.erase( 0, l_title.find_first_not_of( t ) );
  218. this->filetitle = plstring_wcsdup( l_title.c_str() );
  219. this->cached = true;
  220. }
  221. else
  222. this->filetitle = 0;
  223. }
  224. void pl_entry::SetLengthMilliseconds( int length )
  225. {
  226. if ( length <= 0 )
  227. this->length = -1000;
  228. else
  229. this->length = length;
  230. }
  231. void pl_entry::SetMediahash( const wchar_t *mediahash )
  232. {
  233. plstring_release( this->mediahash );
  234. if ( mediahash && mediahash[ 0 ] )
  235. this->mediahash = plstring_wcsdup( mediahash );
  236. else
  237. this->mediahash = 0;
  238. }
  239. void pl_entry::SetSizeBytes( int size )
  240. {
  241. if ( size <= 0 )
  242. this->size = 0;
  243. else
  244. this->size = size;
  245. }
  246. void pl_entry::SetMetahash( const wchar_t *metahash )
  247. {
  248. plstring_release( this->metahash );
  249. if ( metahash && metahash[ 0 ] )
  250. this->metahash = plstring_wcsdup( metahash );
  251. else
  252. this->metahash = 0;
  253. }
  254. void pl_entry::SetCloudID( const wchar_t *cloud_id )
  255. {
  256. plstring_release( this->cloud_id );
  257. if ( cloud_id && cloud_id[ 0 ] && _wtoi( cloud_id ) > 0 )
  258. this->cloud_id = plstring_wcsdup( cloud_id );
  259. else
  260. this->cloud_id = 0;
  261. }
  262. void pl_entry::SetCloudStatus( const wchar_t *cloud_status )
  263. {
  264. plstring_release( this->cloud_status );
  265. if ( cloud_status && cloud_status[ 0 ] && _wtoi( cloud_status ) >= 0 )
  266. this->cloud_status = plstring_wcsdup( cloud_status );
  267. else
  268. this->cloud_status = 0;
  269. }
  270. void pl_entry::SetCloudDevices( const wchar_t *cloud_devices )
  271. {
  272. plstring_release( this->cloud_devices );
  273. if ( cloud_devices && cloud_devices[ 0 ] )
  274. this->cloud_devices = plstring_wcsdup( cloud_devices );
  275. else
  276. this->cloud_devices = 0;
  277. }