Add configuration flag to suppress wide character

This commit is contained in:
Aki Goto
2013-01-29 00:22:17 +09:00
parent d70ba913e8
commit 967bb85e16
12 changed files with 113 additions and 34 deletions

47
Character.c Normal file
View File

@@ -0,0 +1,47 @@
#include "config.h"
#include "Character.h"
void Character_write(Character character, FILE *stream)
{
#ifdef USE_WCHAR
fprintf(stream, "%lc", character);
#else
fprintf(stream, "%c", character);
#endif
}
void Character_toString(Character character, char *string)
{
#ifdef USE_WCHAR
sprintf(string, "%lc", character);
#else
sprintf(string, "%c", character);
#endif
}
Character Character_read(FILE *stream)
{
#ifdef USE_WCHAR
return fgetwc(stream);
#else
return fgetc(stream);
#endif
}
int Character_isEndOfFile(Character character)
{
#ifdef USE_WCHAR
return character == WEOF;
#else
return character == EOF;
#endif
}
int Character_width(Character character)
{
#ifdef USE_WCHAR
return wcwidth(character);
#else
return 1;
#endif
}

18
Character.h Normal file
View File

@@ -0,0 +1,18 @@
#ifndef CHARACTER_H
#define CHARACTER_H
#include <stdio.h>
#ifdef USE_WCHAR
typedef wchar_t Character;
#else
typedef char Character;
#endif
extern void Character_write(Character character, FILE *stream);
extern void Character_toString(Character character, char *string);
extern Character Character_read(FILE *stream);
extern int Character_isEndOfFile(Character character);
extern int Character_width(Character character);
#endif

View File

