testline/testline.c
2024-01-11 23:21:34 +08:00

239 lines
No EOL
5 KiB
C

#include <stdio.h>
#include <string.h>
#include "src/list/src/list.h"
typedef struct InfoState {
char* info;
list_node_t *pos1, *pos2, *pos3;
list_t *wrap;
list_iterator_t *it;
//list_node_t *node;
} InfoState;
void lcd_blank_line() {
for (int i = 0; i < 15; ++i) {
//lcd.print(' ');
printf(" ");
}
}
int lcd_print_between(char* pos1, char* pos2) {
//int i = 0;
int va = 0;
char* p = pos1;
//int slen = strlen(str);
while (p < pos2) {
if (*p != '\n' && *p != '\r') {
//lcd.print(*(p++));
putchar(*(p++));
++va;
} //else
//if (i == 0){ ++i; } else
//{
// break;
//}
}
if (va < 15) {
for (int j = 15 - va; j > 0; --j) {
//lcd.print(' ');
putchar(' ');
}
}
return va;
}
list_t* aux_gen_wrap_list(list_t* wrap, char* text) {
if (wrap == NULL) {
wrap = list_new();
}
char* curr = text;
int suf = 0;
list_node_t *node;
if (list_rpush(wrap, list_node_new(-1)) == NULL) {
return NULL;
}
while (*curr != '\0') {
//printf("suf: %d\tpos: %d\n", suf, (unsigned int)(curr-text));
if (suf < 15) {
if (*curr != '\n' && *curr != '\r') {
++suf;
} else {
if (list_rpush(wrap, list_node_new((unsigned int)(curr-text))) == NULL) {
return NULL;
}
suf = 0;
}
} else {
if (list_rpush(wrap, list_node_new((unsigned int)(curr-text))) == NULL) {
return NULL;
}
suf = (*curr != '\n' && *curr != '\r') ? 1 : 0;
}
++curr;
}
return wrap;
}
void initInfoState(InfoState* st, char* text) {
st->info = text;
//st->len = strlen(text);
//st->fullLines = st->len / 15;
//st->Nlines = (st->len + 14) / 15;
//st->lastlinelen = st->len % 15;
//st->pos1 = text;
//st->pos2 = text;
//st->pos3 = text;
st->wrap = list_new();
st->wrap = aux_gen_wrap_list(st->wrap, st->info);
st->it = list_iterator_new(st->wrap, LIST_HEAD);
st->pos1 = list_iterator_next(st->it);
st->pos2 = st->pos3 = NULL;
}
void initInfoScreen(InfoState* st, char* text) {
initInfoState(st, text);
//lcd.setCursor(0, 0);
if ((st->pos2 = list_iterator_next(st->it))) {
//printf("hello\n");
printf("%d\n", st->pos2->val);
int tmp = lcd_print_between(st->info, st->info+(int)st->pos2->val);
printf("%d\n", tmp);
}
}
/*
Separate a substring that can be printed on one line, and return the
original string position corresponding to the end of the substring.
*/
/*
char* aux_sub_line(char* str, char substr[16]) {
int suf = 0;
char* curr = str;
memset(substr, 0, 16);
while (*curr != '\0' && suf < 15) {
if (*curr != '\n' && *curr != '\r') {
substr[suf++] = *(curr++);
//++curr;
} else
if (curr == str){ ++curr; } else
{
break;
}
}
//#if !REFRESH
while (suf < 15) {
substr[suf++] = ' ';
}
//#endif
return curr;
}
char* aux_sub_line_rev(char* str, char substr[16]) {
int suf = 0;
char* curr = str;
memset(substr, 0, 16);
while (*curr != '\0' && suf < 15) {
if (*curr != '\n' && *curr != '\r') {
substr[suf++] = *(curr--);
} else
if (curr == str){ --curr; } else
{
break;
}
}
{
int i = 0;
int j = suf - 1;
while (i < j) {
char temp = substr[i];
substr[i] = substr[j];
substr[j] = temp;
i++;
j--;
}
}
//#if !REFRESH
while (suf < 15) {
substr[suf++] = ' ';
}
//#endif
return curr+1;
}
*/
int main() {
char* cinfo = "Far far away, a\n\nbehind the word mountains, far from the countries Vokalia and Consonantia, there live the blind texts.";
// 0 1516 32
list_t *wrap = list_new();
wrap = aux_gen_wrap_list(wrap, cinfo);
list_node_t *node;
list_iterator_t *it = list_iterator_new(wrap, LIST_HEAD);
InfoState st;
initInfoScreen(&st, cinfo);
/*
while ((node = list_iterator_next(it))) {
printf("%d\n", (unsigned int)(node->val));
}
*/
/*
if ((node = list_iterator_next(it))) {
}
if ((node = list_iterator_next(it))) {
//if (node->val == -1) {
char* p = cinfo;
list_node_t *test;
while (*p != '\0') {
//printf("node->val: %d\n", node->val);
if (p - cinfo != node->val) {
printf("%c", *p);
//printf("%d %d\n", (unsigned int)(p - cinfo), (unsigned int)(node->val));
} else {
printf("|\n");
if (*p != '\n' && *p != '\r') {
printf("%c", *p);
}
if ((test = list_iterator_next(it))) {
node = test;
}
}
++p;
}
} else {
printf("%s", cinfo);
}
*/
/*
char line[16];
char* pos = aux_sub_line(cinfo, line);
printf("%d\n%s\n", pos-cinfo, line);
for (int i = 0; i < 4; ++i) {
pos = aux_sub_line(pos, line);
printf("%d\n%s\n", pos-cinfo, line);
}
for (int i = 0; i < 4; ++i) {
pos = aux_sub_line_rev(pos-1, line);
printf("%d\n%s\n", pos-cinfo, line);
}
*/
list_iterator_destroy(it);
list_destroy(wrap);
return 0;
}