I foud out there is a new Boost.Process 0.5 but I cant see how to execute across Windows Linux and Mac ping or echo.
I got it working at leaast on Windows with simple:
#include <string>
#include <iostream>
#include <boost/asio.hpp>
#include <boost/iostreams/device/file_descriptor.hpp>
#include <boost/iostreams/stream.hpp>
#include <boost/process.hpp>
#include <boost/filesystem/path.hpp>
#include <boost/system/error_code.hpp>
namespace bp = boost::process;
namespace bpi = boost::process::initializers;
namespace bio = boost::iostreams;
int main()
{
bp::pipe p = bp::create_pipe();
{
bio::file_descriptor_sink sink(p.sink, bio::close_handle);
boost::filesystem::path p("C:/Windows/System32/cmd.exe");
boost::system::error_code ec;
bp::execute(
bpi::run_exe(p),
bpi::set_cmd_line(L"cmd /c echo --echo-stderr hello"),
bpi::bind_stdout(sink),
bpi::set_on_error(ec)
);
}
bio::file_descriptor_source source(p.source, bio::close_handle);
bio::stream<bio::file_descriptor_source> is(source);
std::string s;
is >> s;
std::cout << s << std::endl;
std::cin.get();
return 0;
}
On windows this works correctly but how to make it crossplatform to work also on Mac and Linux? (I am stupid and do not know how to write one path that would work for any Unix terminal (or at least for Linux Bash and mac default one)) So How to run commandline/terminal utils with Boost.Process 0.5 on Windows and Unix like OSs (better not writing path to terminal each time but just writting app like echo or ping and its arguments)?
...Found related code inside prevoius version:
std::string exe;
std::vector<std::string> args;
#if defined(BOOST_POSIX_API)
exe = "/bin/sh";
args.push_back("sh");
args.push_back("-c");
args.push_back(command);
#elif defined(BOOST_WINDOWS_API)
char sysdir[MAX_PATH];
UINT size = ::GetSystemDirectoryA(sysdir, sizeof(sysdir));
if (!size)
boost::throw_exception(boost::system::system_error(boost::system::error_code(::GetLastError(), boost::system::get_system_category()), "boost::process::launch_shell: GetWindowsDirectory failed"));
BOOST_ASSERT(size < MAX_PATH);
exe = std::string(sysdir) + (sysdir[size - 1] != '\\' ? "\\cmd.exe" : "cmd.exe");
args.push_back("cmd");
args.push_back("/c");
args.push_back(command);
#endif
Under boost.process 0.5 the shell_path() API was introduced so might the following will hook you up
#if defined(BOOST_POSIX_API)
#define SHELL_COMMAND_PREFIX "-c"
#elif defined(BOOST_WINDOWS_API)
#define SHELL_COMMAND_PREFIX "/c"
#endif
filesystem::path shellPath = process::shell_path();
std::string cl = shell_path().string() + " " SHELL_COMMAND_PREFIX " ";
cl += "ping 127.0.0.1";
execute(
set_cmd_line(cl),
throw_on_error()
);
If you really want to hide the #ifdef, I'd go on and edit the boost sources to return also the relevant command prefix (adding new API) , open source after all isn't it ? :).
You could find the relevant sources to edit at boost/process/windows/shell_path.hpp and boost/process/posix/shell_path.hpp
Related
Previously, I have asked Qt translations in C++ project are rendered as question marks in cmd and powershell question. The point of the question was that the help message of .\app -h, translated to Cyrillic, is rendered as ?????????.
Since then, I discovered that in https://github.com/RSATom/Qt/blob/master/qtbase/src/corelib/tools/qcommandlineparser.cpp, the Qt messages are printed with qInfo("%ls", qUtf16Printable(message)) on Windows.
In cmd.exe or poweshell.exe I get question marks ?????????, when trying to output a Cyrillic message this way.
I tried to change the font of cmd.exe and powershell.exe to Lucida Console and to execute chcp 10000 (as proposed here UTF-16 on cmd.exe), but this does not help.
Here is the minimum reproducible example:
// > cl main.cpp /I C:\Qt\5.12.12\msvc2017_64\include
// > link main.obj /LIBPATH C:\Qt\5.12.12\msvc2017_64\lib\Qt5Core.lib
#include "QtCore/QtGlobal"
#include "QtCore/QString"
int main()
{
qInfo("%ls", qUtf16Printable("Привет"));
return 0;
}
// > main.exe
// ??????
I would really appreciate any help. There are two problems here. The first: how can I use QCommandLineParser under Windows. The second: if QCommandLineParser (using qInfo("%ls", qUtf16Printable(message))) is okay and not a bug, then how can I make cmd show it all right.
I don't pretend on the answer, but on Windows for Unicode console output you should do something similar to:
#include <QDebug>
#include <iostream>
#include <string>
#include <fcntl.h>
#include <io.h>
#include <stdio.h>
void myMessageOutput( QtMsgType type, const QMessageLogContext & context, const QString & msg )
{
Q_UNUSED( context );
std::wcout << msg.toStdWString() << std::endl;
if( type == QtFatalMsg )
abort();
}
int main()
{
_setmode( _fileno( stdout ), _O_U16TEXT );
qInstallMessageHandler( myMessageOutput );
const std::wstring rs = L"Привет";
const auto s = QString::fromStdWString( rs );
std::wcout << s.toStdWString() << std::endl;
qDebug() << s;
qInfo() << s;
return 0;
}
Accidentally I found the solution. You should create following manifest file for your executable:
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<assembly manifestVersion="1.0" xmlns="urn:schemas-microsoft-com:asm.v1">
<application>
<windowsSettings>
<activeCodePage xmlns="http://schemas.microsoft.com/SMI/2019/WindowsSettings">UTF-8</activeCodePage>
</windowsSettings>
</application>
</assembly>
Let it will be named as app.manifest. In CMake project just add app.manifest into sources of your executable.
In the main() function add two lines of code:
#include <Windows.h>
int main( int argc, char ** argv )
{
SetConsoleCP( GetACP() );
SetConsoleOutputCP( GetACP() );
}
And voila...
Printing should be done using UTF-8, so:
// 1 - works.
QTextStream s( stdout );
s << QString( "Вот оно!!!\n" ) << Qt::flush;
// 2 - works.
fputs( qPrintable( QString( "Опа-на...\n" ) ), stdout );
// 3 - works.
std::cout << u8"std::cout работает!!!\n";
// 4 - DOES NOT work.
std::wcout << L"wcout НЕ работает!!!\n";
I'm trying to get ru locale on OS without one.
I try to get it from icu using boost::locale::generator. Test program is simple I get locale (success) and then I check if russian letter is alphabetical character (fails). What am I doing wrong?
Here is the code:
#include <iostream>
#include <boost/locale.hpp>
#include <boost/locale/localization_backend.hpp>
using namespace std;
int main()
{
using namespace boost::locale;
localization_backend_manager my = localization_backend_manager::global();
my.select("icu");
boost::locale::generator gen(my);
std::locale loc( gen.generate( "ru_RU.UTF-8" ) );
if( !std::isalpha( L'ф', loc ) )
std::cout<< "Oops\n";
else
std::cout << "Ok\n";
return 0;
}
UPD:
Strangely, to_upper, to_lower, to_tiltle and fold_case works ok. Problem is only with is_alpha
UPD2:
Further research showed, that function that use global locale work ok, but functions that take locale as a param fail.
boost::locale::to_upper( str ) works fine, but boost::to_upper_copy( str, loc ) fails.
#include <Python.h>
#include <fstream>
#include <iostream>
#include <string>
#include <filesystem>
#include <sys/types.h>
#include <dirent.h>
static const char * sPythonCode =
"class Test :\n"
" def __init__(self) : \n"
" self.Disc_ = 0. \n"
" def getset(self) : \n"
" self.Disc_ = 7. \n"
" return self.Disc_ \n";
std::string writeFile()
{
static int iFile = 0;
std::string sFileName(std::string("test") + std::to_string(iFile));
std::ofstream out("py/" + sFileName + ".py");
out << sPythonCode;
out.flush();
out.close();
iFile++;
return sFileName;
}
static bool bPythonOpen = false;
#define PYTHONPATHLEN 501
static void _PyInit()
{
if (!Py_IsInitialized())
{
Py_InitializeEx(0);
}
}
void openPython(void)
{
if (!bPythonOpen)
{
const size_t szBufferN = 1000;
char acLoadPath[szBufferN];
const char *pypath = "./py";
_PyInit();
PyRun_SimpleString("import sys");
PyRun_SimpleString("print('python (%d.%d.%d) initialized' % (sys.version_info.major, sys.version_info.minor, sys.version_info.micro))");
PyRun_SimpleString("print('--------------------------')");
snprintf(acLoadPath, szBufferN, "sys.path.append('%s')", pypath);
PyRun_SimpleString(acLoadPath);
bPythonOpen = true;
}
}
PyObject *loadPythonModule(const char *acModule)
{
PyObject *pyModule = NULL;
if (bPythonOpen && acModule && strcmp(acModule, ""))
{
printf("%s\n", acModule);
pyModule = PyImport_ImportModule(acModule);
if (!pyModule)
{
PyErr_Print();
}
}
return pyModule;
}
void loadPython()
{
std::string sFileName = writeFile();
openPython();
//sleep(1);
PyObject *pPythonModule = loadPythonModule(sFileName.c_str());
if (pPythonModule)
PyDict_DelItemString(PyImport_GetModuleDict(), PyModule_GetName((PyObject *)pPythonModule));
}
int main(int argc, char **argv)
{
for (int i = 0; i < 10; i++)
{
loadPython();
}
}
My working env:
gcc version 8.3.1 20190311 (Red Hat 8.3.1-3) (GCC)
Red Hat Enterprise Linux Server release 7.6 (Maipo)
problem with python 3.6.10 / 3.8.3
Command to compile:
g++ pythontest.cpp -I/opt/python/python3.6.10/include/python3.6m -L/opt/python/python3.6.10/lib -lpython3.6m
create py directory:
mkdir py
When I run this code I have random error on different test file that I load.
Example of output:
python (3.6.10) initialized
--------------------------
test0
test1
test2
test3
ModuleNotFoundError: No module named 'test3'
test4
test5
ModuleNotFoundError: No module named 'test5'
test6
test7
ModuleNotFoundError: No module named 'test7'
test8
test9
ModuleNotFoundError: No module named 'test9'
Good to know:
If I uncomment the line with the sleep it works well
If I remove the iFile++, it works also as it used after an already created file
If I relaunch a second without rm -rf py directory it works also
If I erase file after each run in the loadPython function and remove iFile++ it works also
If I use strace to launch the executable I don't see the problem
For the moment it seems that the Python loader does not see the file on disk, however in case of failure if I print what I have in the directory thanks to dirent I see the testx.py
Please note that we reproduce the error on different Linux servers (not a hardware problem and even on Windows), with Python 2.7.x it works perfectly well.
You should call __import__('importlib').invalidate_caches() each time you modify modules folders to let C Python knows it must read directories again.
Good evening everyone, please I'm writing a Library management application in c++ using virtual studio 2012. I had already writing some codes using Dev c++ it worked but when I switched to visual studio it gives error. It involves creating folders and checking if the folders were actually created. That is using dir and mkdir.
Windows and Linux (POSIX) don't support the same API for most file system functions. You can use Microsoft's platform-specific APIs like CreateDirectory() or use the POSIX-like versions like _mkdir().
If you have a more recent C++ compiler / standard library, you can use the experimental filesystem library that is slated to become part of standard C++, perhaps as early as C++17. If not, you can use Boost.Filesystem from which the pre-standard experimental library was drawn.
Here's a complete, minimal example using Boost.Filesystem, which will work on both Windows and Linux without modification:
#include <iostream>
#include <boost/filesystem.hpp>
namespace fs = boost::filesystem;
int main()
{
if( !fs::exists( "my_dir" ) )
{
if( fs::create_directory( "my_dir" ) )
{
std::cout << "Directory created!\n";
}
}
}
See it run: Coliru.
Here's the same code but with std::experimental::filesystem: Coliru.
You would need the appropriate include and linker paths setup in your build system for either of these to work locally. The biggest "gotcha" using the filesystem is that it throws exceptions for a lot of errors by default. You can either setup try/catch blocks at the appropriate places or pass in an error code param to make it return the status there instead.
#include <stdio.h>
#include <windows.h>
int main() {
if (!CreateDirectoryA("C:\\ABC123", NULL))
{
if (GetLastError() == ERROR_ALREADY_EXISTS)
{
printf("Already Exists");
}else if (GetLastError()== ERROR_PATH_NOT_FOUND)
{
printf("Path not found");
}
}else{
printf("Created..");
}
}
simple function will do.
Thanks alot guys but I found this that solved my problem
#include <iostream>
#include <direct.h>
#include <stdlib.h>
#include <stdio.h>
using namespace std;
void main( void )
{
if( _mkdir( "\\testtmp" ) == 0 )
{
printf( "Directory '\\testtmp' was successfully created\n" );
system( "dir \\testtmp" );
if( _rmdir( "\\testtmp" ) == 0 )
printf( "Directory '\\testtmp' was successfully removed\n" );
else
printf( "Problem removing directory '\\testtmp'\n" );
}
else
printf( "Problem creating directory '\\testtmp'\n" );
int a;
cin >> a;
}
the cin >> a; is just to keep the output screen so I can see the result
I'm building tesseract 3.01 and magick++ into a program because the command line versions were too slow. getting everything compiled correctly took forever but finally I get no errors in my compiler, but as soon as I go to run my console app I get
PS C:\Users\sirus\Documents\Visual Studio 2013\Projects\ConsoleApplication4\Release> .\ConsoleApplication4.exe
first command
actual_tessdata_num_entries_ <= TESSDATA_NUM_ENTRIES:Error:Assert failed:in file ..\ccutil\tessdatamanager.cpp, line 48
PS C:\Users\sirus\Documents\Visual Studio 2013\Projects\ConsoleApplication4\Release> l
searching for this online shows that this is what happens when you have to incorrect language data, but I have tesseract 3.01 and the tesseract 3.01 language data downloaded directly from their google code site.
I'm using visual studio 2013 on windows 8.1 building 32 bit /MD
here is a sample of my source
#include <stdio.h>
#include <string>
#include <iostream>
#include <iomanip>
#include <locale>
#include <sstream>
#include <string>
#include <Magick++.h>
#include "baseapi.h"
#include "allheaders.h"
using namespace Magick;
using namespace std;
using namespace tesseract;
#define MaxRGB ((Magick::Quantum)65545)
tesseract::TessBaseAPI tess;
string cmd = "";
string cmd2 = "";
void img_split(){
string tesseract = "\"C:\\Program Files (x86)\\Tesseract-OCR\\tesseract.exe\" text/";
int rect_y = 141;
string del = "del /s /q text\\*";
system(del.c_str());
for (int x = 1; x < 40; x++){
rect_y += 19;
Image image;
image.read("source.jpg");
Magick::Image* mImage;
Image sub_image(image);
mImage = &sub_image;
//mImage->write("test.jpg");
sub_image.chop(Geometry(1481, rect_y));
sub_image.crop(Geometry(220, 17));
sub_image.quantizeColorSpace(GRAYColorspace);
sub_image.quantizeColors(2);
sub_image.quantizeDither(false);
sub_image.quantize();
sub_image.scale(Geometry(2200,170));
sub_image.antiAlias();
sub_image.compressType(Magick::NoCompression);
//const unsigned char* imagedata = (unsigned char*)mImage;
//tess.SetImage(imagedata, 2200, 170, 1, 17300);
//tess.Recognize(0);
//const char* out = tess.GetUTF8Text();
//cout << "\n\nno idea if this will work: " << out << endl;
sub_image.write("text/" + static_cast<ostringstream*>(&(ostringstream() << x))->str() + ".gif");
//cmd2 = tesseract + static_cast<ostringstream*>(&(ostringstream() << x))->str() + ".png text/" + static_cast<ostringstream*>(&(ostringstream() << x))->str();
//tesseract_ocr(x);
}
}
int main(int argc, char **argv)
{
cout << "first command\n";
//tess.Init(argv[0], "eng", tesseract::OEM_DEFAULT);
tess.Init(*argv, "eng");
tess.SetPageSegMode(tesseract::PSM_AUTO);
cout << "\ntest" << endl;
InitializeMagick(*argv);
//img_split();
return 0;
}
bonus points if you can tell me how to correctly pass the image in sub_image to tesseract's setimage function (because i'm pretty sure what I have wont be right either)
actual_tessdata_num_entries <= TESSDATA_NUM_ENTRIES:Error:Assert
failed:in file ..\ccutil\tessdatamanager.cpp, line 55
If you get error during running tesseract, please check if you use correct version of traineddata (e.g. 3.00 with 3.01). You can not use 3.01 traineddata with tesseract 3.00.
To sum it up you just need to get the traineddata for your version.. or don't even mess with the traineddata and use the ones that it comes with when you download it.
Check out this link below for the solution.
https://code.google.com/p/tesseract-ocr/wiki/FAQ#actual_tessdata_num_entries_<=_TESSDATA_NUM_ENTRIES:Error:Ass