#include "cdefs.h"
#include "cstring.h"

cstring strdupl(cstring s){
  cint i, n = strlen(s);
  cstring result = (cstring)mem_malloc(n+1);
  for (i = 0; i <= n; i++) {result[i] = s[i];};
  return result;
};

cstring strlwr(cstring s){
  cint i, n = strlen(s);
  for (i = 0; i < n; i++)
    if (s[i] >= 65 && s[i] <= 90) s[i] += 32;
  return s;
}

cint strsum(cstring s){
  cint result = 0;
  while (*s != 0) result += *s++;
  return result;
};

cstring strupr(cstring s){
  cint i, n = strlen(s);
  for (i = 0; i < n; i++)
    if (s[i] >= 97 && s[i] <= 122) s[i] -= 32;
  return s;
}

cint str2float(cstring s){
  union {cint i; cfloat f;} r;
  sscanf(s, "%f", &r.f);
  return r.i;
};

cint str2int(cstring s){
  cint result;
  sscanf(s, "%ld", &result);
  return result;
};

cstring float2str(cfloat f){
  cstring result = (cstring)mem_malloc(32);
  sprintf(result, "%g", f);
  return result;
};

cstring int2str(cint i){
  cstring result = (cstring)mem_malloc(12);
  sprintf(result, "%ld", i);
  return result;
};
