home *** CD-ROM | disk | FTP | other *** search
/ High Voltage Shareware / high1.zip / high1 / DIR2 / CBUFF09.ZIP / SRC.ZIP / STRING.C < prev    next >
C/C++ Source or Header  |  1993-11-16  |  6KB  |  273 lines

  1. /*  $Id$
  2.  *  
  3.  *  File    string.c
  4.  *  Part of    ChessBase utilities file format (CBUFF)
  5.  *  Author    Anjo Anjewierden, anjo@swi.psy.uva.nl
  6.  *  Purpose    String manipulations
  7.  *  Works with    GNU CC 2.4.5
  8.  *  
  9.  *  Notice    Copyright (c) 1993  University of Amsterdam
  10.  *  
  11.  *  History    14/11/93  (Created)
  12.  *          14/11/93  (Last modified)
  13.  */ 
  14.  
  15.  
  16. /*------------------------------------------------------------
  17.  *  Directives
  18.  *------------------------------------------------------------*/
  19.  
  20. #include "cbuff.h"
  21.  
  22.  
  23. /*------------------------------------------------------------
  24.  *  Player names
  25.  *------------------------------------------------------------*/
  26.  
  27. /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  28. @node normalisePlayer
  29. @deftypefun {char *} normalisePlayer (char *@var{p})
  30. Normalises the name of a chess player.  Normalisation means that all
  31. special characters are replaced by the defined chess symbols
  32. (@code{-symbols}) option, and that a number of heuristics are
  33. applied.  Currently the heuristics are that name prefixes are uppercased
  34. (e.g. @code{"van Wely"} becomes @code{"Van Wely"}, dots and spaces at
  35. the end of a name are removed, and semicolons and underscores are
  36. replaced by spaces.  The normalised name is returned.  This is a statically
  37. allocated string.
  38. @end deftypefun
  39. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  40.  
  41. char *
  42. normalisePlayer(char *p)
  43. { static char name[MAX_NAME_SIZE+1];
  44.   char tmp[MAX_NAME_SIZE+1];
  45.   char *s, *t;
  46.  
  47.   strcpy(tmp, p);
  48.  
  49.       /* Strip white space. */
  50.   while (isspace(tmp[0]))
  51.     strcpy(tmp, &tmp[1]);
  52.   while (isspace(tmp[strlen(tmp)-1]))
  53.     tmp[strlen(tmp)-1] = '\0';
  54.  
  55.       /* Replace special characters. */
  56.   for (s=tmp, t=name; *s; s++)
  57.   { if (*s & 0x80)            /* Special character */
  58.     { char *sym = mapChessSymbol(*s);
  59.  
  60.       if (sym)
  61.       { strcpy(t, sym);
  62.     t += strlen(sym);
  63.       } else
  64.       { setError(ERR_UNKNOWN_CHARACTER);
  65.     setNameError(p);
  66.     *s = '\0';
  67.     return name;
  68.       }
  69.       continue;
  70.     }
  71.     *t++ = *s;
  72.   }
  73.   *t = '\0';
  74.  
  75.        if (headStringP(name, "van ")) name[0] = 'V';
  76.   else if (headStringP(name, "von ")) name[0] = 'V';
  77.   else if (headStringP(name, "ten ")) name[0] = 'T';
  78.   else if (headStringP(name, "te "))  name[0] = 'T';
  79.   else if (headStringP(name, "di "))  name[0] = 'D';
  80.   else if (headStringP(name, "d' "))  name[0] = 'D';
  81.   else if (headStringP(name, "de "))  name[0] = 'D';
  82.   else if (headStringP(name, "le "))  name[0] = 'L';
  83.   else if (headStringP(name, "den ")) name[0] = 'D';
  84.  
  85.   if (tailStringP(name, "."))    name[strlen(name)-1] = '\0';
  86.   if (tailStringP(name, ",x"))   name[strlen(name)-2] = '\0';
  87.   if (tailStringP(name, ",#"))   name[strlen(name)-2] = '\0';
  88.   replaceString(name, "_;", " ,");
  89.   chopString(name, "([*");
  90.  
  91.   return name;
  92. }
  93.  
  94.  
  95.  
  96. static void    loadSpacePlaces(void);
  97.  
  98.  
  99. char **        SpacePlaces = NULL;    /* List of places with spaces */
  100.  
  101. static void
  102. loadSpacePlaces()
  103. { FILE *fd;
  104.   int count;
  105.   char place[MAX_NAME_SIZE+1];
  106.  
  107.   if (SpacePlaces != NULL)
  108.     return;
  109.  
  110.   fd = fopenCbuffFile("places.def", "r");
  111.   if (foundError())
  112.   { reportError(stderr);
  113.     exit(1);
  114.   }
  115.  
  116.   for (count=0; fgets(place, MAX_NAME_SIZE, fd); count++)
  117.     ;
  118.   fclose(fd);
  119.  
  120.   SpacePlaces = alloc(sizeof(char *) * (count+1));
  121.  
  122.   fd = fopenCbuffFile("places.def", "r");
  123.   for (count=0; fgets(place, MAX_NAME_SIZE, fd); count++)
  124.   { stripString(place);
  125.     SpacePlaces[count] = allocCharp(place);
  126.   }
  127.   fclose(fd);
  128.  
  129.   SpacePlaces[count] = NULL;
  130. }
  131.  
  132.  
  133.  
  134. /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  135. @node normalisePlace
  136. @deftypefun {char *} normalisePlace (char *@var{source})
  137. Returns the place (i.e. the city where a game was played) from the
  138. @var{source} field of a game.
  139. @end deftypefun
  140. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  141.  
  142. char *
  143. normalisePlace(char *source)
  144. { static char name[MAX_NAME_SIZE+1];
  145.   char tmp[MAX_NAME_SIZE+1];
  146.   char *s, *t;
  147.  
  148.   strcpy(tmp, source);
  149.  
  150.       /* Strip white space. */
  151.   while (isspace(tmp[0]))
  152.     strcpy(tmp, &tmp[1]);
  153.   while (isspace(tmp[strlen(tmp)-1]))
  154.     tmp[strlen(tmp)-1] = '\0';
  155.  
  156.       /* Replace special characters. */
  157.   for (s=tmp, t=name; *s; s++)
  158.   { if (*s & 0x80)            /* Special character */
  159.     { char *sym = mapChessSymbol(*s);
  160.  
  161.       if (sym)
  162.       { strcpy(t, sym);
  163.     t += strlen(sym);
  164.       } else
  165.       { setError(ERR_UNKNOWN_CHARACTER);
  166.     setNameError(source);
  167.     *s = '\0';
  168.     return name;
  169.       }
  170.       continue;
  171.     }
  172.     *t++ = *s;
  173.   }
  174.   *t = '\0';
  175.  
  176.   replaceString(name, "_;-:",
  177.               " ,  ");
  178.  
  179.                     /* Bundesliga 2 */
  180.   if (headStringP(name, "2 BL"))         { return "BRD tt"; }
  181.   if (headStringP(name, "2. BL"))        { return "BRD tt"; }
  182.   if (headStringP(name, "2.BL"))         { return "BRD tt"; }
  183.   if (headStringP(name, "2BL"))          { return "BRD tt"; }
  184.  
  185.                     /* Bundesliga */
  186.   if (headStringP(name, "BL "))          { return "BRD tt"; }
  187.   if (headStringP(name, "BL8"))          { return "BRD tt"; }
  188.   if (headStringP(name, "Bundesliga"))   { return "BRD tt"; }
  189.   if (headStringP(name, "Germany Bundesliga")) { return "BRD tt"; }
  190.  
  191.                     /* Wijk aan Zee */
  192.   if (headStringP(name, "Wijk "))        { return "Wijk aan Zee"; }
  193.  
  194.   if (strchr(name, ' '))
  195.   { loadSpacePlaces();
  196.     if (SpacePlaces)
  197.     { int i;
  198.  
  199.       for (i=0; SpacePlaces[i]; i++)
  200.     if (headStringP(name, SpacePlaces[i]))
  201.       return SpacePlaces[i];
  202.     }
  203.   }
  204.  
  205.   chopString(name, "([*<0123456789 ");
  206.  
  207.   return name;
  208. }
  209.  
  210.  
  211. /*------------------------------------------------------------
  212.  *  String utilities
  213.  *------------------------------------------------------------*/
  214.  
  215. bool
  216. headStringP(char *s, char *head)
  217. { if (strncmp(s, head, strlen(head)) == 0)
  218.     return TRUE;
  219.   return FALSE;
  220. }
  221.  
  222.  
  223. bool
  224. tailStringP(char *s, char *tail)
  225. { if (strlen(s) >= strlen(tail))
  226.     if (strcmp(s+strlen(s)-strlen(tail), tail) == 0)
  227.       return TRUE;
  228.   return FALSE;
  229. }
  230.  
  231.  
  232. void
  233. replaceString(char *s, char *in, char *out)
  234. { char *t, *u, *v;
  235.  
  236.   for (t=s; *t; t++)
  237.   { for (u=in,v=out; *u; u++,v++)
  238.       if (*t == *u)
  239.     *t = *v;
  240.   }
  241. }
  242.  
  243.  
  244. void
  245. chopString(char *s, char *specials)
  246. { char *t, *u;
  247.  
  248.   for (t=s; *t; t++)
  249.   { for (u=specials; *u; u++)
  250.     { if (*t == *u)
  251.       { *t = '\0';
  252.     stripString(s);
  253.     return;
  254.       }
  255.     }
  256.   }
  257. }
  258.  
  259.  
  260. void
  261. stripString(char *s)
  262. { char *t;
  263.  
  264.   for (t=s; *t; t++)
  265.     if (!isspace(*t))
  266.       break;
  267.   if (t != s)
  268.     strcpy(s, t);
  269.   t = s + strlen(s) - 1;
  270.   while (isspace(*t) && t >= s)
  271.     *t-- = '\0';
  272. }
  273.