File: ConfigFile.cc

package info (click to toggle)
flactag 2.0.4-5
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid, stretch
  • size: 2,096 kB
  • ctags: 477
  • sloc: sh: 10,777; cpp: 3,283; makefile: 62; sed: 7
file content (130 lines) | stat: -rw-r--r-- 3,209 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/* --------------------------------------------------------------------------

   flactag -- A tagger for single album FLAC files with embedded CUE sheets
   						using data retrieved from the MusicBrainz service

   Copyright (C) 2006-2012 Andrew Hawkins
   Copyright (C) 2011-2012 Daniel Pocock

   This file is part of flactag.

   This program is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
	 the Free Software Foundation, either version 3 of the License, or
	 (at your option) any later version.

   Flactag is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Lesser General Public License for more details.

   You should have received a copy of the GNU General Public
   License along with this library; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

     $Id$

----------------------------------------------------------------------------*/

#include "ConfigFile.h"

#include <stdio.h>
#include <strings.h>

CConfigFile::CConfigFile()
{
	m_Values["BasePath"]="";
	m_Values["SingleDiskFileName"]="";
	m_Values["MultiDiskFileName"]="";
	m_Values["DirectoryCreatePermissions"]="0755";
	m_Values["Server"]="musicbrainz.org";
	m_Values["Port"]="80";
	m_Values["CreateCuesheetAfterRename"]="0";
}

std::string CConfigFile::Value(const std::string& Name) const
{
	std::string Value;

	std::map<std::string,std::string>::const_iterator ThisValue=m_Values.find(Name);
	if (m_Values.end()!=ThisValue)
		Value=(*ThisValue).second;

	return Value;
}

bool CConfigFile::BoolValue(const std::string& Name) const
{
	std::string _val(Value(Name));

	for(unsigned int i = 0; i < _val.size(); i++)
		_val[i] = std::tolower(_val[i]);

	if(_val == "1")
		return true;
	if(_val.compare("true") == 0)
		return true;
	if(_val.compare("yes") == 0)
		return true;

	return false;
}

bool CConfigFile::LoadFile(const std::string& FileName)
{
	bool RetVal=false;

	FILE *fptr=fopen(FileName.c_str(),"rt");
	if (fptr)
	{
		RetVal=true;

		while (!feof(fptr))
		{
			char Buffer[1024];

			fgets(Buffer,sizeof(Buffer),fptr);
			if (!feof(fptr))
			{
				std::string TmpBuff=Buffer;
				std::string::size_type EqualsPos=TmpBuff.find("=");
				if (std::string::npos!=EqualsPos)
				{
					std::string Name=TmpBuff.substr(0,EqualsPos);
					std::string Value=TmpBuff.substr(EqualsPos+1);
					while (Value[Value.length()-1]=='\n' || Value[Value.length()-1]=='\r')
						Value=Value.substr(0,Value.length()-1);

					m_Values[Name]=Value;
				}
			}
		}

		fclose(fptr);
	}

	return RetVal;
}

bool CConfigFile::SaveFile(const std::string& FileName) const
{
	bool RetVal=false;

	FILE *fptr=fopen(FileName.c_str(),"wt");
	if (fptr)
	{
		std::map<std::string,std::string>::const_iterator ThisValue=m_Values.begin();
		while(m_Values.end()!=ThisValue)
		{
			std::string Name=(*ThisValue).first;
			std::string Value=(*ThisValue).second;

			fprintf(fptr,"%s=%s\n",Name.c_str(),Value.c_str());

			++ThisValue;
		}
		RetVal=true;
	}

	return RetVal;
}