how to get char* filename from boost path - boost

for (fs::directory_iterator iter(realPath); iter != end_iter; ++iter )
{
if (iter->path().extension() == ".png"){
fs::path currentPath = iter->path();
const char *filename = const_cast<char*>(currentPath.string().c_str());
std::cout << iter->path().leaf() << std::endl;
processFile(filename);
std::cout <<"Hi" << std::endl;
}
}
This is my code and here processFile functions accepts the filename in char* format. Above code returns a junk value of filename. Don't know what is the best way to get the filename.

It works fine for me here (Boost.1.52, Mac OS X, clang).
Though I have to agree with Miguel, you're doing way to much work here.
for (fs::directory_iterator iter(realPath); iter != end_iter; ++iter )
if (iter->path().extension() == ".png") {
fs::path currentPath = iter->path();
std::cout << currentPath.leaf() << std::endl;
processFile(currentPath.c_str ());
}
and depending on your taste in code, you could get rid of currentPath altogether ( replace it with iter->path() )

Related

libzip: zip_name_locate() fails on specific filename, even trying all possible encoding combinations

I am trying to build a "failsafe" layer on top of libzip but libzip is giving me some trouble here.
First I add a file to my (empty) archive with zip_file_add(...). This has 3 possible user-defined encodings available. Then I try to locate the name with zip_name_locate(...) which also has 3 possible user-defined encodings available.
This mcve checks all possible encoding combinations and all of them fail for the specific filename x%²»Ã-ØÑ–6¨wx.txt. When using a more conventional file.txt filename, zip_name_locate() succeeds every time.
#include <zip.h>
#include <include/libzip.h>//<.pragmas to include the .lib's...
#include <iostream>
#include <vector>
#include <utility>
/*
'zip_file_add' possible encodings:
ZIP_FL_ENC_GUESS
ZIP_FL_ENC_UTF_8
ZIP_FL_ENC_CP437
'zip_name_locate' possible encodings:
ZIP_FL_ENC_RAW
ZIP_FL_ENC_GUESS
ZIP_FL_ENC_STRICT
*/
/*
build encoding pairs (trying all possibilities)
*/
std::vector<std::pair<unsigned, unsigned>>
encoding_pairs{
{ ZIP_FL_ENC_GUESS, ZIP_FL_ENC_RAW },
{ ZIP_FL_ENC_UTF_8, ZIP_FL_ENC_RAW },
{ ZIP_FL_ENC_CP437, ZIP_FL_ENC_RAW },
{ ZIP_FL_ENC_GUESS, ZIP_FL_ENC_GUESS },
{ ZIP_FL_ENC_UTF_8, ZIP_FL_ENC_GUESS },
{ ZIP_FL_ENC_CP437, ZIP_FL_ENC_GUESS },
{ ZIP_FL_ENC_GUESS, ZIP_FL_ENC_STRICT },
{ ZIP_FL_ENC_UTF_8, ZIP_FL_ENC_STRICT },
{ ZIP_FL_ENC_CP437, ZIP_FL_ENC_STRICT },
};
int main(int argc, char** argv) {
const char* file_buf = "hello world";
#if 0
const char* file_name = "file.txt";
#else
const char* file_name = "x%²»Ã-ØÑ–6¨wx.txt";
#endif
zip_error_t ze;
zip_error_init(&ze);
{
zip_source_t* zs = zip_source_buffer_create(nullptr, 0, 1, &ze);
if (zs == NULL)
return -1;
zip_t* z = zip_open_from_source(zs, ZIP_CHECKCONS, &ze);
if (z == NULL)
return -1;
{
zip_source_t* s = zip_source_buffer(z, file_buf, strlen(file_buf), 0);//0 = don't let libzip auto-free the const char* buffer on the stack
if (s == NULL)
return -1;
for (size_t ep = 0; ep < encoding_pairs.size(); ep++) {
std::cout << "ep = " << ep << std::endl;
zip_uint64_t index;
if ((index = zip_file_add(z, file_name, s, encoding_pairs[ep].first)) == -1) {
std::cout << "could not zip_file_add() with encoding " << encoding_pairs[ep].first << std::endl;
continue;
}
if (zip_name_locate(z, file_name, encoding_pairs[ep].second) == -1) {
std::cout << "the name '" << file_name << "' could not be located." << std::endl;
std::cout << " encoding pair: " << encoding_pairs[ep].first << " <-> " << encoding_pairs[ep].second << std::endl;
}
else {
std::cout << "the name was located." << std::endl;
}
if (zip_delete(z, index) == -1)
return -1;
}
}
zip_close(z);
}
zip_error_fini(&ze);
return 0;
}
I don't understand what I might be doing wrong here or if libzip just can't even resolve such a name.
If it can't then what would be the criteria on names to avoid ?
It turns out the problem was the encoding of my source file itself. It was ANSI - So I converted it to UTF8 and it solved the issue.
What I still don't understand is why libzip can't zip_name_locate() a name from an input c-string that is exactly the same as the input c-string used in zip_file_add() (whatever the source file encoding might be). "Lost in translation" perhaps ?
(Special thanks to Thomas Klausner for helping me find the issue).

