Skip to content

Commit

Permalink
Gave the whole thing an (explicit) BSD style LICENSE and got swig (ut…
Browse files Browse the repository at this point in the history
…f) to work.
  • Loading branch information
dvorberg committed Jan 23, 2012
1 parent 6feaebe commit 4639222
Show file tree
Hide file tree
Showing 34 changed files with 923 additions and 82 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Expand Up @@ -5,4 +5,6 @@
*.inc
*.lo
*.o
depend
depend
*.la
header.txt
26 changes: 26 additions & 0 deletions LICENSE
@@ -0,0 +1,26 @@
Copyright (c) 2012 Diedrich Vorberg <diedrich@tux4web.de>

All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:

o Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.

o Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2 changes: 1 addition & 1 deletion Makefile
@@ -1,7 +1,7 @@
include flags.mak

# The sources for the library
SRCS=betacode_greek.c++ cjhebrew.c++
SRCS=betacode_greek.c++ cjhebrew.c++ conversion.c++
OBJS=$(SRCS:%.c++=%.o) code_tables.o

CODE_LISTS=coptic.tbl greek_asterisk.tbl greek_case.tbl\
Expand Down
17 changes: 16 additions & 1 deletion betacode_greek.c++
@@ -1,9 +1,24 @@
// -*- mode: c++; encoding: utf-8; -*_
//
// This file is part of libtransliterate
//
// Copyright 2012 by Diedrich Vorberg <diedrich@tux4web.de>
//
// All Rights Reserved.
//
// For more Information on orm see the README and LICENSE file.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
// CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED.

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

#include "trie.h"
#include "util.h"
#include "code_tables.h"

class forward_greek_trie: public trie<char, uint16_t>, public code_container
Expand Down
17 changes: 16 additions & 1 deletion cjhebrew.c++
@@ -1,9 +1,24 @@
// -*- mode: c++; encoding: utf-8; -*_
//
// This file is part of libtransliterate
//
// Copyright 2012 by Diedrich Vorberg <diedrich@tux4web.de>
//
// All Rights Reserved.
//
// For more Information on orm see the README and LICENSE file.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
// CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED.

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

#include "trie.h"
#include "util.h"
#include "code_tables.h"

class cjhebrew_to_utf16_trie_t:
Expand Down
16 changes: 16 additions & 0 deletions cjhebrew.tbl
@@ -1,3 +1,19 @@
// -*- mode: fundamental; encoding: utf-8; -*_
//
// This file is part of libtransliterate
//
// Copyright 2012 by Diedrich Vorberg <diedrich@tux4web.de>
//
// All Rights Reserved.
//
// For more Information on orm see the README and LICENSE file.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
// CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED.

// o Syntax: Everything after // is ignored.
// o Empty lines are ignored
// o Everything else must be <code> <code point>
Expand Down
16 changes: 16 additions & 0 deletions code_tables.c++
@@ -1,3 +1,19 @@
// -*- mode: c++; encoding: utf-8; -*_
//
// This file is part of libtransliterate
//
// Copyright 2012 by Diedrich Vorberg <diedrich@tux4web.de>
//
// All Rights Reserved.
//
// For more Information on orm see the README and LICENSE file.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
// CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED.

#include "code_tables.h"

void add_greek_asterisk_code_table(code_container *container)
Expand Down
16 changes: 16 additions & 0 deletions code_tables.h
@@ -1,3 +1,19 @@
// -*- mode: c++; encoding: utf-8; -*_
//
// This file is part of libtransliterate
//
// Copyright 2012 by Diedrich Vorberg <diedrich@tux4web.de>
//
// All Rights Reserved.
//
// For more Information on orm see the README and LICENSE file.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
// CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED.

#include <stdint.h>

/* This header defines a mix-in class that allows an object to be passed to
Expand Down
204 changes: 204 additions & 0 deletions conversion.c++
@@ -1,10 +1,214 @@
// -*- mode: c++; encoding: utf-8; -*_
//
// This file is part of libtransliterate
//
// This is based on utf8.h and utf8.c written by Alexey Vatchenko.
//

/*
* Copyright (c) 2007 Alexey Vatchenko <av@bsdua.org>
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/

#include <stdint.h>
#include <sys/types.h>
#include <wchar.h>

#define UTF8_IGNORE_ERROR 0x01
#define UTF8_SKIP_BOM 0x02

#define _NXT 0x80
#define _SEQ2 0xc0
#define _SEQ3 0xe0
#define _SEQ4 0xf0
#define _SEQ5 0xf8
#define _SEQ6 0xfc

#define _BOM 0xfeff

static int
__wchar_forbitten(wchar_t sym)
{

/* Surrogate pairs */
if (sym >= 0xd800 && sym <= 0xdfff)
return (-1);

return (0);
}

static int
__utf8_forbitten(u_char octet)
{

switch (octet) {
case 0xc0:
case 0xc1:
case 0xf5:
case 0xff:
return (-1);
}

return (0);
}

