mirror of
https://github.com/id-Software/GtkRadiant.git
synced 2026-03-20 08:59:32 +01:00
The GtkRadiant sources as originally released under the GPL license.
This commit is contained in:
383
libs/profile/file.cpp
Normal file
383
libs/profile/file.cpp
Normal file
@@ -0,0 +1,383 @@
|
||||
/*
|
||||
Copyright (c) 2001, Loki software, inc.
|
||||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without modification,
|
||||
are permitted provided that the following conditions are met:
|
||||
|
||||
Redistributions of source code must retain the above copyright notice, this list
|
||||
of conditions and the following disclaimer.
|
||||
|
||||
Redistributions in binary form must reproduce the above copyright notice, this
|
||||
list of conditions and the following disclaimer in the documentation and/or
|
||||
other materials provided with the distribution.
|
||||
|
||||
Neither the name of Loki software nor the names of its contributors may be used
|
||||
to endorse or promote products derived from this software without specific prior
|
||||
written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS''
|
||||
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
|
||||
DIRECT,INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
|
||||
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
//
|
||||
// File class, can be a memory file or a regular disk file.
|
||||
// Originally from LeoCAD, used with permission from the author. :)
|
||||
//
|
||||
// Leonardo Zide (leo@lokigames.com)
|
||||
//
|
||||
|
||||
#include "file.h"
|
||||
|
||||
#include <memory.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <stdarg.h>
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////////
|
||||
// File construction/destruction
|
||||
|
||||
MemStream::MemStream()
|
||||
{
|
||||
m_nGrowBytes = 1024;
|
||||
m_nPosition = 0;
|
||||
m_nBufferSize = 0;
|
||||
m_nFileSize = 0;
|
||||
m_pBuffer = NULL;
|
||||
m_bAutoDelete = true;
|
||||
}
|
||||
|
||||
MemStream::MemStream(size_type nLen)
|
||||
{
|
||||
m_nGrowBytes = 1024;
|
||||
m_nPosition = 0;
|
||||
m_nBufferSize = 0;
|
||||
m_nFileSize = 0;
|
||||
m_pBuffer = NULL;
|
||||
m_bAutoDelete = true;
|
||||
|
||||
GrowFile (nLen);
|
||||
}
|
||||
|
||||
FileStream::FileStream()
|
||||
{
|
||||
m_hFile = NULL;
|
||||
m_bCloseOnDelete = false;
|
||||
}
|
||||
|
||||
MemStream::~MemStream()
|
||||
{
|
||||
if (m_pBuffer)
|
||||
Close();
|
||||
|
||||
m_nGrowBytes = 0;
|
||||
m_nPosition = 0;
|
||||
m_nBufferSize = 0;
|
||||
m_nFileSize = 0;
|
||||
}
|
||||
|
||||
FileStream::~FileStream()
|
||||
{
|
||||
if (m_hFile != NULL && m_bCloseOnDelete)
|
||||
Close();
|
||||
}
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////////
|
||||
// File operations
|
||||
|
||||
char* MemStream::ReadString(char* pBuf, size_type nMax)
|
||||
{
|
||||
int nRead = 0;
|
||||
unsigned char ch;
|
||||
|
||||
if (nMax <= 0)
|
||||
return NULL;
|
||||
if (m_nPosition >= m_nFileSize)
|
||||
return NULL;
|
||||
|
||||
while ((--nMax))
|
||||
{
|
||||
if (m_nPosition == m_nFileSize)
|
||||
break;
|
||||
|
||||
ch = m_pBuffer[m_nPosition];
|
||||
m_nPosition++;
|
||||
pBuf[nRead++] = ch;
|
||||
|
||||
if (ch == '\n')
|
||||
break;
|
||||
}
|
||||
|
||||
pBuf[nRead] = '\0';
|
||||
return pBuf;
|
||||
}
|
||||
|
||||
char* FileStream::ReadString(char* pBuf, size_type nMax)
|
||||
{
|
||||
return fgets(pBuf, static_cast<int>(nMax), m_hFile);
|
||||
}
|
||||
|
||||
MemStream::size_type MemStream::read(byte_type* buffer, size_type length)
|
||||
{
|
||||
if (length == 0)
|
||||
return 0;
|
||||
|
||||
if (m_nPosition > m_nFileSize)
|
||||
return 0;
|
||||
|
||||
size_type nRead;
|
||||
if (m_nPosition + length > m_nFileSize)
|
||||
nRead = m_nFileSize - m_nPosition;
|
||||
else
|
||||
nRead = length;
|
||||
|
||||
memcpy((unsigned char*)buffer, (unsigned char*)m_pBuffer + m_nPosition, nRead);
|
||||
m_nPosition += nRead;
|
||||
|
||||
return nRead;
|
||||
}
|
||||
|
||||
FileStream::size_type FileStream::read(byte_type* buffer, size_type length)
|
||||
{
|
||||
return fread(buffer, 1, length, m_hFile);
|
||||
}
|
||||
|
||||
int MemStream::GetChar()
|
||||
{
|
||||
if (m_nPosition > m_nFileSize)
|
||||
return 0;
|
||||
|
||||
unsigned char* ret = (unsigned char*)m_pBuffer + m_nPosition;
|
||||
m_nPosition++;
|
||||
|
||||
return *ret;
|
||||
}
|
||||
|
||||
int FileStream::GetChar()
|
||||
{
|
||||
return fgetc(m_hFile);
|
||||
}
|
||||
|
||||
MemStream::size_type MemStream::write(const byte_type* buffer, size_type length)
|
||||
{
|
||||
if (length == 0)
|
||||
return 0;
|
||||
|
||||
if (m_nPosition + length > m_nBufferSize)
|
||||
GrowFile(m_nPosition + length);
|
||||
|
||||
memcpy((unsigned char*)m_pBuffer + m_nPosition, (unsigned char*)buffer, length);
|
||||
|
||||
m_nPosition += size_type(length);
|
||||
|
||||
if (m_nPosition > m_nFileSize)
|
||||
m_nFileSize = m_nPosition;
|
||||
|
||||
return length;
|
||||
}
|
||||
|
||||
FileStream::size_type FileStream::write(const byte_type* buffer, size_type length)
|
||||
{
|
||||
return fwrite(buffer, 1, length, m_hFile);
|
||||
}
|
||||
|
||||
int MemStream::PutChar(int c)
|
||||
{
|
||||
if (m_nPosition + 1 > m_nBufferSize)
|
||||
GrowFile(m_nPosition + 1);
|
||||
|
||||
unsigned char* bt = (unsigned char*)m_pBuffer + m_nPosition;
|
||||
*bt = c;
|
||||
|
||||
m_nPosition++;
|
||||
|
||||
if (m_nPosition > m_nFileSize)
|
||||
m_nFileSize = m_nPosition;
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
/*!\todo SPoG suggestion: replace printf with operator >> using c++ iostream and strstream */
|
||||
void FileStream::printf(const char* s, ...)
|
||||
{
|
||||
va_list args;
|
||||
|
||||
va_start (args, s);
|
||||
vfprintf(m_hFile, s, args);
|
||||
va_end (args);
|
||||
}
|
||||
|
||||
/*!\todo SPoG suggestion: replace printf with operator >> using c++ iostream and strstream */
|
||||
void MemStream::printf(const char* s, ...)
|
||||
{
|
||||
va_list args;
|
||||
|
||||
char buffer[4096];
|
||||
va_start (args, s);
|
||||
vsprintf(buffer, s, args);
|
||||
va_end (args);
|
||||
write(reinterpret_cast<byte_type*>(buffer), strlen(buffer));
|
||||
}
|
||||
|
||||
int FileStream::PutChar(int c)
|
||||
{
|
||||
return fputc(c, m_hFile);
|
||||
}
|
||||
|
||||
bool FileStream::Open(const char *filename, const char *mode)
|
||||
{
|
||||
m_hFile = fopen(filename, mode);
|
||||
m_bCloseOnDelete = true;
|
||||
|
||||
return (m_hFile != NULL);
|
||||
}
|
||||
|
||||
void MemStream::Close()
|
||||
{
|
||||
m_nGrowBytes = 0;
|
||||
m_nPosition = 0;
|
||||
m_nBufferSize = 0;
|
||||
m_nFileSize = 0;
|
||||
if (m_pBuffer && m_bAutoDelete)
|
||||
free(m_pBuffer);
|
||||
m_pBuffer = NULL;
|
||||
}
|
||||
|
||||
void FileStream::Close()
|
||||
{
|
||||
if (m_hFile != NULL)
|
||||
fclose(m_hFile);
|
||||
|
||||
m_hFile = NULL;
|
||||
m_bCloseOnDelete = false;
|
||||
}
|
||||
|
||||
int MemStream::Seek(offset_type lOff, int nFrom)
|
||||
{
|
||||
size_type lNewPos = m_nPosition;
|
||||
|
||||
if (nFrom == SEEK_SET)
|
||||
lNewPos = lOff;
|
||||
else if (nFrom == SEEK_CUR)
|
||||
lNewPos += lOff;
|
||||
else if (nFrom == SEEK_END)
|
||||
lNewPos = m_nFileSize + lOff;
|
||||
else
|
||||
return (position_type)-1;
|
||||
|
||||
m_nPosition = lNewPos;
|
||||
|
||||
return static_cast<int>(m_nPosition);
|
||||
}
|
||||
|
||||
int FileStream::Seek(offset_type lOff, int nFrom)
|
||||
{
|
||||
fseek (m_hFile, lOff, nFrom);
|
||||
|
||||
return ftell(m_hFile);
|
||||
}
|
||||
|
||||
MemStream::position_type MemStream::GetPosition() const
|
||||
{
|
||||
return m_nPosition;
|
||||
}
|
||||
|
||||
FileStream::position_type FileStream::GetPosition() const
|
||||
{
|
||||
return ftell(m_hFile);
|
||||
}
|
||||
|
||||
void MemStream::GrowFile(size_type nNewLen)
|
||||
{
|
||||
if (nNewLen > m_nBufferSize)
|
||||
{
|
||||
// grow the buffer
|
||||
size_type nNewBufferSize = m_nBufferSize;
|
||||
|
||||
// determine new buffer size
|
||||
while (nNewBufferSize < nNewLen)
|
||||
nNewBufferSize += m_nGrowBytes;
|
||||
|
||||
// allocate new buffer
|
||||
unsigned char* lpNew;
|
||||
if (m_pBuffer == NULL)
|
||||
lpNew = static_cast<unsigned char*>(malloc(nNewBufferSize));
|
||||
else
|
||||
lpNew = static_cast<unsigned char*>(realloc(m_pBuffer, nNewBufferSize));
|
||||
|
||||
m_pBuffer = lpNew;
|
||||
m_nBufferSize = nNewBufferSize;
|
||||
}
|
||||
}
|
||||
|
||||
void MemStream::Flush()
|
||||
{
|
||||
// Nothing to be done
|
||||
}
|
||||
|
||||
void FileStream::Flush()
|
||||
{
|
||||
if (m_hFile == NULL)
|
||||
return;
|
||||
|
||||
fflush(m_hFile);
|
||||
}
|
||||
|
||||
void MemStream::Abort()
|
||||
{
|
||||
Close();
|
||||
}
|
||||
|
||||
void FileStream::Abort()
|
||||
{
|
||||
if (m_hFile != NULL)
|
||||
{
|
||||
// close but ignore errors
|
||||
if (m_bCloseOnDelete)
|
||||
fclose(m_hFile);
|
||||
m_hFile = NULL;
|
||||
m_bCloseOnDelete = false;
|
||||
}
|
||||
}
|
||||
|
||||
void MemStream::SetLength(size_type nNewLen)
|
||||
{
|
||||
if (nNewLen > m_nBufferSize)
|
||||
GrowFile(nNewLen);
|
||||
|
||||
if (nNewLen < m_nPosition)
|
||||
m_nPosition = nNewLen;
|
||||
|
||||
m_nFileSize = nNewLen;
|
||||
}
|
||||
|
||||
void FileStream::SetLength(size_type nNewLen)
|
||||
{
|
||||
fseek(m_hFile, static_cast<long>(nNewLen), SEEK_SET);
|
||||
}
|
||||
|
||||
MemStream::size_type MemStream::GetLength() const
|
||||
{
|
||||
return m_nFileSize;
|
||||
}
|
||||
|
||||
FileStream::size_type FileStream::GetLength() const
|
||||
{
|
||||
size_type nLen, nCur;
|
||||
|
||||
// Seek is a non const operation
|
||||
nCur = ftell(m_hFile);
|
||||
fseek(m_hFile, 0, SEEK_END);
|
||||
nLen = ftell(m_hFile);
|
||||
fseek(m_hFile, static_cast<long>(nCur), SEEK_SET);
|
||||
|
||||
return nLen;
|
||||
}
|
||||
158
libs/profile/file.h
Normal file
158
libs/profile/file.h
Normal file
@@ -0,0 +1,158 @@
|
||||
/*
|
||||
Copyright (c) 2001, Loki software, inc.
|
||||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without modification,
|
||||
are permitted provided that the following conditions are met:
|
||||
|
||||
Redistributions of source code must retain the above copyright notice, this list
|
||||
of conditions and the following disclaimer.
|
||||
|
||||
Redistributions in binary form must reproduce the above copyright notice, this
|
||||
list of conditions and the following disclaimer in the documentation and/or
|
||||
other materials provided with the distribution.
|
||||
|
||||
Neither the name of Loki software nor the names of its contributors may be used
|
||||
to endorse or promote products derived from this software without specific prior
|
||||
written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS''
|
||||
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
|
||||
DIRECT,INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
|
||||
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
//
|
||||
// file.h
|
||||
////////////////////////////////////////////////////
|
||||
|
||||
#if !defined(INCLUDED_PROFILE_FILE_H)
|
||||
#define INCLUDED_PROFILE_FILE_H
|
||||
|
||||
#include "idatastream.h"
|
||||
|
||||
/*!
|
||||
API for data streams
|
||||
|
||||
Based on an initial implementation by Loki software
|
||||
modified to be abstracted and shared across modules
|
||||
|
||||
NOTE: why IDataStream and not IStream? because IStream is defined in windows IDL headers
|
||||
*/
|
||||
|
||||
class IDataStream : public InputStream, public OutputStream
|
||||
{
|
||||
public:
|
||||
typedef int offset_type;
|
||||
typedef std::size_t position_type;
|
||||
|
||||
virtual void IncRef() = 0; ///< Increment the number of references to this object
|
||||
virtual void DecRef() = 0; ///< Decrement the reference count
|
||||
|
||||
virtual position_type GetPosition() const = 0;
|
||||
virtual int Seek(offset_type lOff, int nFrom) = 0;
|
||||
|
||||
virtual void SetLength(size_type nNewLen) = 0;
|
||||
virtual size_type GetLength() const = 0;
|
||||
|
||||
virtual char* ReadString(char* pBuf, size_type nMax) = 0;
|
||||
virtual int GetChar()=0;
|
||||
|
||||
virtual int PutChar(int c)=0;
|
||||
virtual void printf(const char*, ...) = 0; ///< completely matches the usual printf behaviour
|
||||
|
||||
virtual void Abort() = 0;
|
||||
virtual void Flush() = 0;
|
||||
virtual void Close() = 0;
|
||||
};
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
class MemStream : public IDataStream
|
||||
{
|
||||
public:
|
||||
MemStream();
|
||||
MemStream(size_type nLen);
|
||||
virtual ~MemStream();
|
||||
|
||||
int refCount;
|
||||
void IncRef() { refCount++; }
|
||||
void DecRef() { refCount--; if (refCount <= 0) delete this; }
|
||||
|
||||
protected:
|
||||
// MemFile specific:
|
||||
size_type m_nGrowBytes;
|
||||
size_type m_nPosition;
|
||||
size_type m_nBufferSize;
|
||||
size_type m_nFileSize;
|
||||
unsigned char* m_pBuffer;
|
||||
bool m_bAutoDelete;
|
||||
void GrowFile(size_type nNewLen);
|
||||
|
||||
public:
|
||||
position_type GetPosition() const;
|
||||
int Seek(offset_type lOff, int nFrom);
|
||||
void SetLength(size_type nNewLen);
|
||||
size_type GetLength() const;
|
||||
|
||||
unsigned char* GetBuffer() const
|
||||
{ return m_pBuffer; }
|
||||
|
||||
size_type read(byte_type* buffer, size_type length);
|
||||
size_type write(const byte_type* buffer, size_type length);
|
||||
|
||||
char* ReadString(char* pBuf, size_type nMax);
|
||||
int GetChar();
|
||||
|
||||
int PutChar(int c);
|
||||
void printf(const char*, ...); ///< \todo implement on MemStream
|
||||
|
||||
void Abort();
|
||||
void Flush();
|
||||
void Close();
|
||||
bool Open(const char *filename, const char *mode);
|
||||
};
|
||||
|
||||
class FileStream : public IDataStream
|
||||
{
|
||||
public:
|
||||
FileStream();
|
||||
virtual ~FileStream();
|
||||
|
||||
int refCount;
|
||||
void IncRef() { refCount++; }
|
||||
void DecRef() { refCount--; if (refCount <= 0) delete this; }
|
||||
|
||||
protected:
|
||||
// DiscFile specific:
|
||||
FILE* m_hFile;
|
||||
bool m_bCloseOnDelete;
|
||||
|
||||
public:
|
||||
position_type GetPosition() const;
|
||||
int Seek(offset_type lOff, int nFrom);
|
||||
void SetLength(size_type nNewLen);
|
||||
size_type GetLength() const;
|
||||
|
||||
size_type read(byte_type* buffer, size_type length);
|
||||
size_type write(const byte_type* buffer, size_type length);
|
||||
|
||||
char* ReadString(char* pBuf, size_type nMax);
|
||||
int GetChar();
|
||||
|
||||
int PutChar(int c);
|
||||
void printf(const char*, ...); ///< completely matches the usual printf behaviour
|
||||
|
||||
void Abort();
|
||||
void Flush();
|
||||
void Close();
|
||||
bool Open(const char *filename, const char *mode);
|
||||
};
|
||||
|
||||
#endif
|
||||
297
libs/profile/profile.cpp
Normal file
297
libs/profile/profile.cpp
Normal file
@@ -0,0 +1,297 @@
|
||||
/*
|
||||
Copyright (c) 2001, Loki software, inc.
|
||||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without modification,
|
||||
are permitted provided that the following conditions are met:
|
||||
|
||||
Redistributions of source code must retain the above copyright notice, this list
|
||||
of conditions and the following disclaimer.
|
||||
|
||||
Redistributions in binary form must reproduce the above copyright notice, this
|
||||
list of conditions and the following disclaimer in the documentation and/or
|
||||
other materials provided with the distribution.
|
||||
|
||||
Neither the name of Loki software nor the names of its contributors may be used
|
||||
to endorse or promote products derived from this software without specific prior
|
||||
written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS''
|
||||
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
|
||||
DIRECT,INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
|
||||
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
//
|
||||
// Application settings load/save
|
||||
//
|
||||
// Leonardo Zide (leo@lokigames.com)
|
||||
//
|
||||
|
||||
#include "profile.h"
|
||||
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
#include <math.h>
|
||||
|
||||
#include "file.h"
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "str.h"
|
||||
|
||||
|
||||
// =============================================================================
|
||||
// Static functions
|
||||
|
||||
bool read_var (const char *filename, const char *section, const char *key, char *value)
|
||||
{
|
||||
char line[1024], *ptr;
|
||||
FILE *rc;
|
||||
|
||||
rc = fopen (filename, "rt");
|
||||
|
||||
if (rc == NULL)
|
||||
return false;
|
||||
|
||||
while (fgets (line, 1024, rc) != 0)
|
||||
{
|
||||
// First we find the section
|
||||
if (line[0] != '[')
|
||||
continue;
|
||||
|
||||
ptr = strchr (line, ']');
|
||||
*ptr = '\0';
|
||||
|
||||
if (strcmp (&line[1], section) == 0)
|
||||
{
|
||||
while (fgets (line, 1024, rc) != 0)
|
||||
{
|
||||
ptr = strchr (line, '=');
|
||||
|
||||
if (ptr == NULL)
|
||||
{
|
||||
// reached the end of the section
|
||||
fclose (rc);
|
||||
return false;
|
||||
}
|
||||
*ptr = '\0';
|
||||
|
||||
// remove spaces
|
||||
while (line[strlen(line)-1] == ' ')
|
||||
line[strlen(line)-1] = '\0';
|
||||
|
||||
if (strcmp (line, key) == 0)
|
||||
{
|
||||
strcpy (value, ptr+1);
|
||||
fclose (rc);
|
||||
|
||||
if (value[strlen (value)-1] == 10 || value[strlen (value)-1] == 13 || value[strlen (value)-1] == 32)
|
||||
value[strlen (value)-1] = 0;
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fclose (rc);
|
||||
return false;
|
||||
}
|
||||
|
||||
static bool save_var (const char *filename, const char *section, const char *key, const char *value)
|
||||
{
|
||||
char line[1024], *ptr;
|
||||
MemStream old_rc;
|
||||
bool found;
|
||||
FILE *rc;
|
||||
|
||||
rc = fopen (filename, "rb");
|
||||
|
||||
if (rc != NULL)
|
||||
{
|
||||
unsigned int len;
|
||||
void *buf;
|
||||
|
||||
fseek (rc, 0, SEEK_END);
|
||||
len = ftell (rc);
|
||||
rewind (rc);
|
||||
buf = malloc (len);
|
||||
fread (buf, len, 1, rc);
|
||||
old_rc.write (reinterpret_cast<MemStream::byte_type*>(buf), len);
|
||||
free (buf);
|
||||
fclose (rc);
|
||||
old_rc.Seek (0, SEEK_SET);
|
||||
}
|
||||
|
||||
// TTimo: changed to binary writing. It doesn't seem to affect linux version, and win32 version was happending a lot of '\n'
|
||||
rc = fopen (filename, "wb");
|
||||
|
||||
if (rc == NULL)
|
||||
return false;
|
||||
|
||||
// First we need to find the section
|
||||
found = false;
|
||||
while (old_rc.ReadString (line, 1024) != NULL)
|
||||
{
|
||||
fputs (line, rc);
|
||||
|
||||
if (line[0] == '[')
|
||||
{
|
||||
ptr = strchr (line, ']');
|
||||
*ptr = '\0';
|
||||
|
||||
if (strcmp (&line[1], section) == 0)
|
||||
{
|
||||
found = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!found)
|
||||
{
|
||||
fputs ("\n", rc);
|
||||
fprintf (rc, "[%s]\n", section);
|
||||
}
|
||||
|
||||
fprintf (rc, "%s=%s\n", key, value);
|
||||
|
||||
while (old_rc.ReadString (line, 1024) != NULL)
|
||||
{
|
||||
ptr = strchr (line, '=');
|
||||
|
||||
if (ptr != NULL)
|
||||
{
|
||||
*ptr = '\0';
|
||||
|
||||
if (strcmp (line, key) == 0)
|
||||
break;
|
||||
|
||||
*ptr = '=';
|
||||
fputs (line, rc);
|
||||
}
|
||||
else
|
||||
{
|
||||
fputs (line, rc);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
while (old_rc.ReadString (line, 1024) != NULL)
|
||||
fputs (line, rc);
|
||||
|
||||
fclose (rc);
|
||||
return true;
|
||||
}
|
||||
|
||||
// =============================================================================
|
||||
// Global functions
|
||||
|
||||
bool profile_save_int (const char *filename, const char *section, const char *key, int value)
|
||||
{
|
||||
char buf[16];
|
||||
sprintf (buf, "%d", value);
|
||||
return save_var (filename, section, key, buf);
|
||||
}
|
||||
|
||||
bool profile_save_float (const char *filename, const char *section, const char *key, float value)
|
||||
{
|
||||
char buf[16];
|
||||
sprintf (buf, "%f", value);
|
||||
return save_var (filename, section, key, buf);
|
||||
}
|
||||
|
||||
bool profile_save_string (const char * filename, const char *section, const char *key, const char *value)
|
||||
{
|
||||
return save_var (filename, section, key, value);
|
||||
}
|
||||
|
||||
bool profile_save_buffer (const char * rc_path, const char *name, void *buffer, unsigned int size)
|
||||
{
|
||||
bool ret = false;
|
||||
char filename[1024];
|
||||
sprintf (filename, "%s/%s.bin", rc_path, name);
|
||||
FILE *f;
|
||||
|
||||
f = fopen (filename, "wb");
|
||||
|
||||
if (f != NULL)
|
||||
{
|
||||
if (fwrite (buffer, size, 1, f) == 1)
|
||||
ret = true;
|
||||
|
||||
fclose (f);
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
bool profile_load_buffer (const char * rc_path, const char *name, void *buffer, unsigned int *plSize)
|
||||
{
|
||||
char filename[1024];
|
||||
sprintf (filename, "%s/%s.bin", rc_path, name);
|
||||
bool ret = false;
|
||||
unsigned int len;
|
||||
FILE *f;
|
||||
|
||||
f = fopen (filename, "rb");
|
||||
|
||||
if (f != NULL)
|
||||
{
|
||||
fseek (f, 0, SEEK_END);
|
||||
len = ftell (f);
|
||||
rewind (f);
|
||||
|
||||
if (len > *plSize)
|
||||
len = *plSize;
|
||||
else
|
||||
*plSize = len;
|
||||
|
||||
if (fread (buffer, len, 1, f) == 1)
|
||||
ret = true;
|
||||
|
||||
fclose (f);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
int profile_load_int (const char *filename, const char *section, const char *key, int default_value)
|
||||
{
|
||||
char value[1024];
|
||||
|
||||
if (read_var (filename, section, key, value))
|
||||
return atoi (value);
|
||||
else
|
||||
return default_value;
|
||||
}
|
||||
|
||||
float profile_load_float (const char *filename, const char *section, const char *key, float default_value)
|
||||
{
|
||||
char value[1024];
|
||||
|
||||
if (read_var (filename, section, key, value))
|
||||
return static_cast<float>(atof(value));
|
||||
else
|
||||
return default_value;
|
||||
}
|
||||
|
||||
char* profile_load_string (const char *filename, const char *section, const char *key, const char *default_value)
|
||||
{
|
||||
static Str ret;
|
||||
char value[1024];
|
||||
|
||||
if (read_var (filename, section, key, value))
|
||||
ret = value;
|
||||
else
|
||||
ret = default_value;
|
||||
|
||||
return (char*)ret.GetBuffer();
|
||||
}
|
||||
48
libs/profile/profile.h
Normal file
48
libs/profile/profile.h
Normal file
@@ -0,0 +1,48 @@
|
||||
/*
|
||||
Copyright (c) 2001, Loki software, inc.
|
||||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without modification,
|
||||
are permitted provided that the following conditions are met:
|
||||
|
||||
Redistributions of source code must retain the above copyright notice, this list
|
||||
of conditions and the following disclaimer.
|
||||
|
||||
Redistributions in binary form must reproduce the above copyright notice, this
|
||||
list of conditions and the following disclaimer in the documentation and/or
|
||||
other materials provided with the distribution.
|
||||
|
||||
Neither the name of Loki software nor the names of its contributors may be used
|
||||
to endorse or promote products derived from this software without specific prior
|
||||
written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS''
|
||||
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
|
||||
DIRECT,INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
|
||||
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#if !defined(INCLUDED_PROFILE_PROFILE_H)
|
||||
#define INCLUDED_PROFILE_PROFILE_H
|
||||
|
||||
// profile functions - kind of utility lib
|
||||
// they are kind of dumb, they expect to get the path to the .ini file or to the prefs directory when called
|
||||
// load_buffer and save_buffer expect the path only, theyll build a $(pszName).bin file
|
||||
bool profile_save_int (const char *filename, const char *section, const char *key, int value);
|
||||
bool profile_save_float (const char *filename, const char *section, const char *key, float value);
|
||||
bool profile_save_string (const char *filename, const char *section, const char *key, const char *value);
|
||||
bool profile_save_buffer (const char *rc_path, const char *pszName, void *pvBuf, unsigned int lSize);
|
||||
bool profile_load_buffer (const char *rc_path, const char *pszName, void *pvBuf, unsigned int *plSize);
|
||||
int profile_load_int (const char *filename, const char *section, const char *key, int default_value);
|
||||
float profile_load_float (const char *filename, const char *section, const char *key, float default_value);
|
||||
char* profile_load_string (const char *filename, const char *section, const char *key, const char *default_value);
|
||||
// used in the command map code
|
||||
bool read_var (const char *filename, const char *section, const char *key, char *value);
|
||||
|
||||
#endif
|
||||
129
libs/profile/profile.vcproj
Normal file
129
libs/profile/profile.vcproj
Normal file
@@ -0,0 +1,129 @@
|
||||
<?xml version="1.0" encoding="Windows-1252"?>
|
||||
<VisualStudioProject
|
||||
ProjectType="Visual C++"
|
||||
Version="7.10"
|
||||
Name="profile"
|
||||
ProjectGUID="{853632F4-6420-40C5-B80B-38B678E472B8}"
|
||||
Keyword="Win32Proj">
|
||||
<Platforms>
|
||||
<Platform
|
||||
Name="Win32"/>
|
||||
</Platforms>
|
||||
<Configurations>
|
||||
<Configuration
|
||||
Name="Debug|Win32"
|
||||
OutputDirectory="Debug"
|
||||
IntermediateDirectory="Debug"
|
||||
ConfigurationType="4"
|
||||
CharacterSet="2">
|
||||
<Tool
|
||||
Name="VCCLCompilerTool"
|
||||
Optimization="0"
|
||||
AdditionalIncludeDirectories=""../../include";"../../libs";"../../../STLport-4.6/stlport";"../../../libxml2-2.6/include""
|
||||
PreprocessorDefinitions="WIN32;_DEBUG;_LIB"
|
||||
StringPooling="TRUE"
|
||||
MinimalRebuild="TRUE"
|
||||
ExceptionHandling="FALSE"
|
||||
BasicRuntimeChecks="0"
|
||||
RuntimeLibrary="3"
|
||||
UsePrecompiledHeader="0"
|
||||
BrowseInformation="0"
|
||||
WarningLevel="3"
|
||||
Detect64BitPortabilityProblems="TRUE"
|
||||
DebugInformationFormat="4"
|
||||
DisableSpecificWarnings="4610;4510;4512;4505;4100;4127"/>
|
||||
<Tool
|
||||
Name="VCCustomBuildTool"/>
|
||||
<Tool
|
||||
Name="VCLibrarianTool"
|
||||
OutputFile="$(OutDir)/profile.lib"/>
|
||||
<Tool
|
||||
Name="VCMIDLTool"/>
|
||||
<Tool
|
||||
Name="VCPostBuildEventTool"/>
|
||||
<Tool
|
||||
Name="VCPreBuildEventTool"/>
|
||||
<Tool
|
||||
Name="VCPreLinkEventTool"/>
|
||||
<Tool
|
||||
Name="VCResourceCompilerTool"/>
|
||||
<Tool
|
||||
Name="VCWebServiceProxyGeneratorTool"/>
|
||||
<Tool
|
||||
Name="VCXMLDataGeneratorTool"/>
|
||||
<Tool
|
||||
Name="VCManagedWrapperGeneratorTool"/>
|
||||
<Tool
|
||||
Name="VCAuxiliaryManagedWrapperGeneratorTool"/>
|
||||
</Configuration>
|
||||
<Configuration
|
||||
Name="Release|Win32"
|
||||
OutputDirectory="Release"
|
||||
IntermediateDirectory="Release"
|
||||
ConfigurationType="4"
|
||||
CharacterSet="2">
|
||||
<Tool
|
||||
Name="VCCLCompilerTool"
|
||||
GlobalOptimizations="TRUE"
|
||||
InlineFunctionExpansion="2"
|
||||
EnableIntrinsicFunctions="TRUE"
|
||||
OptimizeForWindowsApplication="TRUE"
|
||||
AdditionalIncludeDirectories=""../../include";"../../libs";"../../../STLport-4.6/stlport";"../../../libxml2-2.6/include""
|
||||
PreprocessorDefinitions="WIN32;NDEBUG;_LIB"
|
||||
StringPooling="TRUE"
|
||||
ExceptionHandling="FALSE"
|
||||
RuntimeLibrary="2"
|
||||
BufferSecurityCheck="FALSE"
|
||||
UsePrecompiledHeader="0"
|
||||
WarningLevel="4"
|
||||
Detect64BitPortabilityProblems="TRUE"
|
||||
DebugInformationFormat="3"
|
||||
DisableSpecificWarnings="4610;4510;4512;4505;4100;4127"/>
|
||||
<Tool
|
||||
Name="VCCustomBuildTool"/>
|
||||
<Tool
|
||||
Name="VCLibrarianTool"
|
||||
OutputFile="$(OutDir)/profile.lib"/>
|
||||
<Tool
|
||||
Name="VCMIDLTool"/>
|
||||
<Tool
|
||||
Name="VCPostBuildEventTool"/>
|
||||
<Tool
|
||||
Name="VCPreBuildEventTool"/>
|
||||
<Tool
|
||||
Name="VCPreLinkEventTool"/>
|
||||
<Tool
|
||||
Name="VCResourceCompilerTool"/>
|
||||
<Tool
|
||||
Name="VCWebServiceProxyGeneratorTool"/>
|
||||
<Tool
|
||||
Name="VCXMLDataGeneratorTool"/>
|
||||
<Tool
|
||||
Name="VCManagedWrapperGeneratorTool"/>
|
||||
<Tool
|
||||
Name="VCAuxiliaryManagedWrapperGeneratorTool"/>
|
||||
</Configuration>
|
||||
</Configurations>
|
||||
<References>
|
||||
</References>
|
||||
<Files>
|
||||
<Filter
|
||||
Name="src"
|
||||
Filter="">
|
||||
<File
|
||||
RelativePath=".\file.cpp">
|
||||
</File>
|
||||
<File
|
||||
RelativePath=".\file.h">
|
||||
</File>
|
||||
<File
|
||||
RelativePath=".\profile.cpp">
|
||||
</File>
|
||||
<File
|
||||
RelativePath=".\profile.h">
|
||||
</File>
|
||||
</Filter>
|
||||
</Files>
|
||||
<Globals>
|
||||
</Globals>
|
||||
</VisualStudioProject>
|
||||
Reference in New Issue
Block a user