How to do file inputs via node.js using emscripten? - node.js

I have a C++ project that I have converted into javascript using emscripten. I need help with implementing file input into the program via node. As I understand it the default file system in emscripten uses preloaded data that can only be done on a web page or web worker. I need mine to work with node.js on the command line.
Looking at the documentation I see that there's a way to use NODEFS instead of the default MEMFS which should allow me to do this. However, I'm unsure how I'm supposed to go about this. I don't really understand the test code that's provided.
Here's how the file handling is being done in the original C++ project:
void InputFile(std::string &fileName)
{
std::ifstream in(fileName);
if (in.fail())
{
std::cerr << "ERROR, Could not open " << fileName << std::endl;
exit(1);
}
}
But when I attempt to run the converted program with a file, node project.js -f test.file I get the error message: ERROR, Could not open test.file meaning that opening the file failed. The original C++ project was able to open the file without any issues, so I know there's not problem with the file itself.
I'm not sure what I have to do to make the converted project work with file inputs, any help would very much appreciated.

Explanation
WebAssembly module, built using emscripten, has no information about files in your physical file system. Instead, it uses a virtual file system. All you have to do is to create a link between files on your physical system to the files on the module's virtual system. NODEFS gives you this opportunity.
Quick solution
We will start at modifying your C++ code by adding the aforementioned link between physical and virtual file systems using embedded JS code (with EM_ASM). First (1), we create a directory '/temp' on the virtual file system where all referenced files will be located in. Then (2), we link this new virtual directory with a real physical location (the current working directory '.') where all the referenced files are already.
#include <emscripten.h>
#include <emscripten/bind.h>
#include <iostream>
#include <fstream>
void InputFile(const std::string &fileName)
{
EM_ASM(
FS.mkdir('/temp'); // (1)
FS.mount(NODEFS, {root : '.'}, '/temp');); // (2)
std::ifstream in(std::string("/temp/") + fileName);
if (in.fail())
{
std::cerr << "ERROR, Could not open " << fileName << std::endl;
exit(1);
}
}
EMSCRIPTEN_BINDINGS(Module)
{
emscripten::function("InputFile", &InputFile);
}
Now, because in the WebAssembly module, we are working with the virtual file systems, and not the physical one, each referenced file from the current directory (the root '.') is actually in the virtual directory previously linked ('/temp'). Hence, '/temp' directory precedes the name to the referenced file: std::ifstream in(std::string("/temp/") + fileName);.
Finally, we can compile this file. We force the synchronized compilation (to make sure the require loads the WASM module on time). Moreover, the option -s EXIT_RUNTIME=1 makes sure that the C++ command exit(1); finishes the execution. Also, we need to link Embind (--bind) and NODEFS (-lnodefs.js):
emcc project.cpp -o project.js -s WASM_ASYNC_COMPILATION=0 -s EXIT_RUNTIME=1 --bind -lnodefs.js
Testing
To test the WebAssembly module with the same calling convention as you have mentioned, we can use the following test.js script:
var Module = require('./project.js');
if (process.argv[3] && process.argv[2] === '-f') {
const filename = process.argv[3];
Module.InputFile(filename);
} else {
console.log('Pass the file with -f flag!');
}
To run the file, all you have to do is this: node test.js -f test.file
Comment
This approach works well if the referenced files are in the current working directory. In the case they are not, you could modify the code of the InputFile to extract the directory in which the fileName is, and then, mount the real-to-virtual directory accordingly.

Related

Write txt file in linux for .net Core (Docker)

I am new in Linux and my API was created in .net core and running in Docker. The system i create will write/create a txt file that will input all errors logged in the API. My code to write is this
`public class WriteLogs
{
public void ErrorLogFile(string traceNo, string errorMsg)
{
DirectoryInfo dir = new DirectoryInfo(Startup.errorPath);
if (!dir.Exists)
{
dir.Create();
}
using (StreamWriter swLog = File.AppendText(Startup.errorPath + Startup.errorFileName + DateTime.Now.ToString("MMddyyyy") + ".txt"))
{
swLog.WriteLine(DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss.fff") + " - Trace Number : " + traceNo + " " + errorMsg + "\n");
}
}
}`
the value in my startup is located in my appSettings.json file :
"ErrorPath": "C:\\BP\\",
"ErrorFileName": "BP-ParamLogs_",
This is working in windows environment but when i transfer my program to linux and change the ErrorPath to:
"ErrorPath": "/home/Logs/",
the file was not created.
My question is, do my syntax works in linux to write txt file or my path was wrong?
To answer your specific question about the syntax. \home\Logs\ should be /home/Logs/ as Linux uses forward-slash for path separator.
There's a chance that your program does not have write access to the /home/ directory. I've personally tried a similar program and I ran into System.UnauthorizedAccessException during the logs directory creation step.
Try running your program from the terminal with dotnet run to see the exception you might be getting. If you are also running into the System.UnauthorizedAccessException then run as root with sudo dotnet run
i just solved the problem by using command:
find . -name BP-Param*
from the first directory. After executing it. it was in the docker directory.

How to get NReco.PdfGenerator.LT working with Linux binaries?

I'm using NReco.PdfGenerator.LT to create a PDF document from a HTML page. This works fine on windows if I just point it at the wkhtmltopdf.exe executable, but this does not have the same effect on Linux.
HtmlToPdfConverter cvt = new HtmlToPdfConverter();
cvt.PdfToolPath = GetPdfToolPath();
// Windows: <current-dir>\wkhtmltopdf\win32\bin
// Linux: <current-dir>/wkhtmltopdf/linux/bin
cvt.WkHtmlToPdfExeName = GetPdfToolName();
// Windows: wkhtmltopdf.exe
// Linux: wkhtmltopdf
I just get this error:
Cannot generate PDF: Permission denied
Possible reasons of this error:
linux user who starts .net core program has no permissions to run "wkhtmltopdf". Also ensure that "wkhtmltopdf" file is marked as "executable" (x).
sometimes temp folder returned by Path.GetTempPath() is not accessible; another location for temp files may be specified with HtmlToPdfConverter.TempFilesPath property

