2 * Copyright (C) 2003-2006 Gabest
3 * http://www.gabest.org
5 * This Program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2, or (at your option)
10 * This Program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with GNU Make; see the file COPYING. If not, write to
17 * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
18 * http://www.gnu.org/copyleft/gpl.html
26 #include "xy_logger.h"
28 TCHAR
* G_EXTTYPESTR
[] =
30 _T("srt"), _T("sub"), _T("smi"), _T("psb"),
31 _T("ssa"), _T("ass"), _T("idx"), _T("usf"),
32 _T("xss"), _T("txt"), _T("ssf"), _T("rt"), _T("sup")
35 #define WEBSUBEXT _T(".wse")
37 static int SubFileCompare(const void* elem1
, const void* elem2
)
39 const SubFile
* file1
= ((const SubFile
*)elem1
);
40 const SubFile
* file2
= ((const SubFile
*)elem2
);
42 int rv
= file1
->path_order
- file2
->path_order
;
43 rv
= rv
? rv
: file1
->ext_order
- file2
->ext_order
;
44 rv
= rv
? rv
: file2
->extra_name
.IsEmpty() - file1
->extra_name
.IsEmpty();
45 rv
= rv
? rv
: file1
->extra_name
.CompareNoCase(file2
->extra_name
);
52 bool init(const CString
& load_ext_list
)
54 int extsubnum
= countof(G_EXTTYPESTR
);
55 for (int i
=0;i
<extsubnum
;i
++)
57 ext_map
[CString(G_EXTTYPESTR
[i
])] = -1;
59 for (int i
=0, start
=0;true;i
++)
61 CString ext
= load_ext_list
.Tokenize(_T(";"), start
);
65 if (ext_map
.Lookup(ext
, tmp
))
78 int ext_support_order(const CString
& ext
)
81 return ext_map
.Lookup(ext
, tmp
) ? tmp
: -1;
83 CAtlMap
<CString
, int, CStringElementTraits
<CString
>> ext_map
;
86 void GetSubFileNames(CString fn
, CAtlArray
<CString
>& paths
, CString load_ext_list
, CAtlArray
<SubFile
>& ret
)
90 fn
.Replace('\\', '/');
94 //int i = fn.Find(_T("://"));
95 int i
= fn
.Find(_T("http://"));
96 if(i
> 0) {fn
= _T("http") + fn
.Mid(i
); fWeb
= true;}
99 int l
= fn
.GetLength(), l2
= l
;
100 l2
= fn
.ReverseFind('.');
101 l
= fn
.ReverseFind('/') + 1;
104 CString orgpath
= fn
.Left(l
);
105 CString title
= fn
.Mid(l
, l2
-l
);
106 CString filename
= title
+ _T(".nooneexpectsthespanishinquisition");
108 ExtSupport ext_support
;
109 if (!ext_support
.init(load_ext_list
))
111 XY_LOG_INFO(_T("unexpected error"));
118 for(size_t k
= 0; k
< paths
.GetCount(); k
++)
120 CString path
= paths
[k
];
121 path
.Replace('\\', '/');
123 l
= path
.GetLength();
124 if(l
> 0 && path
[l
-1] != '/') path
+= '/';
126 if(path
.Find(':') == -1 && path
.Find(_T("\\\\")) != 0) path
= orgpath
+ path
;
128 path
.Replace(_T("/./"), _T("/"));
129 path
.Replace('/', '\\');
131 CAtlList
<CString
> sl
;
135 HANDLE hFile
= FindFirstFile(path
+ title
+ _T(".*"), &wfd
);
136 if(hFile
!= INVALID_HANDLE_VALUE
)
140 if(filename
.CompareNoCase(wfd
.cFileName
) != 0)
143 sl
.AddTail(wfd
.cFileName
);
146 while(FindNextFile(hFile
, &wfd
));
153 POSITION pos
= sl
.GetHeadPosition();
156 const CString
& fn
= sl
.GetNext(pos
);
157 int l
= fn
.ReverseFind('.');
158 CString ext
= fn
.Mid(l
+1);
159 int ext_order
= ext_support
.ext_support_order(ext
);
163 f
.full_file_name
= path
+ fn
;
165 int l2
= fn
.Find('.');
172 f
.extra_name
= fn
.Mid(l2
+1, l
-l2
-1);
175 f
.ext_order
= ext_order
;
184 CWebTextFile wtf
; // :)
185 if(wtf
.Open(orgpath
+ title
+ WEBSUBEXT
))
188 while(wtf
.ReadString(fn
) && fn
.Find(_T("://")) >= 0)
191 f
.full_file_name
= fn
;
192 f
.extra_name
= fn
.Mid(fn
.ReverseFind('/')+1);
193 f
.ext_order
= MAXINT32
;
194 f
.path_order
= MAXINT32
;
200 // sort files, this way the user can define the order (movie.00.English.srt, movie.01.Hungarian.srt, etc)
202 qsort(ret
.GetData(), ret
.GetCount(), sizeof(SubFile
), SubFileCompare
);