Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- ** Open a URL from C++ Program - multiplatform
- ** Abrir uma url a partir de um programa em c++ (console application)
- ** Tested in linux and windows
- */
- #include <fstream> //Read and write from a file
- #include <string.h> //strlen
- #include <iostream>
- using namespace std;
- bool openFile(ifstream &ifs, const std::string& name);
- bool checkFileError(string pathFileErrLog);
- bool openUrl(string url);
- void checkPlatform();
- int main() {
- bool r;
- string myUrl = "http://www.travele.com.br";
- checkPlatform();
- cout << "Opening " << myUrl;
- r = openUrl(myUrl);
- if(r)
- cout << ": Success";
- else
- cout << ": Error";
- return 0;
- }
- // Open a file by path and check the success of the operation
- bool openFile(ifstream &ifs, const std::string& name) {
- ifs.open(name.c_str());
- return ifs.is_open();
- }
- //Check error in the command execution
- bool checkFileError(string pathFileErrLog) {
- ifstream file;
- string line;
- openFile(file, pathFileErrLog);
- getline(file, line);
- //cout << "\nline: " << line;
- file.close();
- if(strlen(line.c_str()) > 0)
- return false;
- return true;
- }
- //Open an url in the browser
- bool openUrl(string url) {
- string pathFileErrLog = "./cmd_err.log", cmd;
- string arrCmd[] = {"CmdErrorTEST", "start", "google-chrome", "firefox"}; //enything else ? put here
- for(int i=0; i<sizeof(arrCmd)/sizeof(arrCmd[0]); i++) {
- cmd = arrCmd[i];
- cmd.append(" "); cmd.append(url); cmd.append(" 2>"); cmd.append(pathFileErrLog);
- system(cmd.c_str());
- if(checkFileError(pathFileErrLog))
- return true;
- }
- return false;
- }
- //Check platform where we are in
- void checkPlatform() {
- #ifdef __linux__
- cout << "we're in a linux platform, beautiful!\n\n";
- #else
- cout << "Are you walking in the dark side ?\n\n";
- //Windows fans don't stay angry with me, okay? It is just a joke.
- #endif
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement