/*
 * FreeAdhocUDF - copyright (c) 2004 - 2006 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 "global.h"
#include "string_functions.h"
#include "blob_functions.h"


// pm 21.08.04
// pm 25.08.04   rtrim eingebaut
// gh 01.03.06  added do_MALLOC
// pm 18.09.06  added evaluation of blob_max_segment and get_segment return value

char *blobaspchar(struct blob *blob)
{
    return intern_blobaspchar(blob, 1);
}

// pm 21.08.04
// pm 25.08.04   rtrim eingebaut
// gh 01.03.06  added do_MALLOC
// pm 18.09.06  added evaluation of blob_max_segment and get_segment return value

char *intern_blobaspchar(struct blob *blob, int do_MALLOC)
{
  char *result;
  long bytes_read, bytes_left, total_bytes_read;

/*
  Debug Code

  FILE * logfile = fopen("c:\\daten\\fb\\freeadhocudf.log","a");
  fprintf(logfile,"------\n");
  fprintf(logfile,"blob* = %p\n", blob);
  fclose(logfile);
*/

  bytes_read = 0;
  total_bytes_read = 0;

  if (blob->blob_handle == NULL) {
    result = do_MALLOC ? MALLOC(1) : malloc(1);
  } else {
    result = do_MALLOC ? MALLOC(blob->blob_total_length + 1)
	: 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->get_segment(blob->blob_handle, result + total_bytes_read,
                             blob->blob_max_segment, &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;
}


// pm 21.08.04
char * blobleft (struct blob *blob, int *count)
{
  int from = 0;
  return blobmid(blob,&from,count);
}


// pm 21.08.04
char * blobmid (struct blob *blob, int *from, int *count)
{
  int len = blobsize(blob);
  char * result = 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;
}


// pm 21.08.04
char * blobright (struct blob *blob, int * count)
{
  int f = blobsize(blob) - *count;
  return blobmid(blob, &f, count);
}


// pm 21.08.04
int blobsize (struct blob *blob)
{
  if (blob->blob_handle == NULL)
    return 0;
  else
    return blob->blob_total_length;
}


// pm 21.08.04
// pm 25.08.04   rtrim eingebaut

// According to IB docs, this is how you do it for blobs.
// First pass the "first" argument
// Second, pass the "return" argument (A Blob) as the last argument.
// then you would just declare the external function in
// IB as shown below

/*struct blob * strblob (char *str, struct blob *result)
{
  char *pc = str + strlen(str) - 1;
  while (pc>=str && isspace(*pc))
    --pc;

  result->put_segment(result->blob_handle, str, pc+1-str);
  return result;
} */

struct blob *strblob(char *str, struct blob *result)
{
    char *pc;

    if (str) {
	pc = str + strlen(str) - 1;
	while (pc >= str && isspace(*pc))
	    --pc;
    } else {
	str = "";
	pc = str - 1;
    }

    result->put_segment(result->blob_handle, str, pc + 1 - str);
    return result;
}


// gh 22.02.06
// pm 18.09.06  added evaluation of blob_max_segment and get_segment return value

struct blob *blobcat(struct blob *b1, struct blob *b2, struct blob *result)
{
    long bytes_read, bytes_left;
    char *buffer;
    int 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->get_segment(b1->blob_handle, buffer, b1->blob_max_segment,
              		&bytes_read))
              break;
	    result->put_segment(result->blob_handle, buffer, bytes_read);
	    bytes_left -= bytes_read;
	}
    }

    result->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->get_segment(b2->blob_handle, buffer, b2->blob_max_segment,
		&bytes_read))
              break;
	    result->put_segment(result->blob_handle, buffer, bytes_read);
	    bytes_left -= bytes_read;
	}
    }

    free(buffer);
    return result;
}

// gh 22.02.06
// pm 18.09.06  added evaluation of blob_max_segment and get_segment return value

struct blob *blobcatstr(struct blob *b1, char *str, struct blob *result)
{
    long bytes_read, 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->get_segment(b1->blob_handle, buffer, b1->blob_max_segment,
		&bytes_read))
              break;
	    result->put_segment(result->blob_handle, buffer, bytes_read);
	    bytes_left -= bytes_read;
	}
	free(buffer);
    }

    result->put_segment(result->blob_handle, "\r\n", 2);

    p = str + strlen(str) - 1;
    while (p >= str && isspace(*p))
	--p;
    result->put_segment(result->blob_handle, str, p + 1 - str);

    return result;
}


// gh 28.02.06 

char *excelblob(struct blob *b)
{
    char *string = intern_blobaspchar(b, 0);
    char *result = intern_excelstring(string, 1);
    free(string);
    return result;
}


// gh 28.02.06

struct blob *blobreplacestring(struct blob *b, char *subs, char *replby,
    int *all, int *case_insensitive, struct blob *result)
{
    char *string1 = intern_blobaspchar(b, 0);
    char *string2 = intern_replacestring(string1, subs, replby, all,
	case_insensitive, 0);
    free(string1);
    result = strblob(string2, result);
    free(string2);
    return result;
}


// pm 24.09.06

int blobmaxsegmentlength (struct blob* b)
{
  return b->blob_max_segment;
}


// pm 24.09.06

int blobsegmentcount (struct blob* b)
{
  return b->blob_number_segments;
}


// pm 24.09.06

char * blobline (struct blob * b, int* line)
{
  char *data = intern_blobaspchar(b,0);
  char *result = MALLOC(MAX_STRINGBUFFER_SIZE);
  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 24.09.06

int bloblinecount (struct blob *b)
{
  char *data = intern_blobaspchar(b,0);
  char *p = data;
  int count = 0;

  while (*p)
  {
    if (*p == '\n')
      count++;
    p++;
  }

  if (p > data && p[-1] != '\n')
    count++;

  free(data);

  return count;
}


// pm 24.09.06

int blobcompare (struct blob *b1, struct blob *b2)
{
  char *data1 = intern_blobaspchar(b1,0);
  char *data2 = intern_blobaspchar(b2,0);
  int result = !strcmp(data1,data2);
  free(data1);
  free(data2);
  return result;
}


// pm 24.09.06

int blobsubstr (struct blob *b,char *s)
{
  char *data = intern_blobaspchar(b,0);
  char *pos = strstr(data,s);
  int result = (pos==NULL ? -1 : pos-data);
  free(data);
  return result;
}

