thalassa/lib/inifile/ini_save.cpp
2026-03-19 06:23:52 +05:00

80 lines
2.9 KiB
C++

// +-------------------------------------------------------------------------+
// | I n i F i l e vers. 0.3.30 |
// | Copyright (c) Andrey Vikt. Stolyarov [http://www.croco.net/] 2003-2024 |
// | ----------------------------------------------------------------------- |
// | This is free software. Permission is granted to everyone to use, copy |
// | or modify this software under the terms and conditions of |
// | GNU LESSER GENERAL PUBLIC LICENSE, v. 2.1 |
// | as published by Free Software Foundation (see the file LGPL.txt) |
// | |
// | Please visit http://www.croco.net/software/scriptpp to get a fresh copy |
// | ----------------------------------------------------------------------- |
// | This code is provided strictly and exclusively on the "AS IS" basis. |
// | !!! THERE IS NO WARRANTY OF ANY KIND, NEITHER EXPRESSED NOR IMPLIED !!! |
// +-------------------------------------------------------------------------+
#include <stdio.h>
#include "inifile.hpp"
bool IniFileParser::Save(const char *path)
{
last_error_line = -1;
last_error_description = 0;
FILE *fl = fopen(path, "w");
if(!fl) {
last_error_description = "couldn't open the file";
return false;
}
char buf[1024];
buf[sizeof(buf)-1] = '\0';
for(Group *grp = firstgroup; grp; grp = grp->next) {
for(Section *sect = grp->firstsection; sect; sect=sect->next) {
fprintf(fl, "[%s %s]\n\n", grp->name, sect->name);
for(Parameter *parm = sect->firstparam; parm; parm=parm->next) {
if(!parm->value)
continue;
fprintf(fl, "%s = %s\n", parm->name, parm->value);
}
fprintf(fl, "\n\n");
}
}
fclose(fl);
return true;
}
void IniFileParser::SetParam(const char *groupname, const char *sectionname,
const char *paramname, const char *paramval,
bool exclusive)
{
Section* sect = ProvideSection(groupname, sectionname);
sect->ProvideParameter(paramname)->AddDictionaryData(paramval, exclusive);
}
void IniFileParser::
AddIntegerParameter(const char *grname, const char *sectname,
const char *parmname, long value)
{
char buf[32];
sprintf(buf, "%ld", value);
SetParam(grname, sectname, parmname, buf, false);
}
void IniFileParser::
AddTextParameter(const char *grname, const char *sectname,
const char *parmname, const char *value)
{
SetParam(grname, sectname, parmname, value, false);
}
void IniFileParser::
SetTextParameter(const char *grname, const char *sectname,
const char *parmname, const char *value)
{
SetParam(grname, sectname, parmname, value, true);
}