How can I open a url in a c++ program - url

I tried opening a url in my program but I keep getting errors from "shellapi.h", how can I fix it?
ShellExecuteA(NULL, NULL , "chrome.exe", this->photo.c_str(), NULL, SW_SHOWMAXIMIZED);

There are a number of c++ library available for this, and here you can find something.
I have used the following:
The Silicon C++ Web Framework -siliconframework.org
IXWebSocket - https://github.com/machinezone/IXWebSocket

Probably better to just use the default browser, like this:
// assumes photo.c_str() is valid URL ...
ShellExecuteA(0, NULL, photo.c_str(), NULL, NULL, SW_SHOWDEFAULT);

For a cross-platform solution, you can set this header:
#ifdef _WIN32
static int platform = 1;
#elif _WIN64
static int platform= 1;
#elif __linux__
static int platform = 2;
#elif __APPLE__
static int platform = 3;
#else
static int platform = 0;
#endif
and then create a method to open the URL, with the code below:
std::string str;
switch(platform) {
case 1:
str = "explorer";
break;
case 2:
str = "xdg-open";
break;
case 3:
str = "open";
break;
default:
std::cout << "Should never happen on the 3 defined platforms" << std::endl;
}
str.append(" " + url);
std::system(str.data());
Remember to provide a URL string, in this case a const std::string& url.

Related

`std::optional`: How to reduce redundant copies with rvalue reference and std::move()

In this code,
How many times would the generated string be copied for each case(case 1, case 2)?
Could anyone confirm my answers in the code body?
If so, can I tell case 2 is more efficient than case 1?
Or any other suggestions?
#include <optional>
#include <string>
std::optional<std::string> GenerateString() {
std::string s = "very long string";
return s;
}
class Message {
public:
Message(const std::string &s) : payload_{s} {}
Message(std::string &&s) : payload_{std::move(s)} {}
private:
std::string payload_;
};
int main() {
// case 1
const std::optional<std::string> &opt1 = GenerateString(); // no copy
const std::string &s1 = *opt1; // no copy
Message msg1{s1}; // copy: s1 -> payload_
// case 2
std::optional<std::string> &&opt2 = GenerateString(); // no copy
std::string &&s2 = std::move(*opt2); // no copy
Message msg2{std::move(s2)}; // no copy
return 0;
}

Is there a simple way to convert a lua table to a C++ array or vector?

