Files
mercury/runtime/mercury_bitmap.c
Mark Brown d465fa53cb Update the COPYING.LIB file and references to it.
Discussion of these changes can be found on the Mercury developers
mailing list archives from June 2018.

COPYING.LIB:
    Add a special linking exception to the LGPL.

*:
    Update references to COPYING.LIB.

    Clean up some minor errors that have accumulated in copyright
    messages.
2018-06-09 17:43:12 +10:00

121 lines
3.1 KiB
C

// vim: ts=4 sw=4 expandtab ft=c
// Copyright (C) 2007 The University of Melbourne.
// Copyright (C) 2014, 2016, 2018 The Mercury team.
// This file is distributed under the terms specified in COPYING.LIB.
// mercury_bitmap.c - bitmap handling
#include "mercury_imp.h"
#include "mercury_bitmap.h"
#include <stdio.h>
static int MR_hex_char_to_int(char digit);
static MR_String MR_do_bitmap_to_string(MR_ConstBitmapPtr, MR_bool, MR_bool,
MR_AllocSiteInfoPtr);
// Note that MR_bitmap_cmp and MR_hash_bitmap are actually defined
// as macros in mercury_bitmap.h, if we are using GNU C.
// We define them here whether or not we are using gcc, so that users
// can easily switch between gcc and cc without rebuilding the libraries.
#undef MR_bitmap_cmp
MR_Integer
MR_bitmap_cmp(MR_ConstBitmapPtr b1, MR_ConstBitmapPtr b2)
{
MR_BITMAP_CMP_FUNC_BODY
}
#undef MR_hash_bitmap
MR_Integer
MR_hash_bitmap(MR_ConstBitmapPtr b)
{
MR_HASH_BITMAP_FUNC_BODY
}
static const char hex_digits[] =
{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'A', 'B', 'C', 'D', 'E', 'F'};
static int
MR_hex_char_to_int(char digit)
{
switch (digit) {
case '0': return 0;
case '1': return 1;
case '2': return 2;
case '3': return 3;
case '4': return 4;
case '5': return 5;
case '6': return 6;
case '7': return 7;
case '8': return 8;
case '9': return 9;
case 'A': return 10;
case 'B': return 11;
case 'C': return 12;
case 'D': return 13;
case 'E': return 14;
case 'F': return 15;
default : return -1;
}
}
MR_String
MR_bitmap_to_quoted_string_saved_hp(MR_ConstBitmapPtr b,
MR_AllocSiteInfoPtr alloc_id)
{
return MR_do_bitmap_to_string(b, MR_TRUE, MR_TRUE, alloc_id);
}
static MR_String
MR_do_bitmap_to_string(MR_ConstBitmapPtr b,
MR_bool quote, MR_bool use_saved_hp, MR_AllocSiteInfoPtr alloc_id)
{
MR_String result;
size_t i;
size_t len;
size_t num_bytes;
size_t num_bits_len;
int start;
char num_bits_str[100];
sprintf(num_bits_str, "%" MR_INTEGER_LENGTH_MODIFIER "d", b->num_bits);
num_bits_len = strlen(num_bits_str);
num_bytes = MR_bitmap_length_in_bytes(b->num_bits);
len = 1 + num_bits_len + 1 + num_bytes * 2 + 1;
if (quote) {
len += 2;
}
if (use_saved_hp) {
MR_allocate_aligned_string_saved_hp(result, len, alloc_id);
} else {
MR_allocate_aligned_string_msg(result, len, alloc_id);
}
if (quote) {
result[0] = '"';
result[1] = '<';
result[len - 2] = '>';
result[len - 1] = '"';
start = 2;
} else {
result[0] = '<';
result[len - 1] = '>';
start = 1;
}
strcpy(result + start, num_bits_str);
start += num_bits_len;
result[start++] = ':';
for (i = 0; i < num_bytes; i++) {
result[start++] = hex_digits[(b->elements[i] >> 4) & 0xf];
result[start++] = hex_digits[b->elements[i] & 0xf];
}
result[len] = '\0';
return result;
}