@@ -1,5 +1,4 @@
#define _XOPEN_SOURCE
#include <wchar.h>
#include "config.h"
#include <assert.h>
#include <string.h>
#include <stdlib.h>
@@ -52,7 +51,7 @@ static void toView(Editor this, int lineIndex, int characterIndex, int *resultX,
line = Text_getLine(this->text, j);
x = 0;
for (i = 0; i < Line_size(line); i++) {
wchar_t c;
Character c;
if (j == lineIndex && i == characterIndex) {
break;
@@ -65,7 +64,7 @@ static void toView(Editor this, int lineIndex, int characterIndex, int *resultX,
ts = this->variables.tabstop;
x = x / ts * ts + ts;
} else {
x += wcwidth(c);
x += Character_width(c);
}
if (x >= Window_width(this->window)) {
x = 0;
@@ -114,7 +113,7 @@ void Editor_paint(Editor this)
line = Text_getLine(this->text, lineIndex);
lineIndex++;
for (i = 0; i < Line_size(line); i++) {
wchar_t c;
Character c;
c = Line_getCharacter(line, i);
if (c == '\t') {
@@ -129,8 +128,8 @@ void Editor_paint(Editor this)
}
x = x2;
} else {
fprintf(stdout, "%lc", c);
x += wcwidth(c);
Character_write(c, stdout);
x += Character_width(c);
}
if (x >= Window_width(this->window)) {
@@ -177,7 +176,7 @@ static int viewLastLine(Editor this)
line = Text_getLine(this->text, j);
x = 0;
for (i = 0; i < Line_size(line); i++) {
wchar_t c;
Character c;
c = Line_getCharacter(line, i);
if (c == '\t') {

11
Input.c
View File

@@ -1,5 +1,4 @@
#define _XOPEN_SOURCE
#include <wchar.h>
#include "config.h"
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
@@ -39,7 +38,7 @@ void Input_updateCursorPosition(Input this)
width = 0;
for (i = 0; i < Line_size(this->line); i++) {
width += wcwidth(Line_getCharacter(this->line, i));
width += Character_width(Line_getCharacter(this->line, i));
}
Window_setCursorPosition(this->window, width, 0);
}
@@ -52,11 +51,11 @@ void Input_paint(Input this)
Window_setCursorPosition(this->window, 0, 0);
width = 0;
for (i = 0; i < Line_size(this->line); i++) {
wchar_t c;
Character c;
c = Line_getCharacter(this->line, i);
fprintf(stdout, "%lc", c);
width += wcwidth(c);
Character_write(c, stdout);
width += Character_width(c);
}
for (i = width; i < Window_width(this->window); i++) {
fputc(' ', stdout);

15
Line.c
View File

@@ -1,3 +1,4 @@
#include "config.h"
#include <assert.h>
#include <string.h>
#include <stdlib.h>
@@ -50,14 +51,14 @@ void Line_clear(Line this)
this->size = 0;
}
wchar_t Line_getCharacter(Line this, int index)
Character Line_getCharacter(Line this, int index)
{
assert(index >= 0 && index < this->size);
return *(this->characters + index);
}
void Line_addCharacter(Line this, int index, wchar_t character)
void Line_addCharacter(Line this, int index, Character character)
{
assert(index >= 0 && index <= this->size);
@@ -68,7 +69,7 @@ void Line_addCharacter(Line this, int index, wchar_t character)
this->maxSize *= 2;
}
this->characters = realloc(this->characters, sizeof(wchar_t) * this->maxSize);
this->characters = realloc(this->characters, sizeof(Character) * this->maxSize);
if (this->characters == NULL) {
fprintf(stderr, "realloc failed\n");
exit(EXIT_FAILURE);
@@ -76,7 +77,7 @@ void Line_addCharacter(Line this, int index, wchar_t character)
}
if (index < this->size) {
memmove(this->characters + index + 1, this->characters + index, sizeof(wchar_t) * (this->size - index));
memmove(this->characters + index + 1, this->characters + index, sizeof(Character) * (this->size - index));
}
*(this->characters + index) = character;
this->size++;
@@ -86,7 +87,7 @@ void Line_removeCharacter(Line this, int index)
{
assert(index >= 0 && index < this->size);
memmove(this->characters + index, this->characters + index + 1, sizeof(wchar_t) * (this->size - index - 1));
memmove(this->characters + index, this->characters + index + 1, sizeof(Character) * (this->size - index - 1));
this->size--;
}
@@ -145,7 +146,7 @@ char *Line_toCString(Line this)
length = 0;
for (i = 0; i < this->size; i++) {
sprintf(buf, "%lc", Line_getCharacter(this, i));
Character_toString(Line_getCharacter(this, i), buf);
length += strlen(buf);
}
@@ -157,7 +158,7 @@ char *Line_toCString(Line this)
s = string;
for (i = 0; i < this->size; i++) {
sprintf(s, "%lc", Line_getCharacter(this, i));
Character_toString(Line_getCharacter(this, i), s);
for (; *s != '\0'; s++);
}

8
Line.h
View File

@@ -1,10 +1,12 @@
#ifndef LINE_H
#define LINE_H
#include "Character.h"
struct Line_struct {
int maxSize;
int size;
wchar_t *characters;
Character *characters;
};
typedef struct Line_struct *Line;
@@ -14,8 +16,8 @@ extern void Line_destroy(Line this);
extern Line Line_clone(Line this);
extern int Line_size(Line this);
extern void Line_clear(Line this);
extern wchar_t Line_getCharacter(Line this, int index);
extern void Line_addCharacter(Line this, int index, wchar_t character);
extern Character Line_getCharacter(Line this, int index);
extern void Line_addCharacter(Line this, int index, Character character);
extern void Line_removeCharacter(Line this, int index);
extern void Line_addLine(Line this, int index, Line line);
extern Line Line_subLine(Line this, int index, int size);

View File

@@ -1,7 +1,8 @@
PROGNAME = degvi
CC = gcc
CFLAGS = -Wall
OBJS = main.o Editor.o Input.o Window.o Terminal.o Text.o Line.o
OBJS = main.o Editor.o Input.o Window.o Terminal.o Text.o Line.o Character.o
all: $(PROGNAME)

View File

@@ -1,3 +1,4 @@
#include "config.h"
#include <assert.h>
#include <string.h>
#include <stdlib.h>

9
Text.c
View File

@@ -1,5 +1,4 @@
#define _XOPEN_SOURCE
#include <wchar.h>
#include "config.h"
#include <assert.h>
#include <string.h>
#include <stdlib.h>
@@ -87,7 +86,7 @@ void Text_load(Text this, char *filename)
{
FILE *fp;
Line line;
wchar_t c;
Character c;
fp = fopen(filename, "r");
if (fp == NULL) {
@@ -96,7 +95,7 @@ void Text_load(Text this, char *filename)
}
line = Line_create();
while ((c = fgetwc(fp)) != WEOF) {
while (!Character_isEndOfFile(c = Character_read(fp))) {
if (c == '\n') {
Text_addLine(this, Text_size(this), line);
line = Line_create();
@@ -130,7 +129,7 @@ void Text_save(Text this, char *filename)
for (i = 0; i < Line_size(line); i++) {
char buf[6 + 1];
sprintf(buf, "%lc", Line_getCharacter(line, i));
Character_toString(Line_getCharacter(line, i), buf);
if (fputs(buf, fp) == EOF) {
fprintf(stderr, "fputs failed\n");

View File

@@ -1,3 +1,4 @@
#include "config.h"
#include <stdlib.h>
#include <stdio.h>
#include "Window.h"

11
config.h Normal file
View File

@@ -0,0 +1,11 @@
#ifndef CONFIG_H
#define CONFIG_H
#define USE_WCHAR
#ifdef USE_WCHAR
#define _XOPEN_SOURCE
#include <wchar.h>
#endif
#endif

8
main.c
View File

@@ -1,4 +1,4 @@
#include <wchar.h>
#include "config.h"
#include <locale.h>
#include <stdlib.h>
#include <stdio.h>
@@ -54,10 +54,10 @@ int main(int argc, char **argv)
}
for (;;) {
wchar_t c;
Character c;
c = fgetwc(stdin);
if (c == WEOF) {
c = Character_read(stdin);
if (Character_isEndOfFile(c)) {
break;
}
Editor_keyTyped(editor, c);