How to get source location of #includes using clang libtooling?

Is there any way to get clang::SourceLocation for every #include in file by its clang::FileID or clang::FileEntry or something?
What about using source manager's GetIncludedLoc function which takes fileid as parameter.
SourceManager.GetIncludedLoc(fileid)
Thank's #Hemant for your answer, you're right
I Already found that by myself (in clang 3.8 it is called getIncludeLoc)
but forgot to write here.
I used this to find the location after all #includes where i can put my own.
Here's the function (For sure not the best way) I wrote for this, hope it helps someone
SourceLocation getIncludeLocation(FileID fileID, SourceManager &sm, unsigned carriages) {
return SourceLocation();
set<unsigned> lines;
if (fileID.isInvalid())
for (auto it = sm.fileinfo_begin(); it != sm.fileinfo_end(); it++) {
SourceLocation includeLoc = sm.getIncludeLoc(sm.translateFile(it->first));
if (includeLoc.isValid() && sm.isInFileID(includeLoc, fileID)) {
lines.insert(sm.getSpellingLineNumber(includeLoc));
}
}
unsigned pos(0);
if (!lines.empty()) {
bool first = true;
for (unsigned line :lines) {
if (first)
first = false;
else if ((line - pos) > carriages)
break;
pos = line;
//cout << "Include line:" << pos << endl;
}
//cout << console_hline('-') << endl;
}
cout << sm.getFileEntryForID(fileID)->getName() << endl;
return sm.translateFileLineCol(sm.getFileEntryForID(fileID), ++pos, 1);
}
Also some information about includes can be get by
Preprocessor::GetIncludeFilenameSpelling(SourceLocation Loc, StringRef &Buffer)
and
Lexer::ComputePreamble(StringRef Buffer, const LangOptions &LangOpts, unsigned MaxLines = 0)

FLTK windows in Mac OS X share the same system menu

