1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
|
/*
* utils.c - multibyte-string helpers
* Copyright (c) Clemens Ladisch <clemens@ladisch.de>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* This program 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.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef _XOPEN_SOURCE
#define _XOPEN_SOURCE
#endif
#include "aconfig.h"
#include <limits.h>
#include <stdlib.h>
#include <string.h>
#include <wchar.h>
#include <errno.h>
#include <stdio.h>
#include "utils.h"
#include "mem.h"
/*
* mbs_at_width - compute screen position in a string
*
* For displaying strings on the screen, we have to know how many character
* cells are occupied. This function calculates the position in a multibyte
* string that is at a desired position.
*
* Parameters:
* s: the string
* width: on input, the desired number of character cells; on output, the actual
* position, in character cells, of the return value
* dir: -1 or 1; in which direction to round if a multi-column character goes
* over the desired width
*
* Return value:
* Pointer to the place in the string that is as near the desired width as
* possible. If the string is too short, the return value points to the
* terminating zero. If the last character is a multi-column character that
* goes over the desired width, the return value may be one character cell
* earlier or later than desired, depending on the dir parameter.
* In any case, the return value points after any zero-width characters that
* follow the last character.
*/
const char *mbs_at_width(const char *s, int *width, int dir)
{
size_t len;
wchar_t wc;
int bytes;
int width_so_far, w;
if (*width <= 0)
return s;
mbtowc(NULL, NULL, 0); /* reset shift state */
len = strlen(s);
width_so_far = 0;
while (len && (bytes = mbtowc(&wc, s, len)) > 0) {
w = wcwidth(wc);
if (width_so_far + w > *width && dir < 0)
break;
if (w >= 0)
width_so_far += w;
s += bytes;
len -= bytes;
if (width_so_far >= *width) {
while (len && (bytes = mbtowc(&wc, s, len)) > 0) {
w = wcwidth(wc);
if (w != 0)
break;
s += bytes;
len -= bytes;
}
break;
}
}
*width = width_so_far;
return s;
}
/*
* get_mbs_width - compute screen width of a string
*/
unsigned int get_mbs_width(const char *s)
{
int width;
width = INT_MAX;
mbs_at_width(s, &width, 1);
return width;
}
/*
* get_max_mbs_width - get width of longest string in an array
*/
unsigned int get_max_mbs_width(const char *const *s, unsigned int count)
{
unsigned int max_width, i, len;
max_width = 0;
for (i = 0; i < count; ++i) {
len = get_mbs_width(s[i]);
if (len > max_width)
max_width = len;
}
return max_width;
}
#define MAX_FILE_SIZE 1048576
char *read_file(const char *file_name, unsigned int *file_size)
{
FILE *f;
int err;
char *buf;
unsigned int allocated = 2048;
unsigned int bytes_read;
f = fopen(file_name, "r");
if (!f) {
err = errno;
errno = err;
return NULL;
}
*file_size = 0;
buf = NULL;
do {
allocated *= 2;
buf = crealloc(buf, allocated);
bytes_read = fread(buf + *file_size, 1, allocated - *file_size, f);
*file_size += bytes_read;
} while (*file_size == allocated && allocated < MAX_FILE_SIZE);
fclose(f);
if (*file_size > 0 && buf[*file_size - 1] != '\n' && *file_size < allocated) {
buf[*file_size] = '\n';
++*file_size;
}
return buf;
}
|