PageRenderTime 36ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/ATF2/control-software/epics-3.14.8/extensions/src/ChannelArchiver/ThirdParty/xerces-c-src2_4_0/src/xercesc/util/Platforms/OS390/Path390.cpp

http://atf2flightsim.googlecode.com/
C++ | 607 lines | 405 code | 60 blank | 142 comment | 140 complexity | ce9952bef5aef92bcdacf264068ae66f MD5 | raw file
Possible License(s): BSD-2-Clause, LGPL-2.0, IPL-1.0, BSD-3-Clause
  1. /*
  2. * The Apache Software License, Version 1.1
  3. *
  4. * Copyright (c) 2002 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. * $Id: Path390.cpp,v 1.1.1.1 2009/03/14 06:42:41 whitegr Exp $
  58. */
  59. //#include "stdafx.h"
  60. #include <fstream.h>
  61. #include <stdio.h>
  62. #include <ctype.h>
  63. #include <typeinfo>
  64. #define _XOPEN_SOURCE_EXTENDED 1
  65. #include <stdlib.h>
  66. #include <string.h>
  67. #include "Path390.hpp"
  68. #include <xercesc/util/PlatformUtils.hpp>
  69. #include <xercesc/framework/MemoryManager.hpp>
  70. XERCES_CPP_NAMESPACE_BEGIN
  71. //Constructors:
  72. Path390::Path390() {
  73. _error = 0;
  74. _absolute = false;
  75. _dsnabsolute = false;
  76. _parsestate=PARSE_NONE;
  77. _orgparms=0;
  78. _resultpath=0;
  79. _orgpath=0;
  80. }
  81. Path390::Path390(char * s) {
  82. _error = 0;
  83. _absolute = false;
  84. _dsnabsolute = false;
  85. _parsestate=PARSE_NONE;
  86. _orgparms=0;
  87. _resultpath=0;
  88. _orgpath=0;
  89. setPath(s);
  90. }
  91. //Destructor:
  92. Path390::~Path390() {
  93. if (_orgparms)
  94. XMLPlatformUtils::fgMemoryManager->deallocate(_orgparms);//free (_orgparms);
  95. if (_resultpath)
  96. XMLPlatformUtils::fgMemoryManager->deallocate(_resultpath);//free(_resultpath);
  97. if (_orgpath)
  98. XMLPlatformUtils::fgMemoryManager->deallocate(_orgpath);//free(_orgpath);
  99. }
  100. // This path parser is state driven in order to support an incremental parse of the input path.
  101. // This is so that, for example, someone only wants to determine whether the path is absolute or
  102. // relative, then it will only parse sufficient information to determine this. This information
  103. // is saved in the object so that this does not need to be re-parsed if later one wanted to
  104. // retrieve, for example, the parameters.
  105. // The states are:
  106. // PARSE_NONE - initial state, nothing is parsed.
  107. // PARSE_ABSOLUTE_URI - Absolute or relative path has been determined.
  108. // PARSE_PATHTYPE - The type of the path has been determined
  109. // PARSE_PUNCT - The important delimiters have been located to make later parsing simpler.
  110. // PARSE_PARMS - The parms have been located and retrieved.
  111. // PARSE_PARSED - The path has been fully parsed.
  112. //
  113. // Each of the following methods handle the parsing corresponding to each state.
  114. //
  115. // Determine if the path is absolute or relative.
  116. void Path390::_determine_uri_abs() {
  117. if (_parsestate == PARSE_NONE) {
  118. if (*_curpos == '/') {
  119. _uriabsolute=true;
  120. _curpos++;
  121. } else
  122. _uriabsolute=false;
  123. _parsestate = PARSE_ABSOLUTE_URI;
  124. }
  125. }
  126. // Determine the path type. This could be:
  127. // PATH390_HFS - format is hfs:/xxx/xxx...
  128. // PATH390_DSN1 - format is dsn:/xxx/xxx...
  129. // PATH390_DSN2 - format is dsn://xxxxx...
  130. // PATH390_DD - format is dd:xxx...
  131. // PATH390_OTHER - format is any other paths.
  132. void Path390::_determine_type() {
  133. char firstfour[5];
  134. if (_parsestate == PARSE_ABSOLUTE_URI) {
  135. char * ff=firstfour;
  136. int ffi=0;
  137. while ((_curpos[ffi]) && (ffi<4)) {ff[ffi] = toupper(_curpos[ffi]); ffi++;}
  138. if ( (ffi>=4) && !strncmp(firstfour,"DSN:",4) ) {
  139. _pathtype = PATH390_DSN1;
  140. _curpos+=4;
  141. _absolute = true;
  142. if ( ((*_curpos) && (*_curpos == '/')) && ((*(_curpos+1)) && (*(_curpos+1) == '/')) ) {
  143. _pathtype = PATH390_DSN2;
  144. }
  145. }
  146. else if ( (ffi>=4) && !strncmp(firstfour,"HFS:",4) ) {
  147. _pathtype = PATH390_HFS;
  148. _curpos+=4;
  149. if (*_curpos == '/')
  150. _absolute = true;
  151. else
  152. _absolute = false;
  153. }
  154. else if ( (ffi>=3) && !strncmp(firstfour,"DD:",3) ) {
  155. _absolute = true;
  156. _pathtype = PATH390_DD;
  157. _curpos+=3;
  158. }
  159. else {
  160. _pathtype = PATH390_OTHER;
  161. if (_uriabsolute)
  162. _curpos--;
  163. }
  164. _parsestate = PARSE_PATHTYPE;
  165. }
  166. }
  167. // This takes one pass through the path any determines the location of important delimiters
  168. // including / ; . and (
  169. // It will also detect some error conditions
  170. void Path390::_determine_punct() {
  171. if (_parsestate == PARSE_PATHTYPE) {
  172. char * source = _curpos;
  173. _lastsemi = 0;
  174. _lastslash = 0;
  175. _lastparen = 0;
  176. _parmStart = 0;
  177. _pathEnd = 0;
  178. _numperiods = 0;
  179. _numsemicolons = 0;
  180. _extStart = 0;
  181. while (*source) {
  182. switch (*source) {
  183. case ';':
  184. _lastsemi = source;
  185. _parmStart = source+1;
  186. _numsemicolons++;
  187. break;
  188. case '/':
  189. _lastslash = source;
  190. _pathEnd = source;
  191. break;
  192. case '.':
  193. _extStart = source+1;
  194. _numperiods++;
  195. break;
  196. case '(':
  197. _lastparen = source+1;
  198. break;
  199. }
  200. *source++;
  201. }
  202. if ( (_parmStart) && (_parmStart<=_pathEnd) ) {
  203. _error = ERROR_SEMICOLON_NOT_ALLOWED;
  204. _lastsemi = 0;
  205. _parmStart = 0;
  206. }
  207. if ( ((_pathtype == PATH390_DD) || (_pathtype == PATH390_DSN1) || (_pathtype == PATH390_DSN1)) &&
  208. (_extStart <= _pathEnd) ) {
  209. if (_extStart)
  210. _error = ERROR_PERIOD_NOT_ALLOWED;
  211. _extStart = 0;
  212. }
  213. if (_extStart < _lastparen)
  214. _extStart = 0;
  215. _parsestate = PARSE_PUNCT;
  216. }
  217. }
  218. // This extracts the parameters from the path if there are any. It also determines if the parameters
  219. // contain type=record
  220. void Path390::_determine_parms() {
  221. if (_parsestate == PARSE_PUNCT) {
  222. char * tr = 0;
  223. if (_parmStart) {
  224. _orgparmlen = strlen(_parmStart)+1;
  225. _orgparms = (char*) XMLPlatformUtils::fgMemoryManager->allocate(_orgparmlen * sizeof(char));// (char *) malloc(_orgparmlen);
  226. char * ts=_parmStart;
  227. char * td=_orgparms;
  228. while (*ts)
  229. *td++ = tolower(*ts++);
  230. *td = 0;
  231. *_lastsemi = 0;
  232. tr = strstr(_orgparms,"type=record");
  233. }
  234. if (tr)
  235. _typerecord = tr - _orgparms;
  236. else
  237. _typerecord = -1;
  238. _parsestate = PARSE_PARMS;
  239. }
  240. }
  241. // Complete the rest of the parse.
  242. void Path390::_parse_rest() {
  243. if (_parsestate == PARSE_PARMS) {
  244. char *source;
  245. char *dest;
  246. char * filename_start;
  247. char * tmpPos;
  248. int pathlen = strlen(_curpos);
  249. _resultpath = (char*) XMLPlatformUtils::fgMemoryManager->allocate((pathlen+10) * sizeof(char));//(char *) malloc(pathlen+10);
  250. source = _curpos;
  251. dest = _resultpath;
  252. switch (_pathtype) {
  253. case PATH390_DSN1:
  254. // This format needs to be mangled from a hierarchical (hfs style) path to the
  255. // traditional MVS format. First check for some errors.
  256. if (_lastparen) {
  257. _error = ERROR_NO_PAREN_ALLOWED;
  258. break;
  259. }
  260. if ((_uriabsolute) && (!_absolute)) {
  261. _error = ERROR_ABS_PATH_REQUIRED;
  262. break;
  263. }
  264. if ( ((_extStart) && (_numperiods > 1)) ||
  265. ((!_extStart) && (_numperiods)) ) {
  266. _error = ERROR_NO_EXTRA_PERIODS_ALLOWED;
  267. break;
  268. }
  269. if ( ((_parmStart) && (_numsemicolons > 1)) ||
  270. ((!_parmStart) && (_numsemicolons)) ) {
  271. _error = ERROR_NO_EXTRA_SEMIS_ALLOWED;
  272. break;
  273. }
  274. // start out the result with //
  275. *dest++ = '/';
  276. *dest++ = '/';
  277. // If the input path starts with a / then it is absolute and it must be
  278. // enclosed in 's
  279. _dsnabsolute = false;
  280. if (*source == '/') {
  281. _dsnabsolute = true;
  282. source++;
  283. } else if (_uriabsolute) {
  284. _error = ERROR_MUST_BE_ABSOLUTE;
  285. break;
  286. }
  287. char * pathstart;
  288. pathstart = source;
  289. // Add in the ' if this is an absolute path'
  290. if (_dsnabsolute) *dest++ = '\'';
  291. // If there is a / in the path....
  292. tmpPos = source;
  293. if (_pathEnd > source) {
  294. // copy everything up to the last /, replacing / with .
  295. while( source < _pathEnd ) {
  296. switch( *source ) {
  297. case '/':
  298. *dest = '.';
  299. break;
  300. default:
  301. *dest = *source;
  302. }
  303. dest++; source++;
  304. }
  305. // bump past the last /
  306. source++;
  307. }
  308. // Now we try to locate the extension, and copy that.
  309. filename_start = 0;
  310. if ( _extStart != NULL ) {
  311. tmpPos = _extStart;
  312. if ( (*tmpPos != '\0') && (*tmpPos != ';') && (source != pathstart) )
  313. *dest++='.';
  314. while ( (*tmpPos != '\0') && (*tmpPos != ';') ) {
  315. *dest++ = *tmpPos++;
  316. }
  317. // if there is a filename, add a (
  318. if (source < (_extStart-1)) {
  319. filename_start = tmpPos;
  320. *dest++ = '(';
  321. }
  322. }
  323. else if (source != pathstart)
  324. *dest++ = '.';
  325. // Now we copy in the filename.
  326. tmpPos = source;
  327. while( ((*tmpPos != '\0') && (*tmpPos != ';')) && ((_extStart == NULL) || (tmpPos < (_extStart-1))) ) {
  328. *dest++ = *tmpPos++;
  329. }
  330. // Finally cap off the filename with optional ")"
  331. if ( (_extStart != NULL) && (filename_start) ) *dest++ = ')';
  332. // Add on the ending ' if necessary.
  333. if (_dsnabsolute) *dest++ = '\'';
  334. // make it a null terminated string.
  335. *dest = '\0';
  336. break;
  337. case PATH390_HFS:
  338. // it is in hfs: format. If it is relative, then add on a ./ otherwise
  339. // just copy the string.
  340. if (!_absolute) {
  341. if (_uriabsolute) {
  342. _error = ERROR_MUST_BE_ABSOLUTE;
  343. break;
  344. }
  345. *dest++='.';
  346. *dest++='/';
  347. }
  348. strcpy(dest,source);
  349. break;
  350. case PATH390_DD:
  351. // It's in dd: format. This is similar to the dsn: format, just shorter.
  352. // Start it out with dd:
  353. *dest++='D';
  354. *dest++='D';
  355. *dest++=':';
  356. tmpPos = source;
  357. // if there is a / present in the path...
  358. if (_pathEnd > source) {
  359. // copy everything up to the last /, replacing / with .
  360. while( source < _pathEnd ) {
  361. switch( *source ) {
  362. case '/':
  363. *dest = '.';
  364. break;
  365. default:
  366. *dest = *source;
  367. }
  368. dest++; source++;
  369. }
  370. // bump past the last /
  371. source++;
  372. }
  373. // Now we try to locate the extension, and copy that.
  374. filename_start = 0;
  375. if ( _extStart != NULL ) {
  376. tmpPos = _extStart;
  377. if ( (*tmpPos != '\0') && (*tmpPos != ';') && (source != _curpos) )
  378. *dest++='.';
  379. while ( (*tmpPos != '\0') && (*tmpPos != ';') ) {
  380. *dest++ = *tmpPos++;
  381. }
  382. // if there is a filename, add a (
  383. if (source < (_extStart-1)) {
  384. filename_start = tmpPos;
  385. *dest++ = '(';
  386. }
  387. }
  388. else if (source != _curpos)
  389. *dest++ = '.';
  390. // Now we copy in the filename.
  391. tmpPos = source;
  392. while( ((*tmpPos != '\0') && (*tmpPos != ';')) && ((_extStart == NULL) || (tmpPos < (_extStart-1))) ) {
  393. *dest++ = *tmpPos++;
  394. }
  395. // Finally cap off the filename with optional ")"
  396. if ( (_extStart != NULL) && (filename_start) ) *dest++ = ')';
  397. *dest = '\0';
  398. break;
  399. case PATH390_DSN2:
  400. // This is in dsn: format with the traditional MVS dataset name. Just fall into
  401. // the default case to copy the path to the destination after making sure that
  402. // there are no extra slashes.
  403. {
  404. int lastslash=5;
  405. if (_uriabsolute)
  406. lastslash=6;
  407. if ( (_lastslash) && ((_lastslash-_orgpath)>lastslash) ) {
  408. _error = ERROR_BAD_DSN2;
  409. break;
  410. }
  411. }
  412. default:
  413. // for all other cases simply copy over the string.
  414. strcpy(dest,source);
  415. break;
  416. }
  417. _parsestate = PARSE_PARSED;
  418. }
  419. }
  420. // Public methods start here:
  421. // This sets a new path into the object. Re-initialize everything and do an initial
  422. // parse.
  423. void Path390::setPath(char * s) {
  424. if (_orgparms)
  425. XMLPlatformUtils::fgMemoryManager->deallocate(_orgparms);//free (_orgparms);
  426. if (_resultpath)
  427. XMLPlatformUtils::fgMemoryManager->deallocate(_resultpath);//free(_resultpath);
  428. if (_orgpath)
  429. XMLPlatformUtils::fgMemoryManager->deallocate(_orgpath);//free(_orgpath);
  430. _error = 0;
  431. _orgparms = 0;
  432. _resultpath = 0;
  433. _absolute = false;
  434. _dsnabsolute = false;
  435. _orglen = strlen(s);
  436. _orgpath = (char*) XMLPlatformUtils::fgMemoryManager->allocate((_orglen+1) * sizeof(char));//(char *) malloc(_orglen+1);
  437. strcpy(_orgpath,s);
  438. _curpos = _orgpath;
  439. _parsestate=PARSE_NONE;
  440. // Do an initial parse...
  441. _determine_uri_abs();
  442. _determine_type();
  443. }
  444. // Do the parse to completion and return any errors found.
  445. int Path390::fullParse() {
  446. // Do an initial parse...
  447. _determine_uri_abs();
  448. _determine_type();
  449. _determine_punct();
  450. if (_error) {
  451. // printf("found error-%d\n",_error);
  452. return _error;
  453. }
  454. _determine_parms();
  455. _parse_rest();
  456. return _error;
  457. }
  458. // Get the path in a format which is required by fopen. First make sure that the path is
  459. // completely parsed
  460. char * Path390::getfopenPath() {
  461. _determine_uri_abs();
  462. _determine_type();
  463. _determine_punct();
  464. if (_error) {
  465. // printf("found error-%d\n",_error);
  466. return 0;
  467. }
  468. _determine_parms();
  469. _parse_rest();
  470. if (_error) {
  471. // printf("found error-%d\n",_error);
  472. return 0;
  473. }
  474. if (_resultpath[0])
  475. return _resultpath;
  476. else
  477. return 0;
  478. }
  479. // Get the parms in a format which is required by fopen. First make sure that the path is
  480. // completely parsed
  481. char * Path390::getfopenParms() {
  482. _determine_uri_abs();
  483. _determine_type();
  484. _determine_punct();
  485. if (_error) {
  486. // printf("found error-%d\n",_error);
  487. return 0;
  488. }
  489. _determine_parms();
  490. _parse_rest();
  491. if (_error) {
  492. // printf("found error-%d\n",_error);
  493. return 0;
  494. }
  495. if ( (_orgparms) && (_orgparms[0]) )
  496. return _orgparms;
  497. else
  498. return 0;
  499. }
  500. // return whether there is type=record parameter in the parameter list.
  501. bool Path390::isRecordType() {
  502. _determine_uri_abs();
  503. _determine_type();
  504. _determine_punct();
  505. if (_error) {
  506. // printf("found error-%d\n",_error);
  507. return false;
  508. }
  509. _determine_parms();
  510. _parse_rest();
  511. if (_error) {
  512. // printf("found error-%d\n",_error);
  513. return false;
  514. }
  515. if ( (_orgparms) && (_typerecord>=0) )
  516. return true;
  517. else
  518. return false;
  519. }
  520. // This returns the path type
  521. int Path390::getPathType() {
  522. _determine_uri_abs();
  523. _determine_type();
  524. return _pathtype;
  525. }
  526. // This returns the error code which was found when the path was parsed
  527. int Path390::getError() {
  528. _determine_uri_abs();
  529. _determine_type();
  530. _determine_punct();
  531. if (_error) {
  532. // return _error;
  533. }
  534. _determine_parms();
  535. _parse_rest();
  536. if (_error) {
  537. // return _error;
  538. }
  539. return _error;
  540. }
  541. // returns whether the path is relative or absolute.
  542. bool Path390::isRelative() {
  543. _determine_uri_abs();
  544. _determine_type();
  545. return !(_absolute|_uriabsolute);
  546. }
  547. XERCES_CPP_NAMESPACE_END