When some application in OS X has multiple windows (many open documents, each in own window), it seems that all them share the same system menu, at least in FLTK. Is there a way to find the most recent selected window to send an event to it from the menu?
Here is my setup (Mac OS X 10.6.2, FLTK 1.3.3): have Shell class with system menu. Each time new document is opened, new Shell is created:
#ifdef __APPLE__
void Shell::macOpen(const char *fileName)
{
// If there are empty shell, open the model in it
if (s_empty != 0)
{
...
s_empty = 0;
}
// Otherwise, create new shell with the model
else
{
char *args[1];
args[0] = (char *) fileName;
new Shell(1, args, s_conf, s_dct, fileName, 1);
}
}
#endif
Then I keep track on the most recently selected Shell saving it into static Shell *Shell::s_current:
int Shell::handle(int event)
{
...
case FL_FOCUS:
#ifdef __APPLE__
// We just selected some shell, it is current.
s_current = this;
cout << "Select shell with address: [" << s_current << "]" << endl;
#endif
return 1;
...
}
This piece seems to work, as I can see the traces each time I select some Shell:
Select shell with address: [0x8206db0]
Select shell with address: [0x82375f0]
Select shell with address: [0x5d20650]
Select shell with address: [0x82375f0]
Now, given:
Shell *Shell::currentShell(Fl_Widget *w)
{
cout << "Widget address: [" << w << "]" << endl;
Shell *result = 0;
if (w != 0)
{
result = (Shell *) w->window();
cout << "Widget wingow address: [" << result << "]" << endl;
}
#ifdef __APPLE__
else
{
result = s_current;
cout << "Last selected shell address: [" << result << "]" << endl;
}
#endif
return result;
}
I have some callback:
void Shell::shortcutCB(Fl_Widget *w, void *data)
{
cout << "Shortcut" << endl;
Shell *ref = currentShell(w);
if (ref != 0)
{
...
}
}
When this callback is executed from the menu and more Shells are open, I get the following error:
Bus error
With no trace from either Shell::shortcutCB or Shell::currentShell. When the only Shell is open, all works perfectly. When more Shells are open and I close all but one, the error arises again. There is no problems when the same callback is called from some widget within Shell or is delivered from keyboard shortcut.
Solved the problem with the following 3 steps:
Declaring manu bar in OS X also static (here was the fail):
#ifdef __APPLE__
static Fl_Sys_Menu_Bar *s_menubar;
#else
Fl_Sys_Menu_Bar *m_menubar;
#endif
Save current Shell::s_current not only on FL_FOCUS event, but on any event handled by Shell, that is, each time it returns 1:
int Shell::handle(int event)
{
int result = 0;
switch (event)
{
// Set result = 1 when handling the event
...
}
#ifdef __APPLE__
if (result == 1)
{
// We just selected some shell, it is current.
s_current = this;
cout << "Select shell with address: [" << s_current << "]" << endl;
}
#endif
return result;
}
Use Shell::s_current for menu callbacks on OS X regardless of generating widget of the call:
Shell *Shell::currentShell(Fl_Widget *w)
{
Shell *result = 0;
#ifdef __APPLE__
result = s_current;
cout << "Last selected shell address: [" << result << "]" << endl;
#else
cout << "Widget address: [" << w << "]" << endl;
if (w != 0)
{
result = (Shell *) w->window();
cout << "Widget wingow address: [" << result << "]" << endl;
}
#endif
return result;
}

Accessing return by pointer out of function

