How to implement the ls "filename_[0-5][3-4]?" like class? The result I would like to store in the vector.
Currently I am using system() which is calling ls, but this is not portable under MS.
thanks,
Arman.
The following program lists files in the current directory whose name matches the regular expression filename_[0-5][34]:
#include <boost/filesystem.hpp>
#include <boost/regex.hpp> // also functional,iostream,iterator,string
namespace bfs = boost::filesystem;
struct match : public std::unary_function<bfs::directory_entry,bool> {
bool operator()(const bfs::directory_entry& d) const {
const std::string pat("filename_[0-5][34]");
std::string fn(d.filename());
return boost::regex_match(fn.begin(), fn.end(), boost::regex(pat));
}
};
int main(int argc, char* argv[])
{
transform_if(bfs::directory_iterator("."), bfs::directory_iterator(),
std::ostream_iterator<std::string>(std::cout, "\n"),
match(),
mem_fun_ref(&bfs::directory_entry::filename));
return 0;
}
I omitted the definition of transform_if() for brevity. It isn't a standard function but it should be straightforward to implement.
You can use boost::filesystem which has a portable way to retrieve files in a directory.
Then you can check the files against a regular expression with boost::regex for instance to only keep the ones that match your pattern.
The boost solution is portable, but not optimal on Windows. The reason is that it calls FindFirstFile("*.*") and thus returns everything. Given the globbing pattern, it would be more efficient to call FindFirstFile("filename_?*.*"). You'd still have to filter the results (using e.g. Boost::regex) but this would exclude many files that can't possibly match.
Also, using either method don't forget to filter out directories before doing the regex matching. The check whether an entry is a directory is quite cheap.
Related
I am modifying a Node native extension that is spawning native threads to do some processing. My issue is that I'd like to have the Javascript code provide a filter for the processing to exclude some data.
At this point, I'm passing a JS RegExp string from JS to C++, creating a std::regex instance from it, and passing it around the different structures down to the native thread logic.
My issue now is that despite std::regex using what seems to be the same syntax as ECMAScript regular expressions, the behavior is not the same :(
My original plan was to rely on V8's RegExp engine somehow but trigger the C++ bits directly instead of going from C++ to JS and back. But I wasn't able to find how to do this.
As example, see the following programs using the same regex but yielding different results:
#include <stdio.h>
#include <regex>
int main() {
std::regex re("^(?:(?:(?!(?:\\/|^)\\.).)*?\\/c)$");
std::smatch match;
std::string input("a.b/c");
int result = std::regex_match(input, match, re);
if (result == 1) {
printf("ok");
} else {
printf("nok");
}
return 0;
}
The equivalent JS code:
const re = new RegExp("^(?:(?:(?!(?:\\/|^)\\.).)*?\\/c)$");
const match = re.exec("a.b/c");
if (match) {
console.log("ok");
} else {
console.log("nok");
}
My question then is: What can I do to get the same results I would in JS but in C++? Is it possible to run V8's RegExp from a pure C++ context?
I have 4000 strings and I want to create a perfect hash table with these strings. The strings are known in advance, so my first idea was to use a series of if statements:
if (name=="aaa")
return 1;
else if (name=="bbb")
return 2;
.
.
.
// 4000th `if' statement
However, this would be very inefficient. Is there a better way?
gperf is a tool that does exactly that:
GNU gperf is a perfect hash function generator. For a given list of strings, it produces a hash function and hash table, in form of C or C++ code, for looking up a value depending on the input string. The hash function is perfect, which means that the hash table has no collisions, and the hash table lookup needs a single string comparison only.
According to the documentation, gperf is used to generate the reserved keyword recogniser for lexers in GNU C, GNU C++, GNU Java, GNU Pascal, GNU Modula 3, and GNU indent.
The way it works is described in GPERF: A Perfect Hash Function Generator by Douglas C. Schmidt.
Better later than never, I believe this now finally answers the OP question:
Simply use https://github.com/serge-sans-paille/frozen -- a Compile-time (constexpr) library of immutable containers for C++ (using "perfect hash" under the hood).
On my tests, it performed in pair with the famous GNU's gperf perfect hash C code generator.
On your pseudo-code terms:
#include <frozen/unordered_map.h>
#include <frozen/string.h>
constexpr frozen::unordered_map<frozen::string, int, 2> olaf = {
{"aaa", 1},
{"bbb", 2},
.
.
.
// 4000th element
};
return olaf.at(name);
Will respond in O(1) time rather than OP's O(n)
-- O(n) assuming the compiler wouldn't optimize your if chain, which it might do)
Since the question is still unanswered and I'm about to add the same functionality to my HFT platform, I'll share my inventory for Perfect Hash Algorithms in C++. It is harder than I thought to find an open, flexible and bug free implementation, so I'm sharing the ones I didn't drop yet:
The CMPH library, with a collection of papers and such algorithms -- https://git.code.sf.net/p/cmph/git
BBHash, one more implementation from a paper's author -- https://github.com/rizkg/BBHash
Ademakov's -- another implementation from the paper above -- https://github.com/ademakov/PHF
wahern/phf -- I'm currently inspecting this one and trying to solve some allocation bugs it has when dealing with C++ Strings on huge key sets -- https://github.com/wahern/phf.git
emphf -- seems unmantained -- https://github.com/ot/emphf.git
I believe #NPE's answer is very reasonable, and I doubt it is too much for your application as you seem to imply.
Consider the following example: suppose you have your "engine" logic (that is: your application's functionality) contained in a file called engine.hpp:
// this is engine.hpp
#pragma once
#include <iostream>
void standalone() {
std::cout << "called standalone" << std::endl;
}
struct Foo {
static void first() {
std::cout << "called Foo::first()" << std::endl;
}
static void second() {
std::cout << "called Foo::second()" << std::endl;
}
};
// other functions...
and suppose you want to dispatch the different functions based on the map:
"standalone" dispatches void standalone()
"first" dispatches Foo::first()
"second" dispatches Foo::second()
# other dispatch rules...
You can do that using the following gperf input file (I called it "lookups.gperf"):
%{
#include "engine.hpp"
struct CommandMap {
const char *name;
void (*dispatch) (void);
};
%}
%ignore-case
%language=C++
%define class-name Commands
%define lookup-function-name Lookup
struct CommandMap
%%
standalone, standalone
first, Foo::first
second, Foo::second
Then you can use gperf to create a lookups.hpp file using a simple command:
gperf -tCG lookups.gperf > lookups.hpp
Once I have that in place, the following main subroutine will dispatch commands based on what I type:
#include <iostream>
#include "engine.hpp" // this is my application engine
#include "lookups.hpp" // this is gperf's output
int main() {
std::string command;
while(std::cin >> command) {
auto match = Commands::Lookup(command.c_str(), command.size());
if(match) {
match->dispatch();
} else {
std::cerr << "invalid command" << std::endl;
}
}
}
Compile it:
g++ main.cpp -std=c++11
and run it:
$ ./a.out
standalone
called standalone
first
called Foo::first()
Second
called Foo::second()
SECOND
called Foo::second()
first
called Foo::first()
frst
invalid command
Notice that once you have generated lookups.hpp your application has no dependency whatsoever in gperf.
Disclaimer: I took inspiration for this example from this site.
I have a very large source code(>10,000lines) to change, with some function with fn_i_dont_want, I don't need to change, all other functions I must do some changes.So it's very hard to find such functions in such a large source code.
For example:
int foo_i_dont_want()
{
fn_i_dont_want()
}
int foo_i_want1()
{
fn()
fn1()
}
int foo_i_want2()
{
fn()
fn1()
....
}
I want vim to search all function I want, foo_i_want1, foo_i_want2, the condition is I don't want function with function call fn_i_dont_want.
I've tried to use vim regex like
/{\_.\{-}fn_i_dont_want\_.\{-}}
or
/{\_.*fn_i_dont_want\_.*}
But everything fails, it gets worse when there is some function like this:
int foo_i_dont_want()
{
struct bar = {0,0};
fn_i_dont_want();
}
and vim searches for everything in the pair of bracket{}.
So can somebody help me?
Parsing a complex programming language with regular expression is bound to be incomplete and problematic. Instead:
either use the refactoring capabilities of a real IDE, or
if you have to do this in Vim use :substitute with the confirm flag, and manually accept / decline each match position after inspecting it
Sorry if the title is misleading, I'm currently looking for solutions to the following:
I'm developing a library, for other people to use. They have to follow a strict design concept and the way they structure any additional features within the library. They all use Linux and (Vim) and as such as are allowed to use terminal commands (i.e to be able to compile etc..) and we all use clang as a compiler.
My question is this: Let's suppose I write a function called: "checkCode":
template<typename T>
void checkCode(T&& codeSnippet)
{
//// code
}
I want to make this function run so whenever they type "checkCode" in a terminal this function is therefore called. I know using clang thy have similar functionality, however, this is understandable as you're using the whole of clang. So:
1) Is it possible to just compile a class, and then access each of the functions through
the .dylab | .so file?
2) Might it be a better idea, or, better to take a copy of the source of clang, add this functionality and role it out to those using and contributing to the library? This would be like an additional add-on to clang?
Thanks
you could use one executable and symbolic links to it like busybox:
int main(int argc, char **argv)
{
string programName = argv[0];
size_t lastSlash = programName.find_last_of('/');
if(lastSlash != string::npos)
programName = programName.substr(lastSlash + 1);
if(programName == "function_1")
{
function_1();
return 0;
}
if(programName == "function_2")
{
function_2();
return 0;
}
// ...
// normal main code
return 0;
}
I am using the directory class to get this information but unable to assign this data to a data member of my own class. i am doing an oop project. Furthermore,I want to use the concept of Dynamism(containment).I have created two class, mydirectory and myfiles as under:
class files
{
string fname[25];
public:
files()
{
fname=NULL;
}
};
class directory
{ private:
directory *d;
string *dname[25]; //* to show there may be a subdirectory,there may be not.
files *ff[25]; // data member string *fname[25];
int numd,numf;
public:
directory()
{
numd=0;numf=0;
}
Now when if I want to use the statment:
Directory::GetDirectories("D:\\");
how can I assign the directory names to "dname" of directory class.
I dont want to include a third party software.
also i need help on the topic: how can a file (doc file/pdf/txt/pwt etc) can be opened from c++ code outside the console? I am very worried. please help me. thanks in advance.
I am new to c++ so please forgive if there are any errors in pointer handling, as I am doing this containment for the first time. I also need some reading stuff.
The simplest way to do it in C++ is using boost::filesystem.
As long as the path is a directory you can iterate over it using either a directory_iterator or a recursive_directory_iterator.
eg:
boost::filesystem::path dirname( "D:\\" );
std::vector<boost::filesystem::path> topLevel( directory_iterator(dirName),
directory_iterator() );
std::vector<boost::filesystem::path> wholeDrive(
recursive_directory_iterator(dirName), recursive_directory_iterator() );
As this is marked homework, we're not going to be helping you much by giving you the correct answer. But I will point you in the right direction.
You've indicated you're doing this under Visual C++. Well without using any third party libraries but just what's built in, you'll need to access the Win32 API.
FindFirstFile() & FindNextFile() are what you need.
You'll call FindFirstFile first off to obtain the directory handle.
The parameter is the D:\ that you're passing into your class.
Then call FindNextFile in a while loop.
e.g. The basic principle of using those API's is
HANDLE h = FindFirstFile("D:\\");
WIN32_FIND_DATA data;
while (FindNextFile(h, &data))
{
// Check if it's a directory or not
if (data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY){
// Add to dname
}
}
Consider using std::vector for your dname instead of string*
because you're stuck with 25 entries. Using vector it'll grow for you.
As said CashCow, boost::filesystem
As a general rule, in C++ for such examples, you don't need any pointer. Here a some mistakes you should correct:
string fname[25];
This declares an array of 25 strings. You probably wanted a string of 25 chars ? Well, in std::string, you don't need to care about the length. std::string fname; is enough
std::string file_name;
file_name = "baz.txt";
fname=NULL;
If fname is a string, then it's not a pointer. So you can't assign NULL to it. A std::string is by default initialized as an empty string. You can leave the whole constructor out.
string *dname[25]
I suppose you wanted to have an array of string. Just use :
std::vector<std::string> dnames;
dnames.push_back("foo");
dnames.push_back("bar"); // dnames now contains {"foo","bar"}
And you'll have a dynamically resizable vector of strings.
See : no need of any pointer. No need for any new
Finally I completed the short project.To get the list of files and sub directories, I made use of .NET Framework namespace "System".It has got classes like "FileInfo" and "DirectoryInfo"(both belong to System::IO) which do the above required task.But here,all the string related stuff is of System::String , not of std::string.To convert System::String to std::string, I used the following code(I got this conversion's code from a forum and it worked fine without any error):
string Str2str(String ^a)
{
array<Byte> ^chars = System::Text::Encoding::ASCII->GetBytes(a);
pin_ptr<Byte> charsPointer = &(chars[0]);
char *nativeCharsPointer = reinterpret_cast<char *>(static_cast<unsigned char *>(charsPointer));
string native(nativeCharsPointer, chars->Length);
return native;
}
Here is a short code for getting list of sub directories from a drive(D: drive is going to be searched):
#include<iostream>
#using<mscorlib.dll>
using namespace strd;
using namespace System;
using namespace System::IO;
int main()
{int count=50;
string name[count];//to hold directories names
string b;
int s=0;
DirectoryInfo^ di = gcnew DirectoryInfo("d:\\");
if(di->Exists)
{array<DirectoryInfo^>^diArr = di->GetDirectories();
Collections::IEnumerator^ myEnum = diArr->GetEnumerator();
while ( myEnum->MoveNext() )
{DirectoryInfo^ dri = safe_cast<DirectoryInfo^>(myEnum->Current);
String ^a=(dri->Name->ToString());
int n=b.size();
b=Str2str(a); `// code given in the starting`
if (s<count)
{name[s]=b;
s++;}
}
This involves Managed C++ knowledge. Visit these:
.NET Programming Guide
C++: The Most Powerful Language for .NET Framework Programming
I compiled this on Visual Studio 2008. I will be very grateful if you appriciate my effort.Further suggestions are most welcomed.