Go to the documentation of this file.00001 #pragma once
00002
00003
00004 #define IDS_FILE_READ_ERROR 20001
00005 #define IDS_FILE_WRITE_ERROR 20002
00006 #define IDS_CANNOT_OPEN_FILE 20003
00007 #define IDS_CANNOT_OPEN_FILE_FOR_WRITING 20004
00008
00009 #define _CRT_SECURE_NO_WARNINGS
00010
00011 #define WIN32_LEAN_AND_MEAN
00012 #include "windows.h"
00013
00014 #include "tchar.h"
00015 #include "wchar.h"
00016 #include "malloc.h"
00017 #include "string.h"
00018 #include "assert.h"
00019
00020
00021
00022 #define base_strlen(s) strlen (s)
00023 #define Hnew new
00024 #define tsizeof(x) (sizeof (x) / sizeof (TCHAR))
00025 #define T2B(x) ((x) * sizeof (TCHAR))
00026
00027
00028
00029 TCHAR * vFormatPString (const TCHAR *message, va_list ap);
00030 TCHAR * _cdecl FormatPString (const TCHAR *message, ...);
00031
00032
00033 TCHAR * copy_string (const TCHAR *s);
00034
00035
00036 char * AtoA (const char *a);
00037
00038
00039
00040
00041
00042
00043
00044 void * alloc_block (void *current_block, int datalen);
00045
00046
00047 void free_block (void *block);
00048
00049
00050 void * copy_block (const void *block);
00051
00052
00053 int get_blocklen (const void *block);
00054
00055
00056 int get_buflen (const void *block);
00057
00058
00059
00060
00061
00062 #define TF_READ 1 // existing file
00063 #define TF_WRITE 2 // new file
00064 #define TF_APPEND 3 // append to existing file
00065
00066
00067 #define TF_ANSI 1
00068 #define TF_UTF16LE 2
00069 #define TF_UTF16BE 3
00070 #define TF_UTF8 4
00071
00072 #define TF_UNIX 0x100 // write out line-ends as \n, not \r\n
00073 #define TF_NO_BOM_CHECK 0x200 // don't check for a BOM if explicit encoding
00074
00075
00076
00077 #define TF_EOF -1
00078 #define TF_ERROR -2
00079
00080
00081
00082
00083
00084 #define LEAD_OFFSET (0xD800 - (0x10000 >> 10))
00085 #define SURROGATE_OFFSET (0x10000 - (0xD800 << 10) - 0xDC00)
00086 #define C_TO_LEAD(c) (LEAD_OFFSET + ((c) >> 10))
00087 #define C_TO_TRAIL(c) (0xDC00 + ((c) & 0x3FF))
00088 #define SP_TO_C(lead, trail) (((lead) << 10) + (trail) + SURROGATE_OFFSET)
00089
00090
00091 class TextFileReader;
00092 class TextFileWriter;
00093 class ANSI_TextFileReader;
00094
00095
00096 class TextFile
00097 {
00098 public:
00099
00100
00101 TextFile ();
00102
00103
00104 ~TextFile ();
00105
00106
00107 int Open (const TCHAR *filename, int filemode, int encoding = 0, int bufsize = 0);
00108
00109
00110 int Close (void);
00111
00112
00113 int Flush (void);
00114
00115
00116
00117 int ReadLine (BOOL *data_lost, TCHAR **line_buf);
00118
00119
00120
00121
00122
00123 int ReadChar (void);
00124
00125
00126
00127
00128
00129 int WriteString (BOOL *data_lost, const TCHAR *s);
00130
00131
00132 int FormatString (BOOL *data_lost, const TCHAR *s, ...);
00133
00134
00135 int vFormatString (BOOL *data_lost, const TCHAR *s, va_list ap);
00136
00137
00138
00139
00140 int WriteChar (BOOL *data_lost, int ch);
00141
00142
00143 int GetEncoding (void) { return this->encoding; }
00144
00145
00146 void SetCodePage (int code_page) { this->code_page = code_page; }
00147
00148
00149 void SetUnknownChar (const char *unknown_char);
00150
00151
00152
00153
00154 void SetAnsiPassThru (bool passthru);
00155
00156
00157
00158 inline bool GetAnsiPassThru (void) { return this->ansi_passthru; }
00159
00160 private:
00161
00162
00163 int CreateFileReader (void);
00164
00165
00166 void CreateFileWriter (void);
00167
00168 private:
00169 TCHAR *filename;
00170 int filemode;
00171 int encoding;
00172 int encoding_flags;
00173 bool ansi_passthru;
00174 int bufsize;
00175 HANDLE hFile;
00176 TextFileReader *file_reader;
00177 TextFileWriter *file_writer;
00178 bool need_bom;
00179 int code_page;
00180 char *unknown_char;
00181 };