-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
99 lines (90 loc) · 2.02 KB
/
main.c
File metadata and controls
99 lines (90 loc) · 2.02 KB
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
#include<stdio.h>
unsigned char* getFromChar(unsigned char *argv[]){
unsigned char values[4] = {0,0,0,0};
unsigned char *val_ptr = values;
int size = 4;
if(argv[1][0] < 128){
size = 1;
}else if(argv[1][0] >= 192 && argv[1][0] < 224){
size = 2;
}else if(argv[1][0] >= 224 && argv[1][0] < 240){
size = 3;
}else if(argv[1][0] >= 240 && argv[1][0] < 248){
size = 4;
}
for(int i = 0; i < size; i++){
values[i] = argv[1][i];
}
return val_ptr;
}
unsigned char* getFromInt(int argc, unsigned char *argv[]){
unsigned char values[4] = {0,0,0,0};
unsigned char *val_ptr = values;
for(int i = 1; i < argc; i++){
sscanf(argv[i], "%hhd", &values[i-1]);
}
return val_ptr;
}
void printCode(unsigned char code[]){
for(int i = 0; i < 4; i++){
if(code[i]){
printf("%d ", code[i]);
}
}
}
void printCharacter(unsigned char code[]){
printf("%c%c%c%c", code[0], code[1], code[2], code[3]);
}
int main(int argc, unsigned char *argv[]){
_Bool isChar = 1, showMessage = 1;
int position = -1;
for(int i = 1; i < argc; i++){
if(argv[i][0] != '-' && position == -1){
position = i-1;
}else{
switch(argv[i][1]){
case 'a':
showMessage = 1;
break;
case 'b':
showMessage = 0;
break;
case 'c':
isChar = 1;
break;
case 'h':
argc = 0;
break;
case 'u':
isChar = 0;
break;
}
}
}
if(argc > 1){
unsigned char *values;
if(isChar){
values = getFromChar(&argv[position]);
}else{
values = getFromInt(argc-position, &argv[position]);
}
if(showMessage){
printf("The UTF-8 code ");
printCode(values);
printf("represents the character ");
printCharacter(values);
printf(".\n");
}else{
if(isChar){
printCode(values);
printf("\n");
}else{
printCharacter(values);
printf("\n");
}
}
}else{
printf("\nUsage:\n\tutf8code [character|UTF-8 code] [options]\n\nOptions:\n\t-a\tShow the entire text\n\t-b\tPrint the bare output\n\t-c\tPrint the character\n\t-h\tShow the help menu\n\t-u\tPrint the UTF-8 code\n\n");
}
return 0;
}