aboutsummaryrefslogtreecommitdiff
path: root/Src/playlist/PLSLoader.cpp
blob: 736b5240c134f34983541491aad0350404c37e6c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#include "PLSLoader.h"

#include "../nu/AutoChar.h"
#include "../nu/AutoWide.h"
#include <shlwapi.h>
#include <strsafe.h>


class PLSInfo : public ifc_plentryinfo
{
public:
	PLSInfo( const wchar_t *_filename, int _entryNum ) : filename( _filename ), entryNum( _entryNum )
	{}

	const wchar_t *GetExtendedInfo( const wchar_t *parameter )
	{
		static wchar_t data[ 1024 ];
		wchar_t        fieldbuf[ 100 ] = { 0 };

		StringCchPrintfW( fieldbuf, 100, L"%s%d", parameter, entryNum );
		
		GetPrivateProfileStringW( L"playlist", fieldbuf, L"", data, 1024, filename );
		
		if ( data[ 0 ] )
			return data;
		else
			return 0;
	}

private:
	RECVS_DISPATCH;

	const wchar_t *filename;
	int            entryNum;
};

#define CBCLASS PLSInfo
START_DISPATCH;
CB( IFC_PLENTRYINFO_GETEXTENDEDINFO, GetExtendedInfo )
END_DISPATCH;
#undef CBCLASS

int PLSLoader::OnFileHelper( ifc_playlistloadercallback *playlist, const wchar_t *trackName, const wchar_t *title, int length, const wchar_t *rootPath, ifc_plentryinfo *extraInfo )
{
	if ( length == -1000 )
		length = -1;

	int ret;
	if ( wcsstr( trackName, L"://" ) || PathIsRootW( trackName ) )
	{
		ret = playlist->OnFile( trackName, title, length, extraInfo );
	}
	else
	{
		wchar_t fullPath[ MAX_PATH ] = { 0 };
		if ( PathCombineW( fullPath, rootPath, trackName ) )
		{
			wchar_t canonicalizedPath[ MAX_PATH ] = { 0 };
			PathCanonicalizeW( canonicalizedPath, fullPath );
			ret = playlist->OnFile( canonicalizedPath, title, length, extraInfo );
		}
		else
		{
			ret = ifc_playlistloadercallback::LOAD_CONTINUE;
		}
	}

	return ret;
}

int PLSLoader::Load( const wchar_t *filename, ifc_playlistloadercallback *playlist )
{
	int x, numfiles;
	int ext = 0;
	char fieldbuf[ 100 ] = { 0 };
	char fnbuf[ FILENAME_SIZE ] = { 0 };
	char tmp[ MAX_PATH ] = { 0 };

	wchar_t rootPath[ MAX_PATH ] = { 0 };
	const wchar_t *callbackPath = playlist->GetBasePath();
	if ( callbackPath )
		lstrcpynW( rootPath, callbackPath, MAX_PATH );
	else
	{
		lstrcpynW( rootPath, filename, MAX_PATH );
		PathRemoveFileSpecW( rootPath );
	}

	tmp[ 0 ] = (char)rootPath[ 0 ];
	tmp[ 1 ] = (char)rootPath[ 1 ];
	tmp[ 2 ] = (char)rootPath[ 2 ];

	AutoChar fn( filename );

	numfiles = GetPrivateProfileIntA( "playlist", "NumberOfEntries", 0, fn );
	ext = GetPrivateProfileIntA( "playlist", "Version", 1, fn );
	if ( numfiles == 0 )
		return IFC_PLAYLISTLOADER_FAILED;

	for ( x = 1; x <= numfiles; x++ )
	{
		int flen = -1;
		char ftitle[ FILETITLE_SIZE ] = "";
		StringCchPrintfA( fieldbuf, 100, "File%d", x );
		GetPrivateProfileStringA( "playlist", fieldbuf, "", fnbuf, FILENAME_SIZE, fn );
		if ( ext )
		{
			StringCchPrintfA( fieldbuf, 100, "Title%d", x );
			GetPrivateProfileStringA( "playlist", fieldbuf, "", ftitle, FILETITLE_SIZE, fn );
			StringCchPrintfA( fieldbuf, 100, "Length%d", x );
			flen = GetPrivateProfileIntA( "playlist", fieldbuf, -1, fn );
		}

		if ( *fnbuf )
		{
			char *p;
			char buf[ 512 ] = { 0 };

			p = fnbuf;

			if ( strncmp( p, "\\\\", 2 ) && strncmp( p + 1, ":\\", 2 ) && !strstr( p, ":/" ) )
			{
				if ( p[ 0 ] == '\\' )
				{
					buf[ 0 ] = tmp[ 0 ];
					buf[ 1 ] = tmp[ 1 ];
					lstrcpynA( buf + 2, p, 510 );
					buf[ 511 ] = 0;
					p = buf;
				}
			}

			PLSInfo info( filename, x );

			int ret;
			if ( ftitle[ 0 ] )
			{
				ret = OnFileHelper( playlist, AutoWide( p ), AutoWide( ftitle ), flen * 1000, rootPath, &info );
			}
			else
			{
				ret = OnFileHelper( playlist, AutoWide( p ), 0, -1, rootPath, &info );
			}

			if ( ret != ifc_playlistloadercallback::LOAD_CONTINUE )
			{
				break;
			}
		}
	}

	return IFC_PLAYLISTLOADER_SUCCESS;
}

#define CBCLASS PLSLoader
START_DISPATCH;
CB( IFC_PLAYLISTLOADER_LOAD, Load )
END_DISPATCH;
#undef CBCLASS