GFN.cpp

00001 /* 
00002  *      Copyright (C) 2003-2005 Gabest
00003  *      http://www.gabest.org
00004  *
00005  *  This Program is free software; you can redistribute it and/or modify
00006  *  it under the terms of the GNU General Public License as published by
00007  *  the Free Software Foundation; either version 2, or (at your option)
00008  *  any later version.
00009  *   
00010  *  This Program is distributed in the hope that it will be useful,
00011  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00013  *  GNU General Public License for more details.
00014  *   
00015  *  You should have received a copy of the GNU General Public License
00016  *  along with GNU Make; see the file COPYING.  If not, write to
00017  *  the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. 
00018  *  http://www.gnu.org/copyleft/gpl.html
00019  *
00020  */
00021 
00022 #include "stdafx.h"
00023 #include <io.h>
00024 #include "TextFile.h"
00025 #include "GFN.h"
00026 
00027 TCHAR* exttypestr[] = 
00028 {
00029         _T("srt"), _T("sub"), _T("smi"), _T("psb"), 
00030         _T("ssa"), _T("ass"), _T("idx"), _T("usf"), 
00031         _T("xss"), _T("txt")
00032 };
00033 
00034 static TCHAR* ext[2][countof(exttypestr)] = 
00035 {
00036         {
00037                 _T(".srt"), _T(".sub"), _T(".smi"), _T(".psb"), 
00038                 _T(".ssa"), _T(".ass"), _T(".idx"), _T(".usf"), 
00039                 _T(".xss"), _T(".txt")
00040         },
00041         {
00042                 _T(".*.srt"), _T(".*.sub"), _T(".*.smi"), _T(".*.psb"), 
00043                 _T(".*.ssa"), _T(".*.ass"), _T(".*.dummyidx"), _T(".*.usf"), 
00044                 _T(".*.xss"), _T(".*.txt")
00045         }, 
00046 };
00047 
00048 #define WEBSUBEXT _T(".wse")
00049 
00050 static int SubFileCompare(const void* elem1, const void* elem2)
00051 {
00052         return(((SubFile*)elem1)->fn.CompareNoCase(((SubFile*)elem2)->fn));
00053 }
00054 
00055 void GetSubFileNames(CString fn, CStringArray& paths, SubFiles& ret)
00056 {
00057         ret.RemoveAll();
00058 
00059         int extlistnum = countof(ext);
00060         int extsubnum = countof(ext[0]);
00061 
00062         fn.Replace('\\', '/');
00063 
00064         bool fWeb = false;
00065         {
00066 //              int i = fn.Find(_T("://"));
00067                 int i = fn.Find(_T("http://"));
00068                 if(i > 0) {fn = _T("http") + fn.Mid(i); fWeb = true;}
00069         }
00070 
00071         int     l = fn.GetLength(), l2 = l;
00072         l2 = fn.ReverseFind('.');
00073         l = fn.ReverseFind('/') + 1;
00074         if(l2 < l) l2 = l;
00075 
00076         CString orgpath = fn.Left(l);
00077         CString title = fn.Mid(l, l2-l);
00078         CString filename = title + _T(".nooneexpectsthespanishinquisition");
00079 
00080         if(!fWeb)
00081         {
00082                 struct _tfinddata_t file, file2;
00083                 long hFile, hFile2 = 0;
00084 
00085                 for(int k = 0; k < paths.GetSize(); k++)
00086                 {
00087                         CString path = paths[k];
00088                         path.Replace('\\', '/');
00089 
00090                         l = path.GetLength();
00091                         if(l > 0 && path[l-1] != '/') path += '/';
00092 
00093                         if(path.Find(':') == -1 && path.Find(_T("\\\\")) != 0) path = orgpath + path;
00094 
00095                         path.Replace(_T("/./"), _T("/"));
00096                         path.Replace('/', '\\');
00097 
00098                         {
00099                                 CList<CString> sl;
00100 
00101                                 bool fEmpty = true;
00102 
00103                                 if((hFile = _tfindfirst((LPTSTR)(LPCTSTR)(path + title + "*"), &file)) != -1L)
00104                                 {
00105                                         do
00106                                         {
00107                                                 if(filename.CompareNoCase(file.name)) 
00108                                                 {
00109                                                         fEmpty = false;
00110                                                         // sl.AddTail(path + file.name);
00111                                                 }
00112                                         }
00113                                         while(_tfindnext(hFile, &file) != -1);
00114                                         _findclose(hFile);
00115                                 }
00116 
00117                                 // TODO: use 'sl' in the next step to find files (already a nice speedup as it is now...)
00118                                 if(fEmpty) continue;
00119                         }
00120 
00121                         for(int j = 0; j < extlistnum; j++)
00122                         {
00123                                 for(int i = 0; i < extsubnum; i++)
00124                                 {
00125                                         CString fn2 = path + title + ext[j][i];
00126 
00127                                         if((hFile = _tfindfirst((LPTSTR)(LPCTSTR)fn2, &file)) != -1L)
00128                                         {
00129                                                 do
00130                                                 {
00131                                                         CString fn = path + file.name;
00132 
00133                                                         hFile2 = -1;
00134                                                         if(j == 0 || (hFile2 = _tfindfirst((LPTSTR)(LPCTSTR)(fn.Left(fn.ReverseFind('.')) + _T(".avi")), &file2)) == -1L)
00135                                                         {
00136                                                                 SubFile f;
00137                                                                 f.fn = fn;
00138                                                                 ret.Add(f);
00139                                                         }
00140                                                         
00141                                                         if(hFile2 >= 0) _findclose(hFile2);
00142                                                 }
00143                                                 while(_tfindnext(hFile, &file) != -1);
00144                                                 
00145                                                 _findclose(hFile);
00146                                         }
00147                                 }
00148                         }
00149                 }
00150         }
00151         else if(l > 7)
00152         {
00153                 CWebTextFile wtf; // :)
00154                 if(wtf.Open(orgpath + title + WEBSUBEXT))
00155                 {
00156                         CString fn;
00157                         while(wtf.ReadString(fn) && fn.Find(_T("://")) >= 0)
00158                         {
00159                                 SubFile f;
00160                                 f.fn = fn;
00161                                 ret.Add(f);
00162                         }
00163                 }
00164         }
00165 
00166         // sort files, this way the user can define the order (movie.00.English.srt, movie.01.Hungarian.srt, etc)
00167 
00168         qsort(ret.GetData(), ret.GetSize(), sizeof(SubFile), SubFileCompare);
00169 }

Generated on Tue Dec 13 14:47:54 2005 for guliverkli by  doxygen 1.4.5