/*
 * FreeAdhocUDF - copyright (c) 2004 - 2007 adhoc dataservice GmbH
 * Christoph Theuring / Peter Mandrella / Georg Horn
 * based on FreeUDFLibC - copyright (c) 1999 Gregory Deatz
 *
 * File Name:	blob_functions.c
 * Description: this module contains UDFs to generate and manipulate BLObs.
 *
 * for exported functions list see blob_functions.h
 *
 * Software distributed under the License is distributed on an
 * "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express
 * or implied. See the License for the specific language governing
 * rights and limitations under the License.
 *
*****************************************************************/

#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <stdio.h>
#include <ibase.h>

#include "string_functions.h"
#include "blob_functions.h"

// intern
// pm 2004-08-21
// pm 2004-08-25 rtrim eingebaut
// gh 2006-03-01 added do_MALLOC
// pm 2006-09-18 added evaluation of blob_max_segment and blob_get_segment return value
// gh 2007-08-22 removed do_malloc, changed some types and struct blob to BLOBCALLBACK 
char *intern_blobaspchar(BLOBCALLBACK blob)
{
  char *result;
  ISC_USHORT bytes_read;
  ISC_LONG bytes_left, total_bytes_read;

  bytes_read = 0;
  total_bytes_read = 0;

  if (blob->blob_handle == NULL) {
    result = malloc(1);
  } else {
    result = malloc(blob->blob_total_length + 1);

    memset(result,0,blob->blob_total_length + 1);

    bytes_left = blob->blob_total_length;
    while (bytes_left > 0) {
      if (!blob->blob_get_segment(blob->blob_handle, result + total_bytes_read,
                             blob->blob_total_length, &bytes_read))
        break;

      total_bytes_read += bytes_read;
      bytes_left -= bytes_read;
    }
  }

  while (total_bytes_read>0 && isspace(result[total_bytes_read-1]))
    --total_bytes_read;

  result[total_bytes_read] = '\0';

  return result;
}

// ct 2006-12-04 implementation of NULL for FB 2.0
// gh 2007-08-22 removed do_malloc, changed some types and struct blob to BLOBCALLBACK 
char *blobaspchar(BLOBCALLBACK blob)
{
  if (blob->blob_handle == NULL)
    return NULL;
  else
    return intern_blobaspchar(blob);
}

// pm 2004-08-21
// ct 2006-12-04 implementation of NULL for FB 2.0
// ct 2007-08-13 change int to ISC_LONG
// gh 2007-08-22 removed do_malloc, changed some types and struct blob to BLOBCALLBACK 
char * blobmid (BLOBCALLBACK blob, ISC_LONG *from, int *count)
{
  if (blob->blob_handle == NULL || !from || !count)
    return NULL;
  else
  {
    ISC_LONG len = blob->blob_total_length;
    char * result = intern_blobaspchar(blob);
    int f = *from, c = *count;

    if (c < 0) c = 0;
    if (f < 0) f = 0;
    else 
      if (f > len) f = len;
    if (f + c > len) c = len - f;

    memmove(result, result + f, c);
    result[c] = '\0';

    return result;
  }
}

// ct 2007-01-30
// gh 2007-08-22 removed do_malloc, changed some types and struct blob to BLOBCALLBACK 
char * r_blobmid (BLOBCALLBACK blob, int *from, int *count)
{
  if (blob->blob_handle == NULL || !from || !count)
    return NULL;
  else
  {
    ISC_LONG len = blob->blob_total_length;
    char * result = intern_blobaspchar(blob);
    int f = *from, c = *count;

    if (c < 0) c = 0;
    if (f < 1) f = 1;
    else
      if (f > len) f = len;
    if (f + c > len) c = len - f;

    memmove(result, result + f - 1, c);
    result[c] = '\0';

    return result;
  }
}

