Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1915 lines
56 KiB

  1. /****************************************************************************\
  2. *
  3. * PARSERAT.C -- Code to parse .RAT files
  4. *
  5. * Created: Greg Jones
  6. *
  7. \****************************************************************************/
  8. /*Includes------------------------------------------------------------------*/
  9. #include "stdafx.h"
  10. #include "cnfgprts.h"
  11. #include "parserat.h"
  12. #include <iis64.h>
  13. /****************************************************************************
  14. Some design notes on how this parser works:
  15. A ParenThing is:
  16. '(' identifier [stuff] ')'
  17. where [stuff] could be:
  18. a quoted string
  19. a number
  20. a boolean
  21. a series of ParenThings
  22. in the case of extensions:
  23. a quoted string, followed by
  24. one or more quoted strings and/or ParenThings
  25. The entire .RAT file is a ParenThing, except that it has no identifier, just
  26. a list of ParenThings inside it.
  27. **********************************************************************
  28. We pass the parser a schema for what things it expects -- we have
  29. a big array listing identifiers for each different possible keyword, and
  30. each parser call receives a smaller array containing only those indices
  31. that are valid to occur within that object.
  32. We make PicsRatingSystem, PicsCategory, and PicsEnum derive from a common
  33. base class which supports a virtual function AddItem(ID,data). So at the
  34. top level, we construct an (empty) PicsRatingSystem. We call the parser,
  35. giving it a pointer to that guy, and a structure describing what to parse --
  36. the ParenObject's token is a null string (since the global structure is the
  37. one that doesn't start with a token before its first embedded ParenThing),
  38. and we give a list saying the allowable things in a PicsRatingSystem are
  39. PICS-version, rating-system, rating-service, default, description, extension,
  40. icon, name, category. There is a global table indicating a handler function
  41. for every type of ParenThing, which knows how to create a data structure
  42. completely describing that ParenThing. (That data structure could be as
  43. simple as a number or as complex as allocating and parsing a complete
  44. PicsCategory object.)
  45. The parser walks along, and for each ParenThing he finds, he identifies it
  46. by looking up its token in the list provided by the caller. Each entry in
  47. that list should include a field which indicates whether multiple things
  48. of that identity are allowed (e.g., 'category') or not (e.g., rating-system).
  49. If only one is allowed, then when the parser finds one he marks it as having
  50. been found.
  51. When the parser identifies the ParenThing, he calls its handler function to
  52. completely parse the data in the ParenThing and return that object into an
  53. LPVOID provided by the parser. If that is successful, the parser then calls
  54. its object's AddItem(ID,data) virtual function to add the specified item to
  55. the object, relying on the object itself to know what type "data" points to --
  56. a number, a pointer to a heap string which can be given to ETS::SetTo, a
  57. pointer to a PicsCategory object which can be appended to an array, etc.
  58. The RatFileParser class exists solely to provide a line number shared by
  59. all the parsing routines. This line number is updated as the parser walks
  60. through the file, and is frozen as soon as an error is found. This line
  61. number can later be reported to the user to help localize errors in RAT files.
  62. *****************************************************************************/
  63. /*Globals-------------------------------------------------------------------*/
  64. #define EXTTEXT(n) const CHAR n[]
  65. #define TEXTCONST(name,text) EXTTEXT(name) = (text)
  66. /* Text strings used in parsing rating labels. */
  67. TEXTCONST(szNULL,"");
  68. TEXTCONST(szDoubleCRLF,"\r\n\r\n");
  69. TEXTCONST(szPicsOpening,"(PICS-");
  70. TEXTCONST(szWhitespace," \t\r\n");
  71. TEXTCONST(szExtendedAlphaNum,"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789+-.,;:&=?!*~@#/");
  72. TEXTCONST(szSingleCharTokens,"()\"");
  73. TEXTCONST(szLeftParen,"(");
  74. TEXTCONST(szRightParen,")");
  75. TEXTCONST(szOptional,"optional");
  76. TEXTCONST(szMandatory,"mandatory");
  77. TEXTCONST(szAtOption,"at");
  78. TEXTCONST(szByOption,"by");
  79. TEXTCONST(szCommentOption,"comment");
  80. TEXTCONST(szCompleteLabelOption,"complete-label");
  81. TEXTCONST(szFullOption,"full");
  82. TEXTCONST(szExtensionOption,"extension");
  83. TEXTCONST(szGenericOption,"generic");
  84. TEXTCONST(szShortGenericOption,"gen");
  85. TEXTCONST(szForOption,"for");
  86. TEXTCONST(szMICOption,"MIC-md5");
  87. TEXTCONST(szMD5Option,"md5");
  88. TEXTCONST(szOnOption,"on");
  89. TEXTCONST(szSigOption,"signature-PKCS");
  90. TEXTCONST(szUntilOption,"until");
  91. TEXTCONST(szExpOption,"exp");
  92. TEXTCONST(szRatings,"ratings");
  93. TEXTCONST(szShortRatings,"r");
  94. TEXTCONST(szError,"error");
  95. TEXTCONST(szNoRatings,"no-ratings");
  96. TEXTCONST(szLabelWord,"labels");
  97. TEXTCONST(szShortLabelWord,"l");
  98. TEXTCONST(szShortTrue,"t");
  99. TEXTCONST(szTrue,"true");
  100. TEXTCONST(szShortFalse,"f");
  101. TEXTCONST(szFalse,"false");
  102. TEXTCONST(szNegInf,"-INF");
  103. TEXTCONST(szPosInf,"+INF");
  104. TEXTCONST(szLabel,"label");
  105. TEXTCONST(szName,"name");
  106. TEXTCONST(szValue,"value");
  107. TEXTCONST(szIcon,"icon");
  108. TEXTCONST(szDescription, "description");
  109. TEXTCONST(szCategory, "category");
  110. TEXTCONST(szTransmitAs, "transmit-as");
  111. TEXTCONST(szMin,"min");
  112. TEXTCONST(szMax,"max");
  113. TEXTCONST(szMultivalue,"multivalue");
  114. TEXTCONST(szInteger,"integer");
  115. TEXTCONST(szLabelOnly, "label-only");
  116. TEXTCONST(szPicsVersion,"PICS-version");
  117. TEXTCONST(szRatingSystem,"rating-system");
  118. TEXTCONST(szRatingService,"rating-service");
  119. TEXTCONST(szRatingBureau,"rating-bureau");
  120. TEXTCONST(szBureauRequired,"bureau-required");
  121. TEXTCONST(szDefault,"default");
  122. TEXTCONST(szMultiValue,"multivalue");
  123. TEXTCONST(szUnordered,"unordered");
  124. TEXTCONST(szRatingBureauExtension,"www.w3.org/PICS/service-extensions/label-bureau");
  125. /* define some error codes */
  126. const HRESULT RAT_E_BASE = 0x80050000; /* just a guess at a free area for internal use */
  127. const HRESULT RAT_E_EXPECTEDLEFT = RAT_E_BASE + 1; /* expected left paren */
  128. const HRESULT RAT_E_EXPECTEDRIGHT = RAT_E_BASE + 2; /* expected right paren */
  129. const HRESULT RAT_E_EXPECTEDTOKEN = RAT_E_BASE + 3; /* expected unquoted token */
  130. const HRESULT RAT_E_EXPECTEDSTRING = RAT_E_BASE + 4; /* expected quoted string */
  131. const HRESULT RAT_E_EXPECTEDNUMBER = RAT_E_BASE + 5; /* expected number */
  132. const HRESULT RAT_E_EXPECTEDBOOL = RAT_E_BASE + 6; /* expected boolean */
  133. const HRESULT RAT_E_DUPLICATEITEM = RAT_E_BASE + 7; /* AO_SINGLE item appeared twice */
  134. const HRESULT RAT_E_MISSINGITEM = RAT_E_BASE + 8; /* AO_MANDATORY item not found */
  135. const HRESULT RAT_E_UNKNOWNITEM = RAT_E_BASE + 9; /* unrecognized token */
  136. const HRESULT RAT_E_UNKNOWNMANDATORY= RAT_E_BASE + 10; /* unrecognized mandatory extension */
  137. char PicsDelimChar='/';
  138. class RatFileParser
  139. {
  140. public:
  141. UINT m_nLine;
  142. RatFileParser() { m_nLine = 1; }
  143. LPSTR EatQuotedString(LPSTR pIn);
  144. HRESULT ParseToOpening(LPSTR *ppIn, AllowableOption *paoExpected,
  145. AllowableOption **ppFound);
  146. HRESULT ParseParenthesizedObject(
  147. LPSTR *ppIn, /* where we are in the text stream */
  148. AllowableOption aao[], /* allowable things inside this object */
  149. PicsObjectBase *pObject /* object to set parameters into */
  150. );
  151. char* FindNonWhite(char *pc);
  152. };
  153. /* White returns a pointer to the first whitespace character starting at pc.
  154. */
  155. char* White(char *pc){
  156. assert(pc);
  157. while (1){
  158. if (*pc == '\0' ||
  159. *pc ==' ' ||
  160. *pc == '\t' ||
  161. *pc == '\r' ||
  162. *pc == '\n')
  163. {
  164. return pc;
  165. }
  166. pc++;
  167. }
  168. }
  169. /* NonWhite returns a pointer to the first non-whitespace character starting
  170. * at pc.
  171. */
  172. char* NonWhite(char *pc){
  173. assert(pc);
  174. while (1){
  175. if (*pc != ' ' &&
  176. *pc != '\t' &&
  177. *pc != '\r' &&
  178. *pc != '\n') /* includes null terminator */
  179. {
  180. return pc;
  181. }
  182. pc++;
  183. }
  184. }
  185. /* FindNonWhite returns a pointer to the first non-whitespace character starting
  186. * at pc.
  187. */
  188. char* RatFileParser::FindNonWhite(char *pc)
  189. {
  190. assert(pc);
  191. while (1)
  192. {
  193. if (*pc != ' ' &&
  194. *pc != '\t' &&
  195. *pc != '\r' &&
  196. *pc != '\n') /* includes null terminator */
  197. {
  198. return pc;
  199. }
  200. if (*pc == '\n')
  201. m_nLine++;
  202. pc++;
  203. }
  204. }
  205. /* AppendSlash forces pszString to end in a single slash if it doesn't
  206. * already. This may produce a technically invalid URL (for example,
  207. * "http://gregj/default.htm/", but we're only using the result for
  208. * comparisons against other paths similarly mangled.
  209. */
  210. void AppendSlash(LPSTR pszString)
  211. {
  212. LPSTR pszSlash = ::strrchrf(pszString, '/');
  213. if (pszSlash == NULL || *(pszSlash + 1) != '\0')
  214. ::strcatf(pszString, "/");
  215. }
  216. /* SkipWhitespace(&pszString)
  217. *
  218. * advances pszString past whitespace characters
  219. */
  220. void SkipWhitespace(LPSTR *ppsz)
  221. {
  222. UINT cchWhitespace = ::strspnf(*ppsz, szWhitespace);
  223. *ppsz += cchWhitespace;
  224. }
  225. /* FindTokenEnd(pszStart)
  226. *
  227. * Returns a pointer to the end of a contiguous range of similarly-typed
  228. * characters (whitespace, quote mark, punctuation, or alphanumerics).
  229. */
  230. LPSTR FindTokenEnd(LPSTR pszStart)
  231. {
  232. LPSTR pszEnd = pszStart;
  233. if (*pszEnd == '\0') {
  234. return pszEnd;
  235. }
  236. else if (strchr(szSingleCharTokens, *pszEnd)) {
  237. return ++pszEnd;
  238. }
  239. UINT cch;
  240. cch = ::strspnf(pszEnd, szWhitespace);
  241. if (cch > 0)
  242. return pszEnd + cch;
  243. cch = ::strspnf(pszEnd, szExtendedAlphaNum);
  244. if (cch > 0)
  245. return pszEnd + cch;
  246. return pszEnd; /* unrecognized characters */
  247. }
  248. /* GetBool(LPSTR *ppszToken, BOOL *pfOut)
  249. *
  250. * Parses a boolean value at the given token and returns its value in *pfOut.
  251. * Legal values are 't', 'f', 'true', and 'false'. If success, *ppszToken
  252. * is advanced past the boolean token and any following whitespace. If failure,
  253. * *ppszToken is not modified.
  254. *
  255. * pfOut may be NULL if the caller just wants to eat the token and doesn't
  256. * care about its value.
  257. */
  258. HRESULT GetBool(LPSTR *ppszToken, BOOL *pfOut)
  259. {
  260. BOOL bValue;
  261. LPSTR pszTokenEnd = FindTokenEnd(*ppszToken);
  262. if (IsEqualToken(*ppszToken, pszTokenEnd, szShortTrue) ||
  263. IsEqualToken(*ppszToken, pszTokenEnd, szTrue)) {
  264. bValue = TRUE;
  265. }
  266. else if (IsEqualToken(*ppszToken, pszTokenEnd, szShortFalse) ||
  267. IsEqualToken(*ppszToken, pszTokenEnd, szFalse)) {
  268. bValue = FALSE;
  269. }
  270. else
  271. return ResultFromScode(MK_E_SYNTAX);
  272. if (pfOut != NULL)
  273. *pfOut = bValue;
  274. *ppszToken = pszTokenEnd;
  275. SkipWhitespace(ppszToken);
  276. return NOERROR;
  277. }
  278. /* GetQuotedToken(&pszThisToken, &pszQuotedToken)
  279. *
  280. * Sets pszQuotedToken to point to the contents of the doublequotes.
  281. * pszQuotedToken may be NULL if the caller just wants to eat the token.
  282. * Sets pszThisToken to point to the first character after the closing
  283. * doublequote.
  284. * Fails if pszThisToken doesn't start with a doublequote or doesn't
  285. * contain a closing doublequote.
  286. * The closing doublequote is replaced with a null terminator, iff the
  287. * function does not fail.
  288. */
  289. HRESULT GetQuotedToken(LPSTR *ppszThisToken, LPSTR *ppszQuotedToken)
  290. {
  291. HRESULT hres = ResultFromScode(MK_E_SYNTAX);
  292. LPSTR pszStart = *ppszThisToken;
  293. if (*pszStart != '\"')
  294. return hres;
  295. pszStart++;
  296. LPSTR pszEndQuote = strchrf(pszStart, '\"');
  297. if (pszEndQuote == NULL)
  298. return hres;
  299. *pszEndQuote = '\0';
  300. if (ppszQuotedToken != NULL)
  301. *ppszQuotedToken = pszStart;
  302. *ppszThisToken = pszEndQuote+1;
  303. return NOERROR;
  304. }
  305. BOOL IsEqualToken(LPCSTR pszTokenStart, LPCSTR pszTokenEnd, LPCSTR pszTokenToMatch)
  306. {
  307. UINT cbToken = strlenf(pszTokenToMatch);
  308. if (cbToken != (UINT)(pszTokenEnd - pszTokenStart) || strnicmpf(pszTokenStart, pszTokenToMatch, cbToken))
  309. return FALSE;
  310. return TRUE;
  311. }
  312. /* ParseLiteralToken(ppsz, pszToken) tries to match *ppsz against pszToken.
  313. * If they don't match, an error is returned. If they do match, then *ppsz
  314. * is advanced past the token and any following whitespace.
  315. *
  316. * If ppszInvalid is NULL, then the function is non-destructive in the error
  317. * path, so it's OK to call ParseLiteralToken just to see if a possible literal
  318. * token is what's next; if the token isn't found, whatever was there didn't
  319. * get eaten or anything.
  320. *
  321. * If ppszInvalid is not NULL, then if the token doesn't match, *ppszInvalid
  322. * will be set to *ppsz.
  323. */
  324. HRESULT ParseLiteralToken(LPSTR *ppsz, LPCSTR pszToken, LPCSTR *ppszInvalid)
  325. {
  326. LPSTR pszTokenEnd = FindTokenEnd(*ppsz);
  327. if (!IsEqualToken(*ppsz, pszTokenEnd, pszToken)) {
  328. if (ppszInvalid != NULL)
  329. *ppszInvalid = *ppsz;
  330. return ResultFromScode(MK_E_SYNTAX);
  331. }
  332. *ppsz = pszTokenEnd;
  333. SkipWhitespace(ppsz);
  334. return NOERROR;
  335. }
  336. /* ParseNumber parses a numeric token at the specified position. If the
  337. * number makes sense, the pointer is advanced to the end of the number
  338. * and past any following whitespace, and the numeric value is returned
  339. * in *pnOut. Any non-numeric characters are considered to terminate the
  340. * number without error; it is assumed that higher-level parsing code
  341. * will eventually reject such characters if they're not supposed to be
  342. * there.
  343. *
  344. * pnOut may be NULL if the caller doesn't care about the number being
  345. * returned and just wants to eat it.
  346. *
  347. * Floating point numbers of the form nnn.nnn are rounded to the next
  348. * higher integer and returned as such.
  349. */
  350. HRESULT ParseNumber(LPSTR *ppszNumber, INT *pnOut)
  351. {
  352. HRESULT hres = ResultFromScode(MK_E_SYNTAX);
  353. BOOL fNegative = FALSE;
  354. INT nAccum = 0;
  355. BOOL fNonZeroDecimal = FALSE;
  356. BOOL fInDecimal = FALSE;
  357. BOOL fFoundDigits = FALSE;
  358. LPSTR pszCurrent = *ppszNumber;
  359. /* Handle one sign character. */
  360. if (*pszCurrent == '+') {
  361. pszCurrent++;
  362. }
  363. else if (*pszCurrent == '-') {
  364. pszCurrent++;
  365. fNegative = TRUE;
  366. }
  367. for (;;) {
  368. if (*pszCurrent == '.') {
  369. fInDecimal = TRUE;
  370. }
  371. else if (*pszCurrent >= '0' && *pszCurrent <= '9') {
  372. fFoundDigits = TRUE;
  373. if (fInDecimal) {
  374. if (*pszCurrent > '0') {
  375. fNonZeroDecimal = TRUE;
  376. }
  377. }
  378. else {
  379. nAccum = nAccum * 10 + (*pszCurrent - '0');
  380. }
  381. }
  382. else
  383. break;
  384. pszCurrent++;
  385. }
  386. if (fFoundDigits) {
  387. hres = NOERROR;
  388. if (fNonZeroDecimal)
  389. nAccum++; /* round away from zero if decimal present */
  390. if (fNegative)
  391. nAccum = -nAccum;
  392. }
  393. if (SUCCEEDED(hres)) {
  394. if (pnOut != NULL)
  395. *pnOut = nAccum;
  396. *ppszNumber = pszCurrent;
  397. SkipWhitespace(ppszNumber);
  398. }
  399. return hres;
  400. }
  401. HRESULT ParsePseudoFloat(LPSTR *ppszNumber, INT *pnOut)
  402. {
  403. HRESULT hres = ResultFromScode(MK_E_SYNTAX);
  404. INT val1, val2;
  405. BOOL fInDecimal = FALSE;
  406. CHAR achBuffer[ 256 ]; // ugly
  407. LPSTR pszCurrent = *ppszNumber;
  408. *achBuffer = '\0';
  409. /* Handle one sign character. */
  410. if (*pszCurrent == '+') {
  411. pszCurrent++;
  412. }
  413. else if (*pszCurrent == '-') {
  414. strcatf( achBuffer, "-" );
  415. pszCurrent++;
  416. }
  417. for (;;) {
  418. if (*pszCurrent == '.') {
  419. if ( fInDecimal ) break;
  420. fInDecimal = TRUE;
  421. strcatf( achBuffer, "." );
  422. }
  423. else if (*pszCurrent >= '0' && *pszCurrent <= '9') {
  424. CHAR achFoo[ 2 ] = { '\0', '\0' };
  425. achFoo[ 0 ] = *pszCurrent;
  426. strcatf( achBuffer, achFoo );
  427. }
  428. else
  429. break;
  430. pszCurrent++;
  431. }
  432. if ( !fInDecimal )
  433. {
  434. strcatf( achBuffer, ".0" );
  435. }
  436. if ( sscanf( achBuffer, "%d.%d", &val1, &val2 ) == 2 )
  437. {
  438. hres = NOERROR;
  439. }
  440. if (SUCCEEDED(hres)) {
  441. if (pnOut != NULL)
  442. *pnOut = ( ( val1 << 16 ) & 0xFFFF0000 ) | ( val2 & 0x0000FFFF );
  443. *ppszNumber = pszCurrent;
  444. SkipWhitespace(ppszNumber);
  445. }
  446. return hres;
  447. }
  448. const char szPicsVersionLabel[] = "PICS-";
  449. const UINT cchLabel = (sizeof(szPicsVersionLabel)-1) / sizeof(szPicsVersionLabel[0]);
  450. /* Returns a pointer to the closing doublequote of a quoted string, counting
  451. * linefeeds as we go. Returns NULL if no closing doublequote found.
  452. */
  453. LPSTR RatFileParser::EatQuotedString(LPSTR pIn)
  454. {
  455. LPSTR pszQuote = strchrf(pIn, '\"');
  456. if (pszQuote == NULL)
  457. return NULL;
  458. pIn = strchrf(pIn, '\n');
  459. while (pIn != NULL && pIn < pszQuote) {
  460. m_nLine++;
  461. pIn = strchrf(pIn+1, '\n');
  462. }
  463. return pszQuote;
  464. }
  465. /***************************************************************************
  466. Member functions for ET* classes
  467. ***************************************************************************/
  468. /* ETN */
  469. #ifdef DEBUG
  470. void ETN::Set(int rIn){
  471. Init();
  472. r = rIn;
  473. }
  474. int ETN::Get(){
  475. assert(fIsInit());
  476. return r;
  477. }
  478. #endif
  479. ETN* ETN::Duplicate(){
  480. ETN *pETN=new ETN;
  481. if (fIsInit()) pETN->Set(Get());
  482. return pETN;
  483. }
  484. /* ETB */
  485. #ifdef DEBUG
  486. BOOL ETB::Get()
  487. {
  488. assert(fIsInit());
  489. return m_nFlags & ETB_VALUE;
  490. }
  491. void ETB::Set(BOOL b)
  492. {
  493. m_nFlags = ETB_ISINIT | (b ? ETB_VALUE : 0);
  494. }
  495. #endif
  496. ETB* ETB::Duplicate()
  497. {
  498. assert(fIsInit());
  499. ETB *pETB = new ETB;
  500. if (pETB != NULL)
  501. pETB->m_nFlags = m_nFlags;
  502. return pETB;
  503. }
  504. /* ETS */
  505. ETS::~ETS()
  506. {
  507. if (pc != NULL) {
  508. delete pc;
  509. pc = NULL;
  510. }
  511. }
  512. #ifdef DEBUG
  513. char* ETS::Get()
  514. {
  515. assert(fIsInit());
  516. return pc;
  517. }
  518. #endif
  519. void ETS::Set(const char *pIn)
  520. {
  521. if (pc != NULL)
  522. delete pc;
  523. if (pIn != NULL) {
  524. pc = new char[strlenf(pIn) + 1];
  525. if (pc != NULL) {
  526. strcpyf(pc, pIn);
  527. }
  528. }
  529. else {
  530. pc = NULL;
  531. }
  532. }
  533. void ETS::SetTo(char *pIn)
  534. {
  535. if (pc != NULL)
  536. delete pc;
  537. pc = pIn;
  538. }
  539. ETS* ETS::Duplicate()
  540. {
  541. ETS *pETS=new ETS;
  542. try
  543. {
  544. if (pETS != NULL)
  545. {
  546. pETS->Set(Get());
  547. }
  548. }
  549. catch(...)
  550. {
  551. }
  552. return pETS;
  553. }
  554. /***************************************************************************
  555. Worker functions for inheriting category properties and other
  556. miscellaneous category stuff.
  557. ***************************************************************************/
  558. HRESULT PicsCategory::InitializeMyDefaults(PicsCategory *pCategory)
  559. {
  560. if (!pCategory->etnMin.fIsInit() && etnMin.fIsInit())
  561. pCategory->etnMin.Set(etnMin.Get());
  562. if (!pCategory->etnMax.fIsInit() && etnMax.fIsInit())
  563. pCategory->etnMax.Set(etnMax.Get());
  564. if (!pCategory->etfMulti.fIsInit() && etfMulti.fIsInit())
  565. pCategory->etfMulti.Set(etfMulti.Get());
  566. if (!pCategory->etfInteger.fIsInit() && etfInteger.fIsInit())
  567. pCategory->etfInteger.Set(etfInteger.Get());
  568. if (!pCategory->etfLabelled.fIsInit() && etfLabelled.fIsInit())
  569. pCategory->etfLabelled.Set(etfLabelled.Get());
  570. if (!pCategory->etfUnordered.fIsInit() && etfUnordered.fIsInit())
  571. pCategory->etfUnordered.Set(etfUnordered.Get());
  572. return NOERROR;
  573. }
  574. HRESULT PicsRatingSystem::InitializeMyDefaults(PicsCategory *pCategory)
  575. {
  576. if (m_pDefaultOptions != NULL)
  577. return m_pDefaultOptions->InitializeMyDefaults(pCategory);
  578. return NOERROR; /* no defaults to initialize */
  579. }
  580. HRESULT PicsDefault::InitializeMyDefaults(PicsCategory *pCategory)
  581. {
  582. if (!pCategory->etnMin.fIsInit() && etnMin.fIsInit())
  583. pCategory->etnMin.Set(etnMin.Get());
  584. if (!pCategory->etnMax.fIsInit() && etnMax.fIsInit())
  585. pCategory->etnMax.Set(etnMax.Get());
  586. if (!pCategory->etfMulti.fIsInit() && etfMulti.fIsInit())
  587. pCategory->etfMulti.Set(etfMulti.Get());
  588. if (!pCategory->etfInteger.fIsInit() && etfInteger.fIsInit())
  589. pCategory->etfInteger.Set(etfInteger.Get());
  590. if (!pCategory->etfLabelled.fIsInit() && etfLabelled.fIsInit())
  591. pCategory->etfLabelled.Set(etfLabelled.Get());
  592. if (!pCategory->etfUnordered.fIsInit() && etfUnordered.fIsInit())
  593. pCategory->etfUnordered.Set(etfUnordered.Get());
  594. return NOERROR;
  595. }
  596. HRESULT PicsEnum::InitializeMyDefaults(PicsCategory *pCategory)
  597. {
  598. return E_NOTIMPL; /* should never have a category inherit from an enum */
  599. }
  600. PicsExtension::PicsExtension()
  601. : m_pszRatingBureau(NULL)
  602. {
  603. /* nothing else */
  604. }
  605. PicsExtension::~PicsExtension()
  606. {
  607. delete m_pszRatingBureau;
  608. }
  609. HRESULT PicsExtension::InitializeMyDefaults(PicsCategory *pCategory)
  610. {
  611. return E_NOTIMPL; /* should never have a category inherit from an extension */
  612. }
  613. void PicsCategory::FixupLimits()
  614. {
  615. BOOL fLabelled = (etfLabelled.fIsInit() && etfLabelled.Get());
  616. /*fix up max and min values*/
  617. if (fLabelled ||
  618. (arrpPE.Length()>0 && (!etnMax.fIsInit() || !etnMax.fIsInit())))
  619. {
  620. if (arrpPE.Length() > 0)
  621. {
  622. if (!etnMax.fIsInit())
  623. etnMax.Set(N_INFINITY);
  624. if (!etnMin.fIsInit())
  625. etnMin.Set(P_INFINITY);
  626. for (int z=0;z<arrpPE.Length();++z)
  627. {
  628. if (arrpPE[z]->etnValue.Get() > etnMax.Get()) etnMax.Set(arrpPE[z]->etnValue.Get());
  629. if (arrpPE[z]->etnValue.Get() < etnMin.Get()) etnMin.Set(arrpPE[z]->etnValue.Get());
  630. }
  631. }
  632. else {
  633. etfLabelled.Set(FALSE); /* no enum labels? better not have labelled flag then */
  634. fLabelled = FALSE;
  635. }
  636. }
  637. /*sort labels by value*/
  638. if (fLabelled)
  639. {
  640. int x,y;
  641. PicsEnum *pPE;
  642. for (x=0;x<arrpPE.Length()-1;++x){
  643. for (y=x+1;y<arrpPE.Length();++y){
  644. if (arrpPE[y]->etnValue.Get() < arrpPE[x]->etnValue.Get()){
  645. pPE = arrpPE[x];
  646. arrpPE[x] = arrpPE[y];
  647. arrpPE[y] = pPE;
  648. }
  649. }
  650. }
  651. }
  652. }
  653. void PicsCategory::SetParents(PicsRatingSystem *pOwner)
  654. {
  655. pPRS = pOwner;
  656. UINT cSubCategories = arrpPC.Length();
  657. for (UINT i = 0; i < cSubCategories; i++) {
  658. InitializeMyDefaults(arrpPC[i]); /* subcategory inherits our defaults */
  659. arrpPC[i]->SetParents(pOwner); /* process all subcategories */
  660. }
  661. FixupLimits(); /* inheritance is done, make sure limits make sense */
  662. }
  663. /***************************************************************************
  664. Handler functions which know how to parse the various kinds of content
  665. which can occur within a parenthesized object.
  666. ***************************************************************************/
  667. HRESULT RatParseString(LPSTR *ppszIn, LPVOID *ppOut, RatFileParser *pParser)
  668. {
  669. *ppOut = NULL;
  670. LPSTR pszCurrent = *ppszIn;
  671. if (*pszCurrent != '\"')
  672. return RAT_E_EXPECTEDSTRING;
  673. pszCurrent++;
  674. LPSTR pszEnd = pParser->EatQuotedString(pszCurrent);
  675. if (pszEnd == NULL)
  676. return RAT_E_EXPECTEDSTRING;
  677. UINT cbString = DIFF(pszEnd - pszCurrent);
  678. LPSTR pszNew = new char[cbString + 1];
  679. if (pszNew == NULL)
  680. return E_OUTOFMEMORY;
  681. memcpyf(pszNew, pszCurrent, cbString);
  682. pszNew[cbString] = '\0';
  683. *ppOut = (LPVOID)pszNew;
  684. *ppszIn = pParser->FindNonWhite(pszEnd + 1);
  685. return NOERROR;
  686. }
  687. HRESULT RatParseNumber(LPSTR *ppszIn, LPVOID *ppOut, RatFileParser *pParser)
  688. {
  689. int n;
  690. LPSTR pszCurrent = *ppszIn;
  691. HRESULT hres = ::ParseNumber(&pszCurrent, &n);
  692. if (FAILED(hres))
  693. return RAT_E_EXPECTEDNUMBER;
  694. *(int *)ppOut = n;
  695. LPSTR pszNewline = strchrf(*ppszIn, '\n');
  696. while (pszNewline != NULL && pszNewline < pszCurrent) {
  697. pParser->m_nLine++;
  698. pszNewline = strchrf(pszNewline+1, '\n');
  699. }
  700. *ppszIn = pszCurrent;
  701. return NOERROR;
  702. }
  703. HRESULT RatParsePseudoFloat(LPSTR *ppszIn, LPVOID *ppOut, RatFileParser *pParser)
  704. {
  705. INT n;
  706. LPSTR pszCurrent = *ppszIn;
  707. HRESULT hres = ::ParsePseudoFloat(&pszCurrent, &n);
  708. if (FAILED(hres))
  709. return RAT_E_EXPECTEDNUMBER;
  710. *(INT *)ppOut = n;
  711. LPSTR pszNewline = strchrf(*ppszIn, '\n');
  712. while (pszNewline != NULL && pszNewline < pszCurrent) {
  713. pParser->m_nLine++;
  714. pszNewline = strchrf(pszNewline+1, '\n');
  715. }
  716. *ppszIn = pszCurrent;
  717. return NOERROR;
  718. }
  719. HRESULT RatParseBool(LPSTR *ppszIn, LPVOID *ppOut, RatFileParser *pParser)
  720. {
  721. BOOL b;
  722. /* PICS spec allows a terse way of specifying a TRUE boolean -- leaving
  723. * out the value entirely. In a .RAT file, the result looks like
  724. *
  725. * (unordered)
  726. * (multivalue)
  727. *
  728. * and so on. Called has pointed us at non-whitespace, so if we see
  729. * a closing paren, we know the .RAT file author used this syntax.
  730. */
  731. if (**ppszIn == ')') {
  732. b = TRUE;
  733. }
  734. else {
  735. LPSTR pszCurrent = *ppszIn;
  736. HRESULT hres = ::GetBool(&pszCurrent, &b);
  737. if (FAILED(hres))
  738. return RAT_E_EXPECTEDBOOL;
  739. LPSTR pszNewline = strchrf(*ppszIn, '\n');
  740. while (pszNewline != NULL && pszNewline < pszCurrent) {
  741. pParser->m_nLine++;
  742. pszNewline = strchrf(pszNewline+1, '\n');
  743. }
  744. *ppszIn = pszCurrent;
  745. }
  746. *(LPBOOL)ppOut = b;
  747. return NOERROR;
  748. }
  749. AllowableOption aaoPicsCategory[] = {
  750. { ROID_TRANSMITAS, AO_SINGLE | AO_MANDATORY },
  751. { ROID_NAME, AO_SINGLE },
  752. { ROID_DESCRIPTION, AO_SINGLE },
  753. { ROID_ICON, AO_SINGLE },
  754. { ROID_EXTENSION, 0 },
  755. { ROID_INTEGER, AO_SINGLE },
  756. { ROID_LABELONLY, AO_SINGLE },
  757. { ROID_MIN, AO_SINGLE },
  758. { ROID_MAX, AO_SINGLE },
  759. { ROID_MULTIVALUE, AO_SINGLE },
  760. { ROID_UNORDERED, AO_SINGLE },
  761. { ROID_LABEL, 0 },
  762. { ROID_CATEGORY, 0 },
  763. { ROID_INVALID, 0 }
  764. };
  765. const UINT caoPicsCategory = sizeof(aaoPicsCategory) / sizeof(aaoPicsCategory[0]);
  766. HRESULT RatParseCategory(LPSTR *ppszIn, LPVOID *ppOut, RatFileParser *pParser)
  767. {
  768. /* We must make a copy of the allowable options array because the
  769. * parser will fiddle with the flags in the entries -- specifically,
  770. * setting AO_SEEN. It wouldn't be thread-safe to do this to a
  771. * static array.
  772. */
  773. AllowableOption aao[caoPicsCategory];
  774. ::memcpyf(aao, ::aaoPicsCategory, sizeof(aao));
  775. PicsCategory *pCategory = new PicsCategory;
  776. if (pCategory == NULL)
  777. return E_OUTOFMEMORY;
  778. HRESULT hres = pParser->ParseParenthesizedObject(
  779. ppszIn, /* var containing current ptr */
  780. aao, /* what's legal in this object */
  781. pCategory); /* object to add items back to */
  782. if (FAILED(hres)) {
  783. delete pCategory;
  784. return hres;
  785. }
  786. *ppOut = (LPVOID)pCategory;
  787. return NOERROR;
  788. }
  789. AllowableOption aaoPicsEnum[] = {
  790. { ROID_NAME, AO_SINGLE },
  791. { ROID_DESCRIPTION, AO_SINGLE },
  792. { ROID_VALUE, AO_SINGLE | AO_MANDATORY },
  793. { ROID_ICON, AO_SINGLE },
  794. { ROID_INVALID, 0 }
  795. };
  796. const UINT caoPicsEnum = sizeof(aaoPicsEnum) / sizeof(aaoPicsEnum[0]);
  797. HRESULT RatParseLabel(LPSTR *ppszIn, LPVOID *ppOut, RatFileParser *pParser)
  798. {
  799. /* We must make a copy of the allowable options array because the
  800. * parser will fiddle with the flags in the entries -- specifically,
  801. * setting AO_SEEN. It wouldn't be thread-safe to do this to a
  802. * static array.
  803. */
  804. AllowableOption aao[caoPicsEnum];
  805. ::memcpyf(aao, ::aaoPicsEnum, sizeof(aao));
  806. PicsEnum *pEnum = new PicsEnum;
  807. if (pEnum == NULL)
  808. return E_OUTOFMEMORY;
  809. HRESULT hres = pParser->ParseParenthesizedObject(
  810. ppszIn, /* var containing current ptr */
  811. aao, /* what's legal in this object */
  812. pEnum); /* object to add items back to */
  813. if (FAILED(hres)) {
  814. delete pEnum;
  815. return hres;
  816. }
  817. *ppOut = (LPVOID)pEnum;
  818. return NOERROR;
  819. }
  820. AllowableOption aaoPicsDefault[] = {
  821. { ROID_EXTENSION, 0 },
  822. { ROID_INTEGER, AO_SINGLE },
  823. { ROID_LABELONLY, AO_SINGLE },
  824. { ROID_MAX, AO_SINGLE },
  825. { ROID_MIN, AO_SINGLE },
  826. { ROID_MULTIVALUE, AO_SINGLE },
  827. { ROID_UNORDERED, AO_SINGLE },
  828. { ROID_INVALID, 0 }
  829. };
  830. const UINT caoPicsDefault = sizeof(aaoPicsDefault) / sizeof(aaoPicsDefault[0]);
  831. HRESULT RatParseDefault(LPSTR *ppszIn, LPVOID *ppOut, RatFileParser *pParser)
  832. {
  833. /* We must make a copy of the allowable options array because the
  834. * parser will fiddle with the flags in the entries -- specifically,
  835. * setting AO_SEEN. It wouldn't be thread-safe to do this to a
  836. * static array.
  837. */
  838. AllowableOption aao[caoPicsDefault];
  839. ::memcpyf(aao, ::aaoPicsDefault, sizeof(aao));
  840. PicsDefault *pDefault = new PicsDefault;
  841. if (pDefault == NULL)
  842. return E_OUTOFMEMORY;
  843. HRESULT hres = pParser->ParseParenthesizedObject(
  844. ppszIn, /* var containing current ptr */
  845. aao, /* what's legal in this object */
  846. pDefault); /* object to add items back to */
  847. if (FAILED(hres)) {
  848. delete pDefault;
  849. return hres;
  850. }
  851. *ppOut = (LPVOID)pDefault;
  852. return NOERROR;
  853. }
  854. AllowableOption aaoPicsExtension[] = {
  855. { ROID_MANDATORY, AO_SINGLE },
  856. { ROID_OPTIONAL, AO_SINGLE },
  857. { ROID_INVALID, 0 }
  858. };
  859. const UINT caoPicsExtension = sizeof(aaoPicsExtension) / sizeof(aaoPicsExtension[0]);
  860. HRESULT RatParseExtension(LPSTR *ppszIn, LPVOID *ppOut, RatFileParser *pParser)
  861. {
  862. /* We must make a copy of the allowable options array because the
  863. * parser will fiddle with the flags in the entries -- specifically,
  864. * setting AO_SEEN. It wouldn't be thread-safe to do this to a
  865. * static array.
  866. */
  867. AllowableOption aao[caoPicsExtension];
  868. ::memcpyf(aao, ::aaoPicsExtension, sizeof(aao));
  869. PicsExtension *pExtension = new PicsExtension;
  870. if (pExtension == NULL)
  871. return E_OUTOFMEMORY;
  872. HRESULT hres = pParser->ParseParenthesizedObject(
  873. ppszIn, /* var containing current ptr */
  874. aao, /* what's legal in this object */
  875. pExtension); /* object to add items back to */
  876. if (FAILED(hres)) {
  877. delete pExtension;
  878. return hres;
  879. }
  880. *ppOut = (LPVOID)pExtension;
  881. return NOERROR;
  882. }
  883. /* Since the only extension we support right now is the one for a label
  884. * bureau, we just return the first quoted string we find if the caller
  885. * wants it. If ppOut is NULL, then it's some other extension and the
  886. * caller doesn't care about the data, he just wants it eaten.
  887. */
  888. HRESULT ParseRatExtensionData(LPSTR *ppszIn, RatFileParser *pParser, LPSTR *ppOut)
  889. {
  890. HRESULT hres = NOERROR;
  891. LPSTR pszCurrent = *ppszIn;
  892. /* Must look for closing ')' ourselves to terminate */
  893. while (*pszCurrent != ')') {
  894. if (*pszCurrent == '(') {
  895. pszCurrent = pParser->FindNonWhite(pszCurrent+1); /* skip paren and whitespace */
  896. hres = ParseRatExtensionData(&pszCurrent, pParser, ppOut); /* parentheses contain data */
  897. if (FAILED(hres))
  898. return hres;
  899. if (*pszCurrent != ')')
  900. return RAT_E_EXPECTEDRIGHT;
  901. pszCurrent = pParser->FindNonWhite(pszCurrent+1); /* skip close ) and whitespace */
  902. }
  903. else if (*pszCurrent == '\"') { /* should be just a quoted string */
  904. if (ppOut != NULL && *ppOut == NULL) {
  905. hres = RatParseString(&pszCurrent, (LPVOID *)ppOut, pParser);
  906. }
  907. else {
  908. ++pszCurrent;
  909. LPSTR pszEndQuote = pParser->EatQuotedString(pszCurrent);
  910. if (pszEndQuote == NULL)
  911. return RAT_E_EXPECTEDSTRING;
  912. pszCurrent = pParser->FindNonWhite(pszEndQuote+1); /* skip close " and whitespace */
  913. }
  914. }
  915. else
  916. return RAT_E_UNKNOWNITEM; /* general bad syntax */
  917. }
  918. /* Caller will skip over final ')' for us. */
  919. *ppszIn = pszCurrent;
  920. return NOERROR;
  921. }
  922. HRESULT RatParseMandatory(LPSTR *ppszIn, LPVOID *ppOut, RatFileParser *pParser)
  923. {
  924. LPSTR pszCurrent = *ppszIn;
  925. /* First thing better be a quoted URL identifying the extension. */
  926. if (*pszCurrent != '\"')
  927. return RAT_E_EXPECTEDSTRING;
  928. pszCurrent++;
  929. LPSTR pszEnd = pParser->EatQuotedString(pszCurrent);
  930. if (pszCurrent == NULL)
  931. return RAT_E_EXPECTEDSTRING; /* missing closing " */
  932. /* See if it's the extension for a label bureau. */
  933. LPSTR pszBureau = NULL;
  934. LPSTR *ppData = NULL;
  935. if (IsEqualToken(pszCurrent, pszEnd, ::szRatingBureauExtension)) {
  936. ppData = &pszBureau;
  937. }
  938. pszCurrent = pParser->FindNonWhite(pszEnd+1); /* skip closing " and whitespace */
  939. HRESULT hres = ParseRatExtensionData(&pszCurrent, pParser, ppData);
  940. if (FAILED(hres))
  941. return hres;
  942. *ppOut = pszBureau; /* return label bureau string if that's what we found */
  943. *ppszIn = pszCurrent;
  944. if (ppData == NULL)
  945. return RAT_E_UNKNOWNMANDATORY; /* we didn't recognize it */
  946. else
  947. return NOERROR;
  948. }
  949. /* RatParseOptional uses the code in RatParseMandatory to parse the extension
  950. * data, in case an extension that should be optional comes in as mandatory.
  951. * We then detect RatParseMandatory rejecting the thing as unrecognized and
  952. * allow it through, since here it's optional.
  953. */
  954. HRESULT RatParseOptional(LPSTR *ppszIn, LPVOID *ppOut, RatFileParser *pParser)
  955. {
  956. HRESULT hres = RatParseMandatory(ppszIn, ppOut, pParser);
  957. if (hres == RAT_E_UNKNOWNMANDATORY)
  958. hres = S_OK;
  959. return hres;
  960. }
  961. /***************************************************************************
  962. Code to identify the opening keyword of a parenthesized object and
  963. associate it with content.
  964. ***************************************************************************/
  965. /* The following array is indexed by RatObjectID values. */
  966. struct {
  967. LPCSTR pszToken; /* token by which we identify it */
  968. RatObjectHandler pHandler; /* function which parses the object's contents */
  969. } aObjectDescriptions[] = {
  970. { szNULL, NULL },
  971. { NULL, NULL }, /* placeholder for comparing against no token */
  972. { szPicsVersion, RatParsePseudoFloat },
  973. { szRatingSystem, RatParseString },
  974. { szRatingService, RatParseString },
  975. { szRatingBureau, RatParseString },
  976. { szBureauRequired, RatParseBool },
  977. { szCategory, RatParseCategory },
  978. { szTransmitAs, RatParseString },
  979. { szLabel, RatParseLabel },
  980. { szValue, RatParseNumber },
  981. { szDefault, RatParseDefault },
  982. { szDescription, RatParseString },
  983. { szExtensionOption, RatParseExtension },
  984. { szMandatory, RatParseMandatory },
  985. { szOptional, RatParseOptional },
  986. { szIcon, RatParseString },
  987. { szInteger, RatParseBool },
  988. { szLabelOnly, RatParseBool },
  989. { szMax, RatParseNumber },
  990. { szMin, RatParseNumber },
  991. { szMultiValue, RatParseBool },
  992. { szName, RatParseString },
  993. { szUnordered, RatParseBool }
  994. };
  995. /* ParseToOpening eats the opening '(' of a parenthesized object, and
  996. * verifies that the token just inside it is one of the expected ones.
  997. * If so, *ppIn is advanced past that token to the next non-whitespace
  998. * character; otherwise, an error is returned.
  999. *
  1000. * For example, if *ppIn is pointing at "(PICS-version 1.1)", and
  1001. * ROID_PICSVERSION is in the allowable option table supplied, then
  1002. * NOERROR is returned and *ppIn will point at "1.1)".
  1003. *
  1004. * If the function is successful, *ppFound is set to point to the element
  1005. * in the allowable-options table which matches the type of thing this
  1006. * object actually is.
  1007. */
  1008. HRESULT RatFileParser::ParseToOpening(LPSTR *ppIn, AllowableOption *paoExpected,
  1009. AllowableOption **ppFound)
  1010. {
  1011. LPSTR pszCurrent = *ppIn;
  1012. pszCurrent = FindNonWhite(pszCurrent);
  1013. if (*pszCurrent != '(')
  1014. return RAT_E_EXPECTEDLEFT;
  1015. pszCurrent = FindNonWhite(pszCurrent+1); /* skip '(' and whitespace */
  1016. LPSTR pszTokenEnd = FindTokenEnd(pszCurrent);
  1017. for (; paoExpected->roid != ROID_INVALID; paoExpected++) {
  1018. LPCSTR pszThisToken = aObjectDescriptions[paoExpected->roid].pszToken;
  1019. /* Special case for beginning of RAT file structure: no token at all. */
  1020. if (pszThisToken == NULL) {
  1021. if (*pszCurrent == '(') {
  1022. *ppIn = pszCurrent;
  1023. *ppFound = paoExpected;
  1024. return NOERROR;
  1025. }
  1026. else {
  1027. return RAT_E_EXPECTEDLEFT;
  1028. }
  1029. }
  1030. else if (IsEqualToken(pszCurrent, pszTokenEnd, pszThisToken))
  1031. break;
  1032. }
  1033. if (paoExpected->roid != ROID_INVALID) {
  1034. *ppIn = FindNonWhite(pszTokenEnd); /* skip token and whitespace */
  1035. *ppFound = paoExpected;
  1036. return NOERROR;
  1037. }
  1038. else
  1039. return RAT_E_UNKNOWNITEM;
  1040. }
  1041. /***************************************************************************
  1042. The top-level entrypoint for parsing out a whole rating system.
  1043. ***************************************************************************/
  1044. AllowableOption aaoPicsRatingSystem[] = {
  1045. { ROID_PICSVERSION, AO_SINGLE | AO_MANDATORY },
  1046. { ROID_RATINGSYSTEM, AO_SINGLE | AO_MANDATORY },
  1047. { ROID_RATINGSERVICE, AO_SINGLE | AO_MANDATORY },
  1048. { ROID_RATINGBUREAU, AO_SINGLE },
  1049. { ROID_BUREAUREQUIRED, AO_SINGLE },
  1050. { ROID_DEFAULT, 0 },
  1051. { ROID_DESCRIPTION, AO_SINGLE },
  1052. { ROID_EXTENSION, 0 },
  1053. { ROID_ICON, AO_SINGLE },
  1054. { ROID_NAME, AO_SINGLE },
  1055. { ROID_CATEGORY, AO_MANDATORY },
  1056. { ROID_INVALID, 0 }
  1057. };
  1058. const UINT caoPicsRatingSystem = sizeof(aaoPicsRatingSystem) / sizeof(aaoPicsRatingSystem[0]);
  1059. HRESULT PicsRatingSystem::Parse(LPSTR pIn)
  1060. {
  1061. /* This guy is small enough to just init directly on the stack */
  1062. AllowableOption aaoRoot[] = { { ROID_PICSDOCUMENT, 0 }, { ROID_INVALID, 0 } };
  1063. AllowableOption aao[caoPicsRatingSystem];
  1064. ::memcpyf(aao, ::aaoPicsRatingSystem, sizeof(aao));
  1065. AllowableOption *pFound;
  1066. RatFileParser parser;
  1067. HRESULT hres = parser.ParseToOpening(&pIn, aaoRoot, &pFound);
  1068. if (FAILED(hres))
  1069. return hres; /* some error early on */
  1070. hres = parser.ParseParenthesizedObject(
  1071. &pIn, /* var containing current ptr */
  1072. aao, /* what's legal in this object */
  1073. this); /* object to add items back to */
  1074. if (FAILED(hres))
  1075. nErrLine = parser.m_nLine;
  1076. return hres;
  1077. }
  1078. /***************************************************************************
  1079. Callbacks into the various class objects to add parsed properties.
  1080. ***************************************************************************/
  1081. HRESULT PicsRatingSystem::AddItem(RatObjectID roid, LPVOID pData)
  1082. {
  1083. HRESULT hres = S_OK;
  1084. switch (roid) {
  1085. case ROID_PICSVERSION:
  1086. etnPicsVersion.Set((INT_PTR)pData);
  1087. break;
  1088. case ROID_RATINGSYSTEM:
  1089. etstrRatingSystem.SetTo((LPSTR)pData);
  1090. break;
  1091. case ROID_RATINGSERVICE:
  1092. etstrRatingService.SetTo((LPSTR)pData);
  1093. break;
  1094. case ROID_RATINGBUREAU:
  1095. etstrRatingBureau.SetTo((LPSTR)pData);
  1096. break;
  1097. case ROID_BUREAUREQUIRED:
  1098. etbBureauRequired.Set((INT_PTR)pData);
  1099. break;
  1100. case ROID_DEFAULT:
  1101. m_pDefaultOptions = (PicsDefault *)pData;
  1102. break;
  1103. case ROID_DESCRIPTION:
  1104. etstrDesc.SetTo((LPSTR)pData);
  1105. break;
  1106. case ROID_EXTENSION:
  1107. {
  1108. /* just eat extensions for now */
  1109. PicsExtension *pExtension = (PicsExtension *)pData;
  1110. if (pExtension != NULL) {
  1111. /* If this is a rating bureau extension, take his bureau
  1112. * string and store it in this PicsRatingSystem. We now
  1113. * own the memory, so NULL out the extension's pointer to
  1114. * it so he won't delete it.
  1115. */
  1116. if (pExtension->m_pszRatingBureau != NULL) {
  1117. etstrRatingBureau.SetTo(pExtension->m_pszRatingBureau);
  1118. pExtension->m_pszRatingBureau = NULL;
  1119. }
  1120. delete pExtension;
  1121. }
  1122. }
  1123. break;
  1124. case ROID_ICON:
  1125. etstrIcon.SetTo((LPSTR)pData);
  1126. break;
  1127. case ROID_NAME:
  1128. etstrName.SetTo((LPSTR)pData);
  1129. break;
  1130. case ROID_CATEGORY:
  1131. {
  1132. PicsCategory *pCategory = (PicsCategory *)pData;
  1133. hres = arrpPC.Append(pCategory) ? S_OK : E_OUTOFMEMORY;
  1134. if (FAILED(hres)) {
  1135. delete pCategory;
  1136. }
  1137. else {
  1138. InitializeMyDefaults(pCategory); /* category inherits default settings */
  1139. pCategory->SetParents(this); /* set pPRS fields in whole tree */
  1140. }
  1141. }
  1142. break;
  1143. default:
  1144. assert(FALSE); /* shouldn't have been given a ROID that wasn't in
  1145. * the table we passed to the parser! */
  1146. hres = E_UNEXPECTED;
  1147. break;
  1148. }
  1149. return hres;
  1150. }
  1151. void PicsRatingSystem::Dump( void )
  1152. {
  1153. fprintf( stdout,
  1154. "Rating system: %s\n"
  1155. "Version: %d.%d\n"
  1156. "Rating Service: %s\n"
  1157. "Rating bureau: %s\n"
  1158. "Bureau required: %s\n"
  1159. "Description: %s\n"
  1160. "Icon: %s\n"
  1161. "Name: %s\n"
  1162. "Number of categories: %d\n",
  1163. etstrRatingSystem.Get(),
  1164. ( etnPicsVersion.Get() & 0xFFFF0000 ) >> 16,
  1165. etnPicsVersion.Get() & 0x0000FFFF,
  1166. etstrRatingService.Get(),
  1167. etstrRatingBureau.Get(),
  1168. etbBureauRequired.Get() ? "TRUE" : "FALSE",
  1169. etstrDesc.Get(),
  1170. etstrIcon.Get(),
  1171. etstrName.Get(),
  1172. arrpPC.Length() );
  1173. int iCounter = 0;
  1174. for( ; iCounter < arrpPC.Length(); iCounter++ )
  1175. {
  1176. arrpPC[ iCounter ]->Dump();
  1177. }
  1178. }
  1179. //---------------------------------------------------------------
  1180. // boydm
  1181. void PicsRatingSystem::OutputLabels( CString &sz, CString szURL, CString szName, CString szStart, CString szEnd )
  1182. {
  1183. CString szScratch;
  1184. CString szTemp;
  1185. INT_PTR dwVersion = etnPicsVersion.Get();
  1186. // start with the name, and the version number
  1187. szTemp = szPicsOpening;
  1188. szScratch.Format( _T("%s%d.%d"), szTemp, HIWORD(dwVersion), LOWORD(dwVersion) );
  1189. sz += szScratch;
  1190. // add in the URL string - surrounded by quotes and a return
  1191. sz += _T(" \"http://www.rsac.org/ratingsv01.html\" ");
  1192. // start the labels
  1193. sz += szShortLabelWord;
  1194. sz += _T(" ");
  1195. // if it is there, add the by name string
  1196. if ( !szName.IsEmpty() )
  1197. {
  1198. sz += szByOption;
  1199. sz += _T(" \"");
  1200. sz += szName;
  1201. sz += _T("\" ");
  1202. }
  1203. // if it is there, add the start string
  1204. if ( !szStart.IsEmpty() )
  1205. {
  1206. sz += szOnOption;
  1207. sz += _T(" \"");
  1208. sz += szStart;
  1209. sz += _T("\" ");
  1210. }
  1211. // if it is there, add the expiration string
  1212. if ( !szEnd.IsEmpty() )
  1213. {
  1214. sz += szExpOption;
  1215. sz += _T(" \"");
  1216. sz += szEnd;
  1217. sz += _T("\" ");
  1218. }
  1219. // add in the categorical ratings
  1220. DWORD nCat = arrpPC.Length();
  1221. sz += szShortRatings;
  1222. sz += _T(" (");
  1223. for ( DWORD iCat = 0; iCat < nCat; iCat++ )
  1224. {
  1225. arrpPC[iCat]->OutputLabel( sz );
  1226. }
  1227. // trim any trailing whitespace
  1228. sz.TrimRight();
  1229. // close with a parenthesis
  1230. sz += _T(')');
  1231. // end with the closing parenthesis
  1232. sz += _T(")");
  1233. }
  1234. //---------------------------------------------------------------
  1235. HRESULT PicsCategory::AddItem(RatObjectID roid, LPVOID pData)
  1236. {
  1237. HRESULT hres = S_OK;
  1238. switch (roid) {
  1239. case ROID_TRANSMITAS:
  1240. etstrTransmitAs.SetTo((LPSTR)pData);
  1241. break;
  1242. case ROID_NAME:
  1243. etstrName.SetTo((LPSTR)pData);
  1244. break;
  1245. case ROID_DESCRIPTION:
  1246. etstrDesc.SetTo((LPSTR)pData);
  1247. break;
  1248. case ROID_ICON:
  1249. etstrIcon.SetTo((LPSTR)pData);
  1250. break;
  1251. case ROID_EXTENSION:
  1252. { /* we support no extensions below the rating system level */
  1253. PicsExtension *pExtension = (PicsExtension *)pData;
  1254. if (pExtension != NULL)
  1255. delete pExtension;
  1256. }
  1257. break;
  1258. case ROID_INTEGER:
  1259. etfInteger.Set((INT_PTR)pData);
  1260. break;
  1261. case ROID_LABELONLY:
  1262. etfLabelled.Set((INT_PTR)pData);
  1263. break;
  1264. case ROID_MULTIVALUE:
  1265. etfMulti.Set((INT_PTR)pData);
  1266. break;
  1267. case ROID_UNORDERED:
  1268. etfUnordered.Set((INT_PTR)pData);
  1269. break;
  1270. case ROID_MIN:
  1271. etnMin.Set((INT_PTR)pData);
  1272. break;
  1273. case ROID_MAX:
  1274. etnMax.Set((INT_PTR)pData);
  1275. break;
  1276. case ROID_LABEL:
  1277. {
  1278. PicsEnum *pEnum = (PicsEnum *)pData;
  1279. hres = arrpPE.Append(pEnum) ? S_OK : E_OUTOFMEMORY;
  1280. if (FAILED(hres))
  1281. delete pEnum;
  1282. }
  1283. break;
  1284. case ROID_CATEGORY:
  1285. {
  1286. PicsCategory *pCategory = (PicsCategory *)pData;
  1287. /* For a nested category, synthesize the transmit-name from
  1288. * ours and the child's (e.g., parent category 'color' plus
  1289. * child category 'hue' becomes 'color/hue'.
  1290. *
  1291. * Note that the memory we allocate for the new name will be
  1292. * owned by pCategory->etstrTransmitAs. There is no memory
  1293. * leak there.
  1294. */
  1295. UINT cbCombined = strlenf(etstrTransmitAs.Get()) +
  1296. strlenf(pCategory->etstrTransmitAs.Get()) +
  1297. 2; /* for PicsDelimChar + null */
  1298. LPSTR pszTemp = new char[cbCombined];
  1299. if (pszTemp == NULL)
  1300. hres = E_OUTOFMEMORY;
  1301. else {
  1302. sprintf(pszTemp, "%s%c%s", etstrTransmitAs.Get(),
  1303. PicsDelimChar, pCategory->etstrTransmitAs.Get());
  1304. pCategory->etstrTransmitAs.SetTo(pszTemp);
  1305. hres = arrpPC.Append(pCategory) ? S_OK : E_OUTOFMEMORY;
  1306. }
  1307. if (FAILED(hres)) {
  1308. delete pCategory;
  1309. }
  1310. }
  1311. break;
  1312. default:
  1313. assert(FALSE); /* shouldn't have been given a ROID that wasn't in
  1314. * the table we passed to the parser! */
  1315. hres = E_UNEXPECTED;
  1316. break;
  1317. }
  1318. return hres;
  1319. }
  1320. void PicsCategory::Dump( void )
  1321. {
  1322. fprintf( stdout,
  1323. " Transmit As: %s Name: %s Description: %s Icon: %s\n",
  1324. etstrTransmitAs.Get(),
  1325. etstrName.Get(),
  1326. etstrDesc.Get(),
  1327. etstrIcon.Get() );
  1328. int iCounter = 0;
  1329. for( ; iCounter < arrpPE.Length(); iCounter++ )
  1330. {
  1331. arrpPE[ iCounter ]->Dump();
  1332. }
  1333. }
  1334. //---------------------------------------------------------------
  1335. void PicsCategory::OutputLabel( CString &sz )
  1336. {
  1337. CString szCat;
  1338. CString szTransmit = etstrTransmitAs.Get();
  1339. // prepare the category string
  1340. szCat.Format( _T("%s %d "), szTransmit, currentValue );
  1341. sz += szCat;
  1342. }
  1343. //---------------------------------------------------------------
  1344. BOOL PicsCategory::FSetValuePair( CHAR chCat, WORD value )
  1345. {
  1346. CString szCat = etstrTransmitAs.Get();
  1347. // first check to see if this is the right category
  1348. if ( szCat == chCat )
  1349. {
  1350. // success! set the value and return
  1351. currentValue = value;
  1352. return TRUE;
  1353. }
  1354. // try its categorical children
  1355. DWORD nCat = arrpPC.Length();
  1356. for ( DWORD iCat = 0; iCat < nCat; iCat++ )
  1357. {
  1358. // stop at the first successful setting
  1359. if ( arrpPC[iCat]->FSetValuePair(chCat, value) )
  1360. return TRUE;
  1361. }
  1362. // nope
  1363. return FALSE;
  1364. }
  1365. //---------------------------------------------------------------
  1366. HRESULT PicsEnum::AddItem(RatObjectID roid, LPVOID pData)
  1367. {
  1368. HRESULT hres = S_OK;
  1369. switch (roid) {
  1370. case ROID_NAME:
  1371. etstrName.SetTo((LPSTR)pData);
  1372. break;
  1373. case ROID_DESCRIPTION:
  1374. etstrDesc.SetTo((LPSTR)pData);
  1375. break;
  1376. case ROID_ICON:
  1377. etstrIcon.SetTo((LPSTR)pData);
  1378. break;
  1379. case ROID_VALUE:
  1380. etnValue.Set((INT_PTR)pData);
  1381. break;
  1382. default:
  1383. assert(FALSE); /* shouldn't have been given a ROID that wasn't in
  1384. * the table we passed to the parser! */
  1385. hres = E_UNEXPECTED;
  1386. break;
  1387. }
  1388. return hres;
  1389. }
  1390. void PicsEnum::Dump( void )
  1391. {
  1392. fprintf( stdout,
  1393. " %s %s %s %d\n",
  1394. etstrName.Get(),
  1395. etstrDesc.Get(),
  1396. etstrIcon.Get(),
  1397. etnValue.Get() );
  1398. }
  1399. HRESULT PicsDefault::AddItem(RatObjectID roid, LPVOID pData)
  1400. {
  1401. HRESULT hres = S_OK;
  1402. switch (roid) {
  1403. case ROID_EXTENSION:
  1404. { /* we support no extensions below the rating system level */
  1405. PicsExtension *pExtension = (PicsExtension *)pData;
  1406. if (pExtension != NULL)
  1407. delete pExtension;
  1408. }
  1409. break;
  1410. case ROID_INTEGER:
  1411. etfInteger.Set((INT_PTR)pData);
  1412. break;
  1413. case ROID_LABELONLY:
  1414. etfLabelled.Set((INT_PTR)pData);
  1415. break;
  1416. case ROID_MULTIVALUE:
  1417. etfMulti.Set((INT_PTR)pData);
  1418. break;
  1419. case ROID_UNORDERED:
  1420. etfUnordered.Set((INT_PTR)pData);
  1421. break;
  1422. case ROID_MIN:
  1423. etnMin.Set((INT_PTR)pData);
  1424. break;
  1425. case ROID_MAX:
  1426. etnMax.Set((INT_PTR)pData);
  1427. break;
  1428. default:
  1429. assert(FALSE); /* shouldn't have been given a ROID that wasn't in
  1430. * the table we passed to the parser! */
  1431. hres = E_UNEXPECTED;
  1432. break;
  1433. }
  1434. return hres;
  1435. }
  1436. void PicsDefault::Dump( void )
  1437. {
  1438. fprintf( stdout,
  1439. " Default?\n" );
  1440. }
  1441. HRESULT PicsExtension::AddItem(RatObjectID roid, LPVOID pData)
  1442. {
  1443. HRESULT hres = S_OK;
  1444. switch (roid) {
  1445. case ROID_OPTIONAL:
  1446. case ROID_MANDATORY:
  1447. /* Only data we should get is a label bureau string. */
  1448. if (pData != NULL)
  1449. m_pszRatingBureau = (LPSTR)pData;
  1450. break;
  1451. default:
  1452. assert(FALSE); /* shouldn't have been given a ROID that wasn't in
  1453. * the table we passed to the parser! */
  1454. hres = E_UNEXPECTED;
  1455. break;
  1456. }
  1457. return hres;
  1458. }
  1459. void PicsExtension::Dump( void )
  1460. {
  1461. fprintf( stdout,
  1462. " Extension?\n" );
  1463. }
  1464. /***************************************************************************
  1465. The main loop of the parser.
  1466. ***************************************************************************/
  1467. /* ParseParenthesizedObjectContents is called with a text pointer pointing at
  1468. * the first non-whitespace thing following the token identifying the type of
  1469. * object. It parses the rest of the contents of the object, up to and
  1470. * including the ')' which closes it. The array of AllowableOption structures
  1471. * specifies which understood options are allowed to occur within this object.
  1472. */
  1473. HRESULT RatFileParser::ParseParenthesizedObject(
  1474. LPSTR *ppIn, /* where we are in the text stream */
  1475. AllowableOption aao[], /* allowable things inside this object */
  1476. PicsObjectBase *pObject /* object to set parameters into */
  1477. )
  1478. {
  1479. HRESULT hres = S_OK;
  1480. LPSTR pszCurrent = *ppIn;
  1481. AllowableOption *pFound;
  1482. for (pFound = aao; pFound->roid != ROID_INVALID; pFound++) {
  1483. pFound->fdwOptions &= ~AO_SEEN;
  1484. }
  1485. pFound = NULL;
  1486. while (*pszCurrent != ')' && *pszCurrent != '\0' && SUCCEEDED(hres)) {
  1487. hres = ParseToOpening(&pszCurrent, aao, &pFound);
  1488. if (SUCCEEDED(hres)) {
  1489. LPVOID pData;
  1490. hres = (*(aObjectDescriptions[pFound->roid].pHandler))(&pszCurrent, &pData, this);
  1491. if (SUCCEEDED(hres)) {
  1492. if ((pFound->fdwOptions & (AO_SINGLE | AO_SEEN)) == (AO_SINGLE | AO_SEEN))
  1493. hres = RAT_E_DUPLICATEITEM;
  1494. else {
  1495. pFound->fdwOptions |= AO_SEEN;
  1496. hres = pObject->AddItem(pFound->roid, pData);
  1497. if (SUCCEEDED(hres)) {
  1498. if (*pszCurrent != ')')
  1499. hres = RAT_E_EXPECTEDRIGHT;
  1500. else
  1501. pszCurrent = FindNonWhite(pszCurrent+1);
  1502. }
  1503. }
  1504. }
  1505. }
  1506. }
  1507. if (FAILED(hres))
  1508. return hres;
  1509. for (pFound = aao; pFound->roid != ROID_INVALID; pFound++) {
  1510. if ((pFound->fdwOptions & (AO_MANDATORY | AO_SEEN)) == AO_MANDATORY)
  1511. return RAT_E_MISSINGITEM; /* mandatory item not found */
  1512. }
  1513. *ppIn = pszCurrent;
  1514. return hres;
  1515. }
  1516. /*
  1517. int __cdecl main(int argc, char **argv)
  1518. {
  1519. PicsRatingSystem Rating;
  1520. HANDLE hFile;
  1521. HANDLE hFileMapping;
  1522. VOID * pMem;
  1523. assert( argc > 1 );
  1524. hFile = CreateFile( argv[ 1 ],
  1525. GENERIC_READ,
  1526. 0,
  1527. NULL,
  1528. OPEN_EXISTING,
  1529. FILE_ATTRIBUTE_NORMAL,
  1530. NULL );
  1531. if ( hFile == INVALID_HANDLE_VALUE )
  1532. {
  1533. fprintf( stderr, "Error opening file %s\n", argv[ 1 ] );
  1534. return 1;
  1535. }
  1536. hFileMapping = CreateFileMapping( hFile,
  1537. NULL,
  1538. PAGE_READONLY,
  1539. 0,
  1540. 0,
  1541. NULL );
  1542. if ( hFileMapping == NULL )
  1543. {
  1544. fprintf( stderr, "Error creating mapping for %s\n", argv[ 1 ] );
  1545. CloseHandle( hFile );
  1546. return 2;
  1547. }
  1548. pMem = MapViewOfFile( hFileMapping,
  1549. FILE_MAP_READ,
  1550. 0,
  1551. 0,
  1552. 0 );
  1553. if ( pMem == NULL )
  1554. {
  1555. fprintf( stderr, "Error mapping view to file %s\n", argv [1 ] );
  1556. CloseHandle( hFileMapping );
  1557. CloseHandle( hFile );
  1558. return 3;
  1559. }
  1560. Rating.Parse( argv[ 1 ], (LPSTR) pMem );
  1561. fprintf( stdout,
  1562. "Dumping contents of RAT\n" );
  1563. Rating.Dump();
  1564. UnmapViewOfFile( pMem );
  1565. CloseHandle( hFileMapping );
  1566. CloseHandle( hFile );
  1567. return 0;
  1568. }
  1569. */