This repository has been archived by the owner on Jun 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
showfirstwords.c
77 lines (66 loc) · 1.97 KB
/
showfirstwords.c
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
/**
* showfirstwords.c -- вывод первых слов строк на экран
*
* Copyright (c) 2017, Alexander Borodin <[email protected]>
*
* This code is licensed under a MIT-style license.
*/
#include <stdio.h>
#include <assert.h>
#include <string.h>
#include "common.h"
#include "text/text.h"
static void showf_words(int index, char *contents, int cursor, void *data);
int flag = 1;
/**
* Выводит содержимое указанного файла на экран
*/
void showfirstwords(text txt)
{
/* Применяем функцию show_line к каждой строке текста */
process_forward(txt, showf_words, NULL);
}
static void showf_words(int index, char *contents, int cursor, void *data)
{
UNUSED(cursor);
UNUSED(data);
UNUSED(index);
int flag_enter = flag;
int length = strlen(contents);
for(int i = 0; i < length; i++){
if(flag_enter){
/* Skip empty space */
while(contents[i] == ' '){
i++;
}
int flag_exit = 1;
while(flag_exit){
switch(contents[i]){
case ' ':
flag_enter = 0;
flag_exit = 0;
printf("\n");
break;
case '\n':
flag_enter = 1;
flag_exit = 0;
printf("\n");
break;
default:
printf("%c", contents[i]);
if(i + 1 < length){
i++;
} else {
flag_exit = 0;
flag_enter = 1;
}
break;
}
}
}
if(contents[i] == '\n'){
flag_enter = 1;
}
}
flag = flag_enter;
}