Main Page   Namespace List   Class Hierarchy   Compound List   File List   Compound Members   File Members  

util.cpp File Reference

#include "xmltk.h"
#include <sys/sysinfo.h>
#include <sys/types.h>
#include <unistd.h>

Go to the source code of this file.

Functions

BOOL IsEqualCLIID (const CLIID *riid1, const CLIID *riid2)
void fwriteMultiByteUINT (IStream *pstm, unsigned int uInt)
bool freadMultiByteUINT (IStream *pstm, unsigned int *puInt)
void funread (void *buffer, size_t size, size_t count, FILE *stream)
int mystrcmp (char *psz1, char *psz2)
char * mystrdup (char *psz)
size_t mystrlen (char *psz)
unsigned long bytetok (unsigned long l)
int QueryProcessMemoryUsage ()
void _PointToStdout (ITSAXContentHandler *pch)
IFileStream_CreateFileStream (char *psz)
bool IUnknownCL_SetHandler (IUnknownCL *punk, IUnknownCL *punkHandler)

Variables

const unsigned char c_chMask = 0x7F
const unsigned char c_chContinue = 0x80


Function Documentation

bool IUnknownCL_SetHandler IUnknownCL *    punk,
IUnknownCL *    punkHandler
 

Definition at line 238 of file util.cpp.

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 }

BOOL IsEqualCLIID const CLIID   riid1,
const CLIID   riid2
 

Definition at line 9 of file util.cpp.

00010 {
00011     return (memcmp(riid1, riid2, sizeof(*riid1)) == 0);
00012 }

int QueryProcessMemoryUsage  
 

Definition at line 149 of file util.cpp.

00150 {
00151     int cbUsed = 0;
00152 
00153 #ifdef WIN32
00154     myassert(0);    // to be implemented
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         // my psychic powers tell me that the memory usage stat
00166         // is the 23rd token
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 }

IFileStream* _CreateFileStream char *    psz
 

Definition at line 213 of file util.cpp.

00214 {
00215     //
00216     // create a file stream object for this file
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 }

void _PointToStdout ITSAXContentHandler   pch
 

Definition at line 187 of file util.cpp.

00188 {
00189     //
00190     // QI for ITSAX2Bin, which we use to set the output stream
00191     //
00192     ITSAX2Bin *pbin;
00193     if (pch->QueryInterface(&IID_ITSAX2Bin, (void**)&pbin))
00194     {
00195         //
00196         // create the stdout stream
00197         //
00198         IFileStream *pstm;
00199         if (CreateFileStream(&IID_IFileStream, (void**)&pstm))
00200         {
00201             //
00202             // point the stream to stdout, point pch to stream
00203             //
00204             pstm->SetFile(stdout);
00205             pbin->Init(pstm, true);
00206             pstm->Release();
00207         }
00208         pbin->Release();
00209     }
00210 }

unsigned long bytetok unsigned long    l [inline]
 

Definition at line 144 of file util.cpp.

Referenced by QueryProcessMemoryUsage().

00145 {
00146     return (l + 512) >> 10;
00147 }

bool freadMultiByteUINT IStream *    pstm,
unsigned int *    puInt
 

Definition at line 41 of file util.cpp.

00042 {
00043     *puInt = 0;
00044     int i;
00045 
00046     //
00047     // use a loop to guard against overflow
00048     //
00049     for (i = 0; i < 5; i++)     // 5 = ceil(32/7)
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 }

void funread void *    buffer,
size_t    size,
size_t    count,
FILE *    stream
 

Definition at line 70 of file util.cpp.

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 }

void fwriteMultiByteUINT IStream *    pstm,
unsigned int    uInt
 

Definition at line 18 of file util.cpp.

00019 {
00020     unsigned char rgch[5];                          // 5 = ceil(32/7)
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 }

int mystrcmp char *    psz1,
char *    psz2
 

Definition at line 100 of file util.cpp.

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 }

char* mystrdup char *    psz
 

Definition at line 116 of file util.cpp.

00117 {
00118     if (psz == NULL)
00119     {
00120         return NULL;
00121     }
00122     else
00123     {
00124         return strdup(psz);
00125     }
00126 }

size_t mystrlen char *    psz
 

Definition at line 128 of file util.cpp.

00129 {
00130     if (psz)
00131     {
00132         return strlen(psz);
00133     }
00134     else
00135     {
00136         return 0;
00137     }
00138 }


Variable Documentation

const unsigned char c_chContinue = 0x80 [static]
 

Definition at line 16 of file util.cpp.

const unsigned char c_chMask = 0x7F [static]
 

Definition at line 15 of file util.cpp.


Generated on Sat Dec 22 16:01:55 2001 for XMILLforBinaryFormat by doxygen1.2.11.1 written by Dimitri van Heesch, © 1997-2001