summaryrefslogtreecommitdiffstats
path: root/crawl-ref/source/tags.h
diff options
context:
space:
mode:
authorpauldubois <pauldubois@c06c8d41-db1a-0410-9941-cceddc491573>2008-03-17 05:54:42 +0000
committerpauldubois <pauldubois@c06c8d41-db1a-0410-9941-cceddc491573>2008-03-17 05:54:42 +0000
commit8ba172496400cc74a2fa7d343d9859339f1a0f5d (patch)
tree09715fca1c835aca87d0da9d629ad5f9e0157e54 /crawl-ref/source/tags.h
parent314a2c0efb7083bc9703b6e2f6ee4e054d000cc1 (diff)
downloadcrawl-ref-8ba172496400cc74a2fa7d343d9859339f1a0f5d.tar.gz
crawl-ref-8ba172496400cc74a2fa7d343d9859339f1a0f5d.zip
Cleanup/refactoring of tags.cc. No functional changes. I've been
running with and without this patch applied for about a week, and none of my saves have broken, so I'm ready to commit it. - Tag system no longer uses one big (shared!) global buffer. This was the original impetus behind the change... - Change every use of tagHeader into reader or writer (touches a lot). - Split tagHeader into two classes: reader and writer. Turns out every place that used tagHeader only cared about reading or writing and not about tags at all. There was nothing left in tagHeader, so it disappeared along with a bunch of grotty special-case code. - Not done: merge the files.cc read/writeThing code with the tags.cc marshall/unmarshallThing code. This patch is big enough already. git-svn-id: https://crawl-ref.svn.sourceforge.net/svnroot/crawl-ref/trunk@3685 c06c8d41-db1a-0410-9941-cceddc491573
Diffstat (limited to 'crawl-ref/source/tags.h')
-rw-r--r--crawl-ref/source/tags.h137
1 files changed, 56 insertions, 81 deletions
diff --git a/crawl-ref/source/tags.h b/crawl-ref/source/tags.h
index d93d995035..fc2c176b39 100644
--- a/crawl-ref/source/tags.h
+++ b/crawl-ref/source/tags.h
@@ -18,7 +18,7 @@
enum tag_type // used during save/load process to identify data blocks
{
- TAG_VERSION = 0, // should NEVER be read in!
+ TAG_NO_TAG = 0, // should NEVER be read in!
TAG_YOU = 1, // 'you' structure
TAG_YOU_ITEMS, // your items
TAG_YOU_DUNGEON, // dungeon specs (stairs, branches, features)
@@ -41,107 +41,82 @@ enum tag_file_type // file types supported by tag system
TAGTYPE_PLAYER_NAME // Used only to read the player name
};
-struct tagHeader
-{
- short tagID;
- long offset;
+/* ***********************************************************************
+ * writer API
+ * *********************************************************************** */
- // File handle for direct file writes.
- FILE *file;
+class writer
+{
+public:
+ writer(FILE* output)
+ : _file(output), _pbuf(0) {}
+ writer(std::vector<unsigned char>* poutput)
+ : _file(0), _pbuf(poutput) {}
- tagHeader() : tagID(0), offset(0L), file(NULL) { }
- tagHeader(FILE *f) : tagID(0), offset(0L), file(f) { }
- unsigned char readByte();
void writeByte(unsigned char byte);
void write(const void *data, size_t size);
- void read(void *data, size_t size);
- void advance(int skip);
-};
-
-// last updated 22jan2001 {gdl}
-/* ***********************************************************************
- * called from: files tags
- * *********************************************************************** */
-int write2(FILE * file, const char *buffer, unsigned int count);
+private:
+ FILE* _file;
+ std::vector<unsigned char>* _pbuf;
+};
-// last updated 22jan2001 {gdl}
-/* ***********************************************************************
- * called from: files tags
- * *********************************************************************** */
-int read2(FILE * file, char *buffer, unsigned int count);
-
+void marshallByte (writer &, char );
+void marshallShort (writer &, short );
+void marshallLong (writer &, long );
+void marshallFloat (writer &, float );
+void marshallBoolean (writer &, bool );
+void marshallString (writer &, const std::string &, int maxSize = 0);
+void marshallCoord (writer &, const coord_def &);
+void marshallItem (writer &, const item_def &);
-// last updated 22jan2001 {gdl}
/* ***********************************************************************
- * called from: files tags
+ * reader API
* *********************************************************************** */
-void marshallByte(tagHeader &th, char data);
-void marshallShort(tagHeader &th, short data);
-void marshallLong(tagHeader &th, long data);
-void marshallFloat(tagHeader &th, float data);
-void marshallBoolean(tagHeader &th, bool data);
-void marshallString(tagHeader &th, const std::string &data,
- int maxSize = 0);
-void marshallCoord(tagHeader &th, const coord_def &c);
-void marshallItem(tagHeader &th, const item_def &item);
-
-// last updated 22jan2001 {gdl}
-/* ***********************************************************************
- * called from: tags files
- * *********************************************************************** */
-char unmarshallByte(tagHeader &th);
-short unmarshallShort(tagHeader &th);
-long unmarshallLong(tagHeader &th);
-float unmarshallFloat(tagHeader &th);
-bool unmarshallBoolean(tagHeader &th);
-int unmarshallCString(tagHeader &th, char *data, int maxSize);
-std::string unmarshallString(tagHeader &th, int maxSize = 1000);
-void unmarshallCoord(tagHeader &th, coord_def &c);
-void unmarshallItem(tagHeader &th, item_def &item);
-
-std::string make_date_string( time_t in_date );
-time_t parse_date_string( char[20] );
-// last updated 22jan2001 {gdl}
-/* ***********************************************************************
- * called from: acr
- * *********************************************************************** */
-void tag_init(long largest_tag = 100000);
+class reader
+{
+public:
+ reader(FILE* input)
+ : _file(input), _pbuf(0), _read_offset(0) {}
+ reader(const std::vector<unsigned char>& input)
+ : _file(0), _pbuf(&input), _read_offset(0) {}
+ unsigned char readByte();
+ void read(void *data, size_t size);
-// last updated 22jan2001 {gdl}
-/* ***********************************************************************
- * called from: files
- * *********************************************************************** */
-void tag_construct(tagHeader &th, int i);
+private:
+ FILE* _file;
+ const std::vector<unsigned char>* _pbuf;
+ unsigned int _read_offset;
+};
+char unmarshallByte (reader &);
+short unmarshallShort (reader &);
+long unmarshallLong (reader &);
+float unmarshallFloat (reader &);
+bool unmarshallBoolean (reader &);
+int unmarshallCString (reader &, char *data, int maxSize);
+std::string unmarshallString (reader &, int maxSize = 1000);
+void unmarshallCoord (reader &, coord_def &c);
+void unmarshallItem (reader &, item_def &item);
-// last updated 22jan2001 {gdl}
/* ***********************************************************************
- * called from: files
+ * Tag interface
* *********************************************************************** */
-void tag_write(tagHeader &th, FILE *saveFile);
-
-// last updated 22jan2001 {gdl}
-/* ***********************************************************************
- * called from: files
- * *********************************************************************** */
+tag_type tag_read(FILE* inf, char minorVersion);
+void tag_write(tag_type tagID, FILE* outf);
void tag_set_expected(char tags[], int fileType);
-
-
-// last updated 22jan2001 {gdl}
-/* ***********************************************************************
- * called from: files
- * *********************************************************************** */
void tag_missing(int tag, char minorVersion);
-
-// last updated 22jan2001 {gdl}
/* ***********************************************************************
- * called from: files
+ * misc
* *********************************************************************** */
-int tag_read(FILE *fp, char minorVersion);
+
+int write2(FILE * file, const void *buffer, unsigned int count);
+int read2(FILE * file, void *buffer, unsigned int count);
+std::string make_date_string( time_t in_date );
+time_t parse_date_string( char[20] );
#endif // TAGS_H