[NEW_FEATURE] Add new possibility to launch the auto-typing text from command line:

-qnQuoterName
-qfQuoteFileNameFullPath
-qtQuoteText


git-svn-id: svn://svn.tuxfamily.org/svnroot/notepadplus/repository/trunk@1328 f5eea248-9336-0410-98b8-ebc06183d4e3
This commit is contained in:
Don Ho 2015-02-01 12:16:12 +00:00
parent 00f664188f
commit b09a7c17f3
8 changed files with 157 additions and 46 deletions

View File

@ -45,22 +45,65 @@ void printStr(const TCHAR *str2print)
std::string getFileContent(const TCHAR *file2read)
{
const size_t blockSize = 256;
const size_t blockSize = 1024;
char data[blockSize];
std::string wholeFileContent = "";
FILE *fp = generic_fopen(file2read, TEXT("rb"));
_fseeki64 (fp , 0 , SEEK_END);
unsigned __int64 fileSize =_ftelli64(fp);
rewind(fp);
size_t lenFile = 0;
do {
lenFile = fread(data, 1, blockSize - 1, fp);
if (lenFile <= 0) break;
size_t lenFile = fread(data, 1, blockSize, fp);
fclose(fp);
if (lenFile <= 0) return "";
if (fileSize >= blockSize)
if (lenFile >= blockSize - 1)
data[blockSize - 1] = '\0';
else
data[fileSize] = '\0';
return data;
data[lenFile - 1] = '\0';
wholeFileContent += data;
} while (lenFile > 0);
fclose(fp);
return wholeFileContent;
}
char getDriveLetter()
{
char drive = '\0';
TCHAR current[MAX_PATH];
::GetCurrentDirectory(MAX_PATH, current);
int driveNbr = ::PathGetDriveNumber(current);
if (driveNbr != -1)
drive = 'A' + char(driveNbr);
return drive;
}
generic_string relativeFilePathToFullFilePath(const TCHAR *relativeFilePath)
{
generic_string fullFilePathName = TEXT("");
TCHAR fullFileName[MAX_PATH];
BOOL isRelative = ::PathIsRelative(relativeFilePath);
if (isRelative)
{
::GetFullPathName(relativeFilePath, MAX_PATH, fullFileName, NULL);
fullFilePathName += fullFileName;
}
else
{
if ((relativeFilePath[0] == '\\' && relativeFilePath[1] != '\\') || relativeFilePath[0] == '/')
{
fullFilePathName += getDriveLetter();
fullFilePathName += ':';
}
fullFilePathName += relativeFilePath;
}
return fullFilePathName;
}
void writeFileContent(const TCHAR *file2write, const char *content2write)

View File

