Subversion Repositories spk

Rev

Rev 319 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
303 cycrow 1
#pragma once
2
 
3
#include "WString.h"
4
#include "WStringList.h"
5
#include "../File_IO.h"
6
#include "../DirIO.h"
7
 
319 cycrow 8
#ifdef _WIN32
9
#include <windows.h>
10
#include <direct.h>
11
#include <shlobj.h>
12
#else
13
#include <dirent.h> 
14
#include <sys/types.h> 
15
#include <sys/param.h> 
16
#include <sys/stat.h> 
17
#include <unistd.h> 
18
#endif
19
 
303 cycrow 20
namespace Utils
21
{
22
	class CommandLine
23
	{
24
	private:
25
		std::shared_ptr<CFileIO> _file;
319 cycrow 26
		Utils::WStringList		_options;
27
		CDirIO 					_myDoc;
28
		CDirIO 					_tempDir;
303 cycrow 29
		std::vector<Utils::WString> _args;
319 cycrow 30
		CPackages				_p;
303 cycrow 31
 
32
	public:
33
		CommandLine(int argc, char* argv[])
34
		{
35
			_file = std::make_shared<CFileIO>(argv[0]);
36
 
37
			for (int i = 1; i < argc; i++)
38
			{
39
				Utils::WString arg(argv[i]);
40
				if (arg.startsWith(L"--"))
41
				{
42
					arg = arg.substr(2);
43
 
44
					if (arg.contains(L":"))
45
					{
46
						Utils::WString a = arg.token(L":", 1);
47
						Utils::WString b = arg.tokens(L":", 2);
48
						_options.pushBack(a, b);
49
					}
50
					else
51
						_options.pushBack(arg);
52
				}
53
				else
54
					_args.push_back(arg);
55
			}
309 cycrow 56
 
57
			if (_file->dir().empty() || !_file->dir().contains("/"))
58
			{
59
				Utils::WString d;
60
#ifdef _WIN32
61
				d = Utils::WString(_getcwd(NULL, 0));
62
#else
63
				d = Utils::WString(getcwd(NULL, 0));
64
#endif
65
				if (d.empty())
66
					d = L"./";
67
				_file->setDir(d);
68
			}
319 cycrow 69
 
70
			Utils::WString myDoc = L".";
71
#ifdef _WIN32
72
			TCHAR pszPath[MAX_PATH];
73
			if (SUCCEEDED(SHGetFolderPath(NULL, CSIDL_PERSONAL, NULL, SHGFP_TYPE_CURRENT, pszPath)))
74
			{
75
				myDoc = pszPath;
76
			}
77
#endif
78
			_myDoc.setDir(myDoc);
79
			_tempDir.setDir(L".");
80
 
81
#ifdef _WIN32
82
			TCHAR szPath[MAX_PATH + 1];
83
			DWORD result = GetTempPath(MAX_PATH + 1, szPath);
84
			if (result > 0)
85
				_tempDir.setDir(szPath);
86
#endif
87
 
88
			_p.startup(L".", _tempDir.dir(), _myDoc.dir());
303 cycrow 89
		}
90
 
91
		const std::shared_ptr<CFileIO> file() const
92
		{
93
			return _file;
94
		}
95
 
96
		const Utils::WString& cmdName() const
97
		{
98
			return _file->filename();
99
		}
100
 
101
		const Utils::WString& cmdDir() const
102
		{
103
			return _file->dir();
104
		}
105
 
319 cycrow 106
		const Utils::WString& tempDir() const
107
		{
108
			return _tempDir.dir();
109
		}
110
 
111
		const Utils::WString& myDoc() const
112
		{
113
			return _myDoc.dir();
114
		}
115
 
303 cycrow 116
		const Utils::WStringList options() const
117
		{
118
			return _options;
119
		}
120
 
121
		const std::vector<Utils::WString>& args() const
122
		{
123
			return _args;
124
		}
125
 
126
		size_t argCount() const
127
		{
128
			return _args.size();
129
		}
130
 
131
		Utils::WString arg(size_t i) const
132
		{
133
			if(i >= _args.size())
134
				return Utils::WString::Null();
135
			return _args[i];
136
		}
137
 
138
		bool hasSwitch(const Utils::WString& s) const
139
		{
140
			return _options.contains(s);
141
		}
142
 
143
		Utils::WString switchData(const Utils::WString& s) const
144
		{
145
			if (_options.contains(s))
146
				return _options[s]->data;
147
			return Utils::WString::Null();
148
		}
319 cycrow 149
 
150
		const CPackages& packages() const
151
		{
152
			return _p;
153
		}
154
 
155
		const CDirIO& dirIO() const
156
		{
157
			return _file->dirIO();
158
		}
324 cycrow 159
 
160
		Utils::WString fullFilename(const Utils::WString& s) const
161
		{
162
			return (s.contains(L":")) ? s : _file->dirIO().file(s);
163
		}
303 cycrow 164
	};
165
}