📄 osutils.cxx
字号:
return 0;
}
PString PArgList::GetOptionString(char option, const char * dflt) const
{
return GetOptionStringByIndex(optionLetters.Find(option), dflt);
}
PString PArgList::GetOptionString(const char * option, const char * dflt) const
{
return GetOptionStringByIndex(optionNames.GetValuesIndex(PString(option)), dflt);
}
PString PArgList::GetOptionString(const PString & option, const char * dflt) const
{
return GetOptionStringByIndex(optionNames.GetValuesIndex(option), dflt);
}
PString PArgList::GetOptionStringByIndex(PINDEX idx, const char * dflt) const
{
if (idx < optionString.GetSize() && optionString.GetAt(idx) != NULL)
return optionString[idx];
if (dflt != NULL)
return dflt;
return PString();
}
PStringArray PArgList::GetParameters(PINDEX first, PINDEX last) const
{
PStringArray array;
last += shift;
if (last < 0)
return array;
if (last >= parameterIndex.GetSize())
last = parameterIndex.GetSize()-1;
first += shift;
if (first < 0)
first = 0;
if (first > last)
return array;
array.SetSize(last-first+1);
PINDEX idx = 0;
while (first <= last)
array[idx++] = argumentArray[parameterIndex[first++]];
return array;
}
PString PArgList::GetParameter(PINDEX num) const
{
int idx = shift+(int)num;
if (idx >= 0 && idx < (int)parameterIndex.GetSize())
return argumentArray[parameterIndex[idx]];
IllegalArgumentIndex(idx);
return PString();
}
void PArgList::Shift(int sh)
{
shift += sh;
if (shift < 0)
shift = 0;
else if (shift > (int)parameterIndex.GetSize())
shift = parameterIndex.GetSize() - 1;
}
void PArgList::IllegalArgumentIndex(PINDEX idx) const
{
PError << "attempt to access undefined argument at index "
<< idx << endl;
}
void PArgList::UnknownOption(const PString & option) const
{
PError << "unknown option \"" << option << "\"\n";
}
void PArgList::MissingArgument(const PString & option) const
{
PError << "option \"" << option << "\" requires argument\n";
}
#ifdef P_CONFIG_FILE
///////////////////////////////////////////////////////////////////////////////
// PConfigArgs
PConfigArgs::PConfigArgs(const PArgList & args)
: PArgList(args),
sectionName(config.GetDefaultSection()),
negationPrefix("no-")
{
}
PINDEX PConfigArgs::GetOptionCount(char option) const
{
PINDEX count;
if ((count = PArgList::GetOptionCount(option)) > 0)
return count;
PString stropt = CharToString(option);
if (stropt.IsEmpty())
return 0;
return GetOptionCount(stropt);
}
PINDEX PConfigArgs::GetOptionCount(const char * option) const
{
return GetOptionCount(PString(option));
}
PINDEX PConfigArgs::GetOptionCount(const PString & option) const
{
// if specified on the command line, use that option
PINDEX count = PArgList::GetOptionCount(option);
if (count > 0)
return count;
// if user has specified "no-option", then ignore config file
if (PArgList::GetOptionCount(negationPrefix + option) > 0)
return 0;
return config.HasKey(sectionName, option) ? 1 : 0;
}
PString PConfigArgs::GetOptionString(char option, const char * dflt) const
{
if (PArgList::GetOptionCount(option) > 0)
return PArgList::GetOptionString(option, dflt);
PString stropt = CharToString(option);
if (stropt.IsEmpty()) {
if (dflt != NULL)
return dflt;
return PString();
}
return GetOptionString(stropt, dflt);
}
PString PConfigArgs::GetOptionString(const char * option, const char * dflt) const
{
return GetOptionString(PString(option), dflt);
}
PString PConfigArgs::GetOptionString(const PString & option, const char * dflt) const
{
// if specified on the command line, use that option
if (PArgList::GetOptionCount(option) > 0)
return PArgList::GetOptionString(option, dflt);
// if user has specified "no-option", then ignore config file
if (PArgList::HasOption(negationPrefix + option)) {
if (dflt != NULL)
return dflt;
return PString();
}
return config.GetString(sectionName, option, dflt != NULL ? dflt : "");
}
void PConfigArgs::Save(const PString & saveOptionName)
{
if (PArgList::GetOptionCount(saveOptionName) == 0)
return;
config.DeleteSection(sectionName);
for (PINDEX i = 0; i < optionCount.GetSize(); i++) {
PString optionName = optionNames[i];
if (optionCount[i] > 0 && optionName != saveOptionName) {
if (optionString.GetAt(i) != NULL)
config.SetString(sectionName, optionName, optionString[i]);
else
config.SetBoolean(sectionName, optionName, PTrue);
}
}
}
PString PConfigArgs::CharToString(char ch) const
{
PINDEX index = optionLetters.Find(ch);
if (index == P_MAX_INDEX)
return PString();
if (optionNames.GetAt(index) == NULL)
return PString();
return optionNames[index];
}
#endif // P_CONFIG_ARGS
///////////////////////////////////////////////////////////////////////////////
// PProcess
PProcess * PProcessInstance;
int PProcess::p_argc;
char ** PProcess::p_argv;
char ** PProcess::p_envp;
typedef std::map<PString, PProcessStartup *> PProcessStartupList;
int PProcess::_main(void *)
{
Main();
return terminationValue;
}
void PProcess::PreInitialise(int c, char ** v, char ** e)
{
p_argc = c;
p_argv = v;
p_envp = e;
}
static PProcessStartupList & GetPProcessStartupList()
{
static PProcessStartupList list;
return list;
}
PProcess::PProcess(const char * manuf, const char * name,
WORD major, WORD minor, CodeStatus stat, WORD build)
: manufacturer(manuf), productName(name)
{
PProcessInstance = this;
terminationValue = 0;
majorVersion = major;
minorVersion = minor;
status = stat;
buildNumber = build;
#ifndef P_RTEMS
if (p_argv != 0 && p_argc > 0)
arguments.SetArgs(p_argc-1, p_argv+1);
else {
#if defined(_WIN32)
// Try to get the real image path for this process
#ifndef _WIN32_WCE
GetModuleFileName(GetModuleHandle(NULL), executableFile.GetPointer(1024), 1024);
#else
wchar_t wcsModuleName[1024];
if (GetModuleFileName(GetModuleHandle(NULL), wcsModuleName, 1024))
wcstombs(executableFile.GetPointer(1024), wcsModuleName, 1024);
#endif // ifndef _WIN32_WCE
executableFile.Replace("\\??\\","");
#endif // defined(_WIN32)
// Ok something went wrong, just use the default
if (executableFile.IsEmpty())
executableFile = PString(p_argv[0]);
if (!PFile::Exists(executableFile)) {
PString execFile = executableFile + ".exe";
if (PFile::Exists(execFile))
executableFile = execFile;
}
if (productName.IsEmpty())
productName = executableFile.GetTitle().ToLower();
}
#else // #ifndef P_RTEMS
cout << "Enter program arguments:\n";
arguments.ReadFrom(cin);
#endif
InitialiseProcessThread();
Construct();
#ifdef __MACOSX__
#ifdef HAS_VIDEO
PWLibStupidOSXHacks::loadFakeVideoStuff = 1;
#ifdef USE_SHM_VIDEO_DEVICES
PWLibStupidOSXHacks::loadShmVideoStuff = 1;
#endif // USE_SHM_VIDEO_DEVICES
#endif // HAS_VIDEO
#ifdef HAS_AUDIO
PWLibStupidOSXHacks::loadCoreAudioStuff = 1;
#endif // HAS_AUDIO
#endif // __MACOSX__
// create one instance of each class registered in the
// PProcessStartup abstract factory
PProcessStartupList & startups = GetPProcessStartupList();
{
PProcessStartup * levelSet = PFactory<PProcessStartup>::CreateInstance("SetTraceLevel");
if (levelSet != NULL)
levelSet->OnStartup();
PProcessStartupFactory::KeyList_T list = PProcessStartupFactory::GetKeyList();
PProcessStartupFactory::KeyList_T::const_iterator r;
for (r = list.begin(); r != list.end(); ++r) {
if (*r != "SetTraceLevel") {
PProcessStartup * instance = PProcessStartupFactory::CreateInstance(*r);
instance->OnStartup();
startups.insert(std::pair<PString, PProcessStartup *>(*r, instance));
}
}
}
#if PMEMORY_HEAP
// Now we start looking for memory leaks!
PMemoryHeap::SetIgnoreAllocations(PFalse);
#endif
}
void PProcess::PreShutdown()
{
PProcessStartupList & startups = GetPProcessStartupList();
for (PProcessStartupList::iterator startup = startups.begin(); startup != startups.end(); ++startup)
startup->second->OnShutdown();
}
void PProcess::PostShutdown()
{
PProcessStartupList & startups = GetPProcessStartupList();
for (PProcessStartupList::iterator startup = startups.begin(); startup != startups.end(); ++startup)
delete startup->second;
startups.clear();
}
PProcess & PProcess::Current()
{
if (PProcessInstance == NULL) {
cerr << "Catastrophic failure, PProcess::Current() = NULL!!\n";
#if defined(_MSC_VER) && defined(_DEBUG) && !defined(_WIN32_WCE)
__asm int 3;
#endif
_exit(1);
}
return *PProcessInstance;
}
void PProcess::OnThreadStart(PThread & /*thread*/)
{
}
void PProcess::OnThreadEnded(PThread & /*thread*/)
{
}
PBoolean PProcess::IsInitialised()
{
return PProcessInstance != NULL;
}
PObject::Comparison PProcess::Compare(const PObject & obj) const
{
PAssert(PIsDescendant(&obj, PProcess), PInvalidCast);
return productName.Compare(((const PProcess &)obj).productName);
}
void PProcess::Terminate()
{
#ifdef _WINDLL
FatalExit(terminationValue);
#else
exit(terminationValue);
#endif
}
PString PProcess::GetThreadName() const
{
return GetName();
}
void PProcess::SetThreadName(const PString & /*name*/)
{
}
PTime PProcess::GetStartTime() const
{
return programStartTime;
}
PString PProcess::GetVersion(PBoolean full) const
{
const char * const statusLetter[NumCodeStatuses] =
{ "alpha", "beta", "." };
return psprintf(full ? "%u.%u%s%u" : "%u.%u",
majorVersion, minorVersion, statusLetter[status], buildNumber);
}
void PProcess::SetConfigurationPath(const PString & path)
{
configurationPaths = path.Tokenise(";:", PFalse);
}
///////////////////////////////////////////////////////////////////////////////
bool PProcess::HostSystemURLHandlerInfo::RegisterTypes(const PString & _types, bool force)
{
PStringArray types(_types.Lines());
for (PINDEX i = 0; i < types.GetSize(); ++i) {
PString type = types[i];
HostSystemURLHandlerInfo handler(type);
handler.SetIcon("%base");
handler.SetCommand("open", "%exe %1");
if (!handler.CheckIfRegistered()) {
if (!force)
return false;
handler.Register();
}
}
return true;
}
void PProcess::HostSystemURLHandlerInfo::SetIcon(const PString & _icon)
{
#if _WIN32
PString icon(_icon);
PFilePath exe(PProcess::Current().GetFile());
icon.Replace("%exe", exe, true);
icon.Replace("%base", exe.GetFileName(), true);
iconFileName = icon;
#endif
}
PString PProcess::HostSystemURLHandlerInfo::GetIcon() const
{
#if _WIN32
return iconFileName;
#else
return PString();
#endif
}
void PProcess::HostSystemURLHandlerInfo::SetCommand(const PString & key, const PString & _cmd)
{
#if _WIN32
PString cmd(_cmd);
// do substitutions
PFilePath exe(PProcess::Current().GetFile());
cmd.Replace("%exe", "\"" + exe + "\"", true);
cmd.Replace("%1", "\"%1\"", true);
// save command
cmds.SetAt(key, cmd);
#endif
}
PString PProcess::HostSystemURLHandlerInfo::GetCommand(const PString & key) const
{
#if _WIN32
return cmds(key);
#else
return PString();
#endif
}
bool PProcess::HostSystemURLHandlerInfo::GetFromSystem()
{
#if _WIN32
if (type.IsEmpty())
return false;
// get icon file
{
RegistryKey key("HKEY_CLASSES_ROOT\\" + type + "\\DefaultIcon", RegistryKey::ReadOnly);
key.QueryValue("", iconFileName);
}
// enumerate the commands
{
PString keyRoot("HKEY_CLASSES_ROOT\\" + type + "\\");
RegistryKey key(keyRoot + "shell", RegistryKey::ReadOnly);
PString str;
for (PINDEX idx = 0; key.EnumKey(idx, str); ++idx) {
RegistryKey cmd(keyRoot + "shell\\" + str + "\\command", RegistryKey::ReadOnly);
PString value;
if (cmd.QueryValue("", value))
cmds.SetAt(str, value);
}
}
#endif
return true;
}
bool PProcess::HostSystemURLHandlerInfo::CheckIfRegistered()
{
#if _WIN32
// if no type information in system, definitely not registered
HostSystemURLHandlerInfo currentInfo(type);
if (!currentInfo.GetFromSystem())
return false;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -