Terminal Command Output to array in CPP - c++

I have a folder and subfolder which contain many HTML files. I want to store all the html file paths to an array. I am using C++ and ubuntu.
I know a terminal command - find . -name *.html which gives me all the html file paths.
I want to use these paths to create PDF of these HTML files using WKHTMLTOPDF and threading. How to store these paths and use it?

You could brute-force it by using std::system
http://en.cppreference.com/w/cpp/utility/program/system
to execute your find command and use the output in a C++ program. Or read up on file system traversal in Steven's APUE ('Advanced Programming in the Unix Environment) and do it yourself. Begin with man 3 stat.

Related

How to get file path when opening a file from Finder

I have built a C++ cross-platform application and am struggling with how to get it to work correctly on macOS.
So far, I'm able to run the application manually with the command open /Applications/myApp.app --args /path/to/myFile.ply.
I have associated all ply files with my application but when I double click on it in the finder, the file path is not in argv argument list.
How can I get the double-clicked file path in my application?
You'll need an event loop, normally done using the NSApplicationMain function. Then you need to receive an Apple Event telling what file(s) to open. It will be much easier if you are willing to use some Objective-C or Swift, rather than pure C++. In Objective-C, you'd make an object that conforms to the NSApplicationDelegate protocol and that implements a method application:openURLs: or application:openFile:.
#JWWalker has explained how to solve your problem by altering your code to cope with the macOS GUI environment. Here is a different approach which avoids messing with your C++. Pick the one that suits you needs best.
If your cross-platform application is designed to run from the shell prompt you might want to run it within the macOS Terminal app. You can do this using a small AppleScript application which accepts the file paths passed by the Finder and invokes the Terminal app to run your C++ code.
To do this open Script Editor, you will find it inside Utilities in Applications. Enter the following:
on open passedItems
set convertedPaths to ""
# convert passed macOS paths to posix paths
repeat with nextItem in passedItems
set posixPath to the POSIX path of nextItem # convert macOS alias to posix path
set convertedPaths to convertedPaths & " '" & posixPath & "'" # place in quotes to protect any spaces
end repeat
tell application "Terminal" # open (if required) and activate Terminal
activate
do script "echo " & convertedPaths # just run echo - use the path to your C++ binary instead
end tell
end open
If you can program in C++ you can probably figure that out, if not search for AppleScript and all will become clear.
Now save this as an application, for this demo it was saved in /tmp/bridge (aka /private/tmp/bridge on macOS) as "Bridge.app".
Now create some test files with a suitable extension, e..g something like:
$ cd /tmp/bridge
$ touch a.bridgeDemo 'b c.bridgeDemo'
From Terminal you can open /tmp/bridge in a Finder window using:
$ open /tmp/bridge
In the Finder select any of your test files, do a Get Info and set the file to open with Bridge and then hit Change All...
Now try it: select the test files in the Finder and open them. You should see Terminal open/activate and show you something like:
$ echo '/private/tmp/bridge/a.bridgeDemo' '/private/tmp/bridge/b c.bridgeDemo'
/private/tmp/bridge/a.bridgeDemo /private/tmp/bridge/b c.bridgeDemo
$
Now edit the AppleScript to run your compiled C++ instead of echo and save it in a suitable location. HTH

C++ - get file path on Mac OSX

How would I get the full path to a specific file (on a system running OSX) once the file name is given by the user on a console application?
Any links or help would be great thanks.
You need to write a filesystem walker. C++ filesystem library will be of use. Start from your initial directory and start iterating. You can use DFS/BFS or your own custom algorithm
Another choice would be to simply see find commands source on Linux/Mac.
Edit 1: If you do not want to write a program then use terminal and fire following:
find / -name <whatever your filename is>

Regex to add an extension to a directory full of files

I am new to regular expressions.
I have many irregularly numbered ascii files with no extension: g000554, g000556, g000558, g000561, g000563 ... g001979 etc
I would like to type a regex at the terminal (or in a short script) to add a .dat to all of these files.
So I would like to change them to become: g000554.dat, g000556.dat, g000558.dat, g000561.dat, g000563.dat ... g001979.dat etc
p.s. Sorry I should have provided more info: by terminal I meant a mac terminal and I cannot use the 'rename' command.
I think you're using a linux system. So i provide a bash solution. It works only if your files starts with g and there is no other files in that directory except the files you want to rename.
for i in g*; do mv "$i" "$i.dat"; done
The below would add .dat extension to all the files present in the current directory,
for i in *; do mv "$i" "$i.dat"; done

How to generate a tags file for all C++ files in the other directory?

For example in Windows, I don't want to add the address of the ctags58\ folder of Exuberant Ctags to the Path of my Environment Variables. The only way to generate a tags file with (cmd) Windows Command Processor or Vim (text editor) is to set your current directory (cd) to ctags58 folder, and then now you can run the ctags command.
How to generate a tags file for all C++ files including ".h" files in the other directory such as "C:\Users\USER\Projects\PROJECT\include" address?
And also to generate tags file for all C++ files in the other directory and on all subdirectories?
You don't need to go to the ctags install directory; just invoke it with an absolute path, e.g. "C:\Program Files\ctags58\ctags.exe".
You can also extend the PATH just inside Vim (in your ~/.vimrc), so that you can invoke it from Vim with :!ctags ...:
:let $PATH .= ';C:\Program Files\ctags58'
For recursive tag generation, pass -R or --recurse to ctags.
Note that there are a couple of plugins on vim.org that make updating the tags database easier for you.

Can we list all the *.[c,h,S] files that are used by "make" command to build the linux kernel?

There are a lot of files in the kernel source that are not used most of the times. I wanted to list out only the files that are compiled when I issue a make command.
I thought that only those files that will be compiled are accessed during a make and hence, I tried the following command :
find . -type f -name *.[chS] -anewer Makefile
But I found that many files that are not a part of the required architecture are also being accessed. Please suggest a method in which I could list those filenames along with their path form the kernel source top directory.
Try make cscope and then check out the file list in the cscope.files plaintext file. You need the cscope tool installed on your system.