hw5: Demo sources added
This commit is contained in:
72
05_Environmental/pcredemo.c
Normal file
72
05_Environmental/pcredemo.c
Normal file
@@ -0,0 +1,72 @@
|
||||
#ifndef PCRE2_CODE_UNIT_WIDTH
|
||||
#define PCRE2_CODE_UNIT_WIDTH 8
|
||||
#endif
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <pcre2.h>
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
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;
|
||||
|
||||
if (argc != 3) {
|
||||
printf("Exactly two arguments required: a regex and a subject string\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
pattern = (PCRE2_SPTR)argv[1];
|
||||
subject = (PCRE2_SPTR)argv[2];
|
||||
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));
|
||||
printf("PCRE2 compilation failed at offset %d: %s\n", (int)erroffs,
|
||||
buffer);
|
||||
return 1;
|
||||
}
|
||||
|
||||
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:
|
||||
printf("No match\n");
|
||||
break;
|
||||
default:
|
||||
printf("Matching error %d\n", rc);
|
||||
break;
|
||||
}
|
||||
pcre2_match_data_free(match_data); /* Release memory used for the match */
|
||||
pcre2_code_free(re); /* data and the compiled pattern. */
|
||||
return 1;
|
||||
}
|
||||
|
||||
ovector = pcre2_get_ovector_pointer(match_data);
|
||||
|
||||
for (i = 0; i < rc; i++)
|
||||
printf("%2ld: %.*s\n", ovector[2*i],
|
||||
(int)(ovector[2*i+1] - ovector[2*i]),
|
||||
subject + ovector[2*i]);
|
||||
|
||||
pcre2_match_data_free(match_data); /* Release the memory that was used */
|
||||
pcre2_code_free(re); /* for the match data and the pattern. */
|
||||
|
||||
return 0;
|
||||
}
|
||||
38
05_Environmental/window.c
Normal file
38
05_Environmental/window.c
Normal file
@@ -0,0 +1,38 @@
|
||||
#include <ncurses.h>
|
||||
|
||||
#define DX 3
|
||||
#define ONLELINE 3
|
||||
#define MAXSTR 80
|
||||
|
||||
void main() {
|
||||
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);
|
||||
wprintw(winO, " Entered: %s %s\n", inA, inB);
|
||||
box(winO, 0, 0);
|
||||
wrefresh(winO);
|
||||
} while(*inA);
|
||||
|
||||
endwin();
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user