Unable to load so file from Java in Eclipse On Ubuntu

I have some code that tries to load a C library as follows :-
public ThreadAffinity() {
ctest = (CTest) Native.loadLibrary("ctest", CTest.class);
}
However I get the following error when trying to build the project; The error I get is as follows :-
UnsatisfiedLinkError: Unable to load library 'libctest': liblibctest.so: cannot open shared object file: No such file or directory
at com.sun.jna.NativeLibrary.loadLibrary(NativeLibrary.java:166)
at com.sun.jna.NativeLibrary.getInstance(NativeLibrary.java:239)
at com.sun.jna.Library$Handler.<init>(Library.java:140)
at com.sun.jna.Native.loadLibrary(Native.java:393)
at com.sun.jna.Native.loadLibrary(Native.java:378)
at com.threads.ThreadAffinity.<init>(ThreadAffinity.java:11)
at com.threads.ThreadAffinity.main(ThreadAffinity.java:45)
The current working directory is the root of the project and thats where the so file is located. I also tried modifying the LD_PRELOAD variable to point to my so file; however the error persists.
It works just fine on my OSX where the dylib is located exactly where the so file is currently(project root).
What am I doing wrong?
From the exception:
UnsatisfiedLinkError: Unable to load library 'libctest': liblibctest.so: cannot open shared object file: No such file or directory
It implies you used something like:
public ThreadAffinity() {
ctest = (CTest) Native.loadLibrary("libctest", CTest.class);
}
and not:
public ThreadAffinity() {
ctest = (CTest) Native.loadLibrary("ctest", CTest.class);
}
hence you see the JNA added prefix of lib and postfix of .so added to libctest (liblibctest.so)
LD_PRELOAD is used when you want to prefer one particular version of the same shared library over another, which doesn't apply here.
Define jna.library.path to point to your project root, and JNA should be able to find it.
Also make sure your library has been built as libctest.so and wasn't inadvertently named libctest.dylib.

Which is the environment variable for Mono/C# library DLLs?

I'm running the frozen Debian 7.0 Testing/Wheezy.
Here is my C# sample code:
using System.Windows.Forms;
using System.Drawing;
public class Simple : Form
{
public Simple()
{
Text = "Simple";
Size = new Size(250, 200);
CenterToScreen();
}
static public void Main()
{
Application.Run(new Simple());
}
}
I got the above C# WinForms code sample working in Monodevelop by using the System.Drawing and System.Windows.Forms references as well as in the command line when compiling with the following command:
mcs /tmp/Simple.cs -r:/usr/lib/mono/4.0/System.Windows.Forms.dll \
-r:/usr/lib/mono/4.0/System.Drawing.dll
I'm trying to make the mcs command work without needing to use the -r switch/parameter (which, by the way, I cannot find information on by looking through man mcs - I basically found this switch/parameter on some random website and it worked).
To check if it worked temporarily, I issued
export PATH=$PATH:/usr/lib/mono/4.0/System.Windows.Forms.dll:/usr/lib/mono/4.0/System.Drawing.dll
prior to issuing mcs /tmp/Simple.cs, which failed with the errors within the following output:
deniz#debian:~$ cd /tmp
deniz#debian:/tmp$ export PATH=$PATH:/usr/lib/mono/4.0/System.Windows.Forms.dll:/usr/lib/mono/4.0/System.Drawing.dll
deniz#debian:/tmp$ mcs Simple.cs
Simple.cs(1,14): error CS0234: The type or namespace name `Windows' does not exist in the namespace `System'. Are you missing an assembly reference?
Simple.cs(2,14): error CS0234: The type or namespace name `Drawing' does not exist in the namespace `System'. Are you missing an assembly reference?
Compilation failed: 2 error(s), 0 warnings
deniz#debian:/tmp$
The above output tells me that the mcs compiler/utility is not seeing the dll files but I don't know what else to try.
Any help in getting the WinForms and Drawing libraries to be automatically “looked at” would be greatly appreciated!
I'm trying to make the mcs command work without needing to use the -r switch/parameter
This is not possible, mcs will not look for libraries unless you tell it to look for them (which is done with -r:...).

VC++ 2010 express: How to make a image file output to the Debug directory when compiling?

I'm creating a simple "hello world" vc++/opencv project.
In my code, I want to read and display an image called "opencv.png":
int _tmain(int argc, _TCHAR* argv[])
{
namedWindow( "show_image", WINDOW_AUTOSIZE );
Mat src = imread( "opencv.png" );
imshow( "show_image", src );
char c = waitKey(0);
return 0;
}
Then I put the "opencv.png" under the project root path. But I found when I compiling the project, the "opencv.png" won't be displayed.
In the "helloworld/Debug" directory, there are only 3 files:
helloworld.exe
helloworld.ilk
helloworld.pdb
I have to copy the "opencv.png" manually to "Debug". How to configure the project to let it copy the "opencv.png" to output dir when compiling?
Per James' answer, I think it's a little complicated. Is there any easier way, e.g. embed the "opencv.png" in the final exe file?
You can create a Custom Build Step to perform the copy.
Alternatively, consider placing the image in some known location so that it does not need to be copied (e.g. in a TestData directory), or pass the path to the image via the command line (you can add arguments to the command line in the Debugging page of the project properties).

Resources