📄 cmdline.cpp
字号:
/* //////////////////////////////////////////////////////////////////////////// INTEL CORPORATION PROPRIETARY INFORMATION// This software is supplied under the terms of a license agreement or// nondisclosure agreement with Intel Corporation and may not be copied// or disclosed except in accordance with the terms of that agreement.// Copyright(c) 2002-2005 Intel Corporation. All Rights Reserved.//////*/#include <ctype.h>#include <string.h>#include <stdlib.h>#include <stdio.h>#include "cmdline.h"#include "genalg.h"inline bool IsKeyPrefix(char a) { return (a == '/' || a == '-'); }inline void ConcatenateArgs(char *argv[], int argc, StringA &cmdLine){ for(int i = 1; i < argc; i++) { cmdLine = cmdLine + argv[i]; cmdLine = cmdLine + " "; } cmdLine.CutTail(1);}inline void SkipSpaces (const char* &curr) { while(isspace(*curr)) curr++; }inline bool IsSpaceOrEnd(int value) { return (isspace(value) || (!value)); }inline unsigned int PassDigits(const char* &curr, StringA &str){ unsigned int n = 0; while(isdigit(*curr)) { str = str + (*curr); curr++; n++; } return n;}inline bool Parse(const char* &curr, bool &value){ switch(*curr) { case '+': value = true; break; case '-': value = false; break; default: return false; } curr++; return true;}inline bool Parse(const char* &curr, int &value){ StringA str; if(*curr == '+' || *curr == '-') { str = str + (*curr); curr++; } if(isspace(*curr)) return false; if(!PassDigits(curr, str)) return false; if(!IsSpaceOrEnd(*curr)) return false; value = atoi(str); return true;}inline bool Parse(const char* &curr, double &value){ StringA str; if(*curr == '+' || *curr == '-') { str = str + (*curr); curr++; } if(isspace(*curr)) return false; unsigned int nOfMantDigits = PassDigits(curr, str); if(*curr == '.') { str = str + (*curr); curr++; if(!(*curr)) goto convert; } nOfMantDigits += PassDigits(curr, str); if(!nOfMantDigits) return false; if(*curr == 'e' || *curr == 'E') { str = str + (*curr); curr++; if(*curr == '+' || *curr == '-') { str = str + (*curr); curr++; } if(!PassDigits(curr, str)) return false; } if(!IsSpaceOrEnd(*curr)) return false;convert: value = atof(str); return true;}inline void Parse(const char* &curr, StringA &str){ str.Clear(); while (*curr) { if(isspace(*curr) && IsKeyPrefix(curr[1])) break; str = str + (*curr); curr++; }}bool CommandLine::IsKeyExist(const char *str){ for(List<Key>::Iterator key = m_keys.ItrFront(); key != m_keys.ItrBackBound(); ++key) { if(!strncmp(key->m_name, str, key->m_name.Size())) return true; } return false;}bool CommandLine::AddKey(const char *key, const char *description, bool &value){ if(IsKeyExist(key)) return false; Key a(key, description, value); m_keys.PushBack(); m_keys.Back() = a; return true;}bool CommandLine::AddKey(const char *key, const char *description, int &value){ if(IsKeyExist(key)) return false; Key a(key, description, value); m_keys.PushBack(); m_keys.Back() = a; return true;}bool CommandLine::AddKey(const char *key, const char *description, double &value){ if(IsKeyExist(key)) return false; Key a(key, description, value); m_keys.PushBack(); m_keys.Back() = a; return true;}bool CommandLine::AddKey(const char *key, const char *description, StringA &value){ if(IsKeyExist(key)) return false; Key a(key, description, value); m_keys.PushBack(); m_keys.Back() = a; return true;}bool CommandLine::AddHelpKey(const char *key, const char *description){ if(IsKeyExist(key)) return false; Key a(key, description); m_keys.PushBack(); m_keys.Back() = a; return true;}CommandLine::Key* CommandLine::FindNextKey(const char *str){ Key* retKey = 0; for(List<Key>::Iterator key = m_keys.ItrFront(); key != m_keys.ItrBackBound(); ++key) { if((!key->m_present) && (!strncmp(key->m_name, str, key->m_name.Size()))) { retKey = key; break; } } return retKey;}bool CommandLine::Parse(char *argv[], int argc){ if(argc == 1) return true; StringA cmdLine; ConcatenateArgs(argv, argc, cmdLine); StringA str; const char *curr = cmdLine; while(IsKeyPrefix(*curr)) { curr++; Key* key = FindNextKey(curr); if(key) { key->m_present = true; curr += key->m_name.Size(); SkipSpaces(curr); if(*curr == '=') curr++; SkipSpaces(curr); bool result = true; switch(key->m_type) { case Key::Boolean: result = ::Parse(curr, *(key->m_bool )); break; case Key::Integer: result = ::Parse(curr, *(key->m_int )); break; case Key::Real: result = ::Parse(curr, *(key->m_double)); break; case Key::String: ::Parse(curr, *(key->m_string)); break; case Key::Help: HelpMessage(); break; } SkipSpaces(curr); if(!result) return false; } } if(*curr) return false; return true;}void CommandLine::HelpMessage(){ printf("Command line options:\n"); unsigned int maxSize = 0; List<Key>::Iterator key; for(key = m_keys.ItrFront(); key != m_keys.ItrBackBound(); ++key) { unsigned int size = key->m_name.Size(); if(size > maxSize) maxSize = size; } for(key = m_keys.ItrFront(); key != m_keys.ItrBackBound(); ++key) { printf(" -%s ", (const char*)key->m_name); for(unsigned int i = 0; i < maxSize - key->m_name.Size(); i++) printf(" "); printf("%s\n", (const char*)key->m_description); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -