Created
November 21, 2017 12:26
-
-
Save svenk/621e9da5dda9cb153ce97dae68f92cd9 to your computer and use it in GitHub Desktop.
ViewSourceWith.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* A Firefox Quantum ViewSourceWith replacement for Windows. | |
* When compiled, this behaves like an editor, i.e. you call | |
* it with a file as an argument. It tries to extract then | |
* the real file to open. | |
* | |
* Todo: | |
* | |
* 1) Read editor path from INI file with GetPrivateProfileString | |
* 2) hide the console when opening the real notepad | |
* | |
* On Windows, compile with mingw as | |
* | |
* g++ -mwindows -static resolver.c | |
* | |
* where -mwindows hides the opening console window, | |
* -static removes mingw dll dependencies. | |
* | |
* Check DLL dependencies as | |
* objdump -p a.exe | grep 'DLL Name' | |
* | |
* Written by SvenK at 2017-11-21. | |
**/ | |
#include <stdio.h> | |
#include <fstream> | |
#include <ctime> | |
#include <windows.h> | |
#include <string.h> | |
#include <tchar.h> | |
#include <cctype> | |
#include <sstream> | |
using namespace std; | |
/// string to lowercase | |
std::string lower(std::string& input) { | |
std::string data = input; | |
for(int i=0;i<data.length();i++) data[i]=tolower(data[i]); | |
//std::transform(data.begin(), data.end(), data.begin(), ::tolower); | |
return data; | |
} | |
// quick string contains | |
bool contains(std::string haystack, std::string needle) { | |
return haystack.find(needle) != std::string::npos; | |
} | |
std::string time_as_string() { | |
time_t rawtime; | |
struct tm * timeinfo; | |
char buffer[80]; | |
time (&rawtime); | |
timeinfo = localtime(&rawtime); | |
strftime(buffer,sizeof(buffer),"%d-%m-%Y %I:%M:%S",timeinfo); | |
std::string str(buffer); | |
return str; | |
} | |
// Quick and dirty | |
bool file_exists(const std::string& name) { | |
ifstream f(name.c_str()); | |
return f.good(); | |
} | |
// get path to this exe | |
std::string getexepath() | |
{ | |
char result[ MAX_PATH ]; | |
return std::string( result, GetModuleFileName( NULL, result, MAX_PATH ) ); | |
} | |
std::string dirnameOf(const std::string& fname) | |
{ | |
size_t pos = fname.find_last_of("\\/"); | |
return (std::string::npos == pos) | |
? "" | |
: fname.substr(0, pos); | |
} | |
int windows_system(const char *cmd) | |
{ | |
PROCESS_INFORMATION p_info; | |
STARTUPINFO s_info; | |
LPSTR cmdline, programpath; | |
memset(&s_info, 0, sizeof(s_info)); | |
memset(&p_info, 0, sizeof(p_info)); | |
s_info.cb = sizeof(s_info); | |
cmdline = _tcsdup(TEXT(cmd)); | |
programpath = _tcsdup(TEXT(cmd)); | |
if (CreateProcess(programpath, cmdline, NULL, NULL, 0, 0, NULL, NULL, &s_info, &p_info)) | |
{ | |
WaitForSingleObject(p_info.hProcess, INFINITE); | |
CloseHandle(p_info.hProcess); | |
CloseHandle(p_info.hThread); | |
} | |
} | |
void message(std::string msg){ | |
MessageBox( NULL, TEXT(msg.c_str()), TEXT("Resolver code"), MB_OK ); | |
} | |
void open_in_notebook(std::string filename) { | |
std::stringstream cmd; | |
cmd << "notepad "; | |
cmd << filename; | |
system(cmd.str().c_str()); | |
// Todo: Try to hide the console window spawned by system. | |
// these two doest do anything: | |
//windows_system(cmd.str().c_str()); | |
//WinExec(cmd.str().c_str(), SW_HIDE); | |
} | |
int main(int argc, char** argv) { | |
string exe = getexepath(); | |
string exedir = dirnameOf(exe); | |
std::string meta_name = "t29.localfile"; | |
// open file in current directory | |
ofstream log; | |
log.open((exedir + "\\last.log").c_str(), ios::trunc); | |
log << "Last job log\n"; | |
log << "Of: " << exe << "\n"; | |
log << "In: " << exedir << "\n"; | |
log << "At: "<<time_as_string() << "\n"; | |
log << "Called with argc=" << argc << " arguments\n"; | |
for(int i=0; i<argc; i++) { | |
log << "Argument "<<i<<": '"<<argv[i]<<"'\n"; | |
} | |
// try to open the given file input. | |
if(argc != 2) { | |
message("Usage: resolver <path to file>."); | |
return 1; | |
} | |
ifstream given; | |
const std::string filename = argv[1]; | |
given.open(filename.c_str()); | |
string line; | |
while(getline(given, line)) { | |
std::string lline = lower(line); | |
// poor man's HTML parsing. Assuming meta tags in a single line | |
if(contains(lline, "meta") && | |
contains(lline, "name") && | |
contains(lline, "content")) { | |
log << "Found meta line: " << line << "\n"; | |
if(contains(lline, meta_name.c_str())) { | |
int attrstart = line.find("content") + string("content='").length(); | |
int attrend = line.find_first_of("\"'", attrstart); | |
string localfile = line.substr(attrstart, attrend-attrstart); | |
log << "Found "<< meta_name << "='" << localfile << "'\n"; | |
if(!file_exists(localfile)) { | |
message("Indicated filename does not exist"); | |
} | |
open_in_notebook(localfile); | |
return 0; | |
} | |
} | |
} | |
message("Opening editor instead"); | |
open_in_notebook(filename); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment