00001
00002
00003
00004
00005
00006 #ifndef __FORMAT_H__
00007 #define __FORMAT_H__
00008
00009 #include <string>
00010 #include <vector>
00011
00012 #include "externs.h"
00013
00014
00015
00016 enum fs_op_type
00017 {
00018 FSOP_COLOUR,
00019 FSOP_TEXT,
00020 };
00021
00022 class formatted_string
00023 {
00024 public:
00025 formatted_string(int init_colour = 0);
00026 explicit formatted_string(const std::string &s, int init_colour = 0);
00027
00028 operator std::string() const;
00029 void display(int start = 0, int end = -1) const;
00030 std::string tostring(int start = 0, int end = -1) const;
00031 std::string to_colour_string() const;
00032
00033 void cprintf(const char *s, ...);
00034 void cprintf(const std::string &s);
00035 void add_glyph(glyph g);
00036 void textcolor(int color);
00037 formatted_string substr(size_t index, size_t length=std::string::npos) const;
00038 void all_caps();
00039
00040 void clear();
00041 bool empty();
00042
00043 void swap(formatted_string& other);
00044
00045 std::string::size_type length() const;
00046 std::string html_dump() const;
00047
00048 bool operator < (const formatted_string &other) const;
00049 const formatted_string &operator += (const formatted_string &other);
00050 char &operator [] (size_t idx);
00051
00052 public:
00053 static formatted_string parse_string(
00054 const std::string &s,
00055 bool eot_ends_format = true,
00056 bool (*process_tag)(const std::string &tag) = NULL,
00057 int main_colour = LIGHTGREY);
00058
00059 static void parse_string_to_multiple(
00060 const std::string &s,
00061 std::vector<formatted_string> &out);
00062
00063 static int get_colour(const std::string &tag);
00064
00065 private:
00066 int find_last_colour() const;
00067
00068 static void parse_string1(
00069 const std::string &s,
00070 formatted_string &fs,
00071 std::vector<int> &colour_stack,
00072 bool (*process_tag)(const std::string &tag));
00073
00074 public:
00075 struct fs_op
00076 {
00077 fs_op_type type;
00078 int x, y;
00079 bool relative;
00080 std::string text;
00081
00082 fs_op(int color)
00083 : type(FSOP_COLOUR), x(color), y(-1), relative(false), text()
00084 {
00085 }
00086
00087 fs_op(const std::string &s)
00088 : type(FSOP_TEXT), x(-1), y(-1), relative(false), text(s)
00089 {
00090 }
00091
00092 operator fs_op_type () const
00093 {
00094 return type;
00095 }
00096 void display() const;
00097 };
00098
00099 typedef std::vector<fs_op> oplist;
00100 oplist ops;
00101 };
00102
00103 int count_linebreaks(const formatted_string& fs);
00104
00105 int tagged_string_tag_length(const std::string& s);
00106 int tagged_string_printable_length(const std::string& s);
00107 std::string tagged_string_substr(const std::string& s, int start, int end);
00108 void display_tagged_block(const std::string& s);
00109
00110 #endif