#include <X11/Intrinsic.h>
#include <X11/StringDefs.h>

#include <stdio.h>
#include <string.h>
#include <ctype.h>

#include "filmbuf.h"
#include "popup.h"
#include "utility.h"
#include "xregal.h"

static Buffer *ubuf=NULL;

char *insert_qv_links(char *str) {
	int quote_found;
	int underscore_found;
	char namebuf[128];
	char linkbuf[128];
	char *cp;
	char *quote_start;
	char *underscore_start;
	char *copiedto;

	/* printf("In insert_qv_links(): %s", str); */
	if (ubuf==NULL)
		ubuf=CreateBuffer("qvlinks", 8192, 4096, 262144);
	ClearBuffer(ubuf);
	cp=str;
	copiedto=str;
	quote_found=0;
	underscore_found=0;
	while (*cp!='\0') {
		
		if ((*cp == '\'') || (*cp == '`')) {
			if (quote_found) {
				if ((strncmp(cp+1, " (qv)", 5)==0) && (cp>quote_start+1)) {
					AddToBufferN(ubuf, copiedto, quote_start-copiedto);
					strncpy(namebuf, quote_start+1, cp-quote_start-1);
					namebuf[cp-quote_start-1]='\0';
					sprintf(linkbuf, "<l -1:%s>%s<e>", namebuf, namebuf);
					AddToBuffer(ubuf, linkbuf);
					cp += 5;
					copiedto=cp+1;
					quote_found=0;
					}
				else
					quote_start=cp;
				}
			else {
				quote_start=cp;
				quote_found=1;
				}
			}

		if (*cp == '_') {
			if (underscore_found) {
				if ((strncmp(cp+1, " (qv)", 5)==0) && (cp>underscore_start+1)) {
					AddToBufferN(ubuf, copiedto, underscore_start-copiedto);
					strncpy(namebuf, underscore_start+1, cp-underscore_start-1);
					namebuf[cp-underscore_start-1]='\0';
					sprintf(linkbuf, "<l MOV:%s>%s<e>", namebuf, namebuf);
					AddToBuffer(ubuf, linkbuf);
					cp += 5;
					copiedto=cp+1;
					underscore_found=0;
					}
				else
					underscore_start=cp;
				}
			else {
				underscore_start=cp;
				underscore_found=1;
				}
			}
		cp++;
		}

	AddToBuffer(ubuf, copiedto);
	return ubuf->buf;
	}

/* FUNCTIONS */
/* --------- */

/* change spaces to '+' and non-alphanumerics to hexadecimal */

void urlencode(char *url, char *searchtype, char *searchstring) {
	char buf[256];
	char *src, *dst;

	src=searchstring;
	dst=buf;
	while (*src != '\0') {
		if (*src == ' ') 
			*dst++ = '+';
		else if ((isalnum(*src)) || (strchr("()", *src) != NULL))
			*dst++ = *src;
		else {
			sprintf(dst, "%%%2X", *src & 255);  /* fix by Claus Schotten */
			dst += 3;
			}
		src++;
		}	
	*dst='\0';
	sprintf(url, "%s%s%s", imdbsite, searchtype, buf);
	}



/* returns true if ch is whitespace */

Boolean whitespace(char ch) {
	if (ch=='\0')
		return False;
	return (strchr(" \t", ch)!=NULL);
	}

/* returns true if ch is whitespace (including newlines) */

Boolean whitespace2(char ch) {
	if (ch=='\0')
		return False;
	return (strchr(" \t\n", ch)!=NULL);
	}


/* remove leading and trailing space and compress spaces */

void trim_text(char *dest, char *src) {

	/* skip leading space */
	while (whitespace(*src))
		src++;
	while ((*src!='\0') && (*src!='\n')) {
		if (whitespace(*src)) {
			*dest++=' ';
			while (whitespace(*src)) src++;
			}
		else 
			*dest++ = *src++;
		}

	/* trim trailing white space */
	dest--;
	while (whitespace(*dest))
		dest--;
	*(dest+1)='\0';
	}


/* remove leading and trailing space and compress spaces and newlines */

void trim_text2(char *dest, char *src) {

	/* skip leading space */
	while (whitespace2(*src))
		src++;
	while (*src!='\0') {
		if (whitespace2(*src)) {
			*dest++=' ';
			while (whitespace2(*src)) src++;
			}
		else 
			*dest++ = *src++;
		}

	/* trim trailing white space */
	dest--;
	while (whitespace2(*dest)) 
		dest--;
	*(dest+1)='\0';
	}
