/* arclynx - simple version of Lynx for Acorn machines
 * (c) 1996 Andrew Wood
 *
 * Routines contained in this file (a_misc.c):
 *   string_lowercase() - last modified 96/07/16
 *   string_length()    - last modified 96/07/17
 *   word_length()      - last modified 96/07/17
 *   word_lterm()       - last modified 96/07/17
 */

#include "arclynx.h"
#include <string.h>


/******************************************************************************
 * string_lowercase(string) - convert string to lower case
 */
void string_lowercase(char *string) {
 static int i;                                  /* general iteration counter */
 for(i=0; i<strlen(string); i++) {                    /* for each character: */
  if ((string[i]>='A') & (string[i]<='Z')) string[i]+=32;    /* lowercase it */
 }
}


/******************************************************************************
 * string_length(string) - return number of printable characters in string
 */
int string_length(char *string) {
 static int count,i;
 count=0; i=0; while(string[i]!=0) {
  if (string[i]>=32) count++;
  switch(string[i]) {
   case LYNX_A_HREF_CODE:
   case LYNX_A_NAME_CODE:i++; i++; break;
   case LYNX_SPACES_CODE:i++; break;
   default:break;
  }
  i++;
 }
 return(count);
}


/******************************************************************************
 * word_length(string) - return number of letters in (non-terminated) word
 */
int word_length(char *string) {
 static int c,count,i;
 count=0; i=0; while(i<256) {
  c=string[i];
  if ((c>='0') & (c<='9')) c='a';
  if ((c>='A') & (c<='Z')) c+=32;
  if ((c>='a') & (c<='z')) {i++; count++;} else {i=256;}
 }
 return(count);
}


/******************************************************************************
 * word_lterm(string) - copy word, lowercase it, return terminated version
 */
char *word_lterm(char *string) {
 static char temp[64];
 strncpy(temp,string,word_length(string));
 temp[word_length(string)]=0;
 string_lowercase(temp);
 return(temp);
}
