Support unrar64.dll
[xy_vsfilter.git] / src / subtitles / GFN.cpp
blob0a9507ca94d31f910b6122708344727f407bbd74
1 /*
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)
8 * any later version.
9 *
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
22 #include "stdafx.h"
23 #include <io.h>
24 #include "TextFile.h"
25 #include "GFN.h"
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);
46 return rv;
49 class ExtSupport
51 public:
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);
62 if (ext!="")
64 int tmp = 0;
65 if (ext_map.Lookup(ext, tmp))
67 ext_map[ext] = i;
70 else
72 break;
75 return true;
78 int ext_support_order(const CString& ext)
80 int tmp = 0;
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)
88 ret.RemoveAll();
90 fn.Replace('\\', '/');
92 bool fWeb = false;
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;
102 if(l2 < l) l2 = l;
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"));
112 return;
115 if(!fWeb)
117 WIN32_FIND_DATA wfd;
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;
133 bool fEmpty = true;
135 HANDLE hFile = FindFirstFile(path + title + _T(".*"), &wfd);
136 if(hFile != INVALID_HANDLE_VALUE)
140 if(filename.CompareNoCase(wfd.cFileName) != 0)
142 fEmpty = false;
143 sl.AddTail(wfd.cFileName);
146 while(FindNextFile(hFile, &wfd));
148 FindClose(hFile);
151 if(fEmpty) continue;
153 POSITION pos = sl.GetHeadPosition();
154 while(pos)
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);
160 if (ext_order>-1)
162 SubFile f;
163 f.full_file_name = path + fn;
165 int l2 = fn.Find('.');
166 if (l2==l)
168 f.extra_name = "";
170 else
172 f.extra_name = fn.Mid(l2+1, l-l2-1);
175 f.ext_order = ext_order;
176 f.path_order = k;
177 ret.Add(f);
182 else if(l > 7)
184 CWebTextFile wtf; // :)
185 if(wtf.Open(orgpath + title + WEBSUBEXT))
187 CString fn;
188 while(wtf.ReadString(fn) && fn.Find(_T("://")) >= 0)
190 SubFile f;
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;
195 ret.Add(f);
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);