What is the easiest way to parse an INI File in C? - c

There are some similar questions about C++, Java and C# so now my question is about C. If I have this config file
[BBDD]
user=**
password=***
database=***
IPServidor=*
port=3***
[Device]
dev=8
Temperatura=5=1001
Humedad=7=1002
Link=8=1003
Volt=9=1004
[Device]
dev=10
Temperatura=5=1012
Humedad=7=1013
Link=8=1014
Volt=9=1015
what is the best way to read the values of Device. I am a linux user. I used glib but I had some problems because there is the same key (Device) so it returns me as the tutorial says only the values of the last Device array. Also Boost as I know has libraries for C++, libconfig also I think is not used for this kind of config files. Finally iniparser has a difficult installation guide for me. Do you think that some solutions like sscanf, fprintf are good?

Finally iniparser has a difficult installation guide for me. Do you think that some solutions like sscanf, fprintf are good?
The iniparser may have a difficult installation, but that's a small tradeoff for code that already works, has already been tested, and handles cases that you haven't thought of.

What problems are you having with using iniparser? I just tried it. I first did make in the iniparser directory, and the code was built. To use the library, I did the following:
gcc test.c ./libiniparser.a
This was because I had created the test program in the same directory as the library. When you include iniparser.h in C++, make sure to do the following:
extern "C"
{
#include "src/iniparser.h"
}

Related

Using 3rd Party C files with C Lion, a beginner perspective

I am in despair for a simple explanation to a simple problem.
I made a program in java that I need to recode in C for performance reasons. So I learned how to program in C. The problem is that C standard libraries do not contain collections (why????) such as a hashtables, treesets, etc. So I found this: https://github.com/srdja/Collections-C.
I use CLion on windows, I know well about coding but NOTHING about compiling, CMake, Linux, etc. My question is: I want to use those external source files my project, why is that so hard ? The tutorial on the link provided above tells me to use Linux command lines and stuff that I don't understand. Online I find stuff about telling me to add commands into CMakelist, none of these work for diverse reasons. I can't even copy all the .c and .h into my project because "they are not part of the project". So can anyone tell me how to make this simple code work ?
#include <stdio.h>
#include "hashtable.h"
int main() {
Hashtable *table;
hashtable_new(&table); //this is a function that creates the new hashtable in the source code of Collections-C
return 0;
}
By the way, because I think it's the same problem, how can I have subdirectories in my project so that I can put my header files away to keep the project tree tidy? I tried to add add_subdirectories($/include) to my CMakelist.txt
I am expecting people telling me that there are many similar questions already, but none of those I found is clear to me.
Thank you if you have the patience to explain this to me.
Henri
This is for C++, but it should work for your C code. In this example, it's defining where to find the OpenSSL and Google Test headers, and how to link with the Google Test library and the OpenSSL library (which is in C, as it turns out):
cmake_minimum_required(VERSION 3.5)
project(stackexample)
set(CMAKE_CXX_STANDARD 11)
find_library(GTest required)
include_directories(${GTEST_INCLUDE_DIRS} /usr/include/openssl)
set(
SOURCE_FILES
StackExample.cpp
StackExample.h
)
add_executable(stackexample ${SOURCE_FILES})
target_link_libraries(stackexample -lgtest -lssl -lcrypto pthread)
Collections-C appears to have an installer, so you would
List the path to its installed headers in the include_directories line
List its installed library in the target_link_libraries line
The solution was to build the library then do stuff with CMake. I followed this tutorial.

gcc undefined reference to `fopen_s'

I don't know if I can ask this question again,
There are many duplicates but none of the solutions actually worked for me
I'm using gcc version 4.6.1 (GCC)
And by trying to build I get
undefined reference to `fopen_s'
Please make your suggestions on how I can solve this issue :)
Note: I'm using Windows 7 / 64-bit
Thanks in advance.
Maybe you're not including the file its written in. Not sure where it is but it'd be something like "#include name.h", unless its a function you wrote yourself, in which case you'll need to create a .h file and include it at the top of both this file and the file you wrote that function in, like this " #include "name.h" "

How can I get a `Makeheaders` binary for Windows?