// pm 2004-08-21
// pm 2004-08-25 rtrim eingebaut
// pm 2006-09-18 added evaluation of blob_max_segment and blob_get_segment return value
// ct 2006-12-04 implementation of NULL for FB 2.0
// gh 2007-08-22 changed some types and struct blob to BLOBCALLBACK 
char * blobleft (BLOBCALLBACK blob, int *count)
{
  if (blob->blob_handle == NULL || !count)
    return NULL;
  else
  {
    ISC_LONG from = 0;
    return blobmid(blob,&from,count);
  }
}

// pm 2004-08-21
// ct 2006-12-04 implementation of NULL for FB 2.0
// gh 2007-08-22 changed some types and struct blob to BLOBCALLBACK 
char * blobright (BLOBCALLBACK blob, int * count)
{
  if (blob->blob_handle == NULL || !count)
    return NULL;
  else
  {
    ISC_LONG f = blob->blob_total_length - *count;
    return blobmid(blob, &f, count);
  }
}

// pm 2004-08-21
// ct 2006-12-04 implementation of NULL for FB 2.0
// gh 2007-08-22 changed some types and struct blob to BLOBCALLBACK 
ISC_LONG * blobsize (BLOBCALLBACK blob)
{
  if (blob->blob_handle == NULL)
    return NULL;
  else
  {
    ISC_LONG * result = malloc(sizeof(ISC_LONG));
    *result = blob->blob_total_length;
    return result;
  }
}

// pm 2004-08-21
// pm 2004-08-25 rtrim eingebaut
// gh 2007-08-22 changed some types and struct blob to BLOBCALLBACK 
// ct 2007-08-22 implementation of NULL for FB 2.0
BLOBCALLBACK strblob(char *str, BLOBCALLBACK result)
{
    char *pc;

    if (str) {
	pc = str + strlen(str) - 1;
	while (pc >= str && isspace(*pc))
	    --pc;
    } else {
	str = "";
	pc = str - 1;
    }

    result->blob_put_segment(result->blob_handle, str, pc + 1 - str);
    return result;
}

// gh 2006-02-22
// pm 2006-09-18 added evaluation of blob_max_segment and blob_get_segment return value
// gh 2007-08-22 changed some types and struct blob to BLOBCALLBACK 
BLOBCALLBACK blobcat(BLOBCALLBACK b1, BLOBCALLBACK b2, BLOBCALLBACK result)
{
    ISC_USHORT bytes_read;
    char *buffer;
    ISC_LONG bytes_left, buflen = 0;

    if (b1 && b1->blob_handle && b1->blob_total_length > buflen) {
	buflen = b1->blob_total_length;
    }
    if (b2 && b2->blob_handle && b2->blob_total_length > buflen) {
	buflen = b2->blob_total_length;
    }
    buffer = malloc(buflen);

    if (b1 && b1->blob_handle) {
	bytes_left = b1->blob_total_length;
	while (bytes_left > 0) {
	    if (!b1->blob_get_segment(b1->blob_handle, buffer, b1->blob_max_segment,
              		&bytes_read))
              break;
	    result->blob_put_segment(result->blob_handle, buffer, bytes_read);
	    bytes_left -= bytes_read;
	}
    }

    result->blob_put_segment(result->blob_handle, "\r\n", 2);

    if (b2 && b2->blob_handle) {
	bytes_left = b2->blob_total_length;
	while (bytes_left > 0) {
	    if (!b2->blob_get_segment(b2->blob_handle, buffer, b2->blob_max_segment,
		&bytes_read))
              break;
	    result->blob_put_segment(result->blob_handle, buffer, bytes_read);
	    bytes_left -= bytes_read;
	}
    }

    free(buffer);
    return result;
}

// gh 2006-02-22
// pm 2006-09-18 added evaluation of blob_max_segment and blob_get_segment return value
// gh 2007-08-22 changed some types and struct blob to BLOBCALLBACK 
BLOBCALLBACK blobcatstr(BLOBCALLBACK b1, char *str, BLOBCALLBACK result)
{
    ISC_USHORT bytes_read;
    ISC_LONG bytes_left;
    char *buffer, *p;

    if (b1 && b1->blob_handle) {
	buffer = malloc(b1->blob_total_length);
	bytes_left = b1->blob_total_length;
	while (bytes_left > 0) {
	    if (!b1->blob_get_segment(b1->blob_handle, buffer, b1->blob_max_segment,
		&bytes_read))
              break;
	    result->blob_put_segment(result->blob_handle, buffer, bytes_read);
	    bytes_left -= bytes_read;
	}
	free(buffer);
    }

    result->blob_put_segment(result->blob_handle, "\r\n", 2);

    p = str + strlen(str) - 1;
    while (p >= str && isspace(*p))
	--p;
    result->blob_put_segment(result->blob_handle, str, p + 1 - str);

    return result;
}

