Correcting case of Visual C++ __FILE__ macro - c++

Related to this question.
We have a code generate tool (for cross platform C++ source files) and I need to correct the path returned by the ____FILE____ macro to have the correct case so that source files generated under Windows will still compile on case sensitive systems (eg. Linux and OSX).
I'm using Qt and have tried a few combinations of QFileInfo and QDir but they all seem to keep the lowercase path passed to it.
Any suggestions?
Thanks

I have a few suggestions, but I am not sure if it will work.
If you have not already tried this. Use QDir to search through the directories for the correct path.
Say for instance that you are looking to include the file "C:/Programming/myProject/oneSource.cpp", then you do the following: Split the incoming path into "c:", "programming", "myproject", "onesource.cpp".
Then you use QDir do search through "C:" for a folder that (ignoring case) matches "programming", but use the path returned from QDir. Rinse and repeat. This way you are not really passing any wrong paths to QT it will simply use the one it reads natively.
Try the exact same thing, but use a different library like dirent or even the native windows API.

You know the filename in your tool, so there's no reason at all to use the __FILE__ macro. Just emit the correct file name as a string literal.

Related

getting include paths to work with Neovim and LSP-zero/Clangd

I'm currently Studying Computer enginering and taking embeded systems class, My isuse is that we use a custom library then compile it in a old version of Codewarrior.
how I would go about creating an include path for my lsp with nvim
I was woundering how I would go about creating an include path for my lsp with nvim, when I am not compiling the code localy but later compiling it with an old IDE
any wisdom would be apreciated.
note: in class we are required to use an exterior editor and the older version of code warrior is verry bad it is used for compiling for our micro controler but is unusable for writting code.
things I have done
I have atempted using compile_commands.json by coppying my vscode config for path location
I have tryed using a .clangd file with -I ...
I have tried other method but had no sucess so far
over all I was hopping to find a solution and have poured over the getting started page and stack overflow for several hours trying diffrent method to no avail.
The easiest approach is probably to use a .clangd file. Based on the path in your comment, the .clangd file should look like this:
CompileFlags:
Add: -I/home/bjc1269/Documents/github/libraries/lib/hc12c/include
A few things that I'm seeing in the .clangd file in your comment that don't work are:
Variable substitutions like ${workspaceFolder}. This is a VSCode feature that works in some VSCode settings like "clangd.arguments", but is not supported in a .clangd file, which is editor-agnostic (for example, it works with editors that don't have a concept of a "workspace").
Referring to your home directory as ~. Expanding ~ to /home/<username> is a feature of your shell. Command-line arguments specified in .clangd are passed directly to the compiler without being processed by the shell, so ~ will not work.
Globs like **. To be honest, I'm not even sure what the intended semantics for this could be in the context of specifying include directories.
Square brackets inside the argument to -I. Square brackets may appear in a .clangd file as YAML syntax for specifying multiple values in a list, for example you might have:
CompileFlags:
Add: [-I/path/to/directory1, -I/path/to/directory2]
But if you write -I=[/path/to/directory], the brackets just get passed on verbatim to the compiler, which does not understand this syntax.
First of all: Welcome to stackoverflow! :D
I'd recommend to use bear for this. You just simply invoke it with your build-command and the clangd LSP will read the includes automatically.

C++ : How to get actual folder path when the path has special folder names

I am trying to find a way to convert a path like this: "%APPDATA%\xyz\Logs\Archive" to this: "C:\Users\abcUser\AppData\Roaming\xyz\Logs\Archive".
I am on Windows platform. I use Unicode character set. I can use C++17 if required. I can use boost libraries if required.
In my search so far, I came across the SHGetKnownFolderPath() function. And there are StackOverflow references that explain how to resolve %APPDATA% to its actual path:
How do I get the application data path in Windows using C++?
C++ CreateDirectory() not working with APPDATA
The Win32 API that expands environment variable references of the form %variable% in strings is ExpandEnvironnmentStrings.

Does a file need an extension to be opened with open() in C/C++?

I'm writing a program using Xcode for school that requires we use the open() system call. I do
int input_file_desc = open(input_path, O_RDONLY);
printf("input file desc %d:\n", input_file_desc);
and it comes up with a -1. The file's path is ~/data_to_read. I set up the command line arguments in xcode. input_path is a const char * that i get from the command line. For some reason it works fine if I change the filename and command line argument to ~/data_to_read.txt. Let me know if more info is needed. thanks.
EDIT: I only tried it with .txt to see if that was the problem, but I still don't know why it needs an extension in the first place. You can have files without an extension right? In which case it should still work, as long as neither the file path nor the argument has an extension, right?
It looks like you are using C, why the C++ tag then? is it allowed? there are easy and good classes in C++ to manipulate files.
However you definitely need to include the extension in the file name (the input_path) , as many files with the same name and different extensions can exist in the same directory, so which one should be opened?
EDIT: it should be known that file extensions are (especially in UNIX-Like OS) only a "helping" thing, they are not really essential. For Example, you could have a file that contains a C++ code but has no .cpp extension, for example its name is foo only with no extensions (or even has a crazy extension like foo.bar). Still you could use the g++ to compile it, because the extension is not really important as long as the content is valid for the application that uses that file.
The way I understand your question, you're asking if a file needs to have an extension to be opened by the C open function, right? The answer to that is no. C does no magic for you, and will attempt to open a file with the exact filename you have specified. If the file has an extension, you must specify it in the api, if not you should not.
Check the error code returned in the errno variable (use strerror or perror to get a human readable error message) to find out what is wrong. That should point you in the right direction.
As other said the extension is simply part of the name.
The problem you are experiencing is probably because your OS is windows and the guys at microsoft had this dead stupid idea of "hiding" the extensions by default so your file seems to be named "test" while indeed it's named "test.txt" because it was created with notepad.
On windows systems is also common to see files named "foo.txt.txt" because this totally dumb idea of hiding/showing/guessing/automatically-adding extensions doesn't work well at all.
You can set the preferences on your computer so that file extension is always shown and this is the best thing to do on a windows system. Even better if you are interested in programming you may consider to switch for programming to an environment like linux that is less hostile to programmers.
A filename doesn't need an extension -- a file can be called "data_to_read". But most filenames on your system do have extensions, it's just that Windows Explorer hides them from you. To open a file from a program, you need to specify this extension.
Right-click on a file in Windows Explorer and select "Properties" to find out whether a filename has an extension. Or look at the files in a Windows Command Prompt console.

C, Linux, getcwd/chdir(): get binary path

I want to open a number of files (log4cxx configs, other logs etc) relative to binary's location.
Unfortunately, both getwd() and getcwd() are giving me the directory, from which I try to run binary at known path, instead of giving me the path where the binary is located (and where the data is).
How to get the app's path to use it with chdir()? Any methods besides argv[0] and without trying to parse /proc/$PID/ (that's not portable enough)?
Walk the PATH and find an executable of the same name as argv[0]?
However, it would probably be better to provide the user a way to configure where the data is. An env var or config file or CL parameter or something. It's very frustrating dealing with programs that try to be helpful but are actually just stupid.
This is exactly the kind of thing autoconf lives for, and supporting those standard directories is pretty much mandatory if you ever want anyone other than the programmers who wrote your software to use it. Once set up properly, to debug out of your home directory all you have to do is pass a different --prefix= value to configure.

