Changeset - r17408:1b41bc18943a
[Not reviewed]
master
0 3 0
alberth - 13 years ago 2011-03-03 20:53:09
alberth@openttd.org
(svn r22168) -Codechange: Move ini file IO and file error reporting to virtual functions.
3 files changed with 34 insertions and 17 deletions:
0 comments (0 inline, 0 general)
src/ini.cpp
Show inline comments
 
@@ -105,3 +105,15 @@ bool IniFile::SaveToDisk(const char *fil
 

	
 
	return true;
 
}
 

	
 
/* virtual */ FILE *IniFile::OpenFile(const char *filename, size_t *size)
 
{
 
	/* Open the text file in binary mode to prevent end-of-line translations
 
	 * done by ftell() and friends, as defined by K&R. */
 
	return FioFOpenFile(filename, "rb", DATA_DIR, size);
 
}
 

	
 
/* virtual */ void IniFile::ReportFileError(const char * const pre, const char * const buffer, const char * const post)
 
{
 
	ShowInfoF("%s%s%s", pre, buffer, post);
 
}
src/ini_load.cpp
Show inline comments
 
@@ -12,10 +12,8 @@
 
#include "stdafx.h"
 
#include "core/alloc_func.hpp"
 
#include "core/mem_func.hpp"
 
#include "debug.h"
 
#include "ini_type.h"
 
#include "string_func.h"
 
#include "fileio_func.h"
 

	
 
/**
 
 * Construct a new in-memory item of an Ini file.
 
@@ -205,19 +203,7 @@ void IniLoadFile::LoadFromDisk(const cha
 
	uint comment_alloc = 0;
 

	
 
	size_t end;
 
	/*
 
	 * Now we are going to open a file that contains no more than simple
 
	 * plain text. That would raise the question: "why open the file as
 
	 * if it is a binary file?". That's simple... Microsoft, in all
 
	 * their greatness and wisdom decided it would be useful if ftell
 
	 * is aware of '\r\n' and "sees" that as a single character. The
 
	 * easiest way to test for that situation is by searching for '\n'
 
	 * and decrease the value every time you encounter a '\n'. This will
 
	 * thus also make ftell "see" the '\r' when it is not there, so the
 
	 * result of ftell will be highly unreliable. So to work around this
 
	 * marvel of wisdom we have to open in as a binary file.
 
	 */
 
	FILE *in = FioFOpenFile(filename, "rb", DATA_DIR, &end);
 
	FILE *in = this->OpenFile(filename, &end);
 
	if (in == NULL) return;
 

	
 
	end += ftell(in);
 
@@ -253,7 +239,7 @@ void IniLoadFile::LoadFromDisk(const cha
 
		/* it's a group? */
 
		if (s[0] == '[') {
 
			if (e[-1] != ']') {
 
				ShowInfoF("ini: invalid group name '%s'", buffer);
 
				this->ReportFileError("ini: invalid group name '", buffer, "'");
 
			} else {
 
				e--;
 
			}
 
@@ -296,7 +282,7 @@ void IniLoadFile::LoadFromDisk(const cha
 
			item->value = (!quoted && e == t) ? NULL : strndup(t, e - t);
 
		} else {
 
			/* it's an orphan item */
 
			ShowInfoF("ini: '%s' outside of group", buffer);
 
			this->ReportFileError("ini: '", buffer, "' outside of group");
 
		}
 
	}
 

	
src/ini_type.h
Show inline comments
 
@@ -61,6 +61,22 @@ struct IniLoadFile {
 
	void RemoveGroup(const char *name);
 

	
 
	void LoadFromDisk(const char *filename);
 

	
 
	/**
 
	 * Open the INI file.
 
	 * @param filename Name of the INI file.
 
	 * @param size [out] Size of the opened file.
 
	 * @return File handle of the opened file, or \c NULL.
 
	 */
 
	virtual FILE *OpenFile(const char *filename, size_t *size) = 0;
 

	
 
	/**
 
	 * Report an error about the file contents.
 
	 * @param pre    Prefix text of the \a buffer part.
 
	 * @param buffer Part of the file with the error.
 
	 * @param post   Suffix text of the \a buffer part.
 
	 */
 
	virtual void ReportFileError(const char * const pre, const char * const buffer, const char * const post) = 0;
 
};
 

	
 
/** Ini file that supports both loading and saving. */
 
@@ -68,6 +84,9 @@ struct IniFile : IniLoadFile {
 
	IniFile(const char * const *list_group_names = NULL);
 

	
 
	bool SaveToDisk(const char *filename);
 

	
 
	virtual FILE *OpenFile(const char *filename, size_t *size);
 
	virtual void ReportFileError(const char * const pre, const char * const buffer, const char * const post);
 
};
 

	
 
#endif /* INI_TYPE_H */
0 comments (0 inline, 0 general)