I have the following function
std::tuple<int,val*>Socket::recv(val* values ) // const
{
char buf [ MAXRECV + 1 ];
memset ( buf, 0, MAXRECV + 1 );
int status = ::recv ( m_sock, buf, MAXRECV, 0 );
if ( status == -1 )
{
std::cout << "status == -1 errno == " << errno << " in Socket::recv\n";
// return std::make_tuple(0,NULL);//this is not working
}
else if ( status == 0 )
{
//return std::make_tuple(0,NULL); //this is not working
}
else
{
struct val* values=(struct val*) buf;
if(!std::isnan(values->val1) &&
!std::isnan(values->val2) &&
!std::isnan(values->val3) &&
!std::isnan(values->val4),
!std::isnan(values->val5),
!std::isnan(values->val6))
printf("received:%f %f %f %f %f %f\n", values->val1, values->val2,
values->val3, values->val4, values->val5, values->val6);
return std::make_tuple(status,values);
}
}
The received values are printed out in to standard output correctly within the function.
But when I try to access these received values out of the function by calling as follows what I get is all 0's.[after creating Socket rcvd object]
Would you tell me how to access these values outside the function?
1.
std::cout << std::get<1>(rcvd.recv(&values)->val1)
<< std::get<1>(rcvd.recv(&values)->val2)
<< std::get<1>(rcvd.recv(&values)->val3)
<< std::get<1>(rcvd.recv(&values)->val4)
<< std::get<1>(rcvd.recv(&values)->val5)
<< std::get<1>(rcvd.recv(&values)->val6)
<< std::endl;
2.
std::cout << std::get<1>(rcvd.recv(&values).val1)
<< std::get<1>(rcvd.recv(&values).val2)
<< std::get<1>(rcvd.recv(&values).val3)
<< std::get<1>(rcvd.recv(&values).val4)
<< std::get<1>(rcvd.recv(&values).val5)
<< std::get<1>(rcvd.recv(&values).val6)
<< std::endl;
3.
std::cout << std::get<1>(rcvd.recv(&values)[0])
<< std::get<1>(rcvd.recv(&values)[1])
<< std::get<1>(rcvd.recv(&values)[2])
<< std::get<1>(rcvd.recv(&values)[3])
<< std::get<1>(rcvd.recv(&values)[4])
<< std::get<1>(rcvd.recv(&values)[5])
<< std::endl;
where "values" comes from
struct val {
val1;
val2;
val3;
val4;
val5;
val6;} values;
All the three options of calling the function or access the struct val could not work for me.
Would you tell me
how to access these received values externally from any function?
how to return zero to struct pointer [NULL is not working ] when status is 0 or -1
Try
return std::make_tuple<int, val*>(0, nullptr);
The type of tuple is deduced from arguments, so by using 0,NULL you are actually using the null constant wich is evaluted to 0 and hence deduced type is <int,int>.
By the way, I see no reason for using NULL in C++11, if you need that really for some reason then cast NULL to val*
static_cast<val*>(NULL);
EDIT:
Other viable alternatives are
val* nullval = nullptr;
return std::make_tuple(0, nullval);
Or
return std::make_tuple(0, static_cast<val*>(nullptr));
Or (as comment suggest)
return {0, nullptr};
Choose the one that seems more clear to you.
You are lucky that the outside function is printing zeroes. It might have as well just dumped the core on you :)
What you are doing is accessing a buffer, that was created on a stack, after that stack was released (once the function's execution finished). That is HIGHLY UNSAFE and, pretty much, illegal.
Instead what you should do is allocate your data buffer in a 'free memory", using functions like malloc (in C) or operator new/new[] (in C++).
The quick fix is to replace the line
char buf [ MAXRECV + 1 ];
with
char * buf = new char [ MAXRECV + 1 ];
And when you do a type casting on line
struct val* values=(struct val*) buf;
you really ought to be sure that what you do is correct. If the sizeof() of you struct val is more than the sizeof(char[MAXRECV + 1]) you'll get in memory access troubles.
After you are done using the returned data buffer don't forget to release it with a call to free (in C) or delete/delete[] (in C++). Otherwise you'd have what is called a memory leak.

How to do a Stack of unique_ptr

Imagine you have a stack of unique_ptr to something (an int to simplify), like:
std::stack< std::unique_ptr<int> > numbers;
numbers.push( std::unique_ptr<int>( new int(42)) );
But if you try to use the top element, without getting it from the stack, you will get an compile error:
if( not numbers.empty() ){
auto lastone = numbers.top();
std::cout << "last " << *lastone << std::endl;
}
You should move out the element, use it, and then put again in the stack:
if( not numbers.empty() ){
auto lastone = std::move(numbers.top());
numbers.pop();
std::cout << "last " << *lastone << std::endl;
numbers.push( std::move(lastone) );
}
Is there a better way to do this?
If you don't intend to actually pop the element but just want to use it inplace, just use a reference:
auto& lastone = numbers.top();
std::cout << "last " << *lastone << std::endl;

Resources