what is wrong in my code getting current page path?

I have my visualstudio vcproj file at c:\vsproj\example\test\test.vcproj
under this path i have some other files like e test.cpp file and also a dll test.dll is there.
so totally under tha path c:\vsproj\example\test i have
1) test.vsproj 2) test.dll 3) test.cpp
normally to get the cuurent folder path we use ".\" so i have applied the technique to get
the dll path which is reside where the test.cpp file is there
now in test.cpp some where else i have written
string str= ".\\test.dll" to get the test.dll path. But i am not getting the dll path into the
my idea is i have to get the path in the variable str="c:\vsproj\example\test\test.dll"
but i am getting ".\test.dll" wat is wrong can u correct me??
variable str; how to get the dll path that in this scenario...
string str= ".\test.dll" to get the
test.dll path. But i am not getting
the dll path into the
my idea is i have to get the path in
the variable
str="c:\vsproj\example\test\test.dll"
but i am getting ".\test.dll" wat is
wrong can u correct me?? variable str;
how to get the dll path that in this
scenario...
It's doing exactly what you're asking it to do. What you appear to want it to do isn't going to be achieved this way.
You'll need to get the current working directory and prepend it to "\test.dll", rather than just specifying "\test.dll".
check with Filemon program. it shows you where the code is trying to search and show you where you do wrong..
String has nothing to do with paths. How could it understand what you want? It is just a mere collection of letters.
As others suggested, try using _getcwd and appending "\test.dll" at the end of it.
The function you're looking for is GetFullPathName(). It works on C strings, not C++ strings though. Have a look at the examples in the linked article.
(You can safely ignore the panicky bits about multi-threaded applications. The same problem actually exists for single-threaded app too. If the current directory changes, ..\xyz\ changes too.)
you can use _getcwd(char* buf, int len) to get the current working directory: