-
Notifications
You must be signed in to change notification settings - Fork 2
/
PortingFunctions.c
1300 lines (1130 loc) · 31.1 KB
/
PortingFunctions.c
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*****************************************************************************
* Copyright (C) 2012- Brad Love : b-rad at next dimension dot cc
* Next Dimension Innovations : http://nextdimension.cc
* http://b-rad.cc
* Copyright 2006 - 2011 Intel Corporation
*
* This file is part of TV-Now
* TV-Now is an Open Source DLNA Media Server. TV-Now's purpose is
* to serve Live TV (and recorded content) over the local network
* to televisions, computers, media players, tablets, and consoles.
* TV-Now delivers EPG data in the DLNA container for compatible
* clients and also offers an html5+jquery tv player with full EPG.
* TV-Now uses the libdvbtee library as its backend.
*
* TV-Now is compatible with:
* - ATSC
* - Clear QAM
* - DVB-T
*
* TV-Now is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Note: All additional terms from Section 7 of GPLv3 apply to this software.
* This includes requiring preservation of specified reasonable legal
* notices or author attributions in the material or in the Appropriate
* Legal Notices displayed by this software.
*
* You should have received a copy of the GNU General Public License v3
* along with TV-Now. If not, see <http://www.gnu.org/licenses/>.
*
* An Apache 2.0 licensed version of this software is privately maintained
* for licensing to interested commercial parties. Apache 2.0 license is
* compatible with the GPLv3, which allows the Apache 2.0 version to be
* included in proprietary systems, while keeping the public GPLv3 version
* completely open source. GPLv3 can NOT be re-licensed as Apache 2.0, since
* Apache 2.0 license is only a subset of GPLv3. To inquire about licensing
* the commercial version of TV-Now contact: tv-now at nextdimension dot cc
*
* Note about contributions and patch submissions:
* The commercial Apache 2.0 version of TV-Now is used as the master.
* The GPLv3 version of TV-Now will be identical to the Apache 2.0 version.
* All contributions and patches are licensed under Apache 2.0
* By submitting a patch you implicitly agree to
* http://www.apache.org/licenses/icla.txt
* You retain ownership and when merged the license will be upgraded to GPLv3.
*
*****************************************************************************/
#define LIVETV_FILESIZE 9384503541759 / 10
#define _CRTDBG_MAP_ALLOC
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "MyString.h"
#include "MicroMediaServer.h"
#include "CdsMediaObject.h"
#include "MimeTypes.h"
#include "PortingFunctions.h"
#include "dvbteeserver.h"
/* Windows 32 */
#ifdef WIN32
#include <windows.h>
#include <time.h>
#include <crtdbg.h>
#endif
/* Win CE */
#ifdef UNDER_CE
#include <Winbase.h>
#endif
/* POSIX */
#ifdef _POSIX
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <dirent.h>
#include <pthread.h>
#include <sys/time.h>
#include <unistd.h>
#endif
/* tv-now root directories */
#define TVNOW_ROOT_ITEMS 3
static int rootIter = 0;
static char rootDirs[TVNOW_ROOT_ITEMS][12] = { "Channels", "EPG", "VOD" };
#define MAX_PATH_LENGTH 1024
void PCRandomize()
{
#ifdef WIN32
srand((int)GetTickCount());
#endif
#ifdef UNDER_CE
srand((int)GetTickCount());
#endif
#ifdef _POSIX
struct timeval tv;
gettimeofday(&tv,NULL);
srand((int)tv.tv_sec);
#endif
}
// Windows Version
void PCCloseDir(void* handle)
{
// printf("%s\n", __func__);
#if NDi_LiveTV
destroy_iterator(handle);
return;
#endif
#ifdef WIN32
FindClose(*((HANDLE*)handle));
free(handle);
#endif
#ifdef UNDER_CE
FindClose(*((HANDLE*)handle));
free(handle);
#endif
#ifdef _POSIX
DIR* dirObj = (DIR*) handle;
closedir(dirObj);
#endif
}
int PCFileClose(void *fileHandle)
{
printf("%s\n", __func__);
/* TODO: Modify to enable unicode char support */
return fclose((FILE*)fileHandle);
}
void* PCFileOpen(const char* fullPath, const char *mode)
{
printf("%s\n", __func__);
/* TODO: Modify to enable unicode char support */
return (void*) fopen64(fullPath, mode);
}
int PCFileRead(void*dest, int itemSize, int itemCount, void *fileHandle)
{
printf("%s\n", __func__);
/* TODO: Modify to enable unicode char support */
return (int) fread(dest, itemSize, itemCount, (FILE*)fileHandle);
}
int PCFileSeek(void *fileHandle, uint64_t offset, int origin)
{
printf("%s\n", __func__);
/* TODO: Modify to enable unicode char support */
return fseek((FILE*)fileHandle, offset, origin);
}
uint64_t PCFileTell(void *fileHandle)
{
printf("%s\n", __func__);
/* TODO: Modify to enable unicode char support */
return ftello((FILE*)fileHandle);
}
static int isDate(const char* date_string)
{
const char *time_details = date_string;
char* endPtr;
struct tm tm;
// printf("%s( %s )\n", __func__, date_string);
endPtr = strptime(time_details, "%m-%d-%Y", &tm);
if (endPtr == NULL || *endPtr != '\0')
{
printf("%s( %s ) - Error\n", __func__, date_string);
return 0;
}
return 1;
}
/* Analyze cdsObj and allocate+fill in all desired metadata fields */
void GetMetaData(const char* path, struct CdsMediaObject *cdsObj)
{
int fdType;
char *title = GetFileName(path, "/", 1);
char *parentDir = NULL;
char *parentTitle = NULL;
char *parentDir2 = NULL;
char *parentTitle2 = NULL;
cdsObj->ProtocolInfo = PROTINFO_VIDEO_MPEG;
if (strncmp(path, "./Channels", 10) == 0)
{
fdType = PCGetFileDirType(path);
if (fdType == 2)
{
// cdsObj->MediaClass = CDS_MEDIACLASS_TUNERCONTAINER;
}
else if (fdType == 1)
{
/*
* <dc:title></dc:title>
* <upnp:channelNr>19</upnp:channelNr>
* <upnp:callSign></upnp:callSign>
* <upnp:networkAffiliation></upnp:networkAffiliation>
* <upnp:channelID>19654</upnp:channelID>
* <upnp:recordable>1</upnp:recordable>
*/
cdsObj->MediaClass = CDS_MEDIACLASS_VIDEOBROADCAST;
cdsObj->CallSign = malloc(64);
channel_name(title, cdsObj->CallSign);
cdsObj->ChannelID = malloc(128);
sprintf(cdsObj->ChannelID, "%s", title);
cdsObj->ChannelNr = (char*)malloc(16);
channel_number(title, cdsObj->ChannelNr);
cdsObj->DeallocateThese |= CDS_ALLOC_Title;
cdsObj->Title = (char*)malloc(strlen(cdsObj->ChannelNr) + strlen(cdsObj->CallSign) + 6);
sprintf(cdsObj->Title, "%s - %s", cdsObj->ChannelNr, cdsObj->CallSign);
cdsObj->ProgramID = NULL;
char* descr_ptr = NULL;
if (get_epg_data_simple(title, &cdsObj->ProgramID, &cdsObj->LongDescription, &descr_ptr, &cdsObj->ScheduledStartTime, &cdsObj->ScheduledDurationTime, &cdsObj->ScheduledEndTime) == 0)
{
free(descr_ptr);
}
cdsObj->Recordable = 1;
cdsObj->uri_target = (char*)malloc(255);
snprintf(cdsObj->uri_target, 255, ":%d/tune=%s&stream/video.mpg", 62080, cdsObj->ChannelID);
}
}
else if (strncmp(path, "./EPG", 5) == 0)
{
fdType = PCGetFileDirType(path);
parentDir = GetParentPath(path, "/", 1);
parentTitle = (parentDir != NULL) ? GetFileName(parentDir, "/", 1) : NULL;
parentDir2 = (parentDir != NULL) ? GetParentPath(parentDir, "/", 1) : NULL;
parentTitle2 = (parentDir2 != NULL) ? GetFileName(parentDir2, "/", 1) : NULL;
if (fdType == 2)
{
if (strcmp(path, "./EPG") == 0)
{
/*
* <upnp:epgProviderName></upnp:epgProviderName>
* <upnp:serviceProvider></upnp:serviceProvider>
*/
}
else if (parentDir != NULL && strcmp(parentDir, "./EPG/") == 0) /* /EPG/ch */
{
/*
* <dc:title>channel callsign</dc:title>
* <upnp:channelID>19654</upnp:channelID>
*/
cdsObj->ChannelID = malloc(128);
sprintf(cdsObj->ChannelID, "%s", title);
cdsObj->CallSign = malloc(64);
channel_name(title, cdsObj->CallSign);
cdsObj->ChannelNr = malloc(16);
channel_number(title, cdsObj->ChannelNr);
cdsObj->DeallocateThese |= CDS_ALLOC_Title;
cdsObj->Title = (char*)malloc(strlen(cdsObj->ChannelNr) +
strlen(cdsObj->CallSign) + 12);
sprintf(cdsObj->Title, "%s - %s", cdsObj->ChannelNr, cdsObj->CallSign);
}
else if (parentDir2 != NULL && strcmp(parentDir2,"./EPG/") == 0) /* /EPG/ch/day */
{
/*
* <dc:title>channel # callsign : date</dc:title>
* <upnp:channelID>19654</upnp:channelID>
* <upnp:dateTimeRange>2015-11-12T00:00:00Z/2015-11-13T00:00:00Z</upnp:dateTimeRange>
*/
cdsObj->ChannelID = malloc(128);
sprintf(cdsObj->ChannelID, "%s", parentTitle);
cdsObj->CallSign = malloc(64);
channel_name(parentTitle, cdsObj->CallSign);
cdsObj->ChannelNr = malloc(16);
channel_number(parentTitle, cdsObj->ChannelNr);
cdsObj->DeallocateThese |= CDS_ALLOC_Title;
cdsObj->Title = (char*)malloc(strlen(cdsObj->ChannelNr) +
strlen(cdsObj->CallSign) +
strlen(title) + 12);
sprintf(cdsObj->Title, "%s - %s : %s", cdsObj->ChannelNr, cdsObj->CallSign, title);
struct tm tm = { 0 };
strptime(title, "%m-%d-%Y", &tm);
tm.tm_hour = 0;
tm.tm_min = 0;
tm.tm_sec = 0;
time_t t_time = mktime(&tm);
cdsObj->DateTimeRange.start = t_time;
cdsObj->DateTimeRange.duration = (24 * 60 * 60);
}
cdsObj->MediaClass = CDS_MEDIACLASS_EPGCONTAINER;
}
else
{
/*
* id="EPG/Airings/15242959"
*
* <dc:title></dc:title>
* <upnp:channelID>19654</upnp:channelID>
* <upnp:scheduledStartTime usage="SCHEDULED_PROGRAM">2015-11-12T00:00:00Z</upnp:scheduledStartTime>
* <upnp:scheduledEndTime>2015-11-12T00:30:00Z</upnp:scheduledEndTime>
* <upnp:scheduledDurationTime>P0:30:00</upnp:scheduledDurationTime>
* <upnp:longDescription></upnp:longDescription>
* <upnp:programID type="xxx.COM"></upnp:programID>
* <upnp:actor></upnp:actor>
* <upnp:episodeNumber></upnp:episodeNumber>
* <upnp:episodeSeason></upnp:episodeSeason>
* <upnp:episodeType>FIRST-RUN</upnp:episodeType>
* <upnp:seriesID></upnp:seriesID>
* <dc:date>2015-11-11T18:00:00Z</dc:date>
* <upnp:genre extended="Talk,Interview">Talk</upnp:genre>
* <dc:language>English</dc:language>
* <upnp:rating type="TVGUIDELINES.ORG">TVG</upnp:rating>
*/
cdsObj->MediaClass = CDS_MEDIACLASS_EPG_VIDEO;
cdsObj->ChannelID = malloc(128);
sprintf(cdsObj->ChannelID, "%s", parentTitle2);
char* old_title = cdsObj->Title;
cdsObj->Title = NULL;
/* description missing */
if (get_epg_data_simple(parentTitle2, &title, &cdsObj->Title, &cdsObj->LongDescription, &cdsObj->ScheduledStartTime, &cdsObj->ScheduledDurationTime, &cdsObj->ScheduledEndTime) != 0)
{
cdsObj->Title = old_title;
}
else
{
cdsObj->DeallocateThese |= CDS_ALLOC_Title;
}
cdsObj->uri_target = (char*)malloc(255);
snprintf(cdsObj->uri_target, 255, ":%d/tune=%s&stream/video.mpg", 62080, cdsObj->ChannelID);
}
}
if (title != NULL) free(title);
if (parentDir != NULL) free(parentDir);
if (parentTitle != NULL) free(parentTitle);
if (parentDir2 != NULL) free(parentDir2);
if (parentTitle2 != NULL) free(parentTitle2);
}
void* PCGetDirFirstFile(const char* directory, char* filename, int filenamelength, uint64_t* filesize)
{
// printf("%s(%s, filename (out), %d, %" PRIu64 ")\n", __func__, directory, filenamelength, filesize != NULL ? *filesize : 0);
#ifdef WIN32
WIN32_FIND_DATA FileData;
HANDLE* hSearch;
char* direx;
hSearch = malloc(sizeof(HANDLE));
direx = malloc(filenamelength + 5);
if (directory[(int) strlen(directory) - 1] == '\\')
{
sprintf(direx,"%s*.*",directory);
}
else
{
sprintf(direx,"%s\\*.*",directory);
}
*hSearch = FindFirstFile(direx, &FileData);
free(direx);
if (*hSearch == INVALID_HANDLE_VALUE)
{
free(hSearch);
hSearch = NULL;
}
else
{
if (filename != NULL)
{
strToUtf8(filename,FileData.cFileName, filenamelength, 0, NULL);
}
if (filesize != NULL)
{
*filesize = FileData.nFileSizeLow;
}
}
return hSearch;
#endif
#ifdef UNDER_CE
WIN32_FIND_DATA FileData;
HANDLE* hSearch; /* must MMS_FREE */
wchar_t* wdirectory; /* must MMS_FREE */
int wDirLen;
int wDirSize;
int mbDirLen;
int mbDirSize;
char* direx; /* must MMS_FREE */
hSearch = malloc(sizeof(HANDLE));
direx = malloc(filenamelength + 5);
if (directory[(int) strlen(directory) - 1] == '\\')
{
sprintf(direx,"%s*.*",directory);
}
else
{
sprintf(direx,"%s\\*.*",directory);
}
mbDirLen = (int) strlen(direx);
mbDirSize = mbDirLen+1;
wDirLen = mbDirLen * 2;
wDirSize = mbDirSize * 2;
wdirectory = (wchar_t*)malloc(wDirSize);
if (mbstowcs(wdirectory,direx,wDirSize) == -1)
{
free(hSearch);
hSearch = NULL;
}
else
{
*hSearch = FindFirstFile(wdirectory, &FileData);
if (*hSearch == INVALID_HANDLE_VALUE)
{
free(hSearch);
hSearch = NULL;
}
else
{
if (filename != NULL)
{
if (strToUtf8(filename,(char*)FileData.cFileName,filenamelength, 1, NULL) == -1)
{
FindClose(*hSearch);
free(hSearch);
hSearch = NULL;
}
else
{
if (filesize != NULL)
{
*filesize = FileData.nFileSizeLow;
}
}
}
}
}
free(direx);
free(wdirectory);
return hSearch;
#endif
#ifdef _POSIX
DIR* dirObj;
struct dirent* dirEntry; /* dirEntry is a pointer to static memory in the C runtime lib for readdir()*/
struct stat64 _si;
char fullPath[1024];
char *title = NULL;
char *parentDir = NULL;
char *parentTitle = NULL;
char *parentDir2 = NULL;
void* retval = NULL;
#if NDi_LiveTV
if (strcmp(directory, "./") == 0)
{
/* TODO: do better */
rootIter = 0;
if (filename != NULL) sprintf(filename, "%s", rootDirs[rootIter++]);
if (filesize != NULL) *filesize = 0;
// printf("%s(%s, filename (%s), %d, %" PRIu64 ")\n", __func__, directory, filename == NULL ? "" : filename, filenamelength, filesize != NULL ? *filesize : 0);
return channel_token(); /* 'fake' token to be freed on closedir */
}
else if (strncmp(directory, "./Channels", 10) == 0)
{
void* tmpC = firstchannel(filename);
if (tmpC != NULL) {
if (filename != NULL && filesize != NULL) {
*filesize = LIVETV_FILESIZE;
}
}
return (void*)tmpC;
}
else if (strncmp(directory, "./EPG", 5) == 0)
{
title = GetFileName(directory, "/", 1);
parentDir = GetParentPath(directory, "/", 1);
parentTitle = GetFileName(parentDir, "/", 1);
parentDir2 = (parentDir != NULL) ? GetParentPath(parentDir, "/", 1) : NULL;
if (strcmp(directory, "./EPG/") == 0 || strcmp(directory, "./EPG/") == 0)
{
char channelName[64] = { 0 };
void* tmpC = firstchannel(filename);
if (tmpC != NULL) {
if (filename != NULL && filesize != NULL) {
*filesize = LIVETV_FILESIZE;
}
}
retval = (void*)tmpC;
}
else if (parentDir != NULL && strcmp(parentDir,"./EPG/") == 0 && ischannel(title))
{
char day_string[64] = { 0 };
void *x = firstEpgDay(title, day_string);
if (filename != NULL) sprintf(filename, "%s", day_string);
if (filename != NULL && filesize != NULL) {
*filesize = 0;
}
retval = (void*)x;
}
else if (parentDir2 != NULL && strcmp(parentDir2,"./EPG/") == 0 &&
ischannel(parentTitle) && isDate(title))
{
char event_string[64] = { 0 };
void* x = firstEpgEvent(parentTitle, title, event_string);
if (filename != NULL) sprintf(filename, "%s", event_string);
if (filename != NULL && filesize != NULL) {
*filesize = LIVETV_FILESIZE;
}
retval = (void*)x;
}
if (title != NULL) free(title);
if (parentDir != NULL) free(parentDir);
if (parentTitle != NULL) free(parentTitle);
if (parentDir2 != NULL) free(parentDir2);
return retval;
}
else
return NULL;
#endif
dirObj = opendir(directory);
if (dirObj != NULL)
{
dirEntry = readdir(dirObj);
if ((dirEntry != NULL) && ((int) strlen(dirEntry->d_name) < filenamelength))
{
if (filename != NULL)
{
strToUtf8(filename, dirEntry->d_name, filenamelength, 0, NULL);
sprintf(fullPath, "%s%s", directory, dirEntry->d_name);
if (filesize != NULL)
{
if (stat64(fullPath, &_si) != -1)
{
if ((_si.st_mode & S_IFDIR) == S_IFDIR)
{
*filesize = 0;
}
else
{
*filesize = _si.st_size;
}
}
}
}
}
}
return dirObj;
#endif
}
// Windows Version
// 0 = No More Files
// 1 = Next File
int PCGetDirNextFile(void* handle, const char* dirName, char* filename, int filenamelength, uint64_t* filesize)
{
// printf("%s(void*, %s, filename (out), %d, %" PRIu64 ")\n", __func__, dirName, filenamelength, filesize != NULL ? *filesize : 0);
#ifdef WIN32
WIN32_FIND_DATA FileData;
if (FindNextFile(*((HANDLE*)handle), &FileData) == 0) {return 0;}
strToUtf8(filename,FileData.cFileName,filenamelength,0, NULL);
if (filesize != NULL)
{
*filesize = FileData.nFileSizeLow;
}
return 1;
#endif
#ifdef UNDER_CE
WIN32_FIND_DATA FileData;
int fnf = 0;
int conv = -1;
fnf = FindNextFile(*((HANDLE*)handle), &FileData);
if (fnf == 0) {return 0;}
conv = strToUtf8(filename, (char*)FileData.cFileName, filenamelength, 1, NULL);
if (conv == -1) {return 0;}
if (filesize != NULL)
{
*filesize = FileData.nFileSizeLow;
}
return 1;
#endif
#ifdef _POSIX
DIR* dirObj;
struct dirent* dirEntry; /* dirEntry is a pointer to static memory in the C runtime lib for readdir()*/
struct stat64 _si;
char fullPath[1024];
int retval = 0;
char *title = NULL;
char *parentDir = NULL;
char *parentTitle = NULL;
char *parentDir2 = NULL;
char *parentTitle2 = NULL;
#if NDi_LiveTV
if (strcmp(dirName, "./") == 0)
{
if (rootIter == 3) return 0;
if (filename != NULL) sprintf(filename, "%s", rootDirs[rootIter++]);
if (filesize != NULL) *filesize = 0;
retval = 1;
}
else if (strncmp(dirName, "./Channels/", 11) == 0)
{
void* tmpC = nextchannel(handle, filename);
if (tmpC != NULL) {
if (filesize != NULL) {
*filesize = LIVETV_FILESIZE;
}
retval = 1;
}
}
else if (strncmp(dirName, "./EPG/", 6) == 0)
{
title = GetFileName(dirName, "/", 1);
parentDir = GetParentPath(dirName, "/", 1);
parentTitle = GetFileName(parentDir, "/", 1);
parentDir2 = (parentDir != NULL) ? GetParentPath(parentDir, "/", 1) : NULL;
parentTitle2 = (parentDir2 != NULL) ? GetFileName(parentDir2, "/", 1) : NULL;
if (strcmp(dirName, "./EPG/") == 0)
{
void* tmpC = nextchannel(handle, filename);
if (tmpC != NULL) {
if (filesize != NULL) {
*filesize = LIVETV_FILESIZE;
}
retval = 1;
}
}
else if (parentDir != NULL && strcmp(parentDir,"./EPG/") == 0 && ischannel(title))
{
char day_string[128] = { 0 };
if (nextEpgDay(handle, title, day_string) != NULL)
{
if (filename != NULL) sprintf(filename, "%s", day_string);
if (filename != NULL && filesize != NULL) {
*filesize = 0;
}
retval = 1;
}
else
retval = 0;
}
else if (parentDir2 != NULL && strcmp(parentDir2,"./EPG/") == 0 &&
ischannel(parentTitle) && isDate(title))
{
char event_string[128] = { 0 };
if (nextEpgEvent(handle, parentTitle, title, event_string) != NULL)
{
if (filename != NULL) sprintf(filename, "%s", event_string);
if (filename != NULL && filesize != NULL) {
*filesize = LIVETV_FILESIZE;
}
retval = 1;
}
else
retval = 0;
}
}
if (title != NULL) free(title);
if (parentDir != NULL) free(parentDir);
if (parentTitle != NULL) free(parentTitle);
if (parentDir2 != NULL) free(parentDir2);
if (parentTitle2 != NULL) free(parentTitle2);
return retval;
#endif
dirObj = (DIR*) handle;
dirEntry = readdir(dirObj);
if ((dirEntry != NULL) && ((int) strlen(dirEntry->d_name) < filenamelength))
{
strToUtf8(filename, dirEntry->d_name, filenamelength, 0, NULL);
sprintf(fullPath, "%s%s", dirName, dirEntry->d_name);
if (filesize != NULL)
{
/* WTF? Cygwin has a memory leak with stat. */
if (stat64(fullPath, &_si) != -1)
{
if ((_si.st_mode & S_IFDIR) == S_IFDIR)
{
*filesize = 0;
}
else
{
*filesize = _si.st_size;
}
}
}
return 1;
}
return 0;
#endif
}
/* returns
* 0 - does not exist
* 1 - 'file'
* 2 - directory
*/
int PCGetFileDirType(char* directory)
{
#ifdef WIN32
DWORD _si;
int dirLen,dirSize;
char *fullpath;
dirLen = (int) strlen(directory);
dirSize = dirLen+1;
fullpath = (char*) malloc(dirSize);
Utf8ToAnsi(fullpath, directory, dirSize);
_si = GetFileAttributes(fullpath);
free(fullpath);
if (_si == 0xFFFFFFFF)
{
return 0;
}
if ((_si & FILE_ATTRIBUTE_DIRECTORY) == 0)
{
return 1;
}
else
{
return 2;
}
#endif
#ifdef UNDER_CE
wchar_t* wfullPath;
DWORD _si;
int mbDirSize;
int wPathSize;
int dirLen,dirSize;
char *fullpath;
int retVal = 0;
dirLen = (int) strlen(directory);
dirSize = dirLen+1;
fullpath = (char*) malloc(dirSize);
Utf8ToAnsi(fullpath, directory, dirSize);
mbDirSize = (int) strlen(fullpath) + 1;
wPathSize = mbDirSize * 2;
wfullPath = (wchar_t*)malloc(wPathSize);
if (mbstowcs(wfullPath,fullpath,wPathSize) == -1)
{
retVal = 0;
}
else
{
_si = GetFileAttributes(wfullPath);
if (_si == 0xFFFFFFFF)
{
retVal = 0;
}
else
{
if ((_si & FILE_ATTRIBUTE_DIRECTORY) == 0)
{
retVal = 1;
}
else
{
retVal = 2;
}
}
}
free(fullpath);
free(wfullPath);
return retVal;
#endif
#ifdef _POSIX
int retval = 0;
// printf("%s(%s)\n", __func__, directory);
#if NDi_LiveTV
if (strcmp(directory, "./") == 0)
return 2;
char *title = GetFileName(directory, "/", 1);
char *parentDir = GetParentPath(directory, "/", 1);
char *parentTitle = GetFileName(parentDir, "/", 1);
char *parentDir2 = (parentDir != NULL) ? GetParentPath(parentDir, "/", 1) : NULL;
char *parentTitle2 = (parentDir2 != NULL) ? GetFileName(parentDir2, "/", 1) : NULL;
/*
printf("%s() title = %s\n", __func__, title = NULL ? "" : title);
printf("%s() parentDir = %s\n", __func__, parentDir == NULL ? "" : parentDir);
printf("%s() parentTitle = %s\n", __func__, parentTitle == NULL ? "" : parentTitle);
printf("%s() parentDir2 = %s\n", __func__, parentDir2 == NULL ? "" : parentDir2);
printf("%s() parentTitle2 = %s\n", __func__, parentTitle2 == NULL ? "" : parentTitle2);
*/
if (strncmp(directory, "./Channels", 10) == 0)
{
if ( (strcmp(directory, "./Channels") == 0) ||
(strcmp(directory, "./Channels/") == 0) )
{
retval = 2;
}
if (ischannel(title)) {
// printf("its a channel\n");
retval = 1;
}
}
else if (strncmp(directory, "./EPG", 5) == 0)
{
if ( (strcmp(directory, "./EPG") == 0) ||
(strcmp(directory, "./EPG/") == 0) )
{
retval = 2;
}
else if (strcmp(parentDir, "./EPG/") == 0)
{
if (ischannel(title)) {
// printf("Channel Aggregation EPG\n");
retval = 2;
}
}
else if (parentTitle2 != NULL && ischannel(parentTitle2) &&
parentTitle != NULL && isDate(parentTitle))
{
if (1) // TODO: isListing(title)
{
// printf("Individual EPG listing\n");
retval = 1;
}
}
else if (parentDir2 != NULL && strcmp(parentDir2, "./EPG/") == 0 &&
ischannel(parentTitle) && isDate(title))
{
// printf("Channel Day of the Week EPG Listing\n");
retval = 2;
}
}
if (title != NULL) free(title);
if (parentDir != NULL) free(parentDir);
if (parentTitle != NULL) free(parentTitle);
if (parentDir2 != NULL) free(parentDir2);
if (parentTitle2 != NULL) free(parentTitle2);
return retval;
#endif
struct stat64 _si;
int dirLen,dirSize;
char *fullpath;
int pathExists;
int retVal = 0;
dirLen = (int) strlen(directory);
dirSize = dirLen+1;
fullpath = (char*) malloc(dirSize);
Utf8ToAnsi(fullpath, directory, dirSize);
pathExists = stat64(fullpath, &_si);
free(fullpath);
if (pathExists != -1)
{
if ((_si.st_mode & S_IFDIR) == S_IFDIR)
{
retVal = 2;
}
else
{
retVal = 1;
}
}
return retVal;
#endif
}
#ifdef _POSIX
/* only needed for posix because readdir returns statically allocated values */
pthread_mutex_t BrowseLock;
#endif
void InitBrowseLock()
{
#ifdef _POSIX
pthread_mutex_init(&BrowseLock, NULL);
#endif
}
void LockBrowse()
{
#ifdef _POSIX
pthread_mutex_lock(&BrowseLock);
#endif
}
void UnlockBrowse()
{
#ifdef _POSIX
pthread_mutex_unlock(&BrowseLock);
#endif
}
void DestroyBrowseLock()
{
#ifdef _POSIX
pthread_mutex_destroy(&BrowseLock);
#endif
}
void EndThisThread()
{
#ifdef _POSIX
pthread_exit(NULL);
#endif
#ifdef WIN32
ExitThread(0);
#endif
#ifdef UNDER_CE
ExitThread(0);
#endif
}
void* SpawnNormalThread(void* method, void* arg)
{
#ifdef _POSIX
int result;
void* (*fptr) (void* a);
pthread_t newThread;
fptr = method;
result = pthread_create(&newThread, NULL, fptr, arg);
pthread_detach(newThread);