Merge with MPC-HC 6d1472b2f18266d92e5bc068667de348c0cd3b3b.
[xy_vsfilter.git] / src / subtitles / GFN.cpp
blob57732123b06acd7880c52abb5f876f571c522d5b
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"
27 TCHAR* exttypestr[] =
29 _T("srt"), _T("sub"), _T("smi"), _T("psb"),
30 _T("ssa"), _T("ass"), _T("idx"), _T("usf"),
31 _T("xss"), _T("txt"), _T("ssf"), _T("rt"), _T("sup")
34 static TCHAR* ext[2][countof(exttypestr)] =
37 _T(".srt"), _T(".sub"), _T(".smi"), _T(".psb"),
38 _T(".ssa"), _T(".ass"), _T(".idx"), _T(".usf"),
39 _T(".xss"), _T(".txt"), _T(".ssf"), _T(".rt"), _T(".sup")
42 _T(".*.srt"), _T(".*.sub"), _T(".*.smi"), _T(".*.psb"),
43 _T(".*.ssa"), _T(".*.ass"), _T(".*.dummyidx"), _T(".*.usf"),
44 _T(".*.xss"), _T(".*.txt"), _T(".*.ssf"), _T(".*.rt"), _T(".*.sup")
45 },
48 #define WEBSUBEXT _T(".wse")
50 static int SubFileCompare(const void* elem1, const void* elem2)
52 return(((SubFile*)elem1)->fn.CompareNoCase(((SubFile*)elem2)->fn));
55 void GetSubFileNames(CString fn, CAtlArray<CString>& paths, CAtlArray<SubFile>& ret)
57 ret.RemoveAll();
59 int extlistnum = countof(ext);
60 int extsubnum = countof(ext[0]);
62 fn.Replace('\\', '/');
64 bool fWeb = false;
66 // int i = fn.Find(_T("://"));
67 int i = fn.Find(_T("http://"));
68 if(i > 0) {fn = _T("http") + fn.Mid(i); fWeb = true;}
71 int l = fn.GetLength(), l2 = l;
72 l2 = fn.ReverseFind('.');
73 l = fn.ReverseFind('/') + 1;
74 if(l2 < l) l2 = l;
76 CString orgpath = fn.Left(l);
77 CString title = fn.Mid(l, l2-l);
78 CString filename = title + _T(".nooneexpectsthespanishinquisition");
80 if(!fWeb)
82 // struct _tfinddata_t file, file2;
83 // long hFile, hFile2 = 0;
85 WIN32_FIND_DATA wfd, wfd2;
86 HANDLE hFile, hFile2;
88 for(int k = 0; k < paths.GetCount(); k++)
90 CString path = paths[k];
91 path.Replace('\\', '/');
93 l = path.GetLength();
94 if(l > 0 && path[l-1] != '/') path += '/';
96 if(path.Find(':') == -1 && path.Find(_T("\\\\")) != 0) path = orgpath + path;
98 path.Replace(_T("/./"), _T("/"));
99 path.Replace('/', '\\');
101 // CAtlList<CString> sl;
103 bool fEmpty = true;
105 if((hFile = FindFirstFile(path + title + _T("*"), &wfd)) != INVALID_HANDLE_VALUE)
109 if(filename.CompareNoCase(wfd.cFileName) != 0)
111 fEmpty = false;
112 // sl.AddTail(path + file.name);
115 while(FindNextFile(hFile, &wfd));
117 FindClose(hFile);
120 // TODO: use 'sl' in the next step to find files (already a nice speedup as it is now...)
121 if(fEmpty) continue;
123 for(int j = 0; j < extlistnum; j++)
125 for(int i = 0; i < extsubnum; i++)
127 if((hFile = FindFirstFile(path + title + ext[j][i], &wfd)) != INVALID_HANDLE_VALUE)
131 CString fn = path + wfd.cFileName;
133 hFile2 = INVALID_HANDLE_VALUE;
135 if(j == 0 || (hFile2 = FindFirstFile(fn.Left(fn.ReverseFind('.')) + _T(".avi"), &wfd2)) == INVALID_HANDLE_VALUE)
137 SubFile f;
138 f.fn = fn;
139 ret.Add(f);
142 if(hFile2 != INVALID_HANDLE_VALUE)
144 FindClose(hFile2);
147 while(FindNextFile(hFile, &wfd));
149 FindClose(hFile);
155 else if(l > 7)
157 CWebTextFile wtf; // :)
158 if(wtf.Open(orgpath + title + WEBSUBEXT))
160 CString fn;
161 while(wtf.ReadString(fn) && fn.Find(_T("://")) >= 0)
163 SubFile f;
164 f.fn = fn;
165 ret.Add(f);
170 // sort files, this way the user can define the order (movie.00.English.srt, movie.01.Hungarian.srt, etc)
172 qsort(ret.GetData(), ret.GetCount(), sizeof(SubFile), SubFileCompare);