Share your file-opener script - Programming On Unix

Users browsing this thread: 1 Guest(s)
jkl
Long time nixers
Almost on-topic: In a C++ software of mine (which I really should develop further... some day), I wrote a function to open a text file with the default editor. I would guess that, if one removes the VISUAL/EDITOR checks, it fulfills this thread’s needs as well.

Note that sanitizing the filename needs to be done beforehand. Better don’t make this function publicly callable. :)

Code:
inline bool openWithEditor(const char* filename) {
    // Opens <filename> in your default editor.
#ifdef _WIN32
    // Windows version, using the Windows API.
    return reinterpret_cast<int>(ShellExecuteA(NULL, "open", filename, NULL, NULL, SW_SHOW)) > 32;
#else
    // Other system, other ways...
    string editor = "";

    if (getenv("VISUAL") != NULL) {
        editor = getenv("VISUAL");
    }
    else if (getenv("EDITOR") != NULL) {
        editor = getenv("EDITOR");
    }
    else {
#ifdef __linux__
        editor = "xdg-open";
#else
        // No standard editor found. Sorry!
        return false;
#endif
    }

    return system(string(editor + " \"" + filename + "\"").c_str()) == 0;
#endif
}

--
<mort> choosing a terrible license just to be spiteful towards others is possibly the most tux0r thing I've ever seen


Messages In This Thread
Share your file-opener script - by seninha - 30-05-2020, 07:40 PM
RE: Share your file/link-opener script - by z3bra - 31-05-2020, 03:55 AM
RE: Share your file/link-opener script - by venam - 31-05-2020, 04:12 AM
RE: Share your file-opener script - by jkl - 10-02-2021, 03:40 PM
RE: Share your file-opener script - by neeasade - 13-02-2021, 12:16 PM
RE: Share your file-opener script - by venam - 07-03-2022, 02:47 AM