Commits

Palmer, 2E0EOL committed c60f9ba

Experimental work (probably doesn't compile) from stristr branch in svn.
Looks like some missing functionality which needs to go into lib/

Comments (0)

Files changed (4)

+test : ddrpstristr.o test.o
+	cc -ansi -pedantic -Wall -g -o test test.o ddrpstristr.o
+
+ddrpstristr.o : ddrpstristr.c ddrpstristr.h
+	cc -g -c -o ddrpstristr.o ddrpstristr.c
+
+test.o : test.c ddrpstristr.h
+	cc -g -c -o test.o test.c
+
+clean:
+	rm -f test test.o ddrpstristr.o
+/*
+  Simple stristr(), like strstr() but case insensitive
+  by David Duncan Ross Palmer, Daybo Logic.
+  Public domain with no warrenty whatsoever.  Use at your own risk.
+*/
+
+#include <string.h>
+#include <stdlib.h>
+#include <ctype.h>
+#ifdef HDRSTOP
+# pragma hdrstop
+#endif /*HDRSTOP*/
+
+#include "ddrpstristr.h"
+
+static char* Dup(const char* str);
+static char* LowerCase(char* str);
+
+char* stristr(const char* s1, const char* s2)
+{
+  char *c1, *c2; /* copies */
+  char* ret = NULL;
+
+  c1 = Dup(s1);
+  c2 = Dup(s2);
+  if ( c1 && c2 ) {
+    ret = strstr(LowerCase(c1), LowerCase(c2));
+    if ( ret ) {
+      /* Have pointer, calculate offset and reapply relative to s1 */
+      unsigned int offset = (unsigned int)ret - (unsigned int)c1;
+      ret = (char*)(s1 + offset);
+    }
+  }
+
+  if ( c1 ) free(c1);
+  if ( c2 ) free(c2);
+  return ret;
+}
+
+static char* Dup(const char* str)
+{
+  char* dup = NULL;
+  if ( str ) {
+    dup = (char*)malloc( sizeof(char) * (strlen(str)+1) );
+    if ( dup )
+      strcpy(dup, str);
+  }
+  return dup;
+}
+
+static char* LowerCase(char* str)
+{
+  if ( str ) {
+    unsigned int i = 0U;
+    while ( str[i] != '\0' ) {
+      if ( isalpha((int)str[i]) )
+        str[i] = (char)tolower((int)str[i]);
+      i++;
+    }
+  }
+  return str;
+}
+/*
+  Simple stristr(), like strstr() but case insensitive
+  by David Duncan Ross Palmer, Daybo Logic.
+  Public domain with no warrenty whatsoever.  Use at your own risk.
+*/
+
+#ifndef __INC_DDRPSTRISTR_H
+#define __INC_DDRPSTRISTR_H
+
+#ifdef __cplusplus
+  extern "C" {
+#endif
+
+char* stristr(const char* s1, const char* s2);
+
+#ifdef __cplusplus
+  }
+#endif
+
+#endif
+#include <stdio.h>
+
+#include "ddrpstristr.h"
+
+int main()
+{
+  const char s1[] = "Total Konfuzion";
+  const char s2[] = "kon";
+  char* instance = stristr(s1, s2);
+
+  if ( instance )
+    printf ("Total %s\n", instance);
+  else
+    puts("Broken.");
+  return 0;
+}