Build Lunapaint from Contrib.
[AROS-Contrib.git] / libini / iniOpen.c
blob8c49cbcec879a1aa5123c12781e3889cae257655
1 /**********************************************************************************
2 * iniOpen
5 **************************************************
6 * This code was created by Peter Harvey @ CodeByDesign.
7 * Released under LGPL 28.JAN.99
9 * Contributions from...
10 * -----------------------------------------------
11 * PAH = Peter Harvey - pharvey@codebydesign.com
12 * -----------------------------------------------
14 * PAH 06.MAR.99 Can now create file-less INI. Pass NULL for
15 * pszFileName. Then copy a file name into hIni->szFileName
16 * before calling iniCommit.
17 **************************************************/
19 #include "inifile_intern.h"
20 #include "ini.h"
21 #include <aros/libcall.h>
23 /*****************************************************************************
24 NAME */
25 AROS_LH7(int, iniOpen,
27 /* SYNOPSIS */
28 AROS_LHA(HINI *, hIni, A0),
29 AROS_LHA(char, *pszFileName, A1),
30 AROS_LHA(char, cComment, D0),
31 AROS_LHA(char, cLeftBracket, D1),
32 AROS_LHA(char, cRightBracket, D2),
33 AROS_LHA(char, cEqual, D3),
34 AROS_LHA(int, bCreate, D4),
36 /* LOCATION */
37 struct Library *, inifileBase, 29, inifile)
39 /* FUNCTION
41 INPUTS
43 RESULT
45 NOTES
47 EXAMPLE
49 BUGS
51 SEE ALSO
53 INTERNALS
55 HISTORY
57 *****************************************************************************/
59 AROS_LIBFUNC_INIT
61 FILE *hFile;
62 char szLine[INI_MAX_LINE+1];
63 char szObjectName[INI_MAX_OBJECT_NAME+1];
64 char szPropertyName[INI_MAX_PROPERTY_NAME+1];
65 char szPropertyValue[INI_MAX_PROPERTY_VALUE+1];
66 int nValidFile;
68 /* INIT STATEMENT */
69 *hIni = malloc( sizeof(INI) );
70 if ( pszFileName )
71 strncpy( (*hIni)->szFileName, pszFileName, ODBC_FILENAME_MAX );
72 else
73 strncpy( (*hIni)->szFileName, "", ODBC_FILENAME_MAX );
74 (*hIni)->cComment = cComment;
75 (*hIni)->cLeftBracket = cLeftBracket;
76 (*hIni)->cRightBracket = cRightBracket;
77 (*hIni)->cEqual = cEqual;
78 (*hIni)->bChanged = FALSE;
79 (*hIni)->hCurObject = NULL;
80 (*hIni)->hFirstObject = NULL;
81 (*hIni)->hLastObject = NULL;
82 (*hIni)->nObjects = 0;
83 (*hIni)->bReadOnly = 0;
85 /* OPEN FILE */
86 if ( pszFileName )
88 hFile = fopen( pszFileName, "r" );
89 if ( !hFile )
91 if ( bCreate == TRUE )
93 hFile = fopen( pszFileName, "w" );
97 if ( !hFile )
99 free( *hIni );
100 *hIni = NULL;
101 return INI_ERROR;
104 nValidFile = _iniScanUntilObject( *hIni, hFile, szLine );
105 if ( nValidFile == INI_SUCCESS )
107 char *ptr;
110 if ( szLine[0] == cLeftBracket )
112 _iniObjectRead( (*hIni), szLine, szObjectName );
113 iniObjectInsert( (*hIni), szObjectName );
115 else if ( (szLine[0] != cComment) && !isspace(szLine[0]) )
117 _iniPropertyRead( (*hIni), szLine, szPropertyName, szPropertyValue );
118 iniPropertyInsert( (*hIni), szPropertyName, szPropertyValue );
121 } while ( (ptr = fgets( szLine, INI_MAX_LINE, hFile )) != NULL );
123 else if ( nValidFile == INI_ERROR )
125 /* INVALID FILE */
126 if ( hFile != NULL )
127 fclose( hFile );
128 free( *hIni );
129 *hIni = NULL;
130 return INI_ERROR;
133 /* CLEANUP */
134 if ( hFile != NULL )
135 fclose( hFile );
137 iniObjectFirst( *hIni );
139 } /* if file given */
141 return INI_SUCCESS;
143 AROS_LIBFUNC_EXIT
144 } /* iniOpen */