/*
 * synergy -- mouse and keyboard sharing utility
 * Copyright (C) 2002 Chris Schoeneman
 * Copyright (C) 2006 Knut St. Osmundsen
 *
 * This package is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * found in the file COPYING that should have accompanied this file.
 *
 * This package is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */

#include "CPMClipboardAnyTextConverter.h"
#include "CLog.h"

//
// CPMClipboardAnyTextConverter
//

CPMClipboardAnyTextConverter::CPMClipboardAnyTextConverter()
{
	// do nothing
}

CPMClipboardAnyTextConverter::~CPMClipboardAnyTextConverter()
{
	// do nothing
}

IClipboard::EFormat
CPMClipboardAnyTextConverter::getFormat() const
{
	return IClipboard::kText;
}
ULONG
CPMClipboardAnyTextConverter::getPMFormatInfo() const
{
    return CFI_POINTER;
}

ULONG
CPMClipboardAnyTextConverter::fromIClipboard(const CString& data) const
{
	// convert linefeeds and then convert to desired encoding
	CString text = doFromIClipboard(convertLinefeedToPM(data));
	UInt32 cb = text.size();

	// copy to memory handle
    PVOID pv = NULL;
    APIRET rc = DosAllocSharedMem(&pv, NULL, cb + 1, PAG_READ | PAG_WRITE | PAG_COMMIT | OBJ_GETTABLE | OBJ_GIVEABLE);
    if (rc == NO_ERROR) {
		const char *psz = text.c_str();
LOG((CLOG_DEBUG "fromIClipboard: psz='%s' cb=%d strlen=%d\n", psz, cb, strlen(psz)));
        return (ULONG)memcpy(pv, psz, cb);
    }
    return 0;
}

void
CPMClipboardAnyTextConverter::freePMData(ULONG ulPMData) const
{
    DosFreeMem((PVOID)ulPMData);
}

CString
CPMClipboardAnyTextConverter::toIClipboard(ULONG data) const
{
	// convert text
    const char *psz = (const char *)data;
LOG((CLOG_DEBUG "toIClipboard: psz='%s' strlen=%d\n", psz, strlen(psz)));
	CString raw = CString(psz);
LOG((CLOG_DEBUG "toIClipboard: raw='%s'\n", raw.c_str()));
	CString text = doToIClipboard(raw);
LOG((CLOG_DEBUG "toIClipboard: text='%s'\n", text.c_str()));

	// convert newlines
	return convertLinefeedToUnix(text);
}

CString
CPMClipboardAnyTextConverter::convertLinefeedToPM(const CString& src) const
{
	// note -- we assume src is a valid UTF-8 string    L

	// count newlines in string
	UInt32 numNewlines = 0;
	UInt32 n           = src.size();
	for (const char* scan = src.c_str(); n > 0; ++scan, --n) {
		if (*scan == '\n') {
			++numNewlines;
		}
	}
	if (numNewlines == 0) {
		return src;
	}

	// allocate new string
	CString dst;
	dst.reserve(src.size() + numNewlines);

	// copy string, converting newlines
	n = src.size();
	for (const char* scan = src.c_str(); n > 0; ++scan, --n) {
		if (scan[0] == '\n') {
			dst += '\r';
		}
		dst += scan[0];
	}

	return dst;
}

CString
CPMClipboardAnyTextConverter::convertLinefeedToUnix(const CString& src) const
{
	// count newlines in string
	UInt32 numNewlines = 0;
	UInt32 n           = src.size();
	for (const char* scan = src.c_str(); n > 0; ++scan, --n) {
		if (scan[0] == '\r' && scan[1] == '\n') {
			++numNewlines;
		}
	}
	if (numNewlines == 0) {
		return src;
	}

	// allocate new string
	CString dst;
	dst.reserve(src.size());

	// copy string, converting newlines
	n = src.size();
	for (const char* scan = src.c_str(); n > 0; ++scan, --n) {
		if (scan[0] != '\r' || scan[1] != '\n') {
			dst += scan[0];
		}
	}

	return dst;
}