@ -109,6 +109,7 @@ bool isInList(const TCHAR *token, const TCHAR *list);
generic_string BuildMenuFileName(int filenameLen, unsigned int pos, const generic_string &filename);
std::string getFileContent(const TCHAR *file2read);
generic_string relativeFilePathToFullFilePath(const TCHAR *relativeFilePath);
void writeFileContent(const TCHAR *file2write, const char *content2write);
class WcharMbcsConvertor {

View File

@ -5994,6 +5994,7 @@ void Notepad_plus::showAllQuotes() const
}
/*
void Notepad_plus::showQuoteFromIndex(int index) const
{
if (index < 0 || index >= nbQuote) return;
@ -6008,6 +6009,27 @@ void Notepad_plus::showQuoteFromIndex(int index) const
HANDLE hThread = ::CreateThread(NULL, 0, threadTextPlayer, &params, 0, NULL);
::CloseHandle(hThread);
}
*/
void Notepad_plus::showQuoteFromIndex(int index) const
{
if (index < 0 || index >= nbQuote) return;
showQuote(quotes[index]._quote, quotes[index]._quoter, index < 20);
}
void Notepad_plus::showQuote(const char *quote, const char *quoter, bool doTrolling) const
{
static TextPlayerParams params;
params._nppHandle = Notepad_plus::_pPublicInterface->getHSelf();
params._text2display = quote;
params._quoter = quoter;
params._pCurrentView = _pEditView;
params._shouldBeTrolling = doTrolling;
HANDLE hThread = ::CreateThread(NULL, 0, threadTextPlayer, &params, 0, NULL);
::CloseHandle(hThread);
}
void Notepad_plus::launchDocumentBackupTask()
{

View File

@ -308,6 +308,7 @@ public:
void launchDocumentBackupTask();
int getQuoteIndexFrom(const char *quoter) const;
void showQuoteFromIndex(int index) const;
void showQuote(const char *quote, const char *quoter, bool doTrolling) const;
private:
Notepad_plus_Window *_pPublicInterface;

View File

@ -217,12 +217,31 @@ void Notepad_plus_Window::init(HINSTANCE hInst, HWND parent, const TCHAR *cmdLin
{
char dest[MAX_PATH];
wcstombs(dest, (cmdLineParams->_easterEggName).c_str(), sizeof(dest));
//::MessageBoxA(NULL, destStr.c_str(), "", MB_OK);
if (cmdLineParams->_quoteType == 0) // Easter Egg Name
{
int iQuote = _notepad_plus_plus_core.getQuoteIndexFrom(dest);
if (iQuote != -1)
{
_notepad_plus_plus_core.showQuoteFromIndex(iQuote);
}
}
else if (cmdLineParams->_quoteType == 1) // command line quote
{
_userQuote = dest;
_notepad_plus_plus_core.showQuote(_userQuote.c_str(), "Anonymous #999", false);
}
else if (cmdLineParams->_quoteType == 2) // content drom file
{
std::string destStr = dest;
generic_string fileName(destStr.begin(), destStr.end());
_userQuote = getFileContent(fileName.c_str());
if (_userQuote != "")
_notepad_plus_plus_core.showQuote(_userQuote.c_str(), "Anonymous #999", false);
}
}
if (cmdLineParams->_showLoadingTime)
{

View File

@ -98,6 +98,7 @@ private:
static const TCHAR _className[32];
bool _isPrelaunch;
bool _disablePluginsManager;
string _userQuote; // keep the availability of this string for thread using
};
#endif //NOTEPAD_PLUS_WINDOW_H

View File

@ -203,9 +203,11 @@ struct CmdLineParams {
LangType _langType;
generic_string _localizationPath;
generic_string _easterEggName;
unsigned char _quoteType;
CmdLineParams() : _isNoPlugin(false), _isReadOnly(false), _isNoSession(false), _isNoTab(false),_showLoadingTime(false),\
_isPreLaunch(false), _line2go(-1), _column2go(-1), _langType(L_EXTERNAL), _isPointXValid(false), _isPointYValid(false),\
_localizationPath(TEXT("")), _easterEggName(TEXT(""))
_localizationPath(TEXT("")), _easterEggName(TEXT("")), _quoteType(0)
{
_point.x = 0;
_point.y = 0;

View File

@ -35,17 +35,6 @@
typedef std::vector<const TCHAR*> ParamVector;
char getDriveLetter(){
char drive = '\0';
TCHAR current[MAX_PATH];
::GetCurrentDirectory(MAX_PATH, current);
int driveNbr = ::PathGetDriveNumber(current);
if (driveNbr != -1)
drive = 'A' + char(driveNbr);
return drive;
}
bool checkSingleFile(const TCHAR * commandLine) {
TCHAR fullpath[MAX_PATH];
@ -134,7 +123,8 @@ bool isInList(const TCHAR *token2Find, ParamVector & params) {
return false;
};
bool getParamVal(TCHAR c, ParamVector & params, generic_string & value) {
bool getParamVal(TCHAR c, ParamVector & params, generic_string & value)
{
value = TEXT("");
int nrItems = params.size();
@ -150,6 +140,26 @@ bool getParamVal(TCHAR c, ParamVector & params, generic_string & value) {
return false;
}
bool getParamValFromString(const TCHAR *str, ParamVector & params, generic_string & value)
{
value = TEXT("");
int nrItems = params.size();
for (int i = 0; i < nrItems; ++i)
{
const TCHAR * token = params.at(i);
generic_string tokenStr = token;
int pos = tokenStr.find(str);
if (pos != -1 && pos == 0)
{
value = (token + lstrlen(str));
params.erase(params.begin() + i);
return true;
}
}
return false;
}
LangType getLangTypeFromParam(ParamVector & params)
{
generic_string langStr;
@ -177,11 +187,36 @@ int getNumberFromParam(char paramName, ParamVector & params, bool & isParamePres
return generic_atoi(numStr.c_str());
};
generic_string getEasterEggNameFromParam(ParamVector & params)
generic_string getEasterEggNameFromParam(ParamVector & params, unsigned char & type)
{
generic_string EasterEggName;
if (!getParamVal('e', params, EasterEggName))
if (!getParamValFromString(TEXT("-qn"), params, EasterEggName)) // get internal easter egg
{
if (!getParamValFromString(TEXT("-qt"), params, EasterEggName)) // get user quote from cmdline argument
{
if (!getParamValFromString(TEXT("-qf"), params, EasterEggName)) // get user quote from a content of file
return TEXT("");
else
{
EasterEggName = relativeFilePathToFullFilePath(EasterEggName.c_str());
type = 2; // quote content in file
}
}
else
type = 1; // commandline quote
}
else
type = 0; // easter egg
generic_string percentTwentyStr = TEXT("%20");
generic_string spaceStr = TEXT(" ");
size_t start_pos = 0;
while ((start_pos = EasterEggName.find(percentTwentyStr, start_pos)) != std::string::npos)
{
EasterEggName.replace(start_pos, percentTwentyStr.length(), spaceStr);
start_pos += spaceStr.length(); // Handles case where 'to' is a substring of 'from'
}
return EasterEggName;
}
@ -233,7 +268,8 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE, LPSTR, int)
cmdLineParams._column2go = getNumberFromParam('c', params, isParamePresent);
cmdLineParams._point.x = getNumberFromParam('x', params, cmdLineParams._isPointXValid);
cmdLineParams._point.y = getNumberFromParam('y', params, cmdLineParams._isPointYValid);
cmdLineParams._easterEggName = getEasterEggNameFromParam(params);
cmdLineParams._easterEggName = getEasterEggNameFromParam(params, cmdLineParams._quoteType);
if (showHelp)
{
@ -270,7 +306,6 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE, LPSTR, int)
// tell the running instance the FULL path to the new files to load
size_t nrFilesToOpen = params.size();
const TCHAR * currentFile;
TCHAR fullFileName[MAX_PATH];
for(size_t i = 0; i < nrFilesToOpen; ++i)
{
@ -278,22 +313,9 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE, LPSTR, int)
if (currentFile[0])
{
//check if relative or full path. Relative paths dont have a colon for driveletter
BOOL isRelative = ::PathIsRelative(currentFile);
quotFileName += TEXT("\"");
if (isRelative)
{
::GetFullPathName(currentFile, MAX_PATH, fullFileName, NULL);
quotFileName += fullFileName;
}
else
{
if ((currentFile[0] == '\\' && currentFile[1] != '\\') || currentFile[0] == '/')
{
quotFileName += getDriveLetter();
quotFileName += ':';
}
quotFileName += currentFile;
}
quotFileName += relativeFilePathToFullFilePath(currentFile);
quotFileName += TEXT("\" ");
}
}