libproperties is a library to parse the Java .properties files. It was writen in pure C. And fully compatible with the Java .properties file format.
If you do not want to use this project as a library, You can add these files in your project and compile with you project together.
- properties.c
- buf.c
mkdir build
cd build/
cmake ../
makemkdir build
cd build/
cmake -D BUILD_SHARED_LIBS=off ../
makeAfter build, we just need type command below:
make testmake cleanIf you do not need special the install directory, you need to setup the CMAKE_INSTALL_PREFIX options:
mkdir build
cd build/
cmake -DCMAKE_INSTALL_PREFIX=./output ../
make
make installIf you do not need special the install directory, the default installed directory is /usr/local/lib/:
mkdir build
cd build/
cmake ../
make
make installThe kernel function of this library is properties_parse
int properties_parse(void* source_context, PROPERTIES_SOURCE_READ source_read, void* handler_context, PROPERTYS_HANDLER handler);It need four parameters:
source_contextandsource_readis used to tell where to read the text from.handler_contextandhandleris used to tell the function how to precess the key-value pairs of the property.
This library have provided two source_read function by default.
- Read input text from a file, please use
properties_source_file_read. - Read input text from simple string, please use
properties_source_file_string.
int test_handler(void* context, char* key, int key_len, char* val, int val_len)
{
printf("[%s]=[%s]\n", key, val);
return 0;
}
int main(int argc, char* argv[])
{
if (argc < 2)
{
printf("Missing parameters: properties_test <FILE>");
return 1;
}
FILE* file = fopen(argv[1], "r");
if (NULL == file)
{
return NULL;
}
int ret = properties_parse(file, properties_source_file_read, NULL, test_handler);
fclose(file);
getchar();
return ret;
}int test_handler(void* context, char* key, int key_len, char* val, int val_len)
{
printf("[%s]=[%s]\n", key, val);
return 0;
}
int main(int argc, char* argv[])
{
char str[] =
"a=b\n"
"c=123\n"
;
struct properties_source_string_t source =
{
str,
str + strlen(str)
};
properties_parse(&source, properties_source_string_read, NULL, test_handler);
getchar();
return 0;
}Windows
Linux/Unix
Windows
premake5 --file=libproperties_test.premake vs2010The vs2010 should be replace with one of below, which is depended the version of the Visual Studio you have installed.
- vs2008
- vs2010
- vs2013
- vs2015
- vs2017
Linux/Unix
premake5 --file=libproperties_test.premake gmakeWindows
Open the libproperties_test.sln with your IDE, and then press F7
Linux
make clean && makeIf your system is Windows, you need open the bash of MSYS2 first.
./test.shThis library is complete compatible with the Java .properties file format which defined in
Properties.java(JDK9).
Please jump to the load function to checkout the full of the definition of the format.
This document is completely and exactly but too much complex.
And there is a simple one which provide by Wikipedia here.
UTF-8
Generally speaking, the properties_source_file_read and properties_source_string_read is enough.
If you want, you can provided a new function yourself. But the function must flow the prototype of PROPERTIES_SOURCE_READ.
Please refer the properties.h to see rules of function PROPERTIES_SOURCE_READ.
If the input file have BOM at the begining of the file. You'd better custom a new input source for it. And skip the BOM bytes when the read function called at the first time.