namespace transliterate
{
size_t utf16_to_utf8(uint16_t *input, char *buffer,
size_t buffer_length)
{
/*
* DESCRIPTION
* This function translates UCS-4 symbols (given in local machine
* byte order) into UTF-8 string.
*
* It takes the following arguments:
* in - input unicode string. It can be null-terminated.
* insize - size of input string in wide characters.
* out - result buffer for utf8 string. If out is NULL,
* function returns size of result buffer.
* outsize - size of result buffer.
*
* RETURN VALUES
* The function returns size of result buffer (in bytes). Zero is returned
* in case of error.
*
* CAVEATS
* If UCS-4 string contains zero symbols, they will be translated
* as regular symbols.
*/

// size_t wchar_to_utf8(const wchar_t *in, size_t insize,
// char *out, size_t outsize,
// int flags)

int flags = 0;

uint16_t *w, *wlim;
wchar_t ch;
u_char *p, *lim, *oc;
size_t total, n;

w = input;
size_t insize = 0;
for (wlim = input; *wlim != 0; wlim++)
{
insize++;
};

//size_t outsize = insize + (insize >> 1);
//u_char *out = malloc( outsize ); // insize *= 1.5

size_t outsize = buffer_length;
u_char *out = (u_char *)buffer;

p = (u_char *)out;
lim = p + outsize;
total = 0;
for (; w < wlim; w++) {
wchar_t W = (wchar_t)*w;

if (__wchar_forbitten(W) != 0) {
if ((flags & UTF8_IGNORE_ERROR) == 0)
return (0);
else
continue;
}

if (W == _BOM && (flags & UTF8_SKIP_BOM) != 0)
continue;

if (W < 0) {
if ((flags & UTF8_IGNORE_ERROR) == 0)
return (0);
continue;
} else if (W <= 0x0000007f)
n = 1;
else if (W <= 0x000007ff)
n = 2;
else if (W <= 0x0000ffff)
n = 3;
else if (W <= 0x001fffff)
n = 4;
else if (W <= 0x03ffffff)
n = 5;
else /* if (W <= 0x7fffffff) */
n = 6;

total += n;

if (out == NULL)
continue;

if (lim - p <= n - 1)
return (0); /* no space left */

/* make it work under different endians */
ch = htonl(W);
oc = (u_char *)&ch;
switch (n) {
case 1:
*p = oc[3];
break;

case 2:
p[1] = _NXT | oc[3] & 0x3f;
p[0] = _SEQ2 | (oc[3] >> 6) | ((oc[2] & 0x07) << 2);
break;

case 3:
p[2] = _NXT | oc[3] & 0x3f;
p[1] = _NXT | (oc[3] >> 6) | ((oc[2] & 0x0f) << 2);
p[0] = _SEQ3 | ((oc[2] & 0xf0) >> 4);
break;

case 4:
p[3] = _NXT | oc[3] & 0x3f;
p[2] = _NXT | (oc[3] >> 6) | ((oc[2] & 0x0f) << 2);
p[1] = _NXT | ((oc[2] & 0xf0) >> 4) |
((oc[1] & 0x03) << 4);
p[0] = _SEQ4 | ((oc[1] & 0x1f) >> 2);
break;

case 5:
p[4] = _NXT | oc[3] & 0x3f;
p[3] = _NXT | (oc[3] >> 6) | ((oc[2] & 0x0f) << 2);
p[2] = _NXT | ((oc[2] & 0xf0) >> 4) |
((oc[1] & 0x03) << 4);
p[1] = _NXT | (oc[1] >> 2);
p[0] = _SEQ5 | oc[0] & 0x03;
break;

case 6:
p[5] = _NXT | oc[3] & 0x3f;
p[4] = _NXT | (oc[3] >> 6) | ((oc[2] & 0x0f) << 2);
p[3] = _NXT | (oc[2] >> 4) | ((oc[1] & 0x03) << 4);
p[2] = _NXT | (oc[1] >> 2);
p[1] = _NXT | oc[0] & 0x3f;
p[0] = _SEQ6 | ((oc[0] & 0x40) >> 6);
break;
}

/*
* NOTE: do not check here for forbitten UTF-8 characters.
* They cannot appear here because we do proper convertion.
*/

p += n;
}

*p = 0;
return total;
}
}
16 changes: 16 additions & 0 deletions coptic.tbl
@@ -1,3 +1,19 @@
// -*- mode: fundamental; encoding: utf-8; -*_
//
// This file is part of libtransliterate
//
// Copyright 2012 by Diedrich Vorberg <diedrich@tux4web.de>
//
// All Rights Reserved.
//
// For more Information on orm see the README and LICENSE file.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
// CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED.

// These codes follow the TLG Beta Code Quick Reference Guide
// July 16, 2009
//
Expand Down

0 comments on commit 4639222

Please sign in to comment.