TandemPlatformUtils.cpp
上传用户:huihehuasu
上传日期:2007-01-10
资源大小:6948k
文件大小:15k
源码类别:

xml/soap/webservice

开发平台:

C/C++

  1. /*
  2.  * The Apache Software License, Version 1.1
  3.  * 
  4.  * Copyright (c) 1999-2000 The Apache Software Foundation.  All rights
  5.  * reserved.
  6.  * 
  7.  * Redistribution and use in source and binary forms, with or without
  8.  * modification, are permitted provided that the following conditions
  9.  * are met:
  10.  * 
  11.  * 1. Redistributions of source code must retain the above copyright
  12.  *    notice, this list of conditions and the following disclaimer. 
  13.  * 
  14.  * 2. Redistributions in binary form must reproduce the above copyright
  15.  *    notice, this list of conditions and the following disclaimer in
  16.  *    the documentation and/or other materials provided with the
  17.  *    distribution.
  18.  * 
  19.  * 3. The end-user documentation included with the redistribution,
  20.  *    if any, must include the following acknowledgment:  
  21.  *       "This product includes software developed by the
  22.  *        Apache Software Foundation (http://www.apache.org/)."
  23.  *    Alternately, this acknowledgment may appear in the software itself,
  24.  *    if and wherever such third-party acknowledgments normally appear.
  25.  * 
  26.  * 4. The names "Xerces" and "Apache Software Foundation" must
  27.  *    not be used to endorse or promote products derived from this
  28.  *    software without prior written permission. For written 
  29.  *    permission, please contact apache@apache.org.
  30.  * 
  31.  * 5. Products derived from this software may not be called "Apache",
  32.  *    nor may "Apache" appear in their name, without prior written
  33.  *    permission of the Apache Software Foundation.
  34.  * 
  35.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
  36.  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  37.  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  38.  * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
  39.  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  40.  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  41.  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  42.  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  43.  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  44.  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  45.  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  46.  * SUCH DAMAGE.
  47.  * ====================================================================
  48.  * 
  49.  * This software consists of voluntary contributions made by many
  50.  * individuals on behalf of the Apache Software Foundation, and was
  51.  * originally based on software copyright (c) 1999, International
  52.  * Business Machines, Inc., http://www.ibm.com .  For more information
  53.  * on the Apache Software Foundation, please see
  54.  * <http://www.apache.org/>.
  55.  */
  56. /*
  57.  * $Log: TandemPlatformUtils.cpp,v $
  58.  * Revision 1.4  2000/03/02 21:10:38  abagchi
  59.  * Added empty function platformTerm()
  60.  *
  61.  * Revision 1.3  2000/03/02 19:55:32  roddey
  62.  * This checkin includes many changes done while waiting for the
  63.  * 1.1.0 code to be finished. I can't list them all here, but a list is
  64.  * available elsewhere.
  65.  *
  66.  * Revision 1.2  2000/02/06 07:48:30  rahulj
  67.  * Year 2K copyright swat.
  68.  *
  69.  * Revision 1.1.1.1  1999/11/09 01:06:24  twl
  70.  * Initial checkin
  71.  *
  72.  * Revision 1.2  1999/11/08 20:45:32  rahul
  73.  * Swat for adding in Product name and CVS comment log variable.
  74.  *
  75.  */
  76. // ---------------------------------------------------------------------------
  77. // NOTE:
  78. //
  79. //  Xerces is not officially supported on Tandem. This file was sent
  80. //  in by one of the Tandem users and is included in the distribution
  81. //  just for convenience. Please send any defects / modification
  82. //  reports to xml4c@us.ibm.com
  83. // ---------------------------------------------------------------------------
  84. // ---------------------------------------------------------------------------
  85. //  Includes
  86. // ---------------------------------------------------------------------------
  87. // XXX #include    <pthread.h>
  88. // XXX #include    <sys/atomic_op.h>
  89. #include    <util/PlatformUtils.hpp>
  90. #include    <util/RuntimeException.hpp>
  91. #include    <util/Janitor.hpp>
  92. #include    <stdio.h>
  93. #include    <stdlib.h>
  94. #include    <errno.h>
  95. #include    <libgen.h>
  96. #include    <sys/timeb.h>
  97. #include    <string.h>
  98. // ---------------------------------------------------------------------------
  99. //  Local Methods
  100. // ---------------------------------------------------------------------------
  101. static void WriteCharStr( FILE* stream, const char* const toWrite)
  102. {
  103.     if (!fputs(toWrite, stream))
  104.     {
  105.         throw XMLPlatformUtilsException("Could not write to standard out/err");
  106.     }
  107. }
  108. static void WriteUStrStdErr( const XMLCh* const toWrite)
  109. {
  110.     char* tmpVal = XMLString::transcode(toWrite);
  111.     if (!fputs(tmpVal, stderr))
  112.     {
  113.         throw XMLPlatformUtilsException("Could not write to standard error file");
  114.     }
  115. }
  116. static void WriteUStrStdOut( const XMLCh* const toWrite)
  117. {
  118.     char* tmpVal = XMLString::transcode(toWrite);
  119.     if (!fputs(tmpVal, stdout))
  120.     {
  121.         throw XMLPlatformUtilsException("Could not write to standard out file");
  122.     }
  123. }
  124. // ---------------------------------------------------------------------------
  125. //  XMLPlatformUtils: Platform init method
  126. // ---------------------------------------------------------------------------
  127. void XMLPlatformUtils::platformInit()
  128. {
  129. }
  130. void XMLPlatformUtils::setupIntlPath()
  131. {
  132.     //
  133.     //  We need to figure out the path to the Intl classes. They will be
  134.     //  in the .Intl subdirectory under this DLL.
  135.     //
  136.     char* tmpBuf = getenv("XML4C2INTLDIR");
  137.     //check if environment variable is set
  138.     if (tmpBuf !=0) { 
  139.         // Store this string in the static member
  140.         char    cBuf[1024];
  141.         strcpy(cBuf, tmpBuf);
  142.         unsigned int pathLen = strlen(tmpBuf);
  143.     
  144.         if (*(tmpBuf + pathLen - 1) != '/')
  145.             strcpy((tmpBuf + pathLen), "/");
  146.         fgIntlPath = new char[strlen(tmpBuf)+1 ]; 
  147.         strcpy((char*)fgIntlPath, tmpBuf);
  148.         return;
  149.     }
  150.     //
  151.     //  If we did not find the environment var, so lets try to go the auto
  152.     //  search route.
  153.     //
  154.     static const char* const pubDLLNameBase = Xerces_DLLName;
  155.     char pubDLLNameReal[256];
  156.     strcpy(pubDLLNameReal, pubDLLNameBase);
  157.     strcat(pubDLLNameReal, gXML4C2VersionStr);
  158.     strcat(pubDLLNameReal, ".a");
  159.     char* libName = new char[ strlen(pubDLLNameReal)];
  160.     strcpy(libName, pubDLLNameReal);
  161.     char* libPath = getenv("LIBPATH");
  162.     //Now create an array of the directory listing in this path
  163.     //First do the searching process for the first directory listing
  164.     char*  libPathName = strtok(libPath, ":");
  165.     char* libfile = new char[ strlen(libPathName) + strlen(libName) +2];
  166.     strcpy(libfile, libPathName);
  167.     strcat(libfile, "/");
  168.     strcat(libfile, libName);
  169.     if ((FILE*)fopen(libfile, "rb"))
  170.     {
  171.         delete libfile;
  172.         libfile=0;
  173.         if (tmpBuf) {
  174.             delete tmpBuf;
  175.             tmpBuf =0;
  176.         }
  177.         tmpBuf = new char[strlen(libPathName)+ strlen("/intlFiles/locales/")+1];
  178.         strcpy(tmpBuf, libPathName);
  179.         strcat(tmpBuf, "/intlFiles/locales/");
  180.         fgIntlPath = new char[strlen(tmpBuf)+1 ];
  181.         strcpy((char*)fgIntlPath, tmpBuf);
  182.                 return;
  183.     }
  184.     //do the searching process for the rest directory listings
  185.     while  (libPathName != NULL)
  186.     {
  187.         libPathName = strtok(0, ":");
  188.         if (libPathName == NULL)
  189.                 break;
  190.         libfile = new char[ strlen(libPathName) + strlen(libName) +2];
  191.         strcpy(libfile, libPathName);
  192.         strcat(libfile, "/");
  193.         strcat(libfile, libName);
  194.         if ((FILE*)fopen(libfile, "rb"))
  195.         {
  196.                 delete libfile;
  197.                 libfile=0;
  198.                 if (tmpBuf) {
  199.                         delete tmpBuf;
  200.                         tmpBuf =0;
  201.                 }
  202.                 tmpBuf = new char[strlen(libPathName)+ strlen("/intlFiles/locales/")+1];
  203.                 strcpy(tmpBuf, libPathName);
  204.                 strcat(tmpBuf, "/intlFiles/locales/");
  205.                 fgIntlPath = new char[strlen(tmpBuf)+1 ];
  206.                 strcpy((char*)fgIntlPath, tmpBuf);
  207.                 return;
  208.         }
  209.     }
  210.     if (fgIntlPath == NULL) {
  211.     printf("Environment variable 'XML4C2INTLDIR' not defined n");
  212.     throw RuntimeException(
  213.             "Environment variable 'XML4C2INTLDIR' not defined."
  214.                           );
  215.     }
  216. }
  217. // ---------------------------------------------------------------------------
  218. //  XMLPlatformUtils: File Methods
  219. // ---------------------------------------------------------------------------
  220. unsigned int XMLPlatformUtils::curFilePos(FileHandle theFile)
  221. {
  222.     // Get the current position
  223.     int curPos = ftell( (FILE*)theFile);
  224.     if (curPos == -1)
  225.         throw XMLPlatformUtilsException("XMLPlatformUtils::curFilePos - Could not get current pos");
  226.     return (unsigned int)curPos;
  227. }
  228. void XMLPlatformUtils::closeFile(FileHandle theFile)
  229. {
  230.     if (fclose((FILE*)theFile))
  231.         throw XMLPlatformUtilsException("XMLPlatformUtils::closeFile - Could not close the file handle");
  232. }
  233. unsigned int XMLPlatformUtils::fileSize(FileHandle theFile)
  234. {
  235.     // Get the current position
  236.     long  int curPos = ftell((FILE*)theFile);
  237.     if (curPos == -1)
  238.         throw XMLPlatformUtilsException("XMLPlatformUtils::fileSize - Could not get current pos");
  239.     // Seek to the end and save that value for return
  240.      if (fseek( (FILE*)theFile, 0, SEEK_END) )
  241.         throw XMLPlatformUtilsException("XMLPlatformUtils::fileSize - Could not seek to end");
  242.     long int retVal = ftell( (FILE*)theFile);
  243.     if (retVal == -1)
  244.         throw XMLPlatformUtilsException("XMLPlatformUtils::fileSize - Could not get the file size");
  245.     // And put the pointer back
  246.     if (fseek( (FILE*)theFile, curPos, SEEK_SET) )
  247.         throw XMLPlatformUtilsException("XMLPlatformUtils::fileSize - Could not seek back to original pos");
  248.     return (unsigned int)retVal;
  249. }
  250. FileHandle XMLPlatformUtils::openFile(const unsigned short* const fileName)
  251. {
  252.     const char* tmpFileName = XMLString::transcode(fileName);
  253.     FileHandle retVal = (FILE*)fopen( tmpFileName , "rb" );
  254.     if (retVal == NULL)
  255.         return 0;
  256.     return retVal;
  257. }
  258. unsigned int
  259. XMLPlatformUtils::readFileBuffer(  FileHandle      theFile
  260.                                 , const unsigned int    toRead
  261.                                 , XMLByte* const  toFill)
  262. {
  263.     size_t noOfItemsRead = fread( (void*) toFill, 1, toRead, (FILE*)theFile);
  264.     if(ferror((FILE*)theFile))
  265.     {
  266.         throw XMLPlatformUtilsException("XMLPlatformUtils::readFileBuffer - Read failed");
  267.     }
  268.     return (unsigned int)noOfItemsRead;
  269. }
  270. void XMLPlatformUtils::resetFile(FileHandle theFile)
  271. {
  272.     // Seek to the start of the file
  273.     if (fseek((FILE*)theFile, 0, SEEK_SET) )
  274.         throw XMLPlatformUtilsException("XMLPlatformUtils::resetFile - Could not seek to beginning");
  275. }
  276. // ---------------------------------------------------------------------------
  277. //  XMLPlatformUtils: Timing Methods
  278. // ---------------------------------------------------------------------------
  279. unsigned long XMLPlatformUtils::getCurrentMillis()
  280. {
  281.     timeb aTime;
  282.     ftime(&aTime);
  283.     return (unsigned long)(aTime.time*1000 + aTime.millitm);
  284. }
  285. XMLCh* XMLPlatformUtils::getBasePath(const XMLCh* const srcPath)
  286. {
  287.     //
  288.     //  NOTE: THe path provided has always already been opened successfully,
  289.     //  so we know that its not some pathological freaky path. It comes in
  290.     //  in native format, and goes out as Unicode always
  291.     //
  292.     char* newSrc = XMLString::transcode(srcPath);
  293.     // Use a local buffer that is big enough for the largest legal path
  294.      char* tmpPath = dirname((char*)newSrc);
  295.     if (!tmpPath)
  296.     {
  297.         throw XMLPlatformUtilsException("XMLPlatformUtils::resetFile - Could not get the base path name");
  298.     }
  299.     char* newXMLString = new char [strlen(tmpPath) +1];
  300.     ArrayJanitor<char> newJanitor(newXMLString);
  301.     strcpy(newXMLString, tmpPath);
  302.         strcat(newXMLString , "/");
  303.     // Return a copy of the path, in Unicode format
  304.     return XMLString::transcode(newXMLString);
  305. }
  306. bool XMLPlatformUtils::isRelative(const XMLCh* const toCheck)
  307. {
  308.     // Check for pathological case of empty path
  309.     if (!toCheck[0])
  310.         return false;
  311.     //
  312.     //  If it starts with a slash, then it cannot be relative. This covers
  313.     //  both something like "TestFile.xml" and an NT Lan type remote path
  314.     //  that starts with a node like "\MyNodeTestFile.xml".
  315.     //
  316.     if (toCheck[0] == XMLCh('/'))
  317.         return false;
  318.     // Else assume its a relative path
  319.     return true;
  320. }
  321. // -----------------------------------------------------------------------
  322. //  Standard out/error support
  323. // -----------------------------------------------------------------------
  324. void XMLPlatformUtils::writeToStdErr(const char* const toWrite)
  325. {
  326.     WriteCharStr(stderr, toWrite);
  327. }
  328. void XMLPlatformUtils::writeToStdErr(const XMLCh* const toWrite)
  329. {
  330.     WriteUStrStdErr(toWrite);
  331. }
  332. void XMLPlatformUtils::writeToStdOut(const XMLCh* const toWrite)
  333. {
  334.     WriteUStrStdOut(toWrite);
  335. }
  336. void XMLPlatformUtils::writeToStdOut(const char* const toWrite)
  337. {
  338.     WriteCharStr(stdout, toWrite);
  339. }
  340. #ifndef __TANDEM
  341. // -----------------------------------------------------------------------
  342. //  Mutex methods 
  343. // -----------------------------------------------------------------------
  344. void XMLPlatformUtils::closeMutex(void* const mtxHandle)
  345. {
  346.     if (mtxHandle == NULL)
  347.         return;
  348.     if (pthread_mutex_destroy( (pthread_mutex_t*)mtxHandle))
  349.     {
  350.         throw XMLPlatformUtilsException("Could not destroy a mutex");
  351.     }
  352.     if ( (pthread_mutex_t*)mtxHandle)
  353.         delete mtxHandle;
  354. }
  355. void XMLPlatformUtils::lockMutex(void* const mtxHandle)
  356. {
  357.     if (mtxHandle == NULL)
  358.         return;
  359.     if (pthread_mutex_lock( (pthread_mutex_t*)mtxHandle))
  360.     {
  361.         throw XMLPlatformUtilsException("Could not lock a mutex");
  362.     }
  363. }
  364. void* XMLPlatformUtils::makeMutex()
  365. {
  366.     pthread_mutex_t* mutex = new pthread_mutex_t;
  367.     if (mutex == NULL) 
  368.     {
  369.         throw XMLPlatformUtilsException("Could not initialize a mutex");
  370.     }
  371.     if (pthread_mutex_init(mutex, NULL))
  372.     {
  373.         throw XMLPlatformUtilsException("Could not create a mutex");
  374.     }
  375.     return (void*)(mutex);
  376. }
  377. void XMLPlatformUtils::unlockMutex(void* const mtxHandle)
  378. {
  379.     if (mtxHandle == NULL)
  380.         return;
  381.     if (pthread_mutex_unlock( (pthread_mutex_t*)mtxHandle))
  382.     {
  383.         throw XMLPlatformUtilsException("Could not unlock a mutex");
  384.     }
  385. }
  386. // -----------------------------------------------------------------------
  387. //  Miscellaneous synchronization methods
  388. // -----------------------------------------------------------------------
  389. void* XMLPlatformUtils::compareAndSwap ( void**      toFill , 
  390.                     const void* const newValue , 
  391.                     const void* const toCompare)
  392. {
  393.     boolean_t boolVar = compare_and_swap((atomic_p)toFill, (int *)&toCompare, (int)newValue );
  394.     return (void *)toCompare;
  395. }
  396. int XMLPlatformUtils::atomicIncrement(int &location)
  397. {
  398.     int retVal = fetch_and_add( (atomic_p)&location, 1);
  399.     return retVal+1;
  400. }
  401. int XMLPlatformUtils::atomicDecrement(int &location)
  402. {
  403.     int retVal = fetch_and_add( (atomic_p)&location, -1);
  404.     return retVal-1;
  405. }
  406. FileHandle XMLPlatformUtils::openStdInHandle()
  407. {
  408.     return (FileHandle)fdopen(dup(0), "rb");
  409. }
  410. #endif
  411. void XMLPlatformUtils::platformTerm()
  412. {
  413.     // We don't have any termination requirements at this time
  414. }