Esempio n. 1
0
// get filename
bool SettingLoader::getFilename(const tstringi &i_name, tstringi *o_path,
								int i_debugLevel) const
{
	// the default filename is ".mayu"
	const tstringi &name = i_name.empty() ? tstringi(_T(".mayu")) : i_name;

	bool isFirstTime = true;

	while (true) {
		// find file from registry
		if (i_name.empty()) {			// called not from 'include'
			Setting::Symbols symbols;
			if (getFilenameFromRegistry(NULL, o_path, &symbols)) {
				if (o_path->empty())
					// find file from home directory
				{
					HomeDirectories pathes;
					getHomeDirectories(&pathes);
					for (HomeDirectories::iterator
							i = pathes.begin(); i != pathes.end(); ++ i) {
						*o_path = *i + _T("\\") + name;
						if (isReadable(*o_path, i_debugLevel))
							goto add_symbols;
					}
					return false;
				} else {
					if (!isReadable(*o_path, i_debugLevel))
						return false;
				}
add_symbols:
				for (Setting::Symbols::iterator
						i = symbols.begin(); i != symbols.end(); ++ i)
					m_setting->m_symbols.insert(*i);
				return true;
			}
		}

		if (!isFirstTime)
			return false;

		// find file from home directory
		HomeDirectories pathes;
		getHomeDirectories(&pathes);
		for (HomeDirectories::iterator i = pathes.begin(); i != pathes.end(); ++ i) {
			*o_path = *i + _T("\\") + name;
			if (isReadable(*o_path, i_debugLevel))
				return true;
		}

		if (!i_name.empty())
			return false;				// called by 'include'

		if (!DialogBox(g_hInst, MAKEINTRESOURCE(IDD_DIALOG_setting),
					   NULL, dlgSetting_dlgProc))
			return false;
	}
}
Esempio n. 2
0
/// get class name and title name
static void getClassNameTitleName(HWND i_hwnd, bool i_isInMenu,
                                  tstringi *o_className,
                                  tstring *o_titleName)
{
    tstringi &className = *o_className;
    tstring &titleName = *o_titleName;

    bool isTheFirstTime = true;

    if (i_isInMenu) {
        className = titleName = _T("MENU");
        isTheFirstTime = false;
    }

    while (true) {
        _TCHAR buf[MAX(GANA_MAX_PATH, GANA_MAX_ATOM_LENGTH)];

        // get class name
        if (i_hwnd)
            GetClassName(i_hwnd, buf, NUMBER_OF(buf));
        else
            GetModuleFileName(GetModuleHandle(NULL), buf, NUMBER_OF(buf));
        buf[NUMBER_OF(buf) - 1] = _T('\0');
        if (isTheFirstTime)
            className = buf;
        else
            className = tstringi(buf) + _T(":") + className;

        // get title name
        if (i_hwnd) {
            GetWindowText(i_hwnd, buf, NUMBER_OF(buf));
            buf[NUMBER_OF(buf) - 1] = _T('\0');
            for (_TCHAR *b = buf; *b; ++ b)
                if (_istlead(*b) && b[1])
                    b ++;
                else if (_istcntrl(*b))
                    *b = _T('?');
        }
        if (isTheFirstTime)
            titleName = buf;
        else
            titleName = tstring(buf) + _T(":") + titleName;

        // next loop or exit
        if (!i_hwnd)
            break;
        i_hwnd = GetParent(i_hwnd);
        isTheFirstTime = false;
    }
}
Esempio n. 3
0
// get home directory path
void getHomeDirectories(HomeDirectories *o_pathes)
{
	tstringi filename;
#ifndef USE_INI
	if (getFilenameFromRegistry(NULL, &filename, NULL) &&
			!filename.empty()) {
		tregex getPath(_T("^(.*[/\\\\])[^/\\\\]*$"));
		tsmatch getPathResult;
		if (boost::regex_match(filename, getPathResult, getPath))
			o_pathes->push_back(getPathResult.str(1));
	}

	const _TCHAR *home = _tgetenv(_T("HOME"));
	if (home)
		o_pathes->push_back(home);

	const _TCHAR *homedrive = _tgetenv(_T("HOMEDRIVE"));
	const _TCHAR *homepath = _tgetenv(_T("HOMEPATH"));
	if (homedrive && homepath)
		o_pathes->push_back(tstringi(homedrive) + homepath);

	const _TCHAR *userprofile = _tgetenv(_T("USERPROFILE"));
	if (userprofile)
		o_pathes->push_back(userprofile);

	_TCHAR buf[GANA_MAX_PATH];
	DWORD len = GetCurrentDirectory(NUMBER_OF(buf), buf);
	if (0 < len && len < NUMBER_OF(buf))
		o_pathes->push_back(buf);
#else //USE_INI
	_TCHAR buf[GANA_MAX_PATH];
#endif //USE_INI

	if (GetModuleFileName(GetModuleHandle(NULL), buf, NUMBER_OF(buf)))
		o_pathes->push_back(pathRemoveFileSpec(buf));
}
Esempio n. 4
0
// get a parsed line.
// if no more lines exist, returns false
bool Parser::getLine(std::vector<Token> *o_tokens)
{
	o_tokens->clear();
	m_lineNumber = m_internalLineNumber;

	tstringi line;
	bool isTokenExist = false;
 continue_getLineLoop:
	while (getLine(&line))
	{
		const _TCHAR *t = line.c_str();

 continue_getTokenLoop:
		while (true)
		{
			// skip white space
			while (*t != _T('\0') && _istspace(*t))
				t ++;
			if (*t == _T('\0') || *t == _T('#'))
				goto break_getTokenLoop; // no more tokens exist
			if (*t == _T('\\') && *(t + 1) == _T('\0'))
				goto continue_getLineLoop; // continue to next line
      
			const _TCHAR *tokenStart = t;
      
			// comma or empty token
			if (*t == _T(','))
			{
				if (!isTokenExist)
					o_tokens->push_back(Token(_T(""), false));
				isTokenExist = false;
				o_tokens->push_back(Token(Token::Type_comma));
				t ++;
				goto continue_getTokenLoop;
			}

			// paren
			if (*t == _T('('))
			{
				o_tokens->push_back(Token(Token::Type_openParen));
				isTokenExist = false;
				t ++;
				goto continue_getTokenLoop;
			}
			if (*t == _T(')'))
			{
				if (!isTokenExist)
					o_tokens->push_back(Token(_T(""), false));
				isTokenExist = true;
				o_tokens->push_back(Token(Token::Type_closeParen));
				t ++;
				goto continue_getTokenLoop;
			}

			isTokenExist = true;
      
			// prefix
			if (m_prefixes)
				for (size_t i = 0; i < m_prefixes->size(); i ++)
					if (_tcsnicmp(tokenStart, m_prefixes->at(i).c_str(),
								  m_prefixes->at(i).size()) == 0)
					{
						o_tokens->push_back(Token(m_prefixes->at(i), false));
						t += m_prefixes->at(i).size();
						goto continue_getTokenLoop;
					}

			// quoted or regexp
			if (*t == _T('"') || *t == _T('\'') ||
				*t == _T('/') || (*t == _T('\\') && *(t + 1) == _T('m') &&
								  *(t + 2) != _T('\0')))
			{
				bool isRegexp = !(*t == _T('"') || *t == _T('\''));
				_TCHAR q[2] = { *t++, _T('\0') }; // quote character
				if (q[0] == _T('\\'))
				{
					t++;
					q[0] = *t++;
				}
				tokenStart = t;
	
				while (*t != _T('\0') && *t != q[0])
				{
					if (*t == _T('\\') && *(t + 1))
						t ++;
					if (_istlead(*t) && *(t + 1))
						t ++;
					t ++;
				}
	
				tstring str =
					interpretMetaCharacters(tokenStart, t - tokenStart, q, isRegexp);
#ifdef _MBCS
				if (isRegexp)
					str = guardRegexpFromMbcs(str.c_str());
#endif
				// concatinate continuous string
				if (!isRegexp &&
					0 < o_tokens->size() && o_tokens->back().isString() &&
					o_tokens->back().isQuoted())
					o_tokens->back().add(str);
				else
					o_tokens->push_back(Token(str, true, isRegexp));
				if (*t != _T('\0'))
					t ++;
				goto continue_getTokenLoop;
			}

			// not quoted
			{
				while (isSymbolChar(*t))
				{
					if (*t == _T('\\'))
						if (*(t + 1))
							t ++;
						else
							break;
					if (_istlead(*t) && *(t + 1))
						t ++;
					t ++;
				}
				if (t == tokenStart)
				{
					ErrorMessage e;
					e << _T("invalid character ");
#ifdef UNICODE
					e << _T("U+");
					e << std::hex; // << std::setw(4) << std::setfill(_T('0'));
					e << (int)(wchar_t)*t;
#else
					e << _T("\\x");
					e << std::hex; // << std::setw(2) << std::setfill(_T('0'));
					e << (int)(u_char)*t;
#endif
					e << std::dec;
					if (_istprint(*t))
						e << _T("(") << *t << _T(")");
					throw e;
				}
	
				_TCHAR *numEnd = NULL;
				long value = _tcstol(tokenStart, &numEnd, 0);
				if (tokenStart == numEnd)
				{
					tstring str = interpretMetaCharacters(tokenStart, t - tokenStart);
					o_tokens->push_back(Token(str, false));
				}
				else
				{
					o_tokens->push_back(
										Token(value, tstringi(tokenStart, numEnd - tokenStart)));
					t = numEnd;
				}
				goto continue_getTokenLoop;
			}
		}
 break_getTokenLoop:
		if (0 < o_tokens->size())
			break;
		m_lineNumber = m_internalLineNumber;
		isTokenExist = false;
	}
  
	return 0 < o_tokens->size();
}