00001 #include "xmltk.h"
00002
00003 #ifndef WIN32
00004 #include <sys/sysinfo.h>
00005 #include <sys/types.h>
00006 #include <unistd.h>
00007 #endif
00008
00009 BOOL IsEqualCLIID(const CLIID *riid1, const CLIID *riid2)
00010 {
00011 return (memcmp(riid1, riid2, sizeof(*riid1)) == 0);
00012 }
00013
00014
00015 static const unsigned char c_chMask = 0x7F;
00016 static const unsigned char c_chContinue = 0x80;
00017
00018 void fwriteMultiByteUINT(IStream *pstm, unsigned int uInt)
00019 {
00020 unsigned char rgch[5];
00021 int i;
00022
00023 for (i = 0; i < ARRAYSIZE(rgch); i++)
00024 {
00025 rgch[i] = (uInt & c_chMask) | c_chContinue;
00026 uInt >>= 7;
00027 if (uInt <= 0)
00028 {
00029 break;
00030 }
00031 }
00032
00033 rgch[0] &= ~c_chContinue;
00034
00035 for ( ; i >= 0; i--)
00036 {
00037 pstm->WriteChar(rgch[i]);
00038 }
00039 }
00040
00041 bool freadMultiByteUINT(IStream *pstm, unsigned int *puInt)
00042 {
00043 *puInt = 0;
00044 int i;
00045
00046
00047
00048
00049 for (i = 0; i < 5; i++)
00050 {
00051 int ch = pstm->ReadChar();
00052 if (ch == EOF)
00053 {
00054 return false;
00055 }
00056 else
00057 {
00058 *puInt <<= 7;
00059 *puInt |= (ch & c_chMask);
00060 if (!(ch & c_chContinue))
00061 {
00062 return true;
00063 }
00064 }
00065 }
00066
00067 return false;
00068 }
00069
00070 void funread(void *buffer, size_t size, size_t count, FILE *stream)
00071 {
00072 for (int i = count - 1; i >= 0; i--)
00073 {
00074 for (int j = (size/sizeof(char)) - 1; j >= 0; j--)
00075 {
00076 void *bufferT = (void*)((size_t)buffer + (size*i));
00077 int ch = ((char*)bufferT)[j];
00078 ungetc(ch, stream);
00079 }
00080 }
00081 }
00082
00083 #ifdef DEBUG
00084 void myassert(bool bCondition)
00085 {
00086 #ifdef WIN32
00087 if (!bCondition)
00088 {
00089 __asm
00090 {
00091 int 3;
00092 }
00093 }
00094 #else
00095 assert(bCondition);
00096 #endif
00097 }
00098 #endif
00099
00100 int mystrcmp(char *psz1, char *psz2)
00101 {
00102 if (psz1 == NULL)
00103 {
00104 return psz2 == NULL ? 0 : 1;
00105 }
00106 else if (psz2 == NULL)
00107 {
00108 return -1;
00109 }
00110 else
00111 {
00112 return strcmp(psz1, psz2);
00113 }
00114 }
00115
00116 char* mystrdup(char *psz)
00117 {
00118 if (psz == NULL)
00119 {
00120 return NULL;
00121 }
00122 else
00123 {
00124 return strdup(psz);
00125 }
00126 }
00127
00128 size_t mystrlen(char *psz)
00129 {
00130 if (psz)
00131 {
00132 return strlen(psz);
00133 }
00134 else
00135 {
00136 return 0;
00137 }
00138 }
00139
00140
00141
00142
00143
00144 inline unsigned long bytetok(unsigned long l)
00145 {
00146 return (l + 512) >> 10;
00147 }
00148
00149 int QueryProcessMemoryUsage()
00150 {
00151 int cbUsed = 0;
00152
00153 #ifdef WIN32
00154 myassert(0);
00155 #else
00156
00157 char szBuf[4096];
00158 sprintf(szBuf, "/proc/%d/stat", getpid());
00159 szBuf[ARRAYSIZE(szBuf)-1] = 0;
00160
00161 FILE *pfile = fopen(szBuf, "r");
00162 if (pfile)
00163 {
00164
00165
00166
00167
00168 fread(szBuf, sizeof(szBuf)-1, 1, pfile);
00169 szBuf[ARRAYSIZE(szBuf)-1] = 0;
00170
00171 char *psz = szBuf;
00172 int i;
00173 for (i = 0; i < 22; i++)
00174 {
00175 psz = strchr(psz, ' ') + 1;
00176 }
00177
00178 cbUsed = bytetok(strtoul(psz, NULL, 10));
00179
00180 fclose(pfile);
00181 }
00182 #endif
00183
00184 return cbUsed;
00185 }
00186
00187 void _PointToStdout(ITSAXContentHandler *pch)
00188 {
00189
00190
00191
00192 ITSAX2Bin *pbin;
00193 if (pch->QueryInterface(&IID_ITSAX2Bin, (void**)&pbin))
00194 {
00195
00196
00197
00198 IFileStream *pstm;
00199 if (CreateFileStream(&IID_IFileStream, (void**)&pstm))
00200 {
00201
00202
00203
00204 pstm->SetFile(stdout);
00205 pbin->Init(pstm, true);
00206 pstm->Release();
00207 }
00208 pbin->Release();
00209 }
00210 }
00211
00212
00213 IFileStream *_CreateFileStream(char *psz)
00214 {
00215
00216
00217
00218 IFileStream *pstm = NULL;
00219 if (CreateFileStream(&IID_IFileStream, (void**)&pstm))
00220 {
00221 if (psz)
00222 {
00223 if (pstm->OpenFile(psz, "r") == false)
00224 {
00225 fprintf(stderr, "error opening %s for read\n", psz);
00226 pstm->Release();
00227 pstm = NULL;
00228 }
00229 }
00230 else
00231 {
00232 pstm->SetFile(stdin);
00233 }
00234 }
00235 return pstm;
00236 }
00237
00238 bool IUnknownCL_SetHandler(IUnknownCL *punk, IUnknownCL *punkHandler)
00239 {
00240 ISetHandler* psh;
00241 bool bRet = punk->QueryInterface(&IID_ISetHandler, (void**)&psh);
00242 if (bRet)
00243 {
00244 bRet = psh->SetHandler(punkHandler);
00245 psh->Release();
00246 }
00247 return bRet;
00248 }
00249