testline/testline.c

308 lines
6.7 KiB
C
Raw Normal View History

2024-01-06 15:42:14 +00:00
#include <stdio.h>
#include <string.h>
#include "src/list/src/list.h"
typedef enum InfoRet {
IR_UNKNOWN = -1,
IR_NORMAL = 0,
IR_UNSUCCESS
} InfoRet;
2024-01-11 15:21:34 +00:00
2024-01-06 15:42:14 +00:00
typedef struct InfoState {
char* info;
2024-01-11 15:21:34 +00:00
list_node_t *pos1, *pos2, *pos3;
list_t *wrap;
list_iterator_t *it;
//list_node_t *node;
2024-01-06 15:42:14 +00:00
} InfoState;
2024-01-11 15:21:34 +00:00
2024-01-06 15:42:14 +00:00
void lcd_blank_line() {
for (int i = 0; i < 15; ++i) {
2024-01-11 15:21:34 +00:00
//lcd.print(' ');
2024-01-12 14:30:17 +00:00
putchar(' ');
2024-01-06 15:42:14 +00:00
}
}
2024-01-12 14:30:17 +00:00
int lcd_print_between(char* str, int pos1, int pos2) {
2024-01-11 15:21:34 +00:00
//int i = 0;
int va = 0;
2024-01-12 14:30:17 +00:00
char* p = str+pos1;
2024-01-11 15:21:34 +00:00
//int slen = strlen(str);
2024-01-12 14:30:17 +00:00
while (p < str+pos2) {
2024-01-11 15:21:34 +00:00
if (*p != '\n' && *p != '\r') {
//lcd.print(*(p++));
2024-01-12 14:30:17 +00:00
putchar(*p);
2024-01-11 15:21:34 +00:00
++va;
} //else
//if (i == 0){ ++i; } else
//{
// break;
//}
2024-01-12 14:30:17 +00:00
++p;
2024-01-06 15:42:14 +00:00
}
2024-01-11 15:21:34 +00:00
if (va < 15) {
for (int j = 15 - va; j > 0; --j) {
//lcd.print(' ');
putchar(' ');
}
}
2024-01-12 14:30:17 +00:00
putchar('!');
2024-01-11 15:21:34 +00:00
return va;
2024-01-06 15:42:14 +00:00
}
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;
2024-01-11 15:21:34 +00:00
if (list_rpush(wrap, list_node_new(-1)) == NULL) {
2024-01-06 15:42:14 +00:00
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;
}
2024-01-12 14:30:17 +00:00
if (list_rpush(wrap, list_node_new((int)(curr-text))) == NULL) {
return NULL;
}
2024-01-06 15:42:14 +00:00
return wrap;
}
2024-01-11 15:21:34 +00:00
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);
2024-01-12 14:30:17 +00:00
st->it = list_iterator_new(st->wrap, LIST_HEAD);
2024-01-11 15:21:34 +00:00
st->pos1 = list_iterator_next(st->it);
st->pos2 = st->pos3 = NULL;
}
InfoRet initInfoScreen(InfoState* st, char* text) {
2024-01-11 15:21:34 +00:00
initInfoState(st, text);
2024-01-12 14:30:17 +00:00
printf("%d\n", st->wrap->len);
2024-01-11 15:21:34 +00:00
//lcd.setCursor(0, 0);
if ((st->pos2 = list_iterator_next(st->it))) {
//printf("hello\n");
2024-01-12 14:30:17 +00:00
//printf("%d\n", st->pos2->val);
int tmp = lcd_print_between(st->info, 0, (int)st->pos2->val);
//printf("%d\n", tmp);
//printf("%d\n", st->wrap->len);
if ((st->pos3 = list_iterator_next(st->it))) {
printf("\n");
//printf("%d\n", st->pos2->val);
//printf("%d\n", st->pos3->val);
int tmp = lcd_print_between(st->info, (int)st->pos2->val, (int)st->pos3->val);
//printf("%d\n", tmp);
//printf("%d\n", st->wrap->len);
} else {
printf("\n");
lcd_blank_line();
}
} else {
return IR_UNKNOWN;
2024-01-11 15:21:34 +00:00
}
return IR_NORMAL;
2024-01-11 15:21:34 +00:00
}
InfoRet scrollDown(InfoState* st) {
if (st->pos2) {
st->pos1 = st->pos2;
} else {
return IR_UNKNOWN;
}
if (st->pos3) {
st->pos2 = st->pos3;
} else {
return IR_UNSUCCESS;
}
int tmp = lcd_print_between(st->info, (int)st->pos1->val, (int)st->pos2->val);
if ((st->pos3 = list_iterator_next(st->it))) {
printf("\n");
int tmp = lcd_print_between(st->info, (int)st->pos2->val, (int)st->pos3->val);
} else {
printf("\n");
lcd_blank_line();
}
return IR_NORMAL;
}
InfoRet scrollUp(InfoState* st) {
2024-01-12 14:30:17 +00:00
}
2024-01-11 15:21:34 +00:00
2024-01-06 15:42:14 +00:00
/*
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.";
2024-01-11 15:21:34 +00:00
// 0 1516 32
2024-01-12 14:30:17 +00:00
char* s2 = " ba\n ab";
// 0 11
2024-01-06 15:42:14 +00:00
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);
node = list_iterator_next(it);
printf("node val: %d\n", node->val);
node = list_iterator_next(it);
printf("node val: %d\n", node->val);
node = list_iterator_prev(it);
printf("node val: %d\n", node->val);
node = list_iterator_prev(it);
printf("node val: %d\n", node->val);
node = list_iterator_prev(it);
printf("node val: %d\n", node->val);
InfoRet ret_code;
2024-01-11 15:21:34 +00:00
InfoState st;
initInfoScreen(&st, cinfo);
for (int i = 0; i < 10; ++i) {
printf("\n");
ret_code = scrollDown(&st);
printf("\n%d\n", ret_code);
}
2024-01-06 15:42:14 +00:00
2024-01-11 15:21:34 +00:00
/*
2024-01-06 15:42:14 +00:00
while ((node = list_iterator_next(it))) {
printf("%d\n", (unsigned int)(node->val));
}
2024-01-11 15:21:34 +00:00
*/
2024-01-06 15:42:14 +00:00
/*
if ((node = list_iterator_next(it))) {
2024-01-11 15:21:34 +00:00
}
if ((node = list_iterator_next(it))) {
//if (node->val == -1) {
2024-01-06 15:42:14 +00:00
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;
}