Добавлено ДЗ5
This commit is contained in:
15
05_Environmental/CMakeLists.txt
Normal file
15
05_Environmental/CMakeLists.txt
Normal file
@@ -0,0 +1,15 @@
|
||||
cmake_minimum_required(VERSION 3.10)
|
||||
|
||||
project(Environmental)
|
||||
|
||||
find_package(Curses REQUIRED)
|
||||
include_directories(${CURSES_INCLUDE_DIRS} )
|
||||
#set(PcreLibs "${PcreLibDir}${PcreCppLib}")
|
||||
|
||||
#set(PcreIncludeDirs "${PcreSrcDir}" "${PcreLibDir}")
|
||||
|
||||
|
||||
add_executable(main main.c)
|
||||
target_link_libraries( main ${CURSES_LIBRARIES} )
|
||||
target_link_libraries( main pcre2-8)
|
||||
|
||||
18
05_Environmental/Readme.md
Normal file
18
05_Environmental/Readme.md
Normal file
@@ -0,0 +1,18 @@
|
||||
Для сборки без UTF поддержки:
|
||||
```
|
||||
mkdir build
|
||||
cd build
|
||||
cmake .. && make
|
||||
```
|
||||
|
||||
Для сборки с UTF поддержкой:
|
||||
```
|
||||
mkdir build
|
||||
cd build
|
||||
cmake .. -DCMAKE_CXX_FLAGS="$(CMAKE_CXX_FLAGS) -DPCRE2_UCP=1" && make
|
||||
```
|
||||
|
||||
Также работает
|
||||
```
|
||||
make clean
|
||||
```
|
||||
106
05_Environmental/main.c
Normal file
106
05_Environmental/main.c
Normal file
@@ -0,0 +1,106 @@
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <ncurses.h>
|
||||
|
||||
#ifndef PCRE2_CODE_UNIT_WIDTH
|
||||
#define PCRE2_CODE_UNIT_WIDTH 8
|
||||
#endif
|
||||
#include <pcre2.h>
|
||||
|
||||
#define DX 3
|
||||
#define ONLELINE 3
|
||||
#define MAXSTR 80
|
||||
|
||||
void main() {
|
||||
pcre2_code* re;
|
||||
PCRE2_SPTR pattern; /* PCRE2_SPTR is a pointer to unsigned code units of */
|
||||
PCRE2_SPTR subject; /* the appropriate width (in this case, 8 bits). */
|
||||
|
||||
int errnum;
|
||||
int i, rc;
|
||||
|
||||
PCRE2_SIZE erroffs;
|
||||
PCRE2_SIZE* ovector;
|
||||
PCRE2_SIZE subject_length;
|
||||
|
||||
pcre2_match_data* match_data;
|
||||
|
||||
WINDOW *winA, *winB, *winO;
|
||||
char inA[MAXSTR], inB[MAXSTR];
|
||||
int c = 0;
|
||||
int half;
|
||||
|
||||
initscr();
|
||||
cbreak();
|
||||
printw("Input:");
|
||||
refresh();
|
||||
|
||||
half = (COLS - 2 * DX) / 2;
|
||||
winA = newwin(ONLELINE, half, DX, DX);
|
||||
winB = newwin(ONLELINE, half, DX, DX + half);
|
||||
winO = newwin(LINES - ONLELINE - DX * 2, (COLS - 2 * DX), DX + ONLELINE, DX);
|
||||
keypad(winA, TRUE);
|
||||
keypad(winB, TRUE);
|
||||
scrollok(winO, TRUE);
|
||||
wmove(winO, 1, 0);
|
||||
do {
|
||||
werase(winA);
|
||||
box(winA, 0, 0);
|
||||
mvwgetnstr(winA, 1, 1, inA, MAXSTR);
|
||||
werase(winB);
|
||||
box(winB, 0, 0);
|
||||
mvwgetnstr(winB, 1, 1, inB, MAXSTR);
|
||||
|
||||
pattern = (PCRE2_SPTR)inA;
|
||||
subject = (PCRE2_SPTR)inB;
|
||||
subject_length = (PCRE2_SIZE)strlen((char*)subject);
|
||||
|
||||
re = pcre2_compile(pattern, PCRE2_ZERO_TERMINATED, PCRE2_UCP, &errnum,
|
||||
&erroffs, NULL);
|
||||
|
||||
if (re == NULL) {
|
||||
PCRE2_UCHAR buffer[256];
|
||||
pcre2_get_error_message(errnum, buffer, sizeof(buffer));
|
||||
wprintw(winO, " PCRE2 compilation failed at offset %d: %s\n",
|
||||
(int)erroffs, buffer);
|
||||
box(winO, 0, 0);
|
||||
wrefresh(winO);
|
||||
continue;
|
||||
}
|
||||
|
||||
match_data = pcre2_match_data_create_from_pattern(re, NULL);
|
||||
|
||||
rc = pcre2_match(re, subject, subject_length, 0, 0, match_data, NULL);
|
||||
|
||||
if (rc < 0) {
|
||||
switch (rc) {
|
||||
case PCRE2_ERROR_NOMATCH:
|
||||
wprintw(winO, " No match\n");
|
||||
break;
|
||||
default:
|
||||
wprintw(winO, " Matching error %d\n", rc);
|
||||
break;
|
||||
}
|
||||
box(winO, 0, 0);
|
||||
wrefresh(winO);
|
||||
continue;
|
||||
}
|
||||
|
||||
ovector = pcre2_get_ovector_pointer(match_data);
|
||||
|
||||
for (i = 0; i < rc; i++) {
|
||||
wprintw(winO, " %2ld: %.*s\n", ovector[2 * i],
|
||||
(int)(ovector[2 * i + 1] - ovector[2 * i]),
|
||||
subject + ovector[2 * i]);
|
||||
}
|
||||
|
||||
box(winO, 0, 0);
|
||||
wrefresh(winO);
|
||||
} while (*inA);
|
||||
|
||||
endwin();
|
||||
|
||||
pcre2_match_data_free(match_data); /* Release the memory that was used */
|
||||
pcre2_code_free(re); /* for the match data and the pattern. */
|
||||
}
|
||||
Reference in New Issue
Block a user