1 ¼Ò½º
#ifndef __RTF_H__
#define __RTF_H__
#include <string>
#include <set>
//////////////////////////////////////////////////////////////////////////////
/// \class RTF
/// \brief °£´ÜÇÑ RTF ÆÄÀÏ ¸®Æ÷Æ® »ý¼ºÀ» À§ÇÑ Å¬·¡½º.
///
/// ¾îµð±îÁö³ª °£´ÜÇÑ ·¹Æ÷Æ® Ãâ·Â¸¸À» ¸ñÇ¥·Î Çϴ Ŭ·¡½ºÀ̱⠶§¹®¿¡, ±â´ÉÀÌ
/// ¸Å¿ì ºÎ½ÇÇÏ°í ´À¸®´Ù. ÇÏÁö¸¸ °³¼±ÇؾßÇÒ Çʿ伺À» ¸ø ´À³¢°Ú´Ù.
///
/// <pre>
/// RTF rtf;
///
/// RTF::STYLES bold;
/// bold.insert(RTF::BOLD);
///
/// rtf.add(RTF::DARK_BLUE, bold, "Á¦¸ñ");
/// rtf.add("\tù¹øÂ° Ç׸ñ : %s", "¹¹°¡ ÁÁÀ»±î?");
/// rtf.add("\tµÎ¹øÂ° Ç׸ñ : %s", "À̰ÍÀÌ ÁÁÀ»±î?");
///
/// rtf.flush("sample.rtf");
/// </pre>
//////////////////////////////////////////////////////////////////////////////
class RTF
{
public:
/// »ö±ò ¹è¿
enum Color
{
BLACK = 0, WHITE, GREY, RED, GREEN, BLUE, CYAN, YELLOW, MAGENTA,
DARK_RED, DARK_GREEN, DARK_BLUE, LIGHT_RED, LIGHT_GREEN, LIGHT_BLUE, ORANGE
};
/// ½ºÅ¸ÀÏ ¹è¿
enum Style
{
BOLD = 0, ITALIC, UNDERLINE, STRIKE
};
typedef std::set<Style> STYLES;
private:
/// ¶óÀÎ Á¤ÀÇ
typedef struct LINE
{
std::string text; ///< ¶óÀÎ ¹®ÀÚ¿
int color; ///< ¶óÀÎ »ö±ò
STYLES styles; ///< ¶óÀÎ ½ºÅ¸ÀÏ
} LINE;
typedef std::list<LINE> LINES;
LINES m_Lines; ///< ±â·ÏÇÒ ¶óÀεé
int m_DefaultColor; ///< ±âº» »ö±ò
STYLES m_DefaultStyles; ///< ±âº» ½ºÅ¸ÀÏ
public:
/// \brief »ý¼ºÀÚ
RTF();
/// \brief ¼Ò¸êÀÚ
virtual ~RTF();
public:
/// \brief ±âº» »ö±ò°ú ½ºÅ¸ÀÏÀ» °¡Áö´Â ¶óÀÎÀ» Ãß°¡ÇÑ´Ù.
/// \param fmt °¡º¯ ÀÎÀÚ Æ÷¸Ë
/// \param ... °¡º¯ ÀÎÀÚµé
void add(const char* fmt, ...);
/// \brief »ö±ò°ú ½ºÅ¸ÀÏÀ» ÁöÁ¤Çؼ ¶óÀÎÀ» Ãß°¡ÇÑ´Ù.
/// \param color »ö±ò
/// \param styles ½ºÅ¸ÀÏ
/// \param fmt °¡º¯ ÀÎÀÚ Æ÷¸Ë
/// \param ... °¡º¯ ÀÎÀÚµé
void add(Color color, const STYLES& styles, const char* fmt, ...);
/// \brief °¡Áö°í ÀÖ´Â ³»¿ëÀ» ½ÇÁ¦ ÆÄÀÏ¿¡´Ù°¡ ±â·ÏÇÑ´Ù.
/// \param filename ÆÄÀÏ À̸§
void flush(const std::string& filename);
public:
/// \name ±âº» »ö±ò ¹Ýȯ/¼³Á¤
/// \{
int getDefaultColor() const { return m_DefaultColor; }
void setDefaultColor(Color color) { m_DefaultColor = color; }
/// \}
/// \name ±âº» ½ºÅ¸ÀÏ ¹Ýȯ/¼³Á¤
/// \{
const STYLES& getDefaultStyles() const { return m_DefaultStyles; }
void setDefaultStyles(const STYLES& styles) { m_DefaultStyles = styles; }
/// \}
};
#endif //__RTF_H__ RTF.cpp
#include "RTF.h"
#include <fstream>
#include <stdio.h>
//////////////////////////////////////////////////////////////////////////////
/// \brief »ý¼ºÀÚ
//////////////////////////////////////////////////////////////////////////////
RTF::RTF()
: m_DefaultColor(BLACK)
{
}
//////////////////////////////////////////////////////////////////////////////
/// \brief ¼Ò¸êÀÚ
//////////////////////////////////////////////////////////////////////////////
RTF::~RTF()
{
m_Lines.clear();
}
//////////////////////////////////////////////////////////////////////////////
/// \brief ±âº» »ö±ò°ú ½ºÅ¸ÀÏÀ» °¡Áö´Â ¶óÀÎÀ» Ãß°¡ÇÑ´Ù.
/// \param fmt °¡º¯ ÀÎÀÚ Æ÷¸Ë
/// \param ... °¡º¯ ÀÎÀÚµé
//////////////////////////////////////////////////////////////////////////////
void RTF::add(const char* fmt, ...)
{
int nchars = 0;
int buflen = 512;
char* buf = NULL;
va_list valist;
va_start(valist, fmt);
do {
buflen = buflen * 2;
buf = reinterpret_cast<char*>(::realloc(buf, buflen));
memset(buf, 0, buflen);
nchars = vsprintf(buf, fmt, valist);
} while (nchars < 0 || nchars > buflen);
va_end(valist);
LINE line;
line.text = buf;
line.color = m_DefaultColor;
line.styles = m_DefaultStyles;
m_Lines.push_back(line);
::free(buf);
}
//////////////////////////////////////////////////////////////////////////////
/// \brief »ö±ò°ú ½ºÅ¸ÀÏÀ» ÁöÁ¤Çؼ ¶óÀÎÀ» Ãß°¡ÇÑ´Ù.
/// \param color »ö±ò
/// \param styles ½ºÅ¸ÀÏ
/// \param fmt °¡º¯ ÀÎÀÚ Æ÷¸Ë
/// \param ... °¡º¯ ÀÎÀÚµé
//////////////////////////////////////////////////////////////////////////////
void RTF::add(Color color, const STYLES& styles, const char* fmt, ...)
{
int nchars = 0;
int buflen = 512;
char* buf = NULL;
va_list valist;
va_start(valist, fmt);
do {
buflen = buflen * 2;
buf = reinterpret_cast<char*>(::realloc(buf, buflen));
memset(buf, 0, buflen);
nchars = vsprintf(buf, fmt, valist);
} while (nchars < 0 || nchars > buflen);
va_end(valist);
LINE line;
line.text = buf;
line.color = color;
line.styles = styles;
m_Lines.push_back(line);
::free(buf);
}
//////////////////////////////////////////////////////////////////////////////
/// \brief °¡Áö°í ÀÖ´Â ³»¿ëÀ» ½ÇÁ¦ ÆÄÀÏ¿¡´Ù°¡ ±â·ÏÇÑ´Ù.
/// \param filename ÆÄÀÏ À̸§
//////////////////////////////////////////////////////////////////////////////
void RTF::flush(const std::string& filename)
{
std::ofstream file(filename.c_str(), std::ios::out | std::ios::trunc);
if (!file) return;
// RTF Çì´õ¸¦ ¾´´Ù.
file << "{\\rtf1\\ansi\\ansicpg949\\deff0{\\fonttbl{\\f0 ±¼¸²Ã¼;}}\\fs20\n" << std::endl;
// Ä÷¯ Å×À̺íÀ» ¾´´Ù.
file << "{\\colortbl;" << std::endl;
file << "\\red255\\green255\\blue255;" << std::endl;
file << "\\red128\\green128\\blue128;" << std::endl;
file << "\\red255\\green0\\blue0;" << std::endl;
file << "\\red0\\green255\\blue0;" << std::endl;
file << "\\red0\\green0\\blue255;" << std::endl;
file << "\\red0\\green255\\blue255;" << std::endl;
file << "\\red255\\green255\\blue0;" << std::endl;
file << "\\red255\\green0\\blue255;" << std::endl;
file << "\\red128\\green0\\blue0;" << std::endl;
file << "\\red0\\green128\\blue0;" << std::endl;
file << "\\red0\\green0\\blue128;" << std::endl;
file << "\\red255\\green128\\blue128;" << std::endl;
file << "\\red128\\green255\\blue128;" << std::endl;
file << "\\red128\\green128\\blue255;" << std::endl;
file << "\\red255\\green128\\blue0;" << std::endl;
file << "}" << std::endl;
for (LINES::const_iterator itr(m_Lines.begin());
itr != m_Lines.end(); ++itr)
{
const LINE& line = *itr;
file << "{\\pard ";
if (line.styles.find(BOLD) != line.styles.end())
file << "\\b";
if (line.styles.find(ITALIC) != line.styles.end())
file << "\\i";
if (line.styles.find(UNDERLINE) != line.styles.end())
file << "\\ul";
if (line.styles.find(STRIKE) != line.styles.end())
file << "\\strike";
file << "\\cf" << line.color << " ";
file << line.text;
file << "\\par}";
}
// ³¡ ºÎºÐÀÇ °ýÈ£¸¦ ½á ÁØ´Ù.
file << "}" << std::endl;
}
SeriousMoin v1 (koMoinMoin 1.0a4 Modified)