1// Copyright 2014 The Android Open Source Project
2//
3// This software is licensed under the terms of the GNU General Public
4// License version 2, as published by the Free Software Foundation, and
5// may be copied, distributed, and modified under those terms.
6//
7// This program is distributed in the hope that it will be useful,
8// but WITHOUT ANY WARRANTY; without even the implied warranty of
9// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10// GNU General Public License for more details.
11
12#ifndef ANDROID_UTILS_PROPERTY_FILE_H
13#define ANDROID_UTILS_PROPERTY_FILE_H
14
15#include <stdbool.h>
16#include <stddef.h>
17
18#include "android/utils/compiler.h"
19
20ANDROID_BEGIN_HEADER
21
22// Parse the content of a property file and retrieve the value of a given
23// named property, or NULL if it is undefined or empty. If a property
24// appears several times in a file, the last definition is returned.
25// |propertyFile| is the address of the file in memory.
26// |propertyFileLen| is its length in bytes.
27// |propertyName| is the name of the property.
28char* propertyFile_getValue(const char* propertyFile,
29                            size_t propertyFileLen,
30                            const char* propertyName);
31
32// Maximum length of a property name (including terminating zero).
33// Any property name that is equal or greater than this value will be
34// considered undefined / ignored.
35#define MAX_PROPERTY_NAME_LEN  32
36
37// Maximum length of a property value (including terminating zero).
38// Any value stored in a file that has a length equal or greater than this
39// will be truncated!.
40#define MAX_PROPERTY_VALUE_LEN 92
41
42// Structure used to hold an iterator over a property file.
43// Usage is simple:
44//    1) Initialize iterator with propertyFileIterator_init()
45//
46//    2) Call propertyFileIterator_next() in a loop. If it returns true
47//       one can read the |name| and |value| zero-terminated strings to
48//       get property names and values, in the order they appear in the
49//       file.
50//
51//       Once propertyFileIterator_next() returns false, you're done.
52//
53typedef struct {
54    char name[MAX_PROPERTY_NAME_LEN];
55    char value[MAX_PROPERTY_VALUE_LEN];
56    // private.
57    const char* p;
58    const char* end;
59} PropertyFileIterator;
60
61// Initialize a PropertyFileIterator.
62// |iter| is the iterator instance.
63// |propertyFile| is the address of the property file in memory.
64// |propertyFileLen| is its lengh in bytes.
65void propertyFileIterator_init(PropertyFileIterator* iter,
66                               const void* propertyFile,
67                               size_t propertyFileLen);
68
69// Extract one property from a property file iterator.
70// Returns true if there is one, or false if the iteration has stopped.
71// If true, one can read |iter->name| and |iter->value| to get the
72// property name and value, respectively, as zero-terminated strings
73// that need to be copied by the caller.
74bool propertyFileIterator_next(PropertyFileIterator* iter);
75
76ANDROID_END_HEADER
77
78#endif  // ANDROID_UTILS_PROPERTY_FILE_H
79