// gh 2006-02-28
// ct 2006-12-04 implementation of NULL for FB 2.0
// gh 2007-08-22 removed do_malloc, changed some types and struct blob to BLOBCALLBACK 
char *excelblob(BLOBCALLBACK blob)
{
  if (blob->blob_handle == NULL)
    return NULL;
  else
  {
    char *string = intern_blobaspchar(blob);
    char *result = intern_excelstring(string, 1);
    free(string);
    return result;
  }
}

// gh 2006-02-28
// gh 2007-08-22 changed some types and struct blob to BLOBCALLBACK 
// gh 2007-08-22 removed do_malloc in call intern_blobaspchar and intern_replacestring
BLOBCALLBACK blobreplacestring(BLOBCALLBACK b, char *subs, char *replby, int *all, int *case_insensitive, BLOBCALLBACK result)
{
    char *string1 = intern_blobaspchar(b);
    char *string2 = intern_replacestring(string1, subs, replby, all, case_insensitive);
    free(string1);
    result = strblob(string2, result);
    free(string2);
    return result;
}

// pm 2006-09-24
// ct 2006-12-04 implementation of NULL for FB 2.0
// gh 2007-08-22 changed some types and struct blob to BLOBCALLBACK 
ISC_LONG * blobmaxsegmentlength (BLOBCALLBACK  blob)
{
  if (blob->blob_handle == NULL)
    return NULL;
  else
  {
    ISC_LONG * result = malloc(sizeof(ISC_LONG));
    *result = blob->blob_max_segment;
    return result;
  }
}

// pm 2006-09-24
// ct 2006-12-04 rewritten as pointer
// gh 2007-08-22 changed some types and struct blob to BLOBCALLBACK 
ISC_LONG * blobsegmentcount (BLOBCALLBACK  blob)
{
  if (blob->blob_handle == NULL)
    return NULL;
  else
  {
    ISC_LONG * result = malloc(sizeof(ISC_LONG));
    *result = blob->blob_number_segments;
    return result;
  }
}

// pm 2006-09-24
// ct 2006-12-04 implementation of NULL for FB 2.0
// gh 2007-08-22 removed do_malloc, changed some types and struct blob to BLOBCALLBACK 
char * blobline (BLOBCALLBACK  blob, int* line)
{
  if (blob->blob_handle == NULL || !line)
    return NULL;
  else
  {
    char *data = intern_blobaspchar(blob);
    char *result = malloc(32760);
    char *p = data;
    char *endpos;

    while ((*line)-- > 1 && p != NULL)
    {
      p = strchr(p,'\n');
      if (p != NULL) p++;
    }

    if (p == NULL)
      *result = 0;
    else
    {
      endpos = strchr(p,'\n');
      if (endpos != NULL)
        *(endpos-1) = 0;
      strcpy(result,p);
    }
    free(data);
    return result;
  }
}

// pm 2006-09-24
// ct 2006-12-04 implementation of NULL for FB 2.0
// gh 2007-08-22 removed do_malloc, changed some types and struct blob to BLOBCALLBACK 
int * blobcompare (BLOBCALLBACK b1, BLOBCALLBACK b2)
{
  if (b1->blob_handle == NULL || b2->blob_handle == NULL)
    return NULL;
  else
  {
    char *data1 = intern_blobaspchar(b1);
    char *data2 = intern_blobaspchar(b2);
    int * result = malloc(sizeof(int));
    *result = !strcmp(data1,data2);
    free(data1);
    free(data2);
    return result;
  }
}

