#ifndef _fastIni_
#define _fastIni_

#ifdef __cplusplus
extern "C" {
#endif
#include <string.h>
#include <stdlib.h>
#include <stdio.h>

#include <ds2_malloc.h>
#include <ds2_types.h>
#include <fs_api.h>

#define MAX_LINE_SIZE 1024
#define INI_COMMENT '#'

//#define DBG_TXT
/*
strings for C
*/
struct cstring
{
	char *string;
	u16 len;
} __attribute__ ((aligned (4)));

extern void cStringClear(struct cstring *src);
extern s8 cStringSet(struct cstring *dest, const char *source, int len);

/*
Ini stuff
*/
typedef enum{
	INI_NONENTRYS = (1 << 0),//read non-entry lines as well
	INI_SORT = (1 << 1),//sort entries and sections
	INI_ADDMISSING = (1 << 2),//adds missing entries to the ini file in ram
	INI_NOSPACES = (1 << 3),//removes spaces before and after = when writing ini file
}_iniModes;


struct _iniEntry{
	struct cstring Name;
	struct cstring Value;
} __attribute__ ((aligned (4)));

struct _iniSection{
	struct cstring Header;
	struct _iniEntry *Entry;
	int entryCount;//number of entries
} __attribute__ ((aligned (4)));

typedef struct _iniFile{	
	struct _iniSection *Sections;
	int blocks;
	u8 mode, error;
	
	void  (*close)      (struct _iniFile *);
	int   (*load)       (struct _iniFile *, char *, u8);
	s8    (*write)		(struct _iniFile *, const char *);
	int   (*getInt)     (const struct _iniFile *, const char *, const char *, int);
	void  (*setInt)     (const struct _iniFile *, const char *, const char *, int);
	void* (*getStr)     (const struct _iniFile *, const char *, const char *, const char *);
	char* (*setStr)     (const struct _iniFile *, const char *, const char *, const char *);
	s8    (*addEntry)	(struct _iniFile *, const char *, const char *);
	s8    (*newFile)	(struct _iniFile *, const char *);
}INIObj __attribute__ ((aligned (4)));

extern void _ini_clean(INIObj *File);
extern int _ini_loadFile(INIObj *iniFile, const char *filePath, u8 readFlags);
extern s8 _ini_newFile(INIObj *iniFile, const char *filePath);
extern s8 _ini_addEntry(INIObj *File, const char *header, const char *input);
extern s8 _ini_writeAll(const INIObj *ini, const char *fileName);
extern int _ini_getInt(const INIObj *File, const char *header, const char *key, int def_value);
extern char *_ini_getStr_char(const INIObj *File, const char *header, const char *key, const char * def_value);
extern void *_ini_getStr(const INIObj *File, const char *header, const char *key, const void *def_value);
extern void initFastIni(INIObj *ini);


#ifdef DBG_TXT
extern void _ini_printAll(const INIObj *File);
#endif

#ifdef __cplusplus
}
#endif

#endif
