home *** CD-ROM | disk | FTP | other *** search
/ PC Pro 1999 January / dppcpro0199a.iso / January / Fp98 / SDK / WebBot / initcaps / initcaps.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1997-09-18  |  2.6 KB  |  133 lines

  1. /*
  2.  * Copyright (c) 1996 Microsoft Corporation.  All rights reserved.
  3.  * File: initcaps.cpp
  4.  *
  5.  * DLL Custom WebBot Example
  6.  */
  7.  
  8. #include "stdio.h"
  9. #include "string.h"
  10. #include "ctype.h"
  11. #include "stdlib.h"
  12. #include "../webbot.h"
  13.  
  14. //////////////////////////////////
  15. // private string utilities 
  16.  
  17. static void UpperCase(char *str)
  18. {
  19.     char *p = NULL;
  20.     for(p=str;p && *p;p++)
  21.         *p = toupper(*p);
  22. }
  23.  
  24. static char *StringCopy(const char *str)
  25. {
  26.     char *p = NULL;
  27.     if(!str)
  28.         return NULL;
  29.     p = (char *)malloc(strlen(str)+1);
  30.     if(!p)
  31.         return NULL;
  32.     strcpy(p,str);
  33.     return p;
  34. }
  35.  
  36. //////////////////////////////
  37. // BOT: SmallCaps
  38.  
  39. BeginWebBotExpand(SmallCaps,ret,bot,cgi,form)
  40. {
  41.     const char *pszText = NULL;
  42.     const char *pszSize = NULL;
  43.     int bBigger = 0;
  44.     int nSize = 0;
  45.     char *pszUCase = NULL;
  46.     int slen = 0;
  47.     char *p = NULL;
  48.     char fontbeg[32];
  49.     char fontend[32];
  50.     char chout[2];
  51.     
  52.     /* fetch parameters and verify them; else return error HTML */
  53.     pszText = bot.GetValue("S-Text");
  54.     pszSize = bot.GetValue("I-Font-Size");
  55.  
  56.     if(!pszText || !pszText[0] 
  57.         || !pszSize || !pszSize[0])
  58.         goto SmallCapsExpandError;
  59.  
  60.     /* convert any non-string parameters and do bounds checking */
  61.     nSize = atoi(pszSize);
  62.     if(nSize < 1)
  63.         nSize = 1;
  64.     if(nSize > 7)
  65.         nSize = 7;
  66.     
  67.     /* copy source string and make it all upper-case */
  68.     pszUCase = StringCopy(pszText);
  69.     if(!pszUCase)
  70.         goto SmallCapsExpandError;
  71.     UpperCase(pszUCase);
  72.  
  73.     /* copy each word in source to output with initial font size as requested */
  74.  
  75.     chout[0] = chout[1] = 0;
  76.  
  77.     sprintf(fontbeg,"<FONT SIZE=%d>",nSize);
  78.     sprintf(fontend,"</FONT>");
  79.  
  80.     p = pszUCase;
  81.     while(p && *p)
  82.     {
  83.         /* find first non-alnum in remainder of string (ingoring apostrophes) */
  84.         int idx = strspn(p,"ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'");
  85.         if(idx > 0)
  86.         {
  87.             int chTmp = p[idx];
  88.             p[idx] = 0;
  89.             chout[0] = *p;
  90.  
  91.             ret.Append(fontbeg);
  92.             ret.Append(chout);
  93.             ret.Append(fontend);
  94.             ret.Append(p+1);
  95.  
  96.             p[idx] = chTmp;
  97.             p += idx;
  98.         }
  99.         else
  100.         {
  101.             /* not found or at beginning */
  102.             chout[0] = *p;
  103.  
  104.             if(isalnum(*p) || *p == '\'')
  105.             {
  106.                 /* no more non-alnum in string; use remainder */
  107.                 ret.Append(fontbeg);
  108.                 ret.Append(chout);
  109.                 ret.Append(fontend);
  110.                 ret.Append(p+1);
  111.                 p += strlen(p);
  112.             }
  113.             else
  114.             {
  115.                 /* just stepping over non-alnum */
  116.                 ret.Append(chout);
  117.                 p++;
  118.             }
  119.         } /* end else */
  120.     } /* end while */
  121.  
  122.     /* free up any memory we allocated */
  123.     free((char *)pszUCase);
  124.  
  125.     return;
  126.  
  127. SmallCapsExpandError:
  128.     ret.Append("<STRONG>[SmallCaps]</STRONG>");
  129.     return;
  130.  
  131. }
  132. EndWebBotExpand
  133.