I'm tired of seperately having to generate a declaration in the header file for most of the functions I'm defining in my C file. Hence, I would like to automatize this.
I've found an ideal application for this: Makeheaders
Unfortunately only the sources seem to be available, no readymade binary.
Documentation: https://www.fossil-scm.org/xfer/doc/trunk/src/makeheaders.html
Code: https://code.launchpad.net/~lockal/makeheaders/head
Does someone know where to get a binary? Would it be hard to somehow build it myself?
You can download the source code from here. It is a single makeheaders.c file.
then you just need to call cl.exe makeheaders.c it will generate a makeheaders.exe that you can use.

Can a C header file only specify a name of another header file

I found a pacman project in github where a file conf.c includes a header file #include "ini.h" where ini.h contains only a single line (i.e no #include statement):
//ini.h
../common/ini.c
I have never seen anyone doing this before! It seems a bit hackish/rough around the edges. My questions are:
Is this legal C?
Is it portable?
Is it recommended?
I would have assumed the answer should be no for all these questions, but I may be learning something new...
edit
From the answers, I see its a Linux symlink. I guess that this means it is not portable to Windows, and would also make it more difficult to read outside a unix environment. I would also imagine that using relative paths (or include directories) instead of symlinks would be a better practice in cases like this for reasons mentioned above...
src/pacman/ini.h is a symbolic link according to the site.
Symbolic link has an information of where the target file is (path name), and I guess it is what is displayed on the site.
The OS will redirect access to that ini.h to ../common/ini.h, which is a normal C code.
I don't see any reason why not. The include statement indicates the compiler to replace that line with the whatever is in the included file

make file running on Linux - how to ignore case sensitive?

I have a huge project, whole written in C language and I have a single make file that is used to compile it. The project C files contains lots of capitalize problems in it's header files, meaning there are tones of header files that were miss-spelled in lots of C files.
The problem is I need to migrate this project to compile on Linux machine and since Linux is case sensitive I got tones of errors.
Is there an elegant way which I can run make file in Linux and tell him to ignore case sensitive?
Any other solution will be welcome as well.
Thanks a lot.
Motti.
You'll have to fix everything by hand and rename every file or fix every place with #include. Even if you have a huge project (comparable with linux kernel), it should be possible to do this during a hour or two. Automation may be possible, but manual way should be better - because script won't be able to guess which name is right - filename, or the name used in #include.
Besides, this situation is a fault of original project developer. If he/she wasn't sloppy and named every header in every #include correctly, this wouldn't happen. Technically, this is a code problem similar to syntax error. The only right way to deal with it is to fix it.
I think it takes not too long to write a small script, which goes thru the directories first, then replaces C headers. Explained:
Scan the headers' folder and collect filenames.
Make a lowercase list of them. You have now original and locased pairs.
Scan the C source files and find each line contains "#include"
Lowercase it.
Find the lowercase filename in the list collected and lowercased from headers.
Replace the source line with the one collected from headers.
You should put the modified files into a separate folder structure, avoid overwriting the whole source with some buggy stuff. Don't forget to create target folders during the source tree scan.
I recommend a script language for that task, I prefer PHP, but just it's the only server-side script language which I know. Yep, it will run for a while, but only once.
(I bet that you will have other difficulties with that project, this problem is not a typical indicator of high quality work.)
Well I can only tell you that you need to change the case of those header files. I don't know that there is any way you can make it automatic but still you can use cscope to do it in a easier way.
http://www.linux-tutorial.info/modules.php?name=ManPage&sec=1&manpage=cscope
You can mount the files on a case-insensitive file system. FAT comes to mind. ntfs-3g does not appear to support this.
I use the find all and replace all functionality of Source Insight when i have to do complete replacement. But your problem seems quite big, but you can try the option to replace every header file name in all occurences of source files using the
"Find All" + "Replace" functionality. You can use notepad++ too for doing the same.
A long time ago there was a great tool under MPW (Macintosh Programmer's Workshop) called Canon. It was used to canonize text files, i.e. make all symbols found in a given refernce list have have the same usage of upper/lower case. This tool would be ideal for a task like this - I wonder if anything similar exists under Linux ?

Resources