I am starting to make my own package manager and am starting to develop a dependency system.
The builfiles are written in lua, they look something like this:
package = {
name = "pfetch",
version = "0.6.0",
source = "https://github.com/dylanaraps/pfetch/archive/0.6.0.tar.gz",
git = false
}
dependencies = {
"some_dep",
"some_dep2"
}
function install()
quantum_install("pfetch", false)
end
Only problem,I have no idea how to convert
dependencies = {
"some_dep",
"some_dep2"
}
To a global c++ array: ["some_dep", "some_dep2"]
Anything in the list that's not valid as a string should be ignored.
Any good way to do this?
Thanks in advance
Note: I am using the C api to interface with lua in C++. I don't know whether Lua's errors use longjmp or C++ exceptions.
Based on the clarification in your comment, something like this will work for you:
#include <iostream>
#include <string>
#include <vector>
#include <lua5.3/lua.hpp>
std::vector<std::string> dependencies;
static int q64795651_set_dependencies(lua_State *L) {
dependencies.clear();
lua_settop(L, 1);
for(lua_Integer i = 1; lua_geti(L, 1, i) != LUA_TNIL; ++i) {
size_t len;
const char *str = lua_tolstring(L, 2, &len);
if(str) {
dependencies.push_back(std::string{str, len});
}
lua_settop(L, 1);
}
return 0;
}
static int q64795651_print_dependencies(lua_State *) {
for(const auto &dep : dependencies) {
std::cout << dep << std::endl;
}
return 0;
}
static const luaL_Reg q64795651lib[] = {
{"set_dependencies", q64795651_set_dependencies},
{"print_dependencies", q64795651_print_dependencies},
{nullptr, nullptr}
};
extern "C"
int luaopen_q64795651(lua_State *L) {
luaL_newlib(L, q64795651lib);
return 1;
}
Demo:
$ g++ -fPIC -shared q64795651.cpp -o q64795651.so
$ lua5.3
Lua 5.3.3 Copyright (C) 1994-2016 Lua.org, PUC-Rio
> q64795651 = require('q64795651')
> dependencies = {
>> "some_dep",
>> "some_dep2"
>> }
> q64795651.set_dependencies(dependencies)
> q64795651.print_dependencies()
some_dep
some_dep2
>
One important pitfall: since you're not sure if Lua is compiled to use longjmp or exceptions for its errors, you need to make sure that you don't have any automatic variables with destructors anywhere that a Lua error could happen. (This is already the case in the code in my answer; just make sure you don't accidentally add any such places when incorporating this into your program.)

Clang-Tidy changes signature of c file, but not the associated header

I have this c file
#include "pointer.h"
int switcher(int * i) {
int a = *i;
switch (a) {
case 1:
return 0;
default:
return 1;
}
}
and the associated header contains only one line
int switcher(int * i);
This compiles using clang.
If I now use clang-tidy (clang-tidy -fix pointer.c -checks=* -header-filter=.* ) I get the following result
#include "pointer.h"
int switcher(const int * i) {
int a = *i;
switch (a) {
case 1:
return 0;
default:
return 1;
}
}
and the header
#ifndef _HOME_GWE_PROJEKTE_CLANG_TIDY_POINTER_H
#define _HOME_GWE_PROJEKTE_CLANG_TIDY_POINTER_H
int switcher(int * i);
#endif
The signature of the function has been changed form (int i) to (const inti) which is nice. The header file has also been altered (guards), but the signature stays the very same. Thus. the code does not compile any more.
My compile_commands.json looks like this
[
{
"directory": "/home/gwe/projekte/clang/tidy",
"command": "clang -c pointer.c -I.",
"file": "pointer.c"
}
]
Is this a clang-tidy bug or am I doing it wrong?
Thanks for your help?
Best regards, Georg
This bug is reported to llvm.org.
bugs.llvm.org//show_bug.cgi?id=33219

Trying to write to file using g_file_set_contents

I just want to use GNOME glib functions to simply write and read a file. I think my syntaxes are wrong in calling the functions. I tried to open a file with g_fopen("filenam.txt", "w"); but it didnt create any file. I also used g_file_set_contents and I am trying to save my Gstring s into a file file.txt with code as
static void events_handler(const uint8_t *pdu, uint16_t len, gpointer user_data)
{
uint8_t *opdu;
uint16_t handle, i, olen;
size_t plen;
//GString *s;
const gchar *s;
gssize length;
length = 100;
handle = get_le16(&pdu[1]);
switch (pdu[0]) {
case ATT_OP_HANDLE_NOTIFY:
s = g_string_new(NULL);
//g_string_printf(s, "Movement data = 0x%04x value: ",handle);
g_file_set_contents("file.txt", s, 100, NULL);
break;
case ATT_OP_HANDLE_IND:
s = g_string_new(NULL);
g_string_printf(s, "Indication handle = 0x%04x value: ",handle);
break;
default:
error("Invalid opcode\n");
return;
}
for (i = 3; i < len; i++)
g_string_append_printf(s, "%02x ", pdu[i]);
rl_printf("%s\n", s->str);
g_string_free(s, TRUE);
if (pdu[0] == ATT_OP_HANDLE_NOTIFY)
return;
opdu = g_attrib_get_buffer(attrib, &plen);
olen = enc_confirmation(opdu, plen);
if (olen > 0)
g_attrib_send(attrib, 0, opdu, olen, NULL, NULL, NULL);
}
You're conflating GString* and gchar*. The g_string_*() functions expect a GString*, and g_file_set_contents() expects gchar*. If you want the raw data, use the str field.
Also, I suggest turning on some more warnings on your compiler, since it really should be complaining during development if you try to do this. Passing -Wall should do the trickā€¦

Lua 'require' but files are only in memory

Setting: I'm using Lua from a C/C++ environment.
I have several lua files on disk. Those are read into memory and some more memory-only lua files become available during runtime. Think e.g. of an editor, with additional unsaved lua files.
So, I have a list<identifier, lua_file_content> in memory. Some of these files have require statements in them. When I try to load all these files to a lua instance (currently via lua_dostring) I get attempt to call global require (a nil value).
Is there a possibility to provide a require function, which replaces the old one and just uses the provided in memory files (those files are on the C side)?
Is there another way of allowing require in these files without having the required files on disk?
An example would be to load the lua stdlib from memory only without altering it. (This is actually my test case.)
Instead of replacing require, why not add a function to package.loaders? The code is nearly the same.
int my_loader(lua_State* state) {
// get the module name
const char* name = lua_tostring(state);
// find if you have such module loaded
if (mymodules.find(name) != mymodules.end())
{
luaL_loadbuffer(state, buffer, size, name);
// the chunk is now at the top of the stack
return 1;
}
// didn't find anything
return 0;
}
// When you load the lua state, insert this into package.loaders
http://www.lua.org/manual/5.1/manual.html#pdf-package.loaders
A pretty straightforward C++ function that would mimic require could be: (pseudocode)
int my_require(lua_State* state) {
// get the module name
const char* name = lua_tostring(state);
// find if you have such module loaded
if (mymodules.find(name) != mymodules.end())
luaL_loadbuffer(state, buffer, size, name);
// the chunk is now at the top of the stack
lua_call(state)
return 1;
}
Expose this function to Lua as require and you're good to go.
I'd also like to add that to completely mimic require's behaviour, you'd probably need to take care of package.loaded, to avoid the code to be loaded twice.
There is no package.loaders in lua 5.2
It called package.searchers now.
#include <stdio.h>
#include <string>
#include <lua.hpp>
std::string module_script;
int MyLoader(lua_State *L)
{
const char *name = luaL_checkstring(L, 1); // Module name
// std::string result = SearchScript(name); // Search your database.
std::string result = module_script; // Just for demo.
if( luaL_loadbuffer(L, result.c_str(), result.size(), name) )
{
printf("%s", lua_tostring(L, -1));
lua_pop(L, 1);
}
return 1;
}
void SetLoader(lua_State* L)
{
lua_register(L, "my_loader", MyLoader);
std::string str;
// str += "table.insert(package.loaders, 2, my_loader) \n"; // Older than lua v5.2
str += "table.insert(package.searchers, 2, my_loader) \n";
luaL_dostring(L, str.c_str());
}
void SetModule()
{
std::string str;
str += "print([[It is add.lua]]) \n";
str += "return { func = function() print([[message from add.lua]]) end } \n";
module_script=str;
}
void LoadMainScript(lua_State* L)
{
std::string str;
str += "dev = require [[add]] \n";
str += "print([[It is main.lua]]) \n";
str += "dev.func() \n";
if ( luaL_loadbuffer(L, str.c_str(), str.size(), "main") )
{
printf("%s", lua_tostring(L, -1));
lua_pop(L, 1);
return;
}
}
int main()
{
lua_State* L = luaL_newstate();
luaL_openlibs(L);
SetModule(L); // Write down module in memory. Lua not load it yet.
SetLoader(L);
LoadMainScript(L);
lua_pcall(L,0,0,0);
lua_close(L);
return 0;
}

Resources