// ct 2007-07-06 version for rFunc similar to blobcompare
// gh 2007-08-22 removed do_malloc, changed some types and struct blob to BLOBCALLBACK 
int * b_strcmp (BLOBCALLBACK b1, BLOBCALLBACK b2)
{
  if (b1->blob_handle == NULL || b2->blob_handle == NULL) {
    return NULL;
  } else {
    char *data1 = intern_blobaspchar(b1);
    char *data2 = intern_blobaspchar(b2);
    int * result = malloc(sizeof(int));
    *result = strcmp(data2,data1);
    free(data2);
    free(data1);
    return result;
  }
}

// pm 2006-09-24
// ct 2006-12-04 implementation of NULL for FB 2.0
// gh 2007-08-22 removed do_malloc, changed some types and struct blob to BLOBCALLBACK 
int * blobsubstr (BLOBCALLBACK blob, char *s)
{
  if (blob->blob_handle == NULL || !s)
    return NULL;
  else
  {
    char *data = intern_blobaspchar(blob);
    char *pos = strstr(data,s);
    int * result = malloc(sizeof(int));
    *result = (pos==NULL ? -1 : pos-data);
    free(data);
    return result;
  }
}

// pm 2006-09-24
// ct 2006-12-04 implementation of NULL for FB 2.0
// gh 2007-08-22 removed do_malloc, changed some types and struct blob to BLOBCALLBACK 
int * bloblinecount (BLOBCALLBACK blob)
{
  if (blob->blob_handle == NULL)
    return NULL;
  else
  {
    char *data = intern_blobaspchar(blob);
    int *result = malloc(sizeof(int));
    char *p = data;
    int count = 0;

    while (*p)
    {
      if (*p == '\n')
        count++;
      p++;
    }
    if (p > data && p[-1] != '\n')
      count++;
    free(data);
    *result = count;
    return result;
  }
}

// ct 2006-12-04
// gh 2007-08-22 removed do_malloc, changed some types and struct blob to BLOBCALLBACK 
int * blobwordcount (BLOBCALLBACK blob)
{
  if (blob->blob_handle == NULL)
    return NULL;
  else
  {
    char *data = intern_blobaspchar(blob);
    int *result = malloc(sizeof(int));
    char *p = data;
    int wort = 0;
    int count = 0;

    while (*p)
    {
      if(*p == ' ' || *p == '\n' || *p == '\t')
        wort = 0;
      else
      {
        if(wort == 0)
        {
          wort = 1;
          count++;
        }
      }
      p++;
    }
    free(data);
    *result = count;
    return result;
  }
}

// ct 2007-05-21 version for rFunc similar to blobsubstr
// gh 2007-08-22 removed do_malloc, changed some types and struct blob to BLOBCALLBACK 
int * b_strpos (char *s, BLOBCALLBACK blob)
{
  if (blob->blob_handle == NULL || !s)
    return NULL;
  else
  {
    char *data = intern_blobaspchar(blob);
    char *pos = strstr(data,s);
    int * result = malloc(sizeof(int));
    *result = (pos==NULL ? 0 : pos-data + 1);
    free(data);
    return result;
  }
}

// gh 2007-07-04 version for rFunc similar to blobsubstr
// gh 2007-08-22 removed do_malloc, changed some types and struct blob to BLOBCALLBACK 
int *b_textpos(char *s, BLOBCALLBACK blob)
{
    if (blob->blob_handle == NULL || !s) {
	return NULL;
    } else {
	char *data = intern_blobaspchar(blob);
	char *data2 = upper(data);
	char *s2 = upper(s);
	char *pos = strstr(data2, s2);
	int *result = malloc(sizeof(int));
	*result = (pos == NULL ? 0 : pos - data2 + 1);
	free(data);
	free(data2);
	free(s2);
	return result;
    }
}

/* Debug code
  FILE * logfile = fopen("/daten/copy/freeadhocudf.log","a");
  fprintf(logfile,"---x---\n");
  fprintf(logfile,"string1:%c\n", string1);
  fclose(logfile);
*/
