cscg24-guacamole

CSCG 2024 Challenge 'Guacamole Mashup'
git clone https://git.sinitax.com/sinitax/cscg24-guacamole
Log | Files | Refs | sfeed.txt

common.c (2477B)


      1/*
      2 * Licensed to the Apache Software Foundation (ASF) under one
      3 * or more contributor license agreements.  See the NOTICE file
      4 * distributed with this work for additional information
      5 * regarding copyright ownership.  The ASF licenses this file
      6 * to you under the Apache License, Version 2.0 (the
      7 * "License"); you may not use this file except in compliance
      8 * with the License.  You may obtain a copy of the License at
      9 *
     10 *   http://www.apache.org/licenses/LICENSE-2.0
     11 *
     12 * Unless required by applicable law or agreed to in writing,
     13 * software distributed under the License is distributed on an
     14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
     15 * KIND, either express or implied.  See the License for the
     16 * specific language governing permissions and limitations
     17 * under the License.
     18 */
     19
     20#include "terminal/types.h"
     21
     22#include <stdbool.h>
     23#include <unistd.h>
     24
     25int guac_terminal_fit_to_range(int value, int min, int max) {
     26
     27    if (value < min) return min;
     28    if (value > max) return max;
     29
     30    return value;
     31
     32}
     33
     34int guac_terminal_encode_utf8(int codepoint, char* utf8) {
     35
     36    int i;
     37    int mask, bytes;
     38
     39    /* Determine size and initial byte mask */
     40    if (codepoint <= 0x007F) {
     41        mask  = 0x00;
     42        bytes = 1;
     43    }
     44    else if (codepoint <= 0x7FF) {
     45        mask  = 0xC0;
     46        bytes = 2;
     47    }
     48    else if (codepoint <= 0xFFFF) {
     49        mask  = 0xE0;
     50        bytes = 3;
     51    }
     52    else if (codepoint <= 0x1FFFFF) {
     53        mask  = 0xF0;
     54        bytes = 4;
     55    }
     56
     57    /* Otherwise, invalid codepoint */
     58    else {
     59        *(utf8++) = '?';
     60        return 1;
     61    }
     62
     63    /* Offset buffer by size */
     64    utf8 += bytes - 1;
     65
     66    /* Add trailing bytes, if any */
     67    for (i=1; i<bytes; i++) {
     68        *(utf8--) = 0x80 | (codepoint & 0x3F);
     69        codepoint >>= 6;
     70    }
     71
     72    /* Set initial byte */
     73    *utf8 = mask | codepoint;
     74
     75    /* Done */
     76    return bytes;
     77
     78}
     79
     80bool guac_terminal_has_glyph(int codepoint) {
     81    return
     82           codepoint != 0
     83        && codepoint != ' '
     84        && codepoint != GUAC_CHAR_CONTINUATION;
     85}
     86
     87int guac_terminal_write_all(int fd, const char* buffer, int size) {
     88
     89    int remaining = size;
     90    while (remaining > 0) {
     91
     92        /* Attempt to write data */
     93        int ret_val = write(fd, buffer, remaining);
     94        if (ret_val <= 0)
     95            return -1;
     96
     97        /* If successful, contine with what data remains (if any) */
     98        remaining -= ret_val;
     99        buffer += ret_val;
    100
    101    }
    102
    103    return size;
    104
    105}
    106