Skip to content

Commit

Permalink
Use UTF-8 data read from config file #72
Browse files Browse the repository at this point in the history
- shapes get additional MBCS values
- metadata is already converted for real to use bxstr_t*
  • Loading branch information
tsjensen committed Apr 29, 2023
1 parent e2e6d9e commit c533bf3
Show file tree
Hide file tree
Showing 20 changed files with 652 additions and 193 deletions.
25 changes: 17 additions & 8 deletions src/boxes.c
Original file line number Diff line number Diff line change
Expand Up @@ -84,23 +84,29 @@ static int build_design(design_t **adesigns, const char *cld)

dp->name = "<Command Line Definition>";
dp->aliases = (char **) calloc(1, sizeof(char *));
dp->created = "now";
dp->created = bxs_from_ascii("now");
dp->revision = "1.0";
dp->sample = "n/a";
dp->sample = bxs_from_ascii("n/a");
dp->indentmode = DEF_INDENTMODE;
dp->padding[BLEF] = 1;
dp->defined_in = "(command line)";
dp->defined_in = bxs_from_ascii("(command line)");

dp->tags = (char **) calloc(2, sizeof(char *));
dp->tags[0] = "transient";

uint32_t *cld_u32 = u32_strconv_from_arg(cld, "UTF-8"); /* CHECK wrong on Windows (UTF-16) or different IME */
bxstr_t *cldW = bxs_from_unicode(cld_u32);
BFREE(cld_u32);

dp->shape[W].height = 1;
dp->shape[W].width = strlen(cld);
dp->shape[W].width = cldW->num_columns;
dp->shape[W].elastic = 1;
rc = genshape(dp->shape[W].width, dp->shape[W].height, &(dp->shape[W].chars));
rc = genshape(dp->shape[W].width, dp->shape[W].height, &(dp->shape[W].chars), &(dp->shape[W].mbcs));
if (rc) {
return rc;
}
bxs_free(dp->shape[W].mbcs[0]);
dp->shape[W].mbcs[0] = cldW;
strcpy(dp->shape[W].chars[0], cld);

for (i = 0; i < NUM_SHAPES; ++i) {
Expand Down Expand Up @@ -139,7 +145,7 @@ static int build_design(design_t **adesigns, const char *cld)
return 1; /* never happens ;-) */
}

rc = genshape(c->width, c->height, &(c->chars));
rc = genshape(c->width, c->height, &(c->chars), &(c->mbcs));
if (rc) {
return rc;
}
Expand Down Expand Up @@ -185,7 +191,7 @@ static void handle_command_line(int argc, char *argv[])
*/
static void handle_config_parsing()
{
char *config_file = discover_config_file(0);
bxstr_t *config_file = discover_config_file(0);
if (config_file == NULL) {
exit(EXIT_FAILURE);
}
Expand Down Expand Up @@ -397,10 +403,13 @@ int main(int argc, char *argv[])
fprintf (stderr, "BOXES STARTING ...\n");
#endif

/* Temporarily set the system encoding, for proper output of --help text etc. */
setlocale(LC_ALL, ""); /* switch from default "C" encoding to system encoding */
encoding = locale_charset();

handle_command_line(argc, argv);

/* Store system character encoding */
setlocale(LC_ALL, ""); /* switch from default "C" encoding to system encoding */
encoding = check_encoding(opt.encoding, locale_charset());
#ifdef DEBUG
fprintf (stderr, "Character Encoding = %s\n", encoding);
Expand Down
22 changes: 13 additions & 9 deletions src/boxes.in.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,10 @@
/* #define LEXER_DEBUG 1 */
/* #define DISCOVERY_DEBUG 1 */


#include <stdio.h>
#include <unitypes.h>

#include "bxstring.h"
#include "regulex.h"
#include "shape.h"

Expand Down Expand Up @@ -56,6 +57,9 @@
#define LINE_MAX_BYTES 16382
#endif

/* Macro to declare a function parameter as intentionally unused in order to avoid compiler warnings */
#define UNUSED(variable) ((void)(variable))


#define BTOP 0 /* for use with sides */
#define BRIG 1
Expand All @@ -64,8 +68,8 @@


typedef struct {
char *search;
char *repstr;
bxstr_t *search;
bxstr_t *repstr;
pcre2_code *prog; /* compiled search pattern */
int line; /* line of definition in config file */
char mode; /* 'g' or 'o' */
Expand All @@ -75,20 +79,20 @@ typedef struct {
typedef struct {
char *name; /* primary name of the box design */
char **aliases; /* zero-terminated array of alias names of the design */
char *author; /* creator of the configuration file entry */
char *designer; /* creator of the original ASCII artwork */
char *created; /* date created, free format */
bxstr_t *author; /* creator of the configuration file entry */
bxstr_t *designer; /* creator of the original ASCII artwork */
bxstr_t *created; /* date created, free format */
char *revision; /* revision number of design */
char *revdate; /* date of current revision */
char *sample; /* the complete sample block in one string */
bxstr_t *revdate; /* date of current revision */
bxstr_t *sample; /* the complete sample block in one string */
char indentmode; /* 'b', 't', or 'n' */
sentry_t shape[NUM_SHAPES];
size_t maxshapeheight; /* height of highest shape in design */
size_t minwidth;
size_t minheight;
int padding[NUM_SIDES];
char **tags;
char *defined_in; /* path to config file where this was defined */
bxstr_t *defined_in; /* path to config file where this was defined */

reprule_t *current_rule;
reprule_t *reprules; /* applied when drawing a box */
Expand Down
7 changes: 4 additions & 3 deletions src/cmdline.c
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ static void usage_short(FILE *st)
*/
void usage_long(FILE *st)
{
char *config_file = discover_config_file(0);
bxstr_t *config_file = discover_config_file(0);

fprintf(st, "%s - draws any kind of box around your text (or removes it)\n", PROJECT);
fprintf(st, " Website: https://boxes.thomasjensen.com/\n");
Expand All @@ -86,7 +86,8 @@ void usage_long(FILE *st)
fprintf(st, " -d name box design [default: first one in file]\n");
fprintf(st, " -e eol Override line break type (experimental) [default: %s]\n",
strcmp(EOL_DEFAULT, "\r\n") == 0 ? "CRLF" : "LF");
fprintf(st, " -f file configuration file [default: %s]\n", config_file != NULL ? config_file : "none");
fprintf(st, " -f file configuration file [default: %s]\n",
config_file != NULL ? bxs_to_output(config_file) : "none");
fprintf(st, " -h print usage information\n");
fprintf(st, " -i mode indentation mode [default: box]\n");
fprintf(st, " -k bool leading/trailing blank line retention on removal\n");
Expand All @@ -100,7 +101,7 @@ void usage_long(FILE *st)
fprintf(st, " -t str tab stop distance and expansion [default: %de]\n", DEF_TABSTOP);
fprintf(st, " -v print version information\n");

BFREE(config_file);
bxs_free(config_file);
}


Expand Down

0 comments on commit c533bf3

Please sign in to comment.