Merge with MPC-HC 6d1472b2f18266d92e5bc068667de348c0cd3b3b.
[xy_vsfilter.git] / src / subtitles / libssf / Split.cpp
blobc7868eaa0340ed8374f5efbb50d29be46d070082
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 "Split.h"
24 #include "Exception.h"
26 namespace ssf
28 Split::Split(LPCWSTR sep, CStringW str, size_t limit, SplitType type)
30 DoSplit(sep, str, limit, type);
33 Split::Split(WCHAR sep, CStringW str, size_t limit, SplitType type)
35 DoSplit(CStringW(sep), str, limit, type);
38 void Split::DoSplit(LPCWSTR sep, CStringW str, size_t limit, SplitType type)
40 RemoveAll();
42 if(size_t seplen = wcslen(sep))
44 for(int i = 0, j = 0, len = str.GetLength();
45 i <= len && (limit == 0 || GetCount() < limit);
46 i = j + (int)seplen)
48 j = str.Find(sep, i);
49 if(j < 0) j = len;
51 CStringW s = i < j ? str.Mid(i, j - i) : L"";
53 switch(type)
55 case Min: s.Trim(); // fall through
56 case Def: if(s.IsEmpty()) break; // else fall through
57 case Max: Add(s); break;
63 int Split::GetAtInt(size_t i)
65 if(i >= GetCount()) throw Exception(_T("Index out of bounds"));
66 return _wtoi(GetAt(i));
69 float Split::GetAtFloat(size_t i)
71 if(i >= GetCount()) throw Exception(_T("Index out of bounds"));
72 return (float)_wtof(GetAt(i));