Mon, 24 Apr 2023 20:54:38 +0200
upgrade to ucx 3.0
--- a/.hgignore Thu Nov 10 18:44:48 2016 +0100 +++ b/.hgignore Mon Apr 24 20:54:38 2023 +0200 @@ -5,3 +5,4 @@ \.conflict\~$ ^build/.*$ ^nbproject/.*$ +^.idea/.*$
--- a/Makefile Thu Nov 10 18:44:48 2016 +0100 +++ b/Makefile Mon Apr 24 20:54:38 2023 +0200 @@ -34,25 +34,17 @@ SRC = frontend.c SRC += c2html.c SRC += highlighter.c -SRC += ucx/allocator.c -SRC += ucx/buffer.c -SRC += ucx/list.c -SRC += ucx/string.c -SRC += ucx/utils.c OBJ = $(SRC:%.c=build/%$(OBJ_EXT)) -all: build/ucx build/$(BIN) +all: build build/$(BIN) build/$(BIN): $(OBJ) $(LD) -o $@ $^ $(LDFLAGS) build/%$(OBJ_EXT): src/%.c $(CC) -o $@ $(CFLAGS) -c $< - -build/ucx: build - $(MKDIR) $@ - + build: $(MKDIR) $@ @@ -65,12 +57,10 @@ -H test/header.html -F test/footer.html ./build/$(BIN) -p test/plain.csp -o build/plain.html \ -H test/header.html -F test/footer.html - -test-v2-regression: test - diff build/ctest.html test/v2-regression/ctest.html && \ - diff build/javatest.html test/v2-regression/javatest.html && \ - diff build/bigtest.html test/v2-regression/bigtest.html && \ - diff build/plain.html test/v2-regression/plain.html + diff build/ctest.html test/gs/ctest.html && \ + diff build/javatest.html test/gs/javatest.html && \ + diff build/bigtest.html test/gs/bigtest.html && \ + diff build/plain.html test/gs/plain.html clean: $(RM) $(RMFLAGS) build
--- a/clang.mk Thu Nov 10 18:44:48 2016 +0100 +++ b/clang.mk Mon Apr 24 20:54:38 2023 +0200 @@ -36,5 +36,5 @@ CC = clang CFLAGS = -O2 LD = clang -LDFLAGS = +LDFLAGS = -lucx OBJ_EXT = .o
--- a/gcc.mk Thu Nov 10 18:44:48 2016 +0100 +++ b/gcc.mk Mon Apr 24 20:54:38 2023 +0200 @@ -36,5 +36,5 @@ CC = gcc CFLAGS = -O2 -std=gnu99 LD = gcc -LDFLAGS = +LDFLAGS = -lucx OBJ_EXT = .o
--- a/src/c2html.c Thu Nov 10 18:44:48 2016 +0100 +++ b/src/c2html.c Mon Apr 24 20:54:38 2023 +0200 @@ -29,131 +29,93 @@ #include "c2html.h" -#include "ucx/list.h" -#include "ucx/utils.h" +#include <cx/array_list.h> +#include <cx/printf.h> -#define try_write(wfnc, str, n, buf, written, maxlen) \ - { \ - size_t m = maxlen-written; \ - written += wfnc(str, 1, n > m ? m : n, buf); \ +size_t c2html_format( + CxList const *lines, + void *outbuf, + cx_write_func wfnc, + c2html_highlighter_func highlighter, + int showln +) { + /* total written bytes */ + size_t written = 0; + + /* compute width of line numbering */ + int lnw = 0; + if (showln) { + size_t no_lines = cxListSize(lines); + for (size_t p = 1; p < no_lines; p *= 10) lnw++; } -static size_t formatlines(c2html_highlighter_func highlighter, UcxList *in, - void *outbuf, write_func wfnc, size_t maxlen, int showlineno) { - /* total written bytes */ - size_t written = 0; - - /* compute width of line numbering */ - int lnw = 0; - if (showlineno) { - size_t lines = ucx_list_size(in); - for (size_t p = 1; p < lines ; p*=10) lnw++; - } - - /* start monospace formatting */ - try_write(wfnc, "<pre>\n", 6, outbuf, written, maxlen); + /* start code formatting */ + written += wfnc("<div class=\"c2html-code\">\n", 1, 26, outbuf); /* process lines */ - size_t lineno = 0; - c2html_highlighter_data* hd = malloc(sizeof(c2html_highlighter_data)); - hd->multiline_comment = 0; - hd->primary_buffer = ucx_buffer_new(NULL, 256, UCX_BUFFER_AUTOEXTEND); - hd->secondary_buffer = ucx_buffer_new(NULL, 32, UCX_BUFFER_AUTOEXTEND); - UcxBuffer *line = ucx_buffer_new(NULL, 1024, UCX_BUFFER_AUTOEXTEND); - - UCX_FOREACH(sourceline, in) { + int lineno = 0; + c2html_highlighter_data hd; + hd.multiline_comment = 0; + cxBufferInit(&hd.primary_buffer, NULL, 256, NULL, CX_BUFFER_AUTO_EXTEND); + cxBufferInit(&hd.secondary_buffer, NULL, 32, NULL, CX_BUFFER_AUTO_EXTEND); + CxBuffer out_line; + cxBufferInit(&out_line, NULL, 128, NULL, CX_BUFFER_AUTO_EXTEND); + + CxIterator in_lines = cxListIterator(lines); + cx_foreach(char*, in_line, in_lines) { /* increase line number and clean line buffer */ lineno++; - ucx_buffer_clear(line); - + cxBufferClear(&out_line); + /* write line number */ - if (showlineno) { - ucx_bprintf(line, "<a class=\"c2html-lineno\" name=\"l%d\" " - "href=\"#l%d\">%*d </a>", - lineno, lineno, lnw, lineno); + if (showln) { + cx_bprintf(&out_line, "<a class=\"c2html-lineno\" name=\"l%d\" " + "href=\"#l%d\">%*d </a>", + lineno, lineno, lnw, lineno); } - + /* process code line */ - highlighter(sourceline->data, line, hd); - + highlighter(in_line, &out_line, &hd); + /* write code line */ - try_write(wfnc, line->space, line->size, outbuf, written, maxlen); - - if (written == maxlen) break; + written += wfnc(out_line.space, 1, out_line.size, outbuf); } - - /* end monospace formatting */ - try_write(wfnc, "</pre>\n", 7, outbuf, written, maxlen); - + + /* end code formatting */ + written += wfnc("</div>\n", 1, 7, outbuf); + /* cleanup and return */ - ucx_buffer_free(hd->primary_buffer); - ucx_buffer_free(hd->secondary_buffer); - free(hd); - ucx_buffer_free(line); - + cxBufferDestroy(&hd.primary_buffer); + cxBufferDestroy(&hd.secondary_buffer); + cxBufferDestroy(&out_line); + return written; } -size_t c2html_formatn(void* inputbuffer, read_func rfnc, - char* ibuf, size_t ibuflen, void* outputbuffer, write_func wfnc, - size_t maxlen, c2html_highlighter_func hltr, int showln) { - - UcxBuffer *content = ucx_buffer_new(NULL, ibuflen*2, UCX_BUFFER_AUTOEXTEND); - ucx_stream_copy(inputbuffer, content, rfnc, (write_func) ucx_buffer_write, - ibuf, ibuflen, (size_t)-1); - - size_t n = c2html_bformatn(content->space, content->size, - outputbuffer, wfnc, maxlen, hltr, showln); - - ucx_buffer_free(content); - return n; -} +size_t c2html_bformat( + char const *inputbuffer, + size_t inputbuflen, + void *outbuf, + cx_write_func wfnc, + c2html_highlighter_func highlighter, + int showln +) { + /* a rough estimate for the number of lines */ + size_t est_cap = 16 + inputbuflen / 40; -size_t c2html_format(void* inputbuffer, read_func rfnc, - char* ibuf, size_t ibuflen, void* outputbuffer, write_func wfnc, - c2html_highlighter_func hltr, int showln) { - return c2html_formatn(inputbuffer, rfnc, ibuf, ibuflen, - outputbuffer, wfnc, (size_t)-1, hltr, showln); -} - -size_t c2html_fformat(FILE* inputfile, char *ibuf, size_t ibuflen, - void* outputbuffer, write_func wfnc, - c2html_highlighter_func hltr, int showln) { - return c2html_format(inputfile, (read_func) fread, ibuf, ibuflen, - outputbuffer, wfnc, hltr, showln); -} - -void c2html_fformatf(FILE *inputfile, char *ibuf, size_t ibuflen, - FILE* outputfile, c2html_highlighter_func hltr, int showln) { - c2html_format(inputfile, (read_func) fread, ibuf, ibuflen, - outputfile, (write_func) fwrite, hltr, showln); -} - -size_t c2html_bformatn(const char* inputbuffer, size_t inputbuflen, - void* outputbuffer, write_func wfnc, - size_t maxlen, c2html_highlighter_func hltr, int showln) { - UcxList *lines = ucx_list_append(NULL, (char*)inputbuffer); - for (size_t i = 1 ; i < inputbuflen ; i++) { - if (inputbuffer[i] == '\n' && i+1 < inputbuflen) { - ucx_list_append(lines, (char*)inputbuffer+i+1); + /* create the line pointer array */ + CxList *lines = cxArrayListCreateSimple(CX_STORE_POINTERS, est_cap); + cxListAdd(lines, inputbuffer); + for (size_t i = 1; i < inputbuflen; i++) { + if (inputbuffer[i] == '\n' && i + 1 < inputbuflen) { + cxListAdd(lines, inputbuffer + i + 1); } } - - size_t n = formatlines(hltr, lines, outputbuffer, wfnc, maxlen, showln); - - ucx_list_free(lines); - return n; -} + + /* invoke the other function */ + size_t n = c2html_format(lines, outbuf, wfnc, highlighter, showln); -size_t c2html_bformat(const char* inputbuffer, size_t inputbuflen, - void* outputbuffer, write_func wfnc, - c2html_highlighter_func hltr, int showln) { - return c2html_bformatn(inputbuffer, inputbuflen, outputbuffer, wfnc, - (size_t)-1, hltr, showln); -} - -void c2html_bformatf(const char* inputbuffer, size_t inputbuflen, - FILE* outputfile, c2html_highlighter_func hltr, int showln) { - c2html_bformatn(inputbuffer, inputbuflen, outputfile, - (write_func) fwrite, (size_t)-1, hltr, showln); -} + /* cleanup and return */ + cxListDestroy(lines); + return n; +} \ No newline at end of file
--- a/src/c2html.h Thu Nov 10 18:44:48 2016 +0100 +++ b/src/c2html.h Mon Apr 24 20:54:38 2023 +0200 @@ -31,90 +31,23 @@ #define C2HTML_H #include <stdio.h> +#include <cx/list.h> #include "highlighter.h" #ifdef __cplusplus extern "C" { #endif -#define VERSION_MAJOR 2 +#define VERSION_MAJOR 3 #define VERSION_MINOR 0 #define VERSION_DEVELOP 0 /* set this to zero for release version */ /** - * Reads a source file from the input buffer and writes at most - * <code>maxlen</code> bytes of the formatted output to the output buffer. - * - * The output buffer must either be large enough to hold <code>maxlen</code> - * bytes or the write function must trigger an automatic extension of the - * buffer. - * - * The input is copied via an intermediate buffer to an internal buffer. - * - * @param inputbuffer the input buffer - * @param rfnc a read function operating for the input buffer - * @param ibuf intermediate processing buffer - * @param ibuflen length of intermediate processing buffer - * @param outputbuffer the output buffer - * @param wfnc a write function for the output buffer - * @param maxlen the maximum amount bytes which will be written to the - * output buffer - * @param hltr the highlighter function - * @param showln zero, if line numbers shall be omitted, nonzero otherwise - * - * @return total amount of bytes written to the output buffer - * - * @see c2html_plain_highlighter() - * @see c2html_c_highlighter() - * @see c2html_java_highlighter() - */ -size_t c2html_formatn(void* inputbuffer, read_func rfnc, - char* ibuf, size_t ibuflen, void* outputbuffer, write_func wfnc, - size_t maxlen, c2html_highlighter_func hltr, int showln); - -/** - * Reads a source file from the input buffer and writes the formatted output - * to an output buffer. - * - * The output buffer must either be large enough to hold the formatted data - * or the write function must trigger an automatic extension of the buffer. + * Writes the formatted source data to the output buffer. * - * The input is copied via an intermediate buffer to an internal buffer. - * - * @param inputbuffer the input buffer - * @param rfnc a read function operating for the input buffer - * @param ibuf intermediate processing buffer - * @param ibuflen length of intermediate processing buffer - * @param outputbuffer the output buffer - * @param wfnc a write function for the output buffer - * @param hltr the highlighter function - * @param showln zero, if line numbers shall be omitted, nonzero otherwise - * - * @return total amount of bytes written to the output buffer - * - * @see c2html_plain_highlighter() - * @see c2html_c_highlighter() - * @see c2html_java_highlighter() - */ -size_t c2html_format(void* inputbuffer, read_func rfnc, - char* ibuf, size_t ibuflen, void* outputbuffer, write_func wfnc, - c2html_highlighter_func hltr, int showln); - -/** - * Reads a source file from the specified input file stream and writes the - * formatted output to an output buffer. - * - * The output buffer must either be large enough to hold the formatted data - * or the write function must trigger an automatic extension of the buffer. - * - * The input is copied via an intermediate buffer to an internal buffer. - * For files, the recommended intermediate buffer length is the file system - * block size. - * - * @param inputfile the input file stream - * @param ibuf intermediate processing buffer - * @param ibuflen length of intermediate processing buffer - * @param outputbuffer the output buffer + * @param inputbuffer the source file data as string + * @param inputbuflen the length of the source file + * @param outbuf the output buffer * @param wfnc a write function for the output buffer * @param hltr the highlighter function * @param showln zero, if line numbers shall be omitted, nonzero otherwise @@ -125,92 +58,32 @@ * @see c2html_c_highlighter() * @see c2html_java_highlighter() */ -size_t c2html_fformat(FILE* inputfile, char *ibuf, size_t ibuflen, - void* outputbuffer, write_func wfnc, - c2html_highlighter_func hltr, int showln); +size_t c2html_bformat(char const* inputbuffer, size_t inputbuflen, + void* outbuf, cx_write_func wfnc, + c2html_highlighter_func hltr, int showln); /** - * Reads a source file from the specified input file stream and directly writes - * the formatted output to the output file stream. - * - * For files, the recommended intermediate buffer length is the file system - * block size. - * - * @param inputfile the input file stream - * @param ibuf intermediate processing buffer - * @param ibuflen length of intermediate processing buffer (recommended: 4 KB) - * @param outputfile the output file stream + * Writes the formatted source data to the output buffer. + * + * This function takes a list of \c char* that point to the beginning of each + * line. These pointers may point directly into the source text and the strings + * do not need to be zero-terminated, but the line-breaks must be included. + * + * @param lines a list of pointers to the beginning of each line + * @param outbuf the output buffer + * @param wfnc a write function for the output buffer * @param hltr the highlighter function * @param showln zero, if line numbers shall be omitted, nonzero otherwise - * + * + * @return total amount of bytes written to the output buffer + * * @see c2html_plain_highlighter() * @see c2html_c_highlighter() * @see c2html_java_highlighter() */ -void c2html_fformatf(FILE *inputfile, char *ibuf, size_t ibuflen, - FILE* outputfile, c2html_highlighter_func hltr, int showln); - - -/** - * Writes at most <code>maxlen</code> bytes of formatted source data to the - * output buffer. - * - * @param inputbuffer the source file data as string - * @param inputbuflen the length of the source file - * @param outputbuffer the output buffer - * @param wfnc a write function for the output buffer - * @param maxlen the maximum amount bytes which will be written to the - * output buffer - * @param hltr the highlighter function - * @param showln zero, if line numbers shall be omitted, nonzero otherwise - * - * @return total amount of bytes written to the output buffer - * - * @see c2html_plain_highlighter() - * @see c2html_c_highlighter() - * @see c2html_java_highlighter() - */ -size_t c2html_bformatn(const char* inputbuffer, size_t inputbuflen, - void* outputbuffer, write_func wfnc, - size_t maxlen, c2html_highlighter_func hltr, int showln); - - -/** - * Writes the formatted source data to the output buffer. - * - * @param inputbuffer the source file data as string - * @param inputbuflen the length of the source file - * @param outputbuffer the output buffer - * @param wfnc a write function for the output buffer - * @param hltr the highlighter function - * @param showln zero, if line numbers shall be omitted, nonzero otherwise - * - * @return total amount of bytes written to the output buffer - * - * @see c2html_plain_highlighter() - * @see c2html_c_highlighter() - * @see c2html_java_highlighter() - */ -size_t c2html_bformat(const char* inputbuffer, size_t inputbuflen, - void* outputbuffer, write_func wfnc, - c2html_highlighter_func hltr, int showln); - - -/** - * Writes the formatted source data directly to the specified file stream. - * - * @param inputbuffer the source file data as string - * @param inputbuflen the length of the source file - * @param outputfile the output file stream - * @param hltr the highlighter function - * @param showln zero, if line numbers shall be omitted, nonzero otherwise - * - * @see c2html_plain_highlighter() - * @see c2html_c_highlighter() - * @see c2html_java_highlighter() - */ -void c2html_bformatf(const char* inputbuffer, size_t inputbuflen, - FILE* outputfile, c2html_highlighter_func hltr, int showln); +size_t c2html_format(CxList const* lines, + void* outbuf, cx_write_func wfnc, + c2html_highlighter_func hltr, int showln); #ifdef __cplusplus }
--- a/src/frontend.c Thu Nov 10 18:44:48 2016 +0100 +++ b/src/frontend.c Mon Apr 24 20:54:38 2023 +0200 @@ -33,9 +33,7 @@ #include <string.h> #include "c2html.h" -#include "ucx/utils.h" - -#define FILEBUF_SIZE 4096 +#include <cx/utils.h> typedef struct { char* outfilename; @@ -45,20 +43,17 @@ int showlinenumbers; } Settings; -static int appendfile(const char *filename, FILE *fout, - char *copybuf, size_t copybuflen, const char *errmsg) { - FILE *headerfile = fopen(filename, "r"); - if (!headerfile) { +static int appendfile(const char *filename, FILE *fout, const char *errmsg) { + FILE *fin = fopen(filename, "r"); + if (!fin) { perror(errmsg); if (fout != stdout) { fclose(fout); } return 1; } - ucx_stream_copy(headerfile, fout, - (read_func) fread, (write_func) fwrite, - copybuf, copybuflen, (size_t)-1); - fclose(headerfile); + cx_stream_copy(fin, fout, (cx_read_func) fread, (cx_write_func) fwrite); + fclose(fin); return 0; } @@ -86,7 +81,7 @@ c2html_highlighter_func hltr = c2html_c_highlighter; /* Parse command line */ - char optc; + int optc; while ((optc = getopt(argc, argv, "hljo:pH:F:vV")) != -1) { switch (optc) { case 'o': @@ -142,15 +137,8 @@ fout = stdout; } - /* Allocate file buffer */ - char *filebuf = malloc(FILEBUF_SIZE); - if (!filebuf) { - perror("Error allocating file buffer"); - return EXIT_FAILURE; - } - /* Prepend header file */ - if (appendfile(settings.headerfile, fout, filebuf, FILEBUF_SIZE, + if (appendfile(settings.headerfile, fout, "Error opening header file")) { return EXIT_FAILURE; } @@ -158,11 +146,17 @@ /* Process input file */ FILE *inputfile = fopen(settings.infilename, "r"); if (inputfile) { - c2html_fformatf( - inputfile, filebuf, FILEBUF_SIZE, - fout, hltr, settings.showlinenumbers + CxBuffer fbuf; + cxBufferInit(&fbuf, NULL, 4096, NULL, CX_BUFFER_AUTO_EXTEND); + cx_stream_copy(inputfile, &fbuf, (cx_read_func) fread, + (cx_write_func) cxBufferWrite); + fclose(inputfile); + c2html_bformat( + fbuf.space, fbuf.size, + fout, (cx_write_func ) fwrite, hltr, + settings.showlinenumbers ); - fclose(inputfile); + cxBufferDestroy(&fbuf); } else { perror("Error opening input file"); if (fout != stdout) { @@ -172,12 +166,10 @@ } /* Append footer file */ - if (appendfile(settings.footerfile, fout, filebuf, FILEBUF_SIZE, + if (appendfile(settings.footerfile, fout, "Error opening footer file")) { return EXIT_FAILURE; } - - free(filebuf); return EXIT_SUCCESS; }
--- a/src/highlighter.c Thu Nov 10 18:44:48 2016 +0100 +++ b/src/highlighter.c Mon Apr 24 20:54:38 2023 +0200 @@ -33,35 +33,35 @@ #include <stdio.h> #include <string.h> #include <ctype.h> -#include "ucx/string.h" -#include "ucx/utils.h" -static void put_htmlescaped(UcxBuffer *dest, char c) { +#include <cx/string.h> + +static void put_htmlescaped(CxBuffer *dest, char c) { if (c == '>') { - ucx_buffer_puts(dest, ">"); + cxBufferPutString(dest, ">"); } else if (c == '<') { - ucx_buffer_puts(dest, "<"); + cxBufferPutString(dest, "<"); } else if (c) { - ucx_buffer_putc(dest, c); + cxBufferPut(dest, c); } } -static void put_htmlescapedstr(UcxBuffer *dest, sstr_t s) { +static void put_htmlescapedstr(CxBuffer *dest, cxstring s) { for (int i = 0 ; i < s.length ; i++) { put_htmlescaped(dest, s.ptr[i]); } } -static int check_keyword(sstr_t word, const char** keywords) { +static int check_keyword(cxstring word, const char** keywords) { for (int i = 0 ; keywords[i] ; i++) { - if (sstrcmp(word, sstr((char*)keywords[i])) == 0) { + if (cx_strcmp(word, cx_str(keywords[i])) == 0) { return 1; } } return 0; } -static int check_capsonly(sstr_t word) { +static int check_capsonly(cxstring word) { for (size_t i = 0 ; i < word.length ; i++) { if (!isupper(word.ptr[i]) && !isdigit(word.ptr[i]) && word.ptr[i] != '_') { @@ -73,7 +73,7 @@ /* Plaintext Highlighter */ -void c2html_plain_highlighter(char *src, UcxBuffer *dest, +void c2html_plain_highlighter(char const *src, CxBuffer *dest, c2html_highlighter_data *hd) { while (*src && *src != '\n') { if (*src != '\r') { @@ -81,7 +81,7 @@ } src++; } - ucx_buffer_putc(dest, '\n'); + cxBufferPut(dest, '\n'); } /* C Highlighter */ @@ -94,15 +94,15 @@ "while", NULL }; -void c2html_c_highlighter(char *src, UcxBuffer *dest, +void c2html_c_highlighter(char const *src, CxBuffer *dest, c2html_highlighter_data *hd) { /* reset buffers without clearing them */ - hd->primary_buffer->size = hd->primary_buffer->pos = 0; - hd->secondary_buffer->size = hd->secondary_buffer->pos = 0; + hd->primary_buffer.size = hd->primary_buffer.pos = 0; + hd->secondary_buffer.size = hd->secondary_buffer.pos = 0; /* alias the buffers for better handling */ - UcxBuffer *wbuf = hd->primary_buffer; - UcxBuffer *ifilebuf = hd->secondary_buffer; + CxBuffer *wbuf = &hd->primary_buffer; + CxBuffer *ifilebuf = &hd->secondary_buffer; /* local information */ size_t sp = (size_t)-1; @@ -113,7 +113,7 @@ /* continue a multi line comment highlighting */ if (hd->multiline_comment) { iscomment = 1; - ucx_buffer_puts(dest, "<span class=\"c2html-comment\">"); + cxBufferPutString(dest, "<span class=\"c2html-comment\">"); } char c; @@ -126,42 +126,42 @@ if (hd->multiline_comment && sp > 0 && src[sp-1] == '*') { iscomment = 0; hd->multiline_comment = 0; - ucx_buffer_puts(dest, "/</span>"); + cxBufferPutString(dest, "/</span>"); continue; } else if (!iscomment && (src[sp+1] == '/' || src[sp+1] == '*')) { iscomment = 1; hd->multiline_comment = (src[sp+1] == '*'); - ucx_buffer_puts(dest, "<span class=\"c2html-comment\">"); + cxBufferPutString(dest, "<span class=\"c2html-comment\">"); } } if (iscomment) { if (c == '\n') { - ucx_buffer_puts(dest, "</span>\n"); + cxBufferPutString(dest, "</span>\n"); } else { put_htmlescaped(dest, c); } } else if (isinclude) { if (c == '<') { - ucx_buffer_puts(dest, + cxBufferPutString(dest, "<span class=\"c2html-stdinclude\"><"); } else if (c == '\"') { if (parseinclude) { - ucx_buffer_puts(dest, "\">"); - ucx_buffer_write(ifilebuf->space, 1, ifilebuf->size, dest); - ucx_buffer_puts(dest, "\"</a>"); + cxBufferPutString(dest, "\">"); + cxBufferWrite(ifilebuf->space, 1, ifilebuf->size, dest); + cxBufferPutString(dest, "\"</a>"); parseinclude = 0; } else { - ucx_buffer_puts(dest, + cxBufferPutString(dest, "<a class=\"c2html-userinclude\" href=\""); - ucx_buffer_putc(ifilebuf, '\"'); + cxBufferPut(ifilebuf, '\"'); parseinclude = 1; } } else if (c == '>') { - ucx_buffer_puts(dest, "></span>"); + cxBufferPutString(dest, "></span>"); } else { if (parseinclude) { - ucx_buffer_putc(ifilebuf, c); + cxBufferPut(ifilebuf, c); } put_htmlescaped(dest, c); } @@ -172,14 +172,14 @@ put_htmlescaped(dest, c); if (c == quote) { isstring = 0; - ucx_buffer_puts(dest, "</span>"); + cxBufferPutString(dest, "</span>"); } else { put_htmlescaped(dest, c); } } else { isstring = 1; quote = c; - ucx_buffer_puts(dest, "<span class=\"c2html-string\">"); + cxBufferPutString(dest, "<span class=\"c2html-string\">"); put_htmlescaped(dest, c); } } else { @@ -187,32 +187,32 @@ put_htmlescaped(dest, c); } else if (isalnum(c) || c == '_' || c == '#') { /* buffer the current word */ - ucx_buffer_putc(wbuf, c); + cxBufferPut(wbuf, c); } else { /* write buffered word, if any */ if (wbuf->size > 0) { - sstr_t word = sstrn(wbuf->space, wbuf->size); + cxstring word = cx_strn(wbuf->space, wbuf->size); int closespan = 1; - sstr_t typesuffix = ST("_t"); + cxstring typesuffix = CX_STR("_t"); if (check_keyword(word, ckeywords)) { - ucx_buffer_puts(dest, + cxBufferPutString(dest, "<span class=\"c2html-keyword\">"); - } else if (sstrsuffix(word, typesuffix)) { - ucx_buffer_puts(dest, + } else if (cx_strsuffix(word, typesuffix)) { + cxBufferPutString(dest, "<span class=\"c2html-type\">"); } else if (word.ptr[0] == '#') { - isinclude = !sstrcmp(word, S("#include")); - ucx_buffer_puts(dest, + isinclude = !cx_strcmp(word, CX_STR("#include")); + cxBufferPutString(dest, "<span class=\"c2html-directive\">"); } else if (check_capsonly(word)) { - ucx_buffer_puts(dest, + cxBufferPutString(dest, "<span class=\"c2html-macroconst\">"); } else { closespan = 0; } put_htmlescapedstr(dest, word); if (closespan) { - ucx_buffer_puts(dest, "</span>"); + cxBufferPutString(dest, "</span>"); } } wbuf->pos = wbuf->size = 0; /* reset word buffer */ @@ -239,14 +239,14 @@ "volatile", "const", "float", "native", "super", "while", NULL }; -void c2html_java_highlighter(char *src, UcxBuffer *dest, +void c2html_java_highlighter(char const *src, CxBuffer *dest, c2html_highlighter_data *hd) { /* reset buffers without clearing them */ - hd->primary_buffer->size = hd->primary_buffer->pos = 0; - hd->secondary_buffer->size = hd->secondary_buffer->pos = 0; + hd->primary_buffer.size = hd->primary_buffer.pos = 0; + hd->secondary_buffer.size = hd->secondary_buffer.pos = 0; /* alias the buffers for better handling */ - UcxBuffer *wbuf = hd->primary_buffer; + CxBuffer *wbuf = &hd->primary_buffer; /* local information */ size_t sp = (size_t)-1; @@ -256,7 +256,7 @@ if (hd->multiline_comment) { iscomment = 1; - ucx_buffer_puts(dest, "<span class=\"c2html-comment\">"); + cxBufferPutString(dest, "<span class=\"c2html-comment\">"); } char c; @@ -269,18 +269,18 @@ if (hd->multiline_comment && sp > 0 && src[sp-1] == '*') { iscomment = 0; hd->multiline_comment = 0; - ucx_buffer_puts(dest, "/</span>"); + cxBufferPutString(dest, "/</span>"); continue; } else if (!iscomment && (src[sp+1] == '/' || src[sp+1] == '*')) { iscomment = 1; hd->multiline_comment = (src[sp+1] == '*'); - ucx_buffer_puts(dest, "<span class=\"c2html-comment\">"); + cxBufferPutString(dest, "<span class=\"c2html-comment\">"); } } if (iscomment) { if (c == '\n') { - ucx_buffer_puts(dest, "</span>\n"); + cxBufferPutString(dest, "</span>\n"); } else { put_htmlescaped(dest, c); } @@ -293,14 +293,14 @@ put_htmlescaped(dest, c); if (c == quote) { isstring = 0; - ucx_buffer_puts(dest, "</span>"); + cxBufferPutString(dest, "</span>"); } else { put_htmlescaped(dest, c); } } else { isstring = 1; quote = c; - ucx_buffer_puts(dest, + cxBufferPutString(dest, "<span class=\"c2html-string\">"); put_htmlescaped(dest, c); } @@ -309,23 +309,23 @@ put_htmlescaped(dest, c); } else if (isalnum(c) || c == '_' || c == '@') { /* buffer the current word */ - ucx_buffer_putc(wbuf, c); + cxBufferPut(wbuf, c); } else { /* write buffered word, if any */ if (wbuf->size > 0) { - sstr_t word = sstrn(wbuf->space, wbuf->size); + cxstring word = cx_strn(wbuf->space, wbuf->size); int closespan = 1; if (check_keyword(word, jkeywords)) { - ucx_buffer_puts(dest, + cxBufferPutString(dest, "<span class=\"c2html-keyword\">"); } else if (isupper(word.ptr[0])) { - ucx_buffer_puts(dest, + cxBufferPutString(dest, "<span class=\"c2html-type\">"); } else if (word.ptr[0] == '@') { - ucx_buffer_puts(dest, + cxBufferPutString(dest, "<span class=\"c2html-directive\">"); } else if (check_capsonly(word)) { - ucx_buffer_puts(dest, + cxBufferPutString(dest, "<span class=\"c2html-macroconst\">"); } else { closespan = 0; @@ -333,7 +333,7 @@ put_htmlescapedstr(dest, word); if (closespan) { - ucx_buffer_puts(dest, "</span>"); + cxBufferPutString(dest, "</span>"); } } wbuf->pos = wbuf->size = 0; /* reset buffer */
--- a/src/highlighter.h Thu Nov 10 18:44:48 2016 +0100 +++ b/src/highlighter.h Mon Apr 24 20:54:38 2023 +0200 @@ -30,7 +30,7 @@ #ifndef C2HTML_HIGHLIGHTER_H #define C2HTML_HIGHLIGHTER_H -#include "ucx/buffer.h" +#include <cx/buffer.h> #ifdef __cplusplus extern "C" { @@ -38,12 +38,12 @@ typedef struct { int multiline_comment; - UcxBuffer* primary_buffer; - UcxBuffer* secondary_buffer; + CxBuffer primary_buffer; + CxBuffer secondary_buffer; } c2html_highlighter_data; #define C2HTML_HIGHLIGHTER_SIGNATURE \ -char*,UcxBuffer*, c2html_highlighter_data* +char const*, CxBuffer*, c2html_highlighter_data* typedef void(*c2html_highlighter_func)(C2HTML_HIGHLIGHTER_SIGNATURE);
--- a/src/ucx/allocator.c Thu Nov 10 18:44:48 2016 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,59 +0,0 @@ -/* - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. - * - * Copyright 2015 Olaf Wintermann. All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ - -#include <stdlib.h> -#include "allocator.h" - -UcxAllocator default_allocator = { - NULL, - ucx_default_malloc, - ucx_default_calloc, - ucx_default_realloc, - ucx_default_free -}; - -UcxAllocator *ucx_default_allocator() { - UcxAllocator *allocator = &default_allocator; - return allocator; -} - -void *ucx_default_malloc(void *ignore, size_t n) { - return malloc(n); -} - -void *ucx_default_calloc(void *ignore, size_t n, size_t size) { - return calloc(n, size); -} - -void *ucx_default_realloc(void *ignore, void *data, size_t n) { - return realloc(data, n); -} - -void ucx_default_free(void *ignore, void *data) { - free(data); -}
--- a/src/ucx/allocator.h Thu Nov 10 18:44:48 2016 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,206 +0,0 @@ -/* - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. - * - * Copyright 2015 Olaf Wintermann. All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ -/** - * Allocator for custom memory management. - * - * An UCX allocator consists of a pointer to the memory area / pool and four - * function pointers to memory management functions operating on this memory - * area / pool. These functions shall behave equivalent to the standard libc - * functions <code>malloc(), calloc(), realloc()</code> and <code>free()</code>. - * - * The signature of the memory management functions is based on the signature - * of the respective libc function but each of them takes the pointer to the - * memory area / pool as first argument. - * - * As the pointer to the memory area / pool can be arbitrarily chosen, any data - * can be provided to the memory management functions. An UcxMempool is just - * one example. - * - * @see mempool.h - * @see UcxMap - * - * @file allocator.h - * @author Mike Becker - * @author Olaf Wintermann - */ - -#ifndef UCX_ALLOCATOR_H -#define UCX_ALLOCATOR_H - -#include "ucx.h" - -#ifdef __cplusplus -extern "C" { -#endif - -/** - * A function pointer to the allocators <code>malloc()</code> function. - * @see UcxAllocator - */ -typedef void*(*ucx_allocator_malloc)(void *pool, size_t n); - -/** - * A function pointer to the allocators <code>calloc()</code> function. - * @see UcxAllocator - */ -typedef void*(*ucx_allocator_calloc)(void *pool, size_t n, size_t size); - -/** - * A function pointer to the allocators <code>realloc()</code> function. - * @see UcxAllocator - */ -typedef void*(*ucx_allocator_realloc)(void *pool, void *data, size_t n); - -/** - * A function pointer to the allocators <code>free()</code> function. - * @see UcxAllocator - */ -typedef void(*ucx_allocator_free)(void *pool, void *data); - -/** - * UCX allocator data structure containing memory management functions. - */ -typedef struct { - /** Pointer to an area of memory or a complex memory pool. - * This pointer will be passed to any memory management function as first - * argument. - */ - void *pool; - /** - * The <code>malloc()</code> function for this allocator. - */ - ucx_allocator_malloc malloc; - /** - * The <code>calloc()</code> function for this allocator. - */ - ucx_allocator_calloc calloc; - /** - * The <code>realloc()</code> function for this allocator. - */ - ucx_allocator_realloc realloc; - /** - * The <code>free()</code> function for this allocator. - */ - ucx_allocator_free free; -} UcxAllocator; - -/** - * Returns a pointer to the default allocator. - * - * The default allocator contains wrappers to the standard libc memory - * management functions. Use this function to get a pointer to a globally - * available allocator. You may also define an own UcxAllocator by assigning - * #UCX_ALLOCATOR_DEFAULT to a variable and pass the address of this variable - * to any function that takes an UcxAllocator as argument. Note that using - * this function is the recommended way of passing a default allocator, thus - * it never runs out of scope. - * - * @return a pointer to the default allocator - * - * @see UCX_ALLOCATOR_DEFAULT - */ -UcxAllocator *ucx_default_allocator(); - -/** - * A wrapper for the standard libc <code>malloc()</code> function. - * @param ignore ignored (may be used by allocators for pooled memory) - * @param n argument passed to <code>malloc()</code> - * @return return value of <code>malloc()</code> - */ -void *ucx_default_malloc(void *ignore, size_t n); -/** - * A wrapper for the standard libc <code>calloc()</code> function. - * @param ignore ignored (may be used by allocators for pooled memory) - * @param n argument passed to <code>calloc()</code> - * @param size argument passed to <code>calloc()</code> - * @return return value of <code>calloc()</code> - */ -void *ucx_default_calloc(void *ignore, size_t n, size_t size); -/** - * A wrapper for the standard libc <code>realloc()</code> function. - * @param ignore ignored (may be used by allocators for pooled memory) - * @param data argumend passed to <code>realloc()</code> - * @param n argument passed to <code>realloc()</code> - * @return return value of <code>realloc()</code> - */ -void *ucx_default_realloc(void *ignore, void *data, size_t n); -/** - * A wrapper for the standard libc <code>free()</code> function. - * @param ignore ignored (may be used by allocators for pooled memory) - * @param data argument passed to <code>free()</code> - */ -void ucx_default_free(void *ignore, void *data); - -/** - * Shorthand for calling an allocators malloc function. - * @param allocator the allocator to use - * @param n size of space to allocate - * @return a pointer to the allocated memory area - */ -#define almalloc(allocator, n) ((allocator)->malloc((allocator)->pool, n)) - -/** - * Shorthand for calling an allocators calloc function. - * @param allocator the allocator to use - * @param n the count of elements the space should be allocated for - * @param size the size of each element - * @return a pointer to the allocated memory area - */ -#define alcalloc(allocator, n, size) \ - ((allocator)->calloc((allocator)->pool, n, size)) - -/** - * Shorthand for calling an allocators realloc function. - * @param allocator the allocator to use - * @param ptr the pointer to the memory area that shall be reallocated - * @param n the new size of the allocated memory area - * @return a pointer to the reallocated memory area - */ -#define alrealloc(allocator, ptr, n) \ - ((allocator)->realloc((allocator)->pool, ptr, n)) - -/** - * Shorthand for calling an allocators free function. - * @param allocator the allocator to use - * @param ptr the pointer to the memory area that shall be freed - */ -#define alfree(allocator, ptr) ((allocator)->free((allocator)->pool, ptr)) - -/** - * Convenient macro for a default allocator <code>struct</code> definition. - */ -#define UCX_ALLOCATOR_DEFAULT {NULL, \ - ucx_default_malloc, ucx_default_calloc, ucx_default_realloc, \ - ucx_default_free } - -#ifdef __cplusplus -} -#endif - -#endif /* UCX_ALLOCATOR_H */ -
--- a/src/ucx/buffer.c Thu Nov 10 18:44:48 2016 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,234 +0,0 @@ -/* - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. - * - * Copyright 2015 Olaf Wintermann. All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ - -#include "buffer.h" -#include <stdarg.h> -#include <stdlib.h> -#include <string.h> - -UcxBuffer *ucx_buffer_new(void *space, size_t capacity, int flags) { - UcxBuffer *buffer = (UcxBuffer*) malloc(sizeof(UcxBuffer)); - if (buffer) { - buffer->flags = flags; - if (!space) { - buffer->space = (char*)malloc(capacity); - if (!buffer->space) { - free(buffer); - return NULL; - } - memset(buffer->space, 0, capacity); - buffer->flags |= UCX_BUFFER_AUTOFREE; - } else { - buffer->space = (char*)space; - } - buffer->capacity = capacity; - buffer->size = 0; - - buffer->pos = 0; - } - - return buffer; -} - -void ucx_buffer_free(UcxBuffer *buffer) { - if ((buffer->flags & UCX_BUFFER_AUTOFREE) == UCX_BUFFER_AUTOFREE) { - free(buffer->space); - } - free(buffer); -} - -UcxBuffer* ucx_buffer_extract( - UcxBuffer *src, size_t start, size_t length, int flags) { - - if (src->size == 0 || length == 0 || start+length > src->capacity) { - return NULL; - } - - UcxBuffer *dst = (UcxBuffer*) malloc(sizeof(UcxBuffer)); - if (dst) { - dst->space = (char*)malloc(length); - if (!dst->space) { - free(dst); - return NULL; - } - dst->capacity = length; - dst->size = length; - dst->flags = flags | UCX_BUFFER_AUTOFREE; - dst->pos = 0; - memcpy(dst->space, src->space+start, length); - } - return dst; -} - -int ucx_buffer_seek(UcxBuffer *buffer, off_t offset, int whence) { - size_t npos; - switch (whence) { - case SEEK_CUR: - npos = buffer->pos; - break; - case SEEK_END: - npos = buffer->size; - break; - case SEEK_SET: - npos = 0; - break; - default: - return -1; - } - - size_t opos = npos; - npos += offset; - - if ((offset > 0 && npos < opos) || (offset < 0 && npos > opos)) { - return -1; - } - - if (npos >= buffer->size) { - return -1; - } else { - buffer->pos = npos; - return 0; - } - -} - -int ucx_buffer_eof(UcxBuffer *buffer) { - return buffer->pos >= buffer->size; -} - -int ucx_buffer_extend(UcxBuffer *buffer, size_t len) { - size_t newcap = buffer->capacity; - - if (buffer->capacity + len < buffer->capacity) { - return -1; - } - - while (buffer->capacity + len > newcap) { - newcap <<= 1; - if (newcap < buffer->capacity) { - return -1; - } - } - - char *newspace = (char*)realloc(buffer->space, newcap); - if (newspace) { - memset(newspace+buffer->size, 0, newcap-buffer->size); - buffer->space = newspace; - buffer->capacity = newcap; - } else { - return -1; - } - - return 0; -} - -size_t ucx_buffer_write(const void *ptr, size_t size, size_t nitems, - UcxBuffer *buffer) { - size_t len = size * nitems; - size_t required = buffer->pos + len; - if (buffer->pos > required) { - return 0; - } - - if (required > buffer->capacity) { - if ((buffer->flags & UCX_BUFFER_AUTOEXTEND) == UCX_BUFFER_AUTOEXTEND) { - if (ucx_buffer_extend(buffer, required - buffer->capacity)) { - return 0; - } - } else { - len = buffer->capacity - buffer->pos; - if (size > 1) { - len -= len%size; - } - } - } - - if (len == 0) { - return len; - } - - memcpy(buffer->space + buffer->pos, ptr, len); - buffer->pos += len; - if(buffer->pos > buffer->size) { - buffer->size = buffer->pos; - } - - return len / size; -} - -size_t ucx_buffer_read(void *ptr, size_t size, size_t nitems, - UcxBuffer *buffer) { - size_t len = size * nitems; - if (buffer->pos + len > buffer->size) { - len = buffer->size - buffer->pos; - if (size > 1) len -= len%size; - } - - if (len <= 0) { - return len; - } - - memcpy(ptr, buffer->space + buffer->pos, len); - buffer->pos += len; - - return len / size; -} - -int ucx_buffer_putc(UcxBuffer *buffer, int c) { - if(buffer->pos >= buffer->capacity) { - if ((buffer->flags & UCX_BUFFER_AUTOEXTEND) == UCX_BUFFER_AUTOEXTEND) { - if(ucx_buffer_extend(buffer, 1)) { - return EOF; - } - } else { - return EOF; - } - } - - c &= 0xFF; - buffer->space[buffer->pos] = (char) c; - buffer->pos++; - if(buffer->pos > buffer->size) { - buffer->size = buffer->pos; - } - return c; -} - -int ucx_buffer_getc(UcxBuffer *buffer) { - if (ucx_buffer_eof(buffer)) { - return EOF; - } else { - int c = buffer->space[buffer->pos]; - buffer->pos++; - return c; - } -} - -size_t ucx_buffer_puts(UcxBuffer *buffer, char *str) { - return ucx_buffer_write((const void*)str, 1, strlen(str), buffer); -}
--- a/src/ucx/buffer.h Thu Nov 10 18:44:48 2016 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,270 +0,0 @@ -/* - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. - * - * Copyright 2015 Olaf Wintermann. All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ - -/** - * @file buffer.h - * - * Advanced buffer implementation. - * - * Instances of UcxBuffer can be used to read from or to write to like one - * would do with a stream. This allows the use of ucx_stream_copy() to copy - * contents from one buffer to another. - * - * Some features for convenient use of the buffer - * can be enabled. See the documentation of the macro constants for more - * information. - * - * @author Mike Becker - * @author Olaf Wintermann - */ - -#ifndef UCX_BUFFER_H -#define UCX_BUFFER_H - -#include "ucx.h" -#include <sys/types.h> -#include <stdio.h> - -#ifdef __cplusplus -extern "C" { -#endif - -/** - * No buffer features enabled (all flags cleared). - */ -#define UCX_BUFFER_DEFAULT 0x00 - -/** - * If this flag is enabled, the buffer will automatically free its contents. - */ -#define UCX_BUFFER_AUTOFREE 0x01 - -/** - * If this flag is enabled, the buffer will automatically extends its capacity. - */ -#define UCX_BUFFER_AUTOEXTEND 0x02 - -/** UCX Buffer. */ -typedef struct { - /** A pointer to the buffer contents. */ - char *space; - /** Current position of the buffer. */ - size_t pos; - /** Current capacity (i.e. maximum size) of the buffer. */ - size_t capacity; - /** Current size of the buffer content. */ - size_t size; - /** - * Flag register for buffer features. - * @see #UCX_BUFFER_DEFAULT - * @see #UCX_BUFFER_AUTOFREE - * @see #UCX_BUFFER_AUTOEXTEND - */ - int flags; -} UcxBuffer; - -/** - * Creates a new buffer. - * - * <b>Note:</b> you may provide <code>NULL</code> as argument for - * <code>space</code>. Then this function will allocate the space and enforce - * the #UCX_BUFFER_AUTOFREE flag. - * - * @param space pointer to the memory area, or <code>NULL</code> to allocate - * new memory - * @param capacity the capacity of the buffer - * @param flags buffer features (see UcxBuffer.flags) - * @return the new buffer - */ -UcxBuffer *ucx_buffer_new(void *space, size_t capacity, int flags); - -/** - * Destroys a buffer. - * - * If the #UCX_BUFFER_AUTOFREE feature is enabled, the contents of the buffer - * are also freed. - * - * @param buffer the buffer to destroy - */ -void ucx_buffer_free(UcxBuffer* buffer); - -/** - * Creates a new buffer and fills it with extracted content from another buffer. - * - * <b>Note:</b> the #UCX_BUFFER_AUTOFREE feature is enforced for the new buffer. - * - * @param src the source buffer - * @param start the start position of extraction - * @param length the count of bytes to extract (must not be zero) - * @param flags feature mask for the new buffer - * @return a new buffer containing the extraction - */ -UcxBuffer* ucx_buffer_extract(UcxBuffer *src, - size_t start, size_t length, int flags); - -/** - * A shorthand macro for the full extraction of the buffer. - * - * @param src the source buffer - * @param flags feature mask for the new buffer - * @return a new buffer with the extracted content - */ -#define ucx_buffer_clone(src,flags) \ - ucx_buffer_extract(src, 0, (src)->capacity, flags) - -/** - * Moves the position of the buffer. - * - * The new position is relative to the <code>whence</code> argument. - * - * SEEK_SET marks the start of the buffer. - * SEEK_CUR marks the current position. - * SEEK_END marks the end of the buffer. - * - * With an offset of zero, this function sets the buffer position to zero - * (SEEK_SET), the buffer size (SEEK_END) or leaves the buffer position - * unchanged (SEEK_CUR). - * - * @param buffer - * @param offset position offset relative to <code>whence</code> - * @param whence one of SEEK_SET, SEEK_CUR or SEEK_END - * @return 0 on success, non-zero if the position is invalid - * - */ -int ucx_buffer_seek(UcxBuffer *buffer, off_t offset, int whence); - -/** - * Clears the buffer by resetting the position and deleting the data. - * - * The data is deleted by a zeroing it with call to <code>memset()</code>. - * - * @param buffer the buffer to be cleared - */ -#define ucx_buffer_clear(buffer) memset(buffer->space, 0, buffer->size); \ - buffer->size = 0; buffer->pos = 0; - -/** - * Tests, if the buffer position has exceeded the buffer capacity. - * - * @param buffer the buffer to test - * @return non-zero, if the current buffer position has exceeded the last - * available byte of the buffer. - */ -int ucx_buffer_eof(UcxBuffer *buffer); - - -/** - * Extends the capacity of the buffer. - * - * <b>Note:</b> The buffer capacity increased by a power of two. I.e. - * the buffer capacity is doubled, as long as it would not hold the current - * content plus the additional required bytes. - * - * <b>Attention:</b> the argument provided is the number of <i>additional</i> - * bytes the buffer shall hold. It is <b>NOT</b> the total number of bytes the - * buffer shall hold. - * - * @param buffer the buffer to extend - * @param additional_bytes the number of additional bytes the buffer shall - * <i>at least</i> hold - * @return 0 on success or a non-zero value on failure - */ -int ucx_buffer_extend(UcxBuffer *buffer, size_t additional_bytes); - -/** - * Writes data to an UcxBuffer. - * - * The position of the buffer is increased by the number of bytes written. - * - * @param ptr a pointer to the memory area containing the bytes to be written - * @param size the length of one element - * @param nitems the element count - * @param buffer the UcxBuffer to write to - * @return the total count of bytes written - */ -size_t ucx_buffer_write(const void *ptr, size_t size, size_t nitems, - UcxBuffer *buffer); - -/** - * Reads data from an UcxBuffer. - * - * The position of the buffer is increased by the number of bytes read. - * - * @param ptr a pointer to the memory area where to store the read data - * @param size the length of one element - * @param nitems the element count - * @param buffer the UcxBuffer to read from - * @return the total number of elements read - */ -size_t ucx_buffer_read(void *ptr, size_t size, size_t nitems, - UcxBuffer *buffer); - -/** - * Writes a character to a buffer. - * - * The least significant byte of the argument is written to the buffer. If the - * end of the buffer is reached and #UCX_BUFFER_AUTOEXTEND feature is enabled, - * the buffer capacity is extended by ucx_buffer_extend(). If the feature is - * disabled or buffer extension fails, <code>EOF</code> is returned. - * - * On successful write the position of the buffer is increased. - * - * @param buffer the buffer to write to - * @param c the character to write as <code>int</code> value - * @return the byte that has bean written as <code>int</code> value or - * <code>EOF</code> when the end of the stream is reached and automatic - * extension is not enabled or not possible - */ -int ucx_buffer_putc(UcxBuffer *buffer, int c); - -/** - * Gets a character from a buffer. - * - * The current position of the buffer is increased after a successful read. - * - * @param buffer the buffer to read from - * @return the character as <code>int</code> value or <code>EOF</code>, if the - * end of the buffer is reached - */ -int ucx_buffer_getc(UcxBuffer *buffer); - -/** - * Writes a string to a buffer. - * - * @param buffer the buffer - * @param str the string - * @return the number of bytes written - */ -size_t ucx_buffer_puts(UcxBuffer *buffer, char *str); - -#ifdef __cplusplus -} -#endif - -#endif /* UCX_BUFFER_H */ -
--- a/src/ucx/list.c Thu Nov 10 18:44:48 2016 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,335 +0,0 @@ -/* - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. - * - * Copyright 2015 Olaf Wintermann. All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ - -#include "list.h" - -UcxList *ucx_list_clone(UcxList *l, copy_func fnc, void *data) { - return ucx_list_clone_a(ucx_default_allocator(), l, fnc, data); -} - -UcxList *ucx_list_clone_a(UcxAllocator *alloc, UcxList *l, - copy_func fnc, void *data) { - UcxList *ret = NULL; - while (l) { - if (fnc) { - ret = ucx_list_append_a(alloc, ret, fnc(l->data, data)); - } else { - ret = ucx_list_append_a(alloc, ret, l->data); - } - l = l->next; - } - return ret; -} - -int ucx_list_equals(const UcxList *l1, const UcxList *l2, - cmp_func fnc, void* data) { - if (l1 == l2) return 1; - - while (l1 != NULL && l2 != NULL) { - if (fnc == NULL) { - if (l1->data != l2->data) return 0; - } else { - if (fnc(l1->data, l2->data, data) != 0) return 0; - } - l1 = l1->next; - l2 = l2->next; - } - - return (l1 == NULL && l2 == NULL); -} - -void ucx_list_free(UcxList *l) { - ucx_list_free_a(ucx_default_allocator(), l); -} - -void ucx_list_free_a(UcxAllocator *alloc, UcxList *l) { - UcxList *e = l, *f; - while (e != NULL) { - f = e; - e = e->next; - alfree(alloc, f); - } -} - -void ucx_list_free_content(UcxList* list, ucx_destructor destr) { - while (list != NULL) { - destr(list->data); - list = list->next; - } -} - -UcxList *ucx_list_append(UcxList *l, void *data) { - return ucx_list_append_a(ucx_default_allocator(), l, data); -} - -UcxList *ucx_list_append_a(UcxAllocator *alloc, UcxList *l, void *data) { - UcxList *nl = (UcxList*) almalloc(alloc, sizeof(UcxList)); - if (!nl) { - return NULL; - } - - nl->data = data; - nl->next = NULL; - if (l) { - UcxList *t = ucx_list_last(l); - t->next = nl; - nl->prev = t; - return l; - } else { - nl->prev = NULL; - return nl; - } -} - -UcxList *ucx_list_prepend(UcxList *l, void *data) { - return ucx_list_prepend_a(ucx_default_allocator(), l, data); -} - -UcxList *ucx_list_prepend_a(UcxAllocator *alloc, UcxList *l, void *data) { - UcxList *nl = ucx_list_append_a(alloc, NULL, data); - if (!nl) { - return NULL; - } - l = ucx_list_first(l); - - if (l) { - nl->next = l; - l->prev = nl; - } - return nl; -} - -UcxList *ucx_list_concat(UcxList *l1, UcxList *l2) { - if (l1) { - UcxList *last = ucx_list_last(l1); - last->next = l2; - if (l2) { - l2->prev = last; - } - return l1; - } else { - return l2; - } -} - -UcxList *ucx_list_last(const UcxList *l) { - if (l == NULL) return NULL; - - const UcxList *e = l; - while (e->next != NULL) { - e = e->next; - } - return (UcxList*)e; -} - -ssize_t ucx_list_indexof(const UcxList *list, const UcxList *elem) { - ssize_t index = 0; - while (list) { - if (list == elem) { - return index; - } - list = list->next; - index++; - } - return -1; -} - -UcxList *ucx_list_get(const UcxList *l, size_t index) { - if (l == NULL) return NULL; - - const UcxList *e = l; - while (e->next && index > 0) { - e = e->next; - index--; - } - - return (UcxList*)(index == 0 ? e : NULL); -} - -ssize_t ucx_list_find(UcxList *l, void *elem, cmp_func fnc, void *cmpdata) { - ssize_t index = 0; - UCX_FOREACH(e, l) { - if (fnc) { - if (fnc(elem, e->data, cmpdata) == 0) { - return index; - } - } else { - if (elem == e->data) { - return index; - } - } - index++; - } - return -1; -} - -int ucx_list_contains(UcxList *l, void *elem, cmp_func fnc, void *cmpdata) { - return ucx_list_find(l, elem, fnc, cmpdata) > -1; -} - -size_t ucx_list_size(const UcxList *l) { - if (l == NULL) return 0; - - const UcxList *e = l; - size_t s = 1; - while (e->next != NULL) { - e = e->next; - s++; - } - - return s; -} - -static UcxList *ucx_list_sort_merge(int length, - UcxList* restrict ls, UcxList* restrict le, UcxList* restrict re, - cmp_func fnc, void* data) { - - UcxList** sorted = (UcxList**) malloc(sizeof(UcxList*)*length); - UcxList *rc, *lc; - - lc = ls; rc = le; - int n = 0; - while (lc && lc != le && rc != re) { - if (fnc(lc->data, rc->data, data) <= 0) { - sorted[n] = lc; - lc = lc->next; - } else { - sorted[n] = rc; - rc = rc->next; - } - n++; - } - while (lc && lc != le) { - sorted[n] = lc; - lc = lc->next; - n++; - } - while (rc && rc != re) { - sorted[n] = rc; - rc = rc->next; - n++; - } - - // Update pointer - sorted[0]->prev = NULL; - for (int i = 0 ; i < length-1 ; i++) { - sorted[i]->next = sorted[i+1]; - sorted[i+1]->prev = sorted[i]; - } - sorted[length-1]->next = NULL; - - UcxList *ret = sorted[0]; - free(sorted); - return ret; -} - -UcxList *ucx_list_sort(UcxList *l, cmp_func fnc, void *data) { - if (l == NULL) { - return NULL; - } - - UcxList *lc; - int ln = 1; - - UcxList *restrict ls = l, *restrict le, *restrict re; - - // check how many elements are already sorted - lc = ls; - while (lc->next != NULL && fnc(lc->next->data, lc->data, data) > 0) { - lc = lc->next; - ln++; - } - le = lc->next; - - if (le == NULL) { - return l; // this list is already sorted :) - } else { - UcxList *rc; - int rn = 1; - rc = le; - // skip already sorted elements - while (rc->next != NULL && fnc(rc->next->data, rc->data, data) > 0) { - rc = rc->next; - rn++; - } - re = rc->next; - - // {ls,...,le->prev} and {rs,...,re->prev} are sorted - merge them - UcxList *sorted = ucx_list_sort_merge(ln+rn, - ls, le, re, - fnc, data); - - // Something left? Sort it! - size_t remainder_length = ucx_list_size(re); - if (remainder_length > 0) { - UcxList *remainder = ucx_list_sort(re, fnc, data); - - // merge sorted list with (also sorted) remainder - l = ucx_list_sort_merge(ln+rn+remainder_length, - sorted, remainder, NULL, fnc, data); - } else { - // no remainder - we've got our sorted list - l = sorted; - } - - return l; - } -} - -UcxList *ucx_list_first(const UcxList *l) { - if (!l) { - return NULL; - } - - const UcxList *e = l; - while (e->prev) { - e = e->prev; - } - return (UcxList *)e; -} - -UcxList *ucx_list_remove(UcxList *l, UcxList *e) { - return ucx_list_remove_a(ucx_default_allocator(), l, e); -} - -UcxList *ucx_list_remove_a(UcxAllocator *alloc, UcxList *l, UcxList *e) { - if (l == e) { - l = e->next; - } - - if (e->next) { - e->next->prev = e->prev; - } - - if (e->prev) { - e->prev->next = e->next; - } - - alfree(alloc, e); - return l; -}
--- a/src/ucx/list.h Thu Nov 10 18:44:48 2016 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,395 +0,0 @@ -/* - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. - * - * Copyright 2015 Olaf Wintermann. All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ -/** - * Doubly linked list implementation. - * - * @file list.h - * @author Mike Becker - * @author Olaf Wintermann - */ - -#ifndef UCX_LIST_H -#define UCX_LIST_H - -#include "ucx.h" -#include "allocator.h" - -#ifdef __cplusplus -extern "C" { -#endif - -/** - * Loop statement for UCX lists. - * - * The first argument is the name of the iteration variable. The scope of - * this variable is limited to the <code>UCX_FOREACH</code> statement. - * - * The second argument is a pointer to the list. In most cases this will be the - * pointer to the first element of the list, but it may also be an arbitrary - * element of the list. The iteration will then start with that element. - * - * - * @param list The first element of the list - * @param elem The variable name of the element - */ -#define UCX_FOREACH(elem,list) \ - for (UcxList* elem = list ; elem != NULL ; elem = elem->next) - -/** - * UCX list type. - * @see UcxList - */ -typedef struct UcxList UcxList; - -/** - * UCX list structure. - */ -struct UcxList { - /** - * List element payload. - */ - void *data; - /** - * Pointer to the next list element or <code>NULL</code>, if this is the - * last element. - */ - UcxList *next; - /** - * Pointer to the previous list element or <code>NULL</code>, if this is - * the first element. - */ - UcxList *prev; -}; - -/** - * Creates an element-wise copy of a list. - * - * This function clones the specified list by creating new list elements and - * copying the data with the specified copy_func(). If no copy_func() is - * specified, a shallow copy is created and the new list will reference the - * same data as the source list. - * - * @param list the list to copy - * @param cpyfnc a pointer to the function that shall copy an element (may be - * <code>NULL</code>) - * @param data additional data for the copy_func() - * @return a pointer to the copy - */ -UcxList *ucx_list_clone(UcxList *list, copy_func cpyfnc, void* data); - -/** - * Creates an element-wise copy of a list using an UcxAllocator. - * - * See ucx_list_clone() for details. - * - * Keep in mind, that you might want to pass the allocator as an (part of the) - * argument for the <code>data</code> parameter, if you want the copy_func() to - * make use of the allocator. - * - * @param allocator the allocator to use - * @param list the list to copy - * @param cpyfnc a pointer to the function that shall copy an element (may be - * <code>NULL</code>) - * @param data additional data for the copy_func() - * @return a pointer to the copy - * @see ucx_list_clone() - */ -UcxList *ucx_list_clone_a(UcxAllocator *allocator, UcxList *list, - copy_func cpyfnc, void* data); - -/** - * Compares two UCX lists element-wise by using a compare function. - * - * Each element of the two specified lists are compared by using the specified - * compare function and the additional data. The type and content of this - * additional data depends on the cmp_func() used. - * - * If the list pointers denote elements within a list, the lists are compared - * starting with the denoted elements. Thus any previous elements are not taken - * into account. This might be useful to check, if certain list tails match - * each other. - * - * @param list1 the first list - * @param list2 the second list - * @param cmpfnc the compare function - * @param data additional data for the compare function - * @return 1, if and only if the two lists equal element-wise, 0 otherwise - */ -int ucx_list_equals(const UcxList *list1, const UcxList *list2, - cmp_func cmpfnc, void* data); - -/** - * Destroys the entire list. - * - * The members of the list are not automatically freed, so ensure they are - * otherwise referenced or destroyed by ucx_list_free_contents(). - * Otherwise, a memory leak is likely to occur. - * - * <b>Caution:</b> the argument <b>MUST</b> denote an entire list (i.e. a call - * to ucx_list_first() on the argument must return the argument itself) - * - * @param list the list to free - * @see ucx_list_free_contents() - */ -void ucx_list_free(UcxList *list); - -/** - * Destroys the entire list using an UcxAllocator. - * - * See ucx_list_free() for details. - * - * @param allocator the allocator to use - * @param list the list to free - * @see ucx_list_free() - */ -void ucx_list_free_a(UcxAllocator *allocator, UcxList *list); - -/** - * Destroys the contents of the specified list by calling the specified - * destructor on each of them. - * - * Note, that the contents are not usable afterwards and the list should be - * destroyed with ucx_list_free(). - * - * @param list the list for which the contents shall be freed - * @param destr the destructor function (e.g. stdlib free()) - * @see ucx_list_free() - */ -void ucx_list_free_content(UcxList* list, ucx_destructor destr); - - -/** - * Inserts an element at the end of the list. - * - * This is generally an O(n) operation, as the end of the list is retrieved with - * ucx_list_last(). - * - * @param list the list where to append the data, or <code>NULL</code> to - * create a new list - * @param data the data to insert - * @return <code>list</code>, if it is not <code>NULL</code> or a pointer to - * the newly created list otherwise - */ -UcxList *ucx_list_append(UcxList *list, void *data); - -/** - * Inserts an element at the end of the list using an UcxAllocator. - * - * See ucx_list_append() for details. - * - * @param allocator the allocator to use - * @param list the list where to append the data, or <code>NULL</code> to - * create a new list - * @param data the data to insert - * @return <code>list</code>, if it is not <code>NULL</code> or a pointer to - * the newly created list otherwise - * @see ucx_list_append() - */ -UcxList *ucx_list_append_a(UcxAllocator *allocator, UcxList *list, void *data); - -/** - * Inserts an element at the beginning of the list. - * - * You <i>should</i> overwrite the old list pointer by calling - * <code>mylist = ucx_list_prepend(mylist, mydata);</code>. However, you may - * also perform successive calls of ucx_list_prepend() on the same list pointer, - * as this function always searchs for the head of the list with - * ucx_list_first(). - * - * @param list the list where to insert the data or <code>NULL</code> to create - * a new list - * @param data the data to insert - * @return a pointer to the new list head - */ -UcxList *ucx_list_prepend(UcxList *list, void *data); - -/** - * Inserts an element at the beginning of the list using an UcxAllocator. - * - * See ucx_list_prepend() for details. - * - * @param allocator the allocator to use - * @param list the list where to insert the data or <code>NULL</code> to create - * a new list - * @param data the data to insert - * @return a pointer to the new list head - * @see ucx_list_prepend() - */ -UcxList *ucx_list_prepend_a(UcxAllocator *allocator, UcxList *list, void *data); - -/** - * Concatenates two lists. - * - * Either of the two arguments may be <code>NULL</code>. - * - * This function modifies the references to the next/previous element of - * the last/first element of <code>list1</code>/<code> - * list2</code>. - * - * @param list1 first list - * @param list2 second list - * @return if <code>list1</code> is <code>NULL</code>, <code>list2</code> is - * returned, otherwise <code>list1</code> is returned - */ -UcxList *ucx_list_concat(UcxList *list1, UcxList *list2); - -/** - * Returns the first element of a list. - * - * If the argument is the list pointer, it is directly returned. Otherwise - * this function traverses to the first element of the list and returns the - * list pointer. - * - * @param elem one element of the list - * @return the first element of the list, the specified element is a member of - */ -UcxList *ucx_list_first(const UcxList *elem); - -/** - * Returns the last element of a list. - * - * If the argument has no successor, it is the last element and therefore - * directly returned. Otherwise this function traverses to the last element of - * the list and returns it. - * - * @param elem one element of the list - * @return the last element of the list, the specified element is a member of - */ -UcxList *ucx_list_last(const UcxList *elem); - -/** - * Returns the list element at the specified index. - * - * @param list the list to retrieve the element from - * @param index index of the element to return - * @return the element at the specified index or <code>NULL</code>, if the - * index is greater than the list size - */ -UcxList *ucx_list_get(const UcxList *list, size_t index); - -/** - * Returns the index of an element. - * - * @param list the list where to search for the element - * @param elem the element to find - * @return the index of the element or -1 if the list does not contain the - * element - */ -ssize_t ucx_list_indexof(const UcxList *list, const UcxList *elem); - -/** - * Returns the element count of the list. - * - * @param list the list whose elements are counted - * @return the element count - */ -size_t ucx_list_size(const UcxList *list); - -/** - * Returns the index of an element containing the specified data. - * - * This function uses a cmp_func() to compare the data of each list element - * with the specified data. If no cmp_func is provided, the pointers are - * compared. - * - * If the list contains the data more than once, the index of the first - * occurrence is returned. - * - * @param list the list where to search for the data - * @param elem the element data - * @param cmpfnc the compare function - * @param data additional data for the compare function - * @return the index of the element containing the specified data or -1 if the - * data is not found in this list - */ -ssize_t ucx_list_find(UcxList *list, void *elem, cmp_func cmpfnc, void *data); - -/** - * Checks, if a list contains a specific element. - * - * An element is found, if ucx_list_find() returns a value greater than -1. - * - * @param list the list where to search for the data - * @param elem the element data - * @param cmpfnc the compare function - * @param data additional data for the compare function - * @return 1, if and only if the list contains the specified element data - * @see ucx_list_find() - */ -int ucx_list_contains(UcxList *list, void *elem, cmp_func cmpfnc, void *data); - -/** - * Sorts an UcxList with natural merge sort. - * - * This function uses O(n) additional temporary memory for merge operations - * that is automatically freed after each merge. - * - * As the head of the list might change, you <b>MUST</b> call this function - * as follows: <code>mylist = ucx_list_sort(mylist, mycmpfnc, mydata);</code>. - * - * @param list the list to sort - * @param cmpfnc the function that shall be used to compare the element data - * @param data additional data for the cmp_func() - * @return the sorted list - */ -UcxList *ucx_list_sort(UcxList *list, cmp_func cmpfnc, void *data); - -/** - * Removes an element from the list. - * - * If the first element is removed, the list pointer changes. So it is - * <i>highly recommended</i> to <i>always</i> update the pointer by calling - * <code>mylist = ucx_list_remove(mylist, myelem);</code>. - * - * @param list the list from which the element shall be removed - * @param element the element to remove - * @return returns the updated list pointer or <code>NULL</code>, if the list - * is now empty - */ -UcxList *ucx_list_remove(UcxList *list, UcxList *element); - -/** - * Removes an element from the list using an UcxAllocator. - * - * See ucx_list_remove() for details. - * - * @param allocator the allocator to use - * @param list the list from which the element shall be removed - * @param element the element to remove - * @return returns the updated list pointer or <code>NULL</code>, if the list - * @see ucx_list_remove() - */ -UcxList *ucx_list_remove_a(UcxAllocator *allocator, UcxList *list, - UcxList *element); - -#ifdef __cplusplus -} -#endif - -#endif /* UCX_LIST_H */ -
--- a/src/ucx/string.c Thu Nov 10 18:44:48 2016 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,381 +0,0 @@ -/* - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. - * - * Copyright 2015 Olaf Wintermann. All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ - -#include <stdlib.h> -#include <string.h> -#include <stdarg.h> -#include <ctype.h> - -#include "string.h" -#include "allocator.h" - -sstr_t sstr(char *cstring) { - sstr_t string; - string.ptr = cstring; - string.length = strlen(cstring); - return string; -} - -sstr_t sstrn(char *cstring, size_t length) { - sstr_t string; - string.ptr = cstring; - string.length = length; - return string; -} - -size_t sstrnlen(size_t n, sstr_t s, ...) { - va_list ap; - size_t size = s.length; - va_start(ap, s); - - for (size_t i = 1 ; i < n ; i++) { - sstr_t str = va_arg(ap, sstr_t); - size += str.length; - } - va_end(ap); - - return size; -} - -static sstr_t sstrvcat_a( - UcxAllocator *a, - size_t count, - sstr_t s1, - sstr_t s2, - va_list ap) { - sstr_t str; - str.ptr = NULL; - str.length = 0; - if(count < 2) { - return str; - } - - sstr_t *strings = (sstr_t*) calloc(count, sizeof(sstr_t)); - if(!strings) { - return str; - } - - // get all args and overall length - strings[0] = s1; - strings[1] = s2; - size_t strlen = s1.length + s2.length; - for (size_t i=2;i<count;i++) { - sstr_t s = va_arg (ap, sstr_t); - strings[i] = s; - strlen += s.length; - } - - // create new string - str.ptr = (char*) almalloc(a, strlen + 1); - str.length = strlen; - if(!str.ptr) { - free(strings); - str.length = 0; - return str; - } - - // concatenate strings - size_t pos = 0; - for (size_t i=0;i<count;i++) { - sstr_t s = strings[i]; - memcpy(str.ptr + pos, s.ptr, s.length); - pos += s.length; - } - - str.ptr[str.length] = '\0'; - - free(strings); - - return str; -} - -sstr_t sstrcat(size_t count, sstr_t s1, sstr_t s2, ...) { - va_list ap; - va_start(ap, s2); - sstr_t s = sstrvcat_a(ucx_default_allocator(), count, s1, s2, ap); - va_end(ap); - return s; -} - -sstr_t sstrcat_a(UcxAllocator *a, size_t count, sstr_t s1, sstr_t s2, ...) { - va_list ap; - va_start(ap, s2); - sstr_t s = sstrvcat_a(a, count, s1, s2, ap); - va_end(ap); - return s; -} - -sstr_t sstrsubs(sstr_t s, size_t start) { - return sstrsubsl (s, start, s.length-start); -} - -sstr_t sstrsubsl(sstr_t s, size_t start, size_t length) { - sstr_t new_sstr; - if (start >= s.length) { - new_sstr.ptr = NULL; - new_sstr.length = 0; - } else { - if (length > s.length-start) { - length = s.length-start; - } - new_sstr.ptr = &s.ptr[start]; - new_sstr.length = length; - } - return new_sstr; -} - -sstr_t sstrchr(sstr_t s, int c) { - for(size_t i=0;i<s.length;i++) { - if(s.ptr[i] == c) { - return sstrsubs(s, i); - } - } - sstr_t n; - n.ptr = NULL; - n.length = 0; - return n; -} - -sstr_t sstrrchr(sstr_t s, int c) { - if (s.length > 0) { - for(size_t i=s.length;i>0;i--) { - if(s.ptr[i-1] == c) { - return sstrsubs(s, i-1); - } - } - } - sstr_t n; - n.ptr = NULL; - n.length = 0; - return n; -} - -sstr_t sstrstr(sstr_t string, sstr_t match) { - if (match.length == 0) { - return string; - } - - for (size_t i = 0 ; i < string.length ; i++) { - sstr_t substr = sstrsubs(string, i); - if (sstrprefix(substr, match)) { - return substr; - } - } - - sstr_t emptystr; - emptystr.length = 0; - emptystr.ptr = NULL; - return emptystr; -} - -sstr_t* sstrsplit(sstr_t s, sstr_t d, ssize_t *n) { - return sstrsplit_a(ucx_default_allocator(), s, d, n); -} - -sstr_t* sstrsplit_a(UcxAllocator *allocator, sstr_t s, sstr_t d, ssize_t *n) { - if (s.length == 0 || d.length == 0) { - *n = -1; - return NULL; - } - - sstr_t* result; - ssize_t nmax = *n; - *n = 1; - - /* special case: exact match - no processing needed */ - if (sstrcmp(s, d) == 0) { - *n = 0; - return NULL; - } - sstr_t sv = sstrdup(s); - if (sv.length == 0) { - *n = -2; - return NULL; - } - - for (size_t i = 0 ; i < s.length ; i++) { - sstr_t substr = sstrsubs(sv, i); - if (sstrprefix(substr, d)) { - (*n)++; - for (size_t j = 0 ; j < d.length ; j++) { - sv.ptr[i+j] = 0; - } - i += d.length - 1; // -1, because the loop will do a i++ - } - if ((*n) == nmax) break; - } - result = (sstr_t*) almalloc(allocator, sizeof(sstr_t)*(*n)); - - if (result) { - char *pptr = sv.ptr; - for (ssize_t i = 0 ; i < *n ; i++) { - size_t l = strlen(pptr); - char* ptr = (char*) almalloc(allocator, l + 1); - if (ptr) { - memcpy(ptr, pptr, l); - ptr[l] = 0; - - result[i] = sstrn(ptr, l); - pptr += l + d.length; - } else { - for (ssize_t j = i-1 ; j >= 0 ; j--) { - alfree(allocator, result[j].ptr); - } - alfree(allocator, result); - *n = -2; - break; - } - } - } else { - *n = -2; - } - - free(sv.ptr); - - return result; -} - -int sstrcmp(sstr_t s1, sstr_t s2) { - if (s1.length == s2.length) { - return memcmp(s1.ptr, s2.ptr, s1.length); - } else if (s1.length > s2.length) { - return 1; - } else { - return -1; - } -} - -int sstrcasecmp(sstr_t s1, sstr_t s2) { - if (s1.length == s2.length) { -#ifdef _WIN32 - return _strnicmp(s1.ptr, s2.ptr, s1.length); -#else - return strncasecmp(s1.ptr, s2.ptr, s1.length); -#endif - } else if (s1.length > s2.length) { - return 1; - } else { - return -1; - } -} - -sstr_t sstrdup(sstr_t s) { - return sstrdup_a(ucx_default_allocator(), s); -} - -sstr_t sstrdup_a(UcxAllocator *allocator, sstr_t s) { - sstr_t newstring; - newstring.ptr = (char*)almalloc(allocator, s.length + 1); - if (newstring.ptr) { - newstring.length = s.length; - newstring.ptr[newstring.length] = 0; - - memcpy(newstring.ptr, s.ptr, s.length); - } else { - newstring.length = 0; - } - - return newstring; -} - -sstr_t sstrtrim(sstr_t string) { - sstr_t newstr = string; - - while (newstr.length > 0 && isspace(*newstr.ptr)) { - newstr.ptr++; - newstr.length--; - } - while (newstr.length > 0 && isspace(newstr.ptr[newstr.length-1])) { - newstr.length--; - } - - return newstr; -} - -int sstrprefix(sstr_t string, sstr_t prefix) { - if (string.length == 0) { - return prefix.length == 0; - } - if (prefix.length == 0) { - return 1; - } - - if (prefix.length > string.length) { - return 0; - } else { - return memcmp(string.ptr, prefix.ptr, prefix.length) == 0; - } -} - -int sstrsuffix(sstr_t string, sstr_t suffix) { - if (string.length == 0) { - return suffix.length == 0; - } - if (suffix.length == 0) { - return 1; - } - - if (suffix.length > string.length) { - return 0; - } else { - return memcmp(string.ptr+string.length-suffix.length, - suffix.ptr, suffix.length) == 0; - } -} - -sstr_t sstrlower(sstr_t string) { - sstr_t ret = sstrdup(string); - for (size_t i = 0; i < ret.length ; i++) { - ret.ptr[i] = tolower(ret.ptr[i]); - } - return ret; -} - -sstr_t sstrlower_a(UcxAllocator *allocator, sstr_t string) { - sstr_t ret = sstrdup_a(allocator, string); - for (size_t i = 0; i < ret.length ; i++) { - ret.ptr[i] = tolower(ret.ptr[i]); - } - return ret; -} - -sstr_t sstrupper(sstr_t string) { - sstr_t ret = sstrdup(string); - for (size_t i = 0; i < ret.length ; i++) { - ret.ptr[i] = toupper(ret.ptr[i]); - } - return ret; -} - -sstr_t sstrupper_a(UcxAllocator *allocator, sstr_t string) { - sstr_t ret = sstrdup_a(allocator, string); - for (size_t i = 0; i < ret.length ; i++) { - ret.ptr[i] = toupper(ret.ptr[i]); - } - return ret; -}
--- a/src/ucx/string.h Thu Nov 10 18:44:48 2016 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,457 +0,0 @@ -/* - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. - * - * Copyright 2015 Olaf Wintermann. All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ -/** - * Bounded string implementation. - * - * The UCX strings (<code>sstr_t</code>) provide an alternative to C strings. - * The main difference to C strings is, that <code>sstr_t</code> does <b>not - * need to be <code>NULL</code>-terminated</b>. Instead the length is stored - * within the structure. - * - * When using <code>sstr_t</code>, developers must be full aware of what type - * of string (<code>NULL</code>-terminated) or not) they are using, when - * accessing the <code>char* ptr</code> directly. - * - * The UCX string module provides some common string functions, known from - * standard libc, working with <code>sstr_t</code>. - * - * @file string.h - * @author Mike Becker - * @author Olaf Wintermann - */ - -#ifndef UCX_STRING_H -#define UCX_STRING_H - -#include "ucx.h" -#include "allocator.h" -#include <stddef.h> - -/** Shortcut for a <code>sstr_t struct</code> literal. */ -#define ST(s) { (char*)s, sizeof(s)-1 } - -/** Shortcut for the conversion of a C string to a <code>sstr_t</code>. */ -#define S(s) sstrn((char*)s, sizeof(s)-1) - -#ifdef __cplusplus -extern "C" { -#endif - -/** - * The UCX string structure. - */ -typedef struct { - /** A reference to the string (<b>not necessarily <code>NULL</code> - * -terminated</b>) */ - char *ptr; - /** The length of the string */ - size_t length; -} sstr_t; - -/** - * Creates a new sstr_t based on a C string. - * - * The length is implicitly inferred by using a call to <code>strlen()</code>. - * - * <b>Note:</b> the sstr_t will hold a <i>reference</i> to the C string. If you - * do want a copy, use sstrdup() on the return value of this function. - * - * @param cstring the C string to wrap - * @return a new sstr_t containing the C string - * - * @see sstrn() - */ -sstr_t sstr(char *cstring); - -/** - * Creates a new sstr_t of the specified length based on a C string. - * - * <b>Note:</b> the sstr_t will hold a <i>reference</i> to the C string. If you - * do want a copy, use sstrdup() on the return value of this function. - * - * @param cstring the C string to wrap - * @param length the length of the string - * @return a new sstr_t containing the C string - * - * @see sstr() - * @see S() - */ -sstr_t sstrn(char *cstring, size_t length); - - -/** - * Returns the cumulated length of all specified strings. - * - * At least one string must be specified. - * - * <b>Attention:</b> if the count argument does not match the count of the - * specified strings, the behavior is undefined. - * - * @param count the total number of specified strings (so at least 1) - * @param string the first string - * @param ... all other strings - * @return the cumulated length of all strings - */ -size_t sstrnlen(size_t count, sstr_t string, ...); - -/** - * Concatenates two or more strings. - * - * The resulting string will be allocated by standard <code>malloc()</code>. - * So developers <b>MUST</b> pass the sstr_t.ptr to <code>free()</code>. - * - * The sstr_t.ptr of the return value will <i>always</i> be <code>NULL</code>- - * terminated. - * - * @param count the total number of strings to concatenate - * @param s1 first string - * @param s2 second string - * @param ... all remaining strings - * @return the concatenated string - */ -sstr_t sstrcat(size_t count, sstr_t s1, sstr_t s2, ...); - -/** - * Concatenates two or more strings using an UcxAllocator. - * - * See sstrcat() for details. - * - * @param a the allocator to use - * @param count the total number of strings to concatenate - * @param s1 first string - * @param s2 second string - * @param ... all remaining strings - * @return the concatenated string - */ -sstr_t sstrcat_a(UcxAllocator *a, size_t count, sstr_t s1, sstr_t s2, ...); - - -/** - * Returns a substring starting at the specified location. - * - * <b>Attention:</b> the new string references the same memory area as the - * input string and will <b>NOT</b> be <code>NULL</code>-terminated. - * Use sstrdup() to get a copy. - * - * @param string input string - * @param start start location of the substring - * @return a substring of <code>string</code> starting at <code>start</code> - * - * @see sstrsubsl() - * @see sstrchr() - */ -sstr_t sstrsubs(sstr_t string, size_t start); - -/** - * Returns a substring with a maximum length starting at the specified location. - * - * <b>Attention:</b> the new string references the same memory area as the - * input string and will <b>NOT</b> be <code>NULL</code>-terminated. - * Use sstrdup() to get a copy. - * - * @param string input string - * @param start start location of the substring - * @param length the maximum length of the substring - * @return a substring of <code>string</code> starting at <code>start</code> - * with a maximum length of <code>length</code> - * - * @see sstrsubs() - * @see sstrchr() - */ -sstr_t sstrsubsl(sstr_t string, size_t start, size_t length); - -/** - * Returns a substring starting at the location of the first occurrence of the - * specified character. - * - * If the string does not contain the character, an empty string is returned. - * - * @param string the string where to locate the character - * @param chr the character to locate - * @return a substring starting at the first location of <code>chr</code> - * - * @see sstrsubs() - */ -sstr_t sstrchr(sstr_t string, int chr); - -/** - * Returns a substring starting at the location of the last occurrence of the - * specified character. - * - * If the string does not contain the character, an empty string is returned. - * - * @param string the string where to locate the character - * @param chr the character to locate - * @return a substring starting at the last location of <code>chr</code> - * - * @see sstrsubs() - */ -sstr_t sstrrchr(sstr_t string, int chr); - -/** - * Returns a substring starting at the location of the first occurrence of the - * specified string. - * - * If the string does not contain the other string, an empty string is returned. - * - * If <code>match</code> is an empty string, the complete <code>string</code> is - * returned. - * - * @param string the string to be scanned - * @param match string containing the sequence of characters to match - * @return a substring starting at the first occurrence of - * <code>match</code>, or an empty string, if the sequence is not - * present in <code>string</code> - */ -sstr_t sstrstr(sstr_t string, sstr_t match); - -/** - * Splits a string into parts by using a delimiter string. - * - * This function will return <code>NULL</code>, if one of the following happens: - * <ul> - * <li>the string length is zero</li> - * <li>the delimeter length is zero</li> - * <li>the string equals the delimeter</li> - * <li>memory allocation fails</li> - * </ul> - * - * The integer referenced by <code>count</code> is used as input and determines - * the maximum size of the resulting array, i.e. the maximum count of splits to - * perform + 1. - * - * The integer referenced by <code>count</code> is also used as output and is - * set to - * <ul> - * <li>-2, on memory allocation errors</li> - * <li>-1, if either the string or the delimiter is an empty string</li> - * <li>0, if the string equals the delimiter</li> - * <li>1, if the string does not contain the delimiter</li> - * <li>the count of array items, otherwise</li> - * </ul> - * - * If the string starts with the delimiter, the first item of the resulting - * array will be an empty string. - * - * If the string ends with the delimiter and the maximum list size is not - * exceeded, the last array item will be an empty string. - * - * <b>Attention:</b> The array pointer <b>AND</b> all sstr_t.ptr of the array - * items must be manually passed to <code>free()</code>. Use sstrsplit_a() with - * an allocator to managed memory, to avoid this. - * - * @param string the string to split - * @param delim the delimiter string - * @param count IN: the maximum size of the resulting array (0 = no limit), - * OUT: the actual size of the array - * @return a sstr_t array containing the split strings or - * <code>NULL</code> on error - * - * @see sstrsplit_a() - */ -sstr_t* sstrsplit(sstr_t string, sstr_t delim, ssize_t *count); - -/** - * Performing sstrsplit() using an UcxAllocator. - * - * <i>Read the description of sstrsplit() for details.</i> - * - * The memory for the sstr_t.ptr pointers of the array items and the memory for - * the sstr_t array itself are allocated by using the UcxAllocator.malloc() - * function. - * - * <b>Note:</b> the allocator is not used for memory that is freed within the - * same call of this function (locally scoped variables). - * - * @param allocator the UcxAllocator used for allocating memory - * @param string the string to split - * @param delim the delimiter string - * @param count IN: the maximum size of the resulting array (0 = no limit), - * OUT: the actual size of the array - * @return a sstr_t array containing the split strings or - * <code>NULL</code> on error - * - * @see sstrsplit() - */ -sstr_t* sstrsplit_a(UcxAllocator *allocator, sstr_t string, sstr_t delim, - ssize_t *count); - -/** - * Compares two UCX strings with standard <code>memcmp()</code>. - * - * At first it compares the sstr_t.length attribute of the two strings. The - * <code>memcmp()</code> function is called, if and only if the lengths match. - * - * @param s1 the first string - * @param s2 the second string - * @return -1, if the length of s1 is less than the length of s2 or 1, if the - * length of s1 is greater than the length of s2 or the result of - * <code>memcmp()</code> otherwise (i.e. 0 if the strings match) - */ -int sstrcmp(sstr_t s1, sstr_t s2); - -/** - * Compares two UCX strings ignoring the case. - * - * At first it compares the sstr_t.length attribute of the two strings. If and - * only if the lengths match, both strings are compared char by char ignoring - * the case. - * - * @param s1 the first string - * @param s2 the second string - * @return -1, if the length of s1 is less than the length of s2 or 1, if the - * length of s1 is greater than the length of s2 or the difference between the - * first two differing characters otherwise (i.e. 0 if the strings match and - * no characters differ) - */ -int sstrcasecmp(sstr_t s1, sstr_t s2); - -/** - * Creates a duplicate of the specified string. - * - * The new sstr_t will contain a copy allocated by standard - * <code>malloc()</code>. So developers <b>MUST</b> pass the sstr_t.ptr to - * <code>free()</code>. - * - * The sstr_t.ptr of the return value will <i>always</i> be <code>NULL</code>- - * terminated. - * - * @param string the string to duplicate - * @return a duplicate of the string - * @see sstrdup_a() - */ -sstr_t sstrdup(sstr_t string); - -/** - * Creates a duplicate of the specified string using an UcxAllocator. - * - * The new sstr_t will contain a copy allocated by the allocators - * ucx_allocator_malloc function. So it is implementation depended, whether the - * returned sstr_t.ptr pointer must be passed to the allocators - * ucx_allocator_free function manually. - * - * The sstr_t.ptr of the return value will <i>always</i> be <code>NULL</code>- - * terminated. - * - * @param allocator a valid instance of an UcxAllocator - * @param string the string to duplicate - * @return a duplicate of the string - * @see sstrdup() - */ -sstr_t sstrdup_a(UcxAllocator *allocator, sstr_t string); - -/** - * Omits leading and trailing spaces. - * - * This function returns a new sstr_t containing a trimmed version of the - * specified string. - * - * <b>Note:</b> the new sstr_t references the same memory, thus you - * <b>MUST NOT</b> pass the sstr_t.ptr of the return value to - * <code>free()</code>. It is also highly recommended to avoid assignments like - * <code>mystr = sstrtrim(mystr);</code> as you lose the reference to the - * source string. Assignments of this type are only permitted, if the - * sstr_t.ptr of the source string does not need to be freed or if another - * reference to the source string exists. - * - * @param string the string that shall be trimmed - * @return a new sstr_t containing the trimmed string - */ -sstr_t sstrtrim(sstr_t string); - -/** - * Checks, if a string has a specific prefix. - * @param string the string to check - * @param prefix the prefix the string should have - * @return 1, if and only if the string has the specified prefix, 0 otherwise - */ -int sstrprefix(sstr_t string, sstr_t prefix); - -/** - * Checks, if a string has a specific suffix. - * @param string the string to check - * @param suffix the suffix the string should have - * @return 1, if and only if the string has the specified suffix, 0 otherwise - */ -int sstrsuffix(sstr_t string, sstr_t suffix); - -/** - * Returns a lower case version of a string. - * - * This function creates a duplicate of the input string, first. See the - * documentation of sstrdup() for the implications. - * - * @param string the input string - * @return the resulting lower case string - * @see sstrdup() - */ -sstr_t sstrlower(sstr_t string); - -/** - * Returns a lower case version of a string. - * - * This function creates a duplicate of the input string, first. See the - * documentation of sstrdup_a() for the implications. - * - * @param allocator the allocator used for duplicating the string - * @param string the input string - * @return the resulting lower case string - * @see sstrdup_a() - */ -sstr_t sstrlower_a(UcxAllocator *allocator, sstr_t string); - -/** - * Returns a upper case version of a string. - * - * This function creates a duplicate of the input string, first. See the - * documentation of sstrdup() for the implications. - * - * @param string the input string - * @return the resulting upper case string - * @see sstrdup() - */ -sstr_t sstrupper(sstr_t string); - -/** - * Returns a upper case version of a string. - * - * This function creates a duplicate of the input string, first. See the - * documentation of sstrdup_a() for the implications. - * - * @param allocator the allocator used for duplicating the string - * @param string the input string - * @return the resulting upper case string - * @see sstrdup_a() - */ -sstr_t sstrupper_a(UcxAllocator *allocator, sstr_t string); - -#ifdef __cplusplus -} -#endif - -#endif /* UCX_STRING_H */
--- a/src/ucx/ucx.h Thu Nov 10 18:44:48 2016 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,138 +0,0 @@ -/* - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. - * - * Copyright 2015 Olaf Wintermann. All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ -/** - * Main UCX Header providing most common definitions. - * - * @file ucx.h - * @author Mike Becker - * @author Olaf Wintermann - */ - -#ifndef UCX_H -#define UCX_H - -/** Major UCX version as integer constant. */ -#define UCX_VERSION_MAJOR 0 - -/** Minor UCX version as integer constant. */ -#define UCX_VERSION_MINOR 9 - -/** The UCX version in format [major].[minor] */ -#define UCX_VERSION UCX_VERSION_MAJOR.UCX_VERSION_MINOR - -#include <stdlib.h> - -#ifdef _WIN32 -#if !(defined __ssize_t_defined || defined _SSIZE_T_) -#include <BaseTsd.h> -typedef SSIZE_T ssize_t; -#define __ssize_t_defined -#define _SSIZE_T_ -#endif /* __ssize_t_defined and _SSIZE_T */ -#else /* !_WIN32 */ -#include <sys/types.h> -#endif /* _WIN32 */ - -#ifdef __cplusplus -#ifndef _Bool -#define _Bool bool -#define restrict -#endif -/** Use C naming even when compiling with C++. */ -#define UCX_EXTERN extern "C" -extern "C" { -#else -/** Pointless in C. */ -#define UCX_EXTERN -#endif - - -/** - * A function pointer to a destructor function. - * @see ucx_mempool_setdestr() - * @see ucx_mempool_regdestr() - */ -typedef void(*ucx_destructor)(void*); - -/** - * Function pointer to a compare function. - * - * The compare function shall take three arguments: the two values that shall be - * compared and optional additional data. - * The function shall then return -1 if the first argument is less than the - * second argument, 1 if the first argument is greater than the second argument - * and 0 if both arguments are equal. If the third argument is - * <code>NULL</code>, it shall be ignored. - */ -typedef int(*cmp_func)(void*,void*,void*); - -/** - * Function pointer to a copy function. - * - * The copy function shall create a copy of the first argument and may use - * additional data provided by the second argument. If the second argument is - * <code>NULL</code>, it shall be ignored. - - * <b>Attention:</b> if pointers returned by functions of this type may be - * passed to <code>free()</code> depends on the implementation of the - * respective <code>copy_func</code>. - */ -typedef void*(*copy_func)(void*,void*); - -/** - * Function pointer to a write function. - * - * The signature of the write function shall be compatible to the signature - * of standard <code>fwrite</code>, though it may use arbitrary data types for - * source and destination. - * - * The arguments shall contain (in ascending order): a pointer to the source, - * the length of one element, the element count and a pointer to the - * destination. - */ -typedef size_t(*write_func)(const void*, size_t, size_t, void*); - -/** - * Function pointer to a read function. - * - * The signature of the read function shall be compatible to the signature - * of standard <code>fread</code>, though it may use arbitrary data types for - * source and destination. - * - * The arguments shall contain (in ascending order): a pointer to the - * destination, the length of one element, the element count and a pointer to - * the source. - */ -typedef size_t(*read_func)(void*, size_t, size_t, void*); - -#ifdef __cplusplus -} -#endif - -#endif /* UCX_H */ -
--- a/src/ucx/utils.c Thu Nov 10 18:44:48 2016 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,259 +0,0 @@ -/* - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. - * - * Copyright 2015 Olaf Wintermann. All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ - -#include "utils.h" -#include <math.h> -#include <stdio.h> -#include <limits.h> -#include <errno.h> - -/* COPY FUCNTIONS */ -void* ucx_strcpy(void* s, void* data) { - char *str = (char*) s; - size_t n = 1+strlen(str); - char *cpy = (char*) malloc(n); - memcpy(cpy, str, n); - return cpy; -} - -void* ucx_memcpy(void* m, void* n) { - size_t k = *((size_t*)n); - void *cpy = malloc(k); - memcpy(cpy, m, k); - return cpy; -} - -size_t ucx_stream_copy(void *src, void *dest, read_func readfnc, - write_func writefnc, char* buf, size_t bufsize, size_t n) { - if(n == 0 || bufsize == 0) { - return 0; - } - - char *lbuf; - size_t ncp = 0; - - if(buf) { - lbuf = buf; - } else { - lbuf = (char*)malloc(bufsize); - if(lbuf == NULL) { - return 0; - } - } - - size_t r; - size_t rn = bufsize > n ? n : bufsize; - while((r = readfnc(lbuf, 1, rn, src)) != 0) { - r = writefnc(lbuf, 1, r, dest); - ncp += r; - n -= r; - rn = bufsize > n ? n : bufsize; - if(r == 0 || n == 0) { - break; - } - } - - if (lbuf != buf) { - free(lbuf); - } - - return ncp; -} - -/* COMPARE FUNCTIONS */ - -int ucx_strcmp(void *s1, void *s2, void *data) { - return strcmp((char*)s1, (char*)s2); -} - -int ucx_strncmp(void *s1, void *s2, void *n) { - return strncmp((char*)s1, (char*)s2, *((size_t*) n)); -} - -int ucx_intcmp(void *i1, void *i2, void *data) { - int a = *((int*) i1); - int b = *((int*) i2); - if (a == b) { - return 0; - } else { - return a < b ? -1 : 1; - } -} - -int ucx_floatcmp(void *f1, void *f2, void *epsilon) { - float a = *((float*) f1); - float b = *((float*) f2); - float e = !epsilon ? 1e-6f : *((float*)epsilon); - if (fabsf(a - b) < e) { - return 0; - } else { - return a < b ? -1 : 1; - } -} - -int ucx_doublecmp(void *d1, void *d2, void *epsilon) { - double a = *((float*) d1); - double b = *((float*) d2); - double e = !epsilon ? 1e-14 : *((double*)epsilon); - if (fabs(a - b) < e) { - return 0; - } else { - return a < b ? -1 : 1; - } -} - -int ucx_ptrcmp(void *ptr1, void *ptr2, void *data) { - intptr_t p1 = (intptr_t) ptr1; - intptr_t p2 = (intptr_t) ptr2; - if (p1 == p2) { - return 0; - } else { - return p1 < p2 ? -1 : 1; - } -} - -int ucx_memcmp(void *ptr1, void *ptr2, void *n) { - return memcmp(ptr1, ptr2, *((size_t*)n)); -} - -/* PRINTF FUNCTIONS */ - -#ifdef va_copy -#define UCX_PRINTF_BUFSIZE 256 -#else -#pragma message("WARNING: C99 va_copy macro not supported by this platform" \ - " - limiting ucx_*printf to 2 KiB") -#define UCX_PRINTF_BUFSIZE 0x800 -#endif - -int ucx_fprintf(void *stream, write_func wfc, const char *fmt, ...) { - int ret; - va_list ap; - va_start(ap, fmt); - ret = ucx_vfprintf(stream, wfc, fmt, ap); - va_end(ap); - return ret; -} - -int ucx_vfprintf(void *stream, write_func wfc, const char *fmt, va_list ap) { - char buf[UCX_PRINTF_BUFSIZE]; -#ifdef va_copy - va_list ap2; - va_copy(ap2, ap); - int ret = vsnprintf(buf, UCX_PRINTF_BUFSIZE, fmt, ap); - if (ret < 0) { - return ret; - } else if (ret < UCX_PRINTF_BUFSIZE) { - return (int)wfc(buf, 1, ret, stream); - } else { - if (ret == INT_MAX) { - errno = ENOMEM; - return -1; - } - - int len = ret + 1; - char *newbuf = (char*)malloc(len); - if (!newbuf) { - return -1; - } - - ret = vsnprintf(newbuf, len, fmt, ap2); - if (ret > 0) { - ret = (int)wfc(newbuf, 1, ret, stream); - } - free(newbuf); - } - return ret; -#else - int ret = vsnprintf(buf, UCX_PRINTF_BUFSIZE, fmt, ap); - if (ret < 0) { - return ret; - } else if (ret < UCX_PRINTF_BUFSIZE) { - return (int)wfc(buf, 1, ret, stream); - } else { - errno = ENOMEM; - return -1; - } -#endif -} - -sstr_t ucx_asprintf(UcxAllocator *allocator, const char *fmt, ...) { - va_list ap; - sstr_t ret; - va_start(ap, fmt); - ret = ucx_vasprintf(allocator, fmt, ap); - va_end(ap); - return ret; -} - -sstr_t ucx_vasprintf(UcxAllocator *a, const char *fmt, va_list ap) { - sstr_t s; - s.ptr = NULL; - s.length = 0; - char buf[UCX_PRINTF_BUFSIZE]; -#ifdef va_copy - va_list ap2; - va_copy(ap2, ap); - int ret = vsnprintf(buf, UCX_PRINTF_BUFSIZE, fmt, ap); - if (ret > 0 && ret < UCX_PRINTF_BUFSIZE) { - s.ptr = (char*)almalloc(a, ret + 1); - if (s.ptr) { - s.length = (size_t)ret; - memcpy(s.ptr, buf, ret); - s.ptr[s.length] = '\0'; - } - } else if (ret == INT_MAX) { - errno = ENOMEM; - } else { - int len = ret + 1; - s.ptr = (char*)almalloc(a, len); - if (s.ptr) { - ret = vsnprintf(s.ptr, len, fmt, ap2); - if (ret < 0) { - free(s.ptr); - s.ptr = NULL; - } else { - s.length = (size_t)ret; - } - } - } -#else - int ret = vsnprintf(buf, UCX_PRINTF_BUFSIZE, fmt, ap); - if (ret > 0 && ret < UCX_PRINTF_BUFSIZE) { - s.ptr = (char*)almalloc(a, ret + 1); - if (s.ptr) { - s.length = (size_t)ret; - memcpy(s.ptr, buf, ret); - s.ptr[s.length] = '\0'; - } - } else { - errno = ENOMEM; - } -#endif - return s; -}
--- a/src/ucx/utils.h Thu Nov 10 18:44:48 2016 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,254 +0,0 @@ -/* - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. - * - * Copyright 2015 Olaf Wintermann. All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ - -/** - * @file utils.h - * - * Compare, copy and printf functions. - * - * @author Mike Becker - * @author Olaf Wintermann - */ - -#ifndef UCX_UTILS_H -#define UCX_UTILS_H - -#ifdef __cplusplus -extern "C" { -#endif - -#include "ucx.h" -#include "string.h" -#include "allocator.h" -#include <inttypes.h> -#include <string.h> -#include <stdarg.h> - -/** - * Copies a string. - * @param s the string to copy - * @param data omitted - * @return a pointer to a copy of s1 that can be passed to free(void*) - */ -void *ucx_strcpy(void *s, void *data); - -/** - * Copies a memory area. - * @param m a pointer to the memory area - * @param n a pointer to the size_t containing the size of the memory area - * @return a pointer to a copy of the specified memory area that can - * be passed to free(void*) - */ -void *ucx_memcpy(void *m, void *n); - - -/** - * Reads data from a stream and writes it to another stream. - * - * @param src the source stream - * @param dest the destination stream - * @param rfnc the read function - * @param wfnc the write function - * @param buf a pointer to the copy buffer or <code>NULL</code> if a buffer - * shall be implicitly created on the heap - * @param bufsize the size of the copy buffer - if <code>NULL</code> was - * provided for <code>buf</code>, this is the size of the buffer that shall be - * implicitly created - * @param n the maximum number of bytes that shall be copied - * @return the total number of bytes copied - */ -size_t ucx_stream_copy(void *src, void *dest, read_func rfnc, write_func wfnc, - char* buf, size_t bufsize, size_t n); - -/** - * Shorthand for ucx_stream_copy using the default copy buffer. - * - * @param src the source stream - * @param dest the destination stream - * @param rfnc the read function - * @param wfnc the write function - * @return total number of bytes copied - */ -#define ucx_stream_hcopy(src,dest,rfnc,wfnc) ucx_stream_copy(\ - src, dest, (read_func)rfnc, (write_func)wfnc, NULL, 0x100, (size_t)-1) - -/** - * Shorthand for ucx_stream_copy using the default copy buffer and a copy limit. - * - * @param src the source stream - * @param dest the destination stream - * @param rfnc the read function - * @param wfnc the write function - * @param n maximum number of bytes that shall be copied - * @return total number of bytes copied - */ -#define ucx_stream_ncopy(src,dest,rfnc,wfnc, n) ucx_stream_copy(\ - src, dest, (read_func)rfnc, (write_func)wfnc, NULL, 0x100, n) - -/** - * Wraps the strcmp function. - * @param s1 string one - * @param s2 string two - * @param data omitted - * @return the result of strcmp(s1, s2) - */ -int ucx_strcmp(void *s1, void *s2, void *data); - -/** - * Wraps the strncmp function. - * @param s1 string one - * @param s2 string two - * @param n a pointer to the size_t containing the third strncmp parameter - * @return the result of strncmp(s1, s2, *n) - */ -int ucx_strncmp(void *s1, void *s2, void *n); - -/** - * Compares two integers of type int. - * @param i1 pointer to integer one - * @param i2 pointer to integer two - * @param data omitted - * @return -1, if *i1 is less than *i2, 0 if both are equal, - * 1 if *i1 is greater than *i2 - */ -int ucx_intcmp(void *i1, void *i2, void *data); - -/** - * Compares two real numbers of type float. - * @param f1 pointer to float one - * @param f2 pointer to float two - * @param data if provided: a pointer to precision (default: 1e-6f) - * @return -1, if *f1 is less than *f2, 0 if both are equal, - * 1 if *f1 is greater than *f2 - */ - -int ucx_floatcmp(void *f1, void *f2, void *data); - -/** - * Compares two real numbers of type double. - * @param d1 pointer to double one - * @param d2 pointer to double two - * @param data if provided: a pointer to precision (default: 1e-14) - * @return -1, if *d1 is less than *d2, 0 if both are equal, - * 1 if *d1 is greater than *d2 - */ -int ucx_doublecmp(void *d1, void *d2, void *data); - -/** - * Compares two pointers. - * @param ptr1 pointer one - * @param ptr2 pointer two - * @param data omitted - * @return -1 if ptr1 is less than ptr2, 0 if both are equal, - * 1 if ptr1 is greater than ptr2 - */ -int ucx_ptrcmp(void *ptr1, void *ptr2, void *data); - -/** - * Compares two memory areas. - * @param ptr1 pointer one - * @param ptr2 pointer two - * @param n a pointer to the size_t containing the third parameter for memcmp - * @return the result of memcmp(ptr1, ptr2, *n) - */ -int ucx_memcmp(void *ptr1, void *ptr2, void *n); - -/** - * A <code>printf()</code> like function which writes the output to a stream by - * using a write_func(). - * @param stream the stream the data is written to - * @param wfc the write function - * @param fmt format string - * @param ... additional arguments - * @return the total number of bytes written - */ -int ucx_fprintf(void *stream, write_func wfc, const char *fmt, ...); - -/** - * <code>va_list</code> version of ucx_fprintf(). - * @param stream the stream the data is written to - * @param wfc the write function - * @param fmt format string - * @param ap argument list - * @return the total number of bytes written - * @see ucx_fprintf() - */ -int ucx_vfprintf(void *stream, write_func wfc, const char *fmt, va_list ap); - -/** - * A <code>printf()</code> like function which allocates space for a sstr_t - * the result is written to. - * - * <b>Attention</b>: The sstr_t data is allocated with the allocators - * ucx_allocator_malloc() function. So it is implementation dependent, if - * the returned sstr_t.ptr pointer must be passed to the allocators - * ucx_allocator_free() function manually. - * - * <b>Note</b>: The sstr_t.ptr of the return value will <i>always</i> be - * <code>NULL</code>-terminated. - * - * @param allocator the UcxAllocator used for allocating the result sstr_t - * @param fmt format string - * @param ... additional arguments - * @return a sstr_t containing the formatted string - */ -sstr_t ucx_asprintf(UcxAllocator *allocator, const char *fmt, ...); - -/** Shortcut for ucx_asprintf() with default allocator. */ -#define ucx_sprintf(fmt, ...) \ - ucx_asprintf(ucx_default_allocator(), fmt, __VA_ARGS__) - -/** - * <code>va_list</code> version of ucx_asprintf(). - * - * @param allocator the UcxAllocator used for allocating the result sstr_t - * @param fmt format string - * @param ap argument list - * @return a sstr_t containing the formatted string - * @see ucx_asprintf() - */ -sstr_t ucx_vasprintf(UcxAllocator *allocator, const char *fmt, va_list ap); - -/** - * A <code>printf()</code> like function which writes the output to an - * UcxBuffer. - * - * @param buffer the buffer the data is written to - * @param ... format string and additional arguments - * @return the total number of bytes written - * @see ucx_fprintf() - */ -#define ucx_bprintf(buffer, ...) ucx_fprintf((UcxBuffer*)buffer, \ - (write_func)ucx_buffer_write, __VA_ARGS__) - -#ifdef __cplusplus -} -#endif - -#endif /* UCX_UTILS_H */ -
--- a/test/ctestfile.c Thu Nov 10 18:44:48 2016 +0100 +++ b/test/ctestfile.c Mon Apr 24 20:54:38 2023 +0200 @@ -199,7 +199,7 @@ char* util_path_to_url(DavSession *sn, char *path) { char *space = malloc(256); - UcxBuffer *url = ucx_buffer_new(space, 256, UCX_BUFFER_AUTOEXTEND); + UcxBuffer *url = ucx_buffer_new(space, 256, CX_BUFFER_AUTO_EXTEND); // add base url ucx_buffer_write(sn->base_url, 1, strlen(sn->base_url), url);
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test/gs/bigtest.html Mon Apr 24 20:54:38 2023 +0200 @@ -0,0 +1,865 @@ +<!DOCTYPE html> +<html> + <head> + <title>c2html</title> + <style type="text/css"> + div.c2html-code { + white-space: pre; + font-family: monospace; + } + a.c2html-lineno { + /* as long as user-select isn't widely spread, we throw the bomb */ + -webkit-user-select: none; + -moz-user-select: none; + -ms-user-select: none; + user-select: none; + display: inline-block; + font-style: italic; + text-decoration: none; + color: grey; + } + span.c2html-keyword { + color: blue; + } + span.c2html-macroconst { + color: cornflowerblue; + } + span.c2html-type { + color: cornflowerblue; + } + span.c2html-directive { + color: green; + } + span.c2html-string { + color: darkorange; + } + span.c2html-comment { + color: grey; + } + span.c2html-stdinclude { + color: darkorange; + } + span.c2html-userinclude { + color: darkorange; + } + a.c2html-userinclude { + color: darkorange; + text-decoration: underline; + } + </style> + </head> + <body> + +<div class="c2html-code"> +<a class="c2html-lineno" name="l1" href="#l1"> 1 </a><span class="c2html-comment">/*</span> +<a class="c2html-lineno" name="l2" href="#l2"> 2 </a><span class="c2html-comment"> * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.</span> +<a class="c2html-lineno" name="l3" href="#l3"> 3 </a><span class="c2html-comment"> *</span> +<a class="c2html-lineno" name="l4" href="#l4"> 4 </a><span class="c2html-comment"> * Copyright 2014 Mike Becker. All rights reserved.</span> +<a class="c2html-lineno" name="l5" href="#l5"> 5 </a><span class="c2html-comment"> *</span> +<a class="c2html-lineno" name="l6" href="#l6"> 6 </a><span class="c2html-comment"> * Redistribution and use in source and binary forms, with or without</span> +<a class="c2html-lineno" name="l7" href="#l7"> 7 </a><span class="c2html-comment"> * modification, are permitted provided that the following conditions are met:</span> +<a class="c2html-lineno" name="l8" href="#l8"> 8 </a><span class="c2html-comment"> *</span> +<a class="c2html-lineno" name="l9" href="#l9"> 9 </a><span class="c2html-comment"> * 1. Redistributions of source code must retain the above copyright</span> +<a class="c2html-lineno" name="l10" href="#l10"> 10 </a><span class="c2html-comment"> * notice, this list of conditions and the following disclaimer.</span> +<a class="c2html-lineno" name="l11" href="#l11"> 11 </a><span class="c2html-comment"> *</span> +<a class="c2html-lineno" name="l12" href="#l12"> 12 </a><span class="c2html-comment"> * 2. Redistributions in binary form must reproduce the above copyright</span> +<a class="c2html-lineno" name="l13" href="#l13"> 13 </a><span class="c2html-comment"> * notice, this list of conditions and the following disclaimer in the</span> +<a class="c2html-lineno" name="l14" href="#l14"> 14 </a><span class="c2html-comment"> * documentation and/or other materials provided with the distribution.</span> +<a class="c2html-lineno" name="l15" href="#l15"> 15 </a><span class="c2html-comment"> *</span> +<a class="c2html-lineno" name="l16" href="#l16"> 16 </a><span class="c2html-comment"> * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"</span> +<a class="c2html-lineno" name="l17" href="#l17"> 17 </a><span class="c2html-comment"> * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE</span> +<a class="c2html-lineno" name="l18" href="#l18"> 18 </a><span class="c2html-comment"> * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE</span> +<a class="c2html-lineno" name="l19" href="#l19"> 19 </a><span class="c2html-comment"> * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE</span> +<a class="c2html-lineno" name="l20" href="#l20"> 20 </a><span class="c2html-comment"> * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR</span> +<a class="c2html-lineno" name="l21" href="#l21"> 21 </a><span class="c2html-comment"> * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF</span> +<a class="c2html-lineno" name="l22" href="#l22"> 22 </a><span class="c2html-comment"> * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS</span> +<a class="c2html-lineno" name="l23" href="#l23"> 23 </a><span class="c2html-comment"> * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN</span> +<a class="c2html-lineno" name="l24" href="#l24"> 24 </a><span class="c2html-comment"> * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)</span> +<a class="c2html-lineno" name="l25" href="#l25"> 25 </a><span class="c2html-comment"> * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE</span> +<a class="c2html-lineno" name="l26" href="#l26"> 26 </a><span class="c2html-comment"> * POSSIBILITY OF SUCH DAMAGE.</span> +<a class="c2html-lineno" name="l27" href="#l27"> 27 </a><span class="c2html-comment"> *</span> +<a class="c2html-lineno" name="l28" href="#l28"> 28 </a><span class="c2html-comment"> */</span> +<a class="c2html-lineno" name="l29" href="#l29"> 29 </a> +<a class="c2html-lineno" name="l30" href="#l30"> 30 </a><span class="c2html-directive">#include</span> <a class="c2html-userinclude" href="rules.h">"rules.h"</a> +<a class="c2html-lineno" name="l31" href="#l31"> 31 </a><span class="c2html-directive">#include</span> <a class="c2html-userinclude" href="chess.h">"chess.h"</a> +<a class="c2html-lineno" name="l32" href="#l32"> 32 </a><span class="c2html-directive">#include</span> <span class="c2html-stdinclude"><string.h></span> +<a class="c2html-lineno" name="l33" href="#l33"> 33 </a><span class="c2html-directive">#include</span> <span class="c2html-stdinclude"><stdlib.h></span> +<a class="c2html-lineno" name="l34" href="#l34"> 34 </a><span class="c2html-directive">#include</span> <span class="c2html-stdinclude"><sys/time.h></span> +<a class="c2html-lineno" name="l35" href="#l35"> 35 </a> +<a class="c2html-lineno" name="l36" href="#l36"> 36 </a><span class="c2html-keyword">static</span> GameState gamestate_copy_sim(GameState *gamestate) { +<a class="c2html-lineno" name="l37" href="#l37"> 37 </a> GameState simulation = *gamestate; +<a class="c2html-lineno" name="l38" href="#l38"> 38 </a> <span class="c2html-keyword">if</span> (simulation.lastmove) { +<a class="c2html-lineno" name="l39" href="#l39"> 39 </a> MoveList *lastmovecopy = malloc(<span class="c2html-keyword">sizeof</span>(MoveList)); +<a class="c2html-lineno" name="l40" href="#l40"> 40 </a> *lastmovecopy = *(simulation.lastmove); +<a class="c2html-lineno" name="l41" href="#l41"> 41 </a> simulation.movelist = simulation.lastmove = lastmovecopy; +<a class="c2html-lineno" name="l42" href="#l42"> 42 </a> } +<a class="c2html-lineno" name="l43" href="#l43"> 43 </a> +<a class="c2html-lineno" name="l44" href="#l44"> 44 </a> <span class="c2html-keyword">return</span> simulation; +<a class="c2html-lineno" name="l45" href="#l45"> 45 </a>} +<a class="c2html-lineno" name="l46" href="#l46"> 46 </a> +<a class="c2html-lineno" name="l47" href="#l47"> 47 </a><span class="c2html-keyword">void</span> gamestate_init(GameState *gamestate) { +<a class="c2html-lineno" name="l48" href="#l48"> 48 </a> memset(gamestate, <span class="c2html-macroconst">0</span>, <span class="c2html-keyword">sizeof</span>(GameState)); +<a class="c2html-lineno" name="l49" href="#l49"> 49 </a> +<a class="c2html-lineno" name="l50" href="#l50"> 50 </a> Board initboard = { +<a class="c2html-lineno" name="l51" href="#l51"> 51 </a> {<span class="c2html-macroconst">WROOK</span>, <span class="c2html-macroconst">WKNIGHT</span>, <span class="c2html-macroconst">WBISHOP</span>, <span class="c2html-macroconst">WQUEEN</span>, <span class="c2html-macroconst">WKING</span>, <span class="c2html-macroconst">WBISHOP</span>, <span class="c2html-macroconst">WKNIGHT</span>, <span class="c2html-macroconst">WROOK</span>}, +<a class="c2html-lineno" name="l52" href="#l52"> 52 </a> {<span class="c2html-macroconst">WPAWN</span>, <span class="c2html-macroconst">WPAWN</span>, <span class="c2html-macroconst">WPAWN</span>, <span class="c2html-macroconst">WPAWN</span>, <span class="c2html-macroconst">WPAWN</span>, <span class="c2html-macroconst">WPAWN</span>, <span class="c2html-macroconst">WPAWN</span>, <span class="c2html-macroconst">WPAWN</span>}, +<a class="c2html-lineno" name="l53" href="#l53"> 53 </a> {<span class="c2html-macroconst">0</span>, <span class="c2html-macroconst">0</span>, <span class="c2html-macroconst">0</span>, <span class="c2html-macroconst">0</span>, <span class="c2html-macroconst">0</span>, <span class="c2html-macroconst">0</span>, <span class="c2html-macroconst">0</span>, <span class="c2html-macroconst">0</span>}, +<a class="c2html-lineno" name="l54" href="#l54"> 54 </a> {<span class="c2html-macroconst">0</span>, <span class="c2html-macroconst">0</span>, <span class="c2html-macroconst">0</span>, <span class="c2html-macroconst">0</span>, <span class="c2html-macroconst">0</span>, <span class="c2html-macroconst">0</span>, <span class="c2html-macroconst">0</span>, <span class="c2html-macroconst">0</span>}, +<a class="c2html-lineno" name="l55" href="#l55"> 55 </a> {<span class="c2html-macroconst">0</span>, <span class="c2html-macroconst">0</span>, <span class="c2html-macroconst">0</span>, <span class="c2html-macroconst">0</span>, <span class="c2html-macroconst">0</span>, <span class="c2html-macroconst">0</span>, <span class="c2html-macroconst">0</span>, <span class="c2html-macroconst">0</span>}, +<a class="c2html-lineno" name="l56" href="#l56"> 56 </a> {<span class="c2html-macroconst">0</span>, <span class="c2html-macroconst">0</span>, <span class="c2html-macroconst">0</span>, <span class="c2html-macroconst">0</span>, <span class="c2html-macroconst">0</span>, <span class="c2html-macroconst">0</span>, <span class="c2html-macroconst">0</span>, <span class="c2html-macroconst">0</span>}, +<a class="c2html-lineno" name="l57" href="#l57"> 57 </a> {<span class="c2html-macroconst">BPAWN</span>, <span class="c2html-macroconst">BPAWN</span>, <span class="c2html-macroconst">BPAWN</span>, <span class="c2html-macroconst">BPAWN</span>, <span class="c2html-macroconst">BPAWN</span>, <span class="c2html-macroconst">BPAWN</span>, <span class="c2html-macroconst">BPAWN</span>, <span class="c2html-macroconst">BPAWN</span>}, +<a class="c2html-lineno" name="l58" href="#l58"> 58 </a> {<span class="c2html-macroconst">BROOK</span>, <span class="c2html-macroconst">BKNIGHT</span>, <span class="c2html-macroconst">BBISHOP</span>, <span class="c2html-macroconst">BQUEEN</span>, <span class="c2html-macroconst">BKING</span>, <span class="c2html-macroconst">BBISHOP</span>, <span class="c2html-macroconst">BKNIGHT</span>, <span class="c2html-macroconst">BROOK</span>} +<a class="c2html-lineno" name="l59" href="#l59"> 59 </a> }; +<a class="c2html-lineno" name="l60" href="#l60"> 60 </a> memcpy(gamestate->board, initboard, <span class="c2html-keyword">sizeof</span>(Board)); +<a class="c2html-lineno" name="l61" href="#l61"> 61 </a>} +<a class="c2html-lineno" name="l62" href="#l62"> 62 </a> +<a class="c2html-lineno" name="l63" href="#l63"> 63 </a><span class="c2html-keyword">void</span> gamestate_cleanup(GameState *gamestate) { +<a class="c2html-lineno" name="l64" href="#l64"> 64 </a> MoveList *elem; +<a class="c2html-lineno" name="l65" href="#l65"> 65 </a> elem = gamestate->movelist; +<a class="c2html-lineno" name="l66" href="#l66"> 66 </a> <span class="c2html-keyword">while</span> (elem) { +<a class="c2html-lineno" name="l67" href="#l67"> 67 </a> MoveList *cur = elem; +<a class="c2html-lineno" name="l68" href="#l68"> 68 </a> elem = elem->next; +<a class="c2html-lineno" name="l69" href="#l69"> 69 </a> free(cur); +<a class="c2html-lineno" name="l70" href="#l70"> 70 </a> }; +<a class="c2html-lineno" name="l71" href="#l71"> 71 </a>} +<a class="c2html-lineno" name="l72" href="#l72"> 72 </a> +<a class="c2html-lineno" name="l73" href="#l73"> 73 </a><span class="c2html-comment">/* MUST be called IMMEDIATLY after applying a move to work correctly */</span> +<a class="c2html-lineno" name="l74" href="#l74"> 74 </a><span class="c2html-keyword">static</span> <span class="c2html-keyword">void</span> format_move(GameState *gamestate, Move *move) { +<a class="c2html-lineno" name="l75" href="#l75"> 75 </a> <span class="c2html-keyword">char</span> *string = move->string; +<a class="c2html-lineno" name="l76" href="#l76"> 76 </a> +<a class="c2html-lineno" name="l77" href="#l77"> 77 </a> <span class="c2html-comment">/* at least 8 characters should be available, wipe them out */</span> +<a class="c2html-lineno" name="l78" href="#l78"> 78 </a> memset(string, <span class="c2html-macroconst">0</span>, <span class="c2html-macroconst">8</span>); +<a class="c2html-lineno" name="l79" href="#l79"> 79 </a> +<a class="c2html-lineno" name="l80" href="#l80"> 80 </a> <span class="c2html-comment">/* special formats for castling */</span> +<a class="c2html-lineno" name="l81" href="#l81"> 81 </a> <span class="c2html-keyword">if</span> ((move->piece&<span class="c2html-macroconst">PIECE_MASK</span>) == <span class="c2html-macroconst">KING</span> && +<a class="c2html-lineno" name="l82" href="#l82"> 82 </a> abs(move->tofile-move->fromfile) == <span class="c2html-macroconst">2</span>) { +<a class="c2html-lineno" name="l83" href="#l83"> 83 </a> <span class="c2html-keyword">if</span> (move->tofile==fileidx(<span class="c2html-string">'c'</span>)) { +<a class="c2html-lineno" name="l84" href="#l84"> 84 </a> memcpy(string, <span class="c2html-string">"O-O-O"</span>, <span class="c2html-macroconst">5</span>); +<a class="c2html-lineno" name="l85" href="#l85"> 85 </a> } <span class="c2html-keyword">else</span> { +<a class="c2html-lineno" name="l86" href="#l86"> 86 </a> memcpy(string, <span class="c2html-string">"O-O"</span>, <span class="c2html-macroconst">3</span>); +<a class="c2html-lineno" name="l87" href="#l87"> 87 </a> } +<a class="c2html-lineno" name="l88" href="#l88"> 88 </a> } +<a class="c2html-lineno" name="l89" href="#l89"> 89 </a> +<a class="c2html-lineno" name="l90" href="#l90"> 90 </a> <span class="c2html-comment">/* start by notating the piece character */</span> +<a class="c2html-lineno" name="l91" href="#l91"> 91 </a> string[<span class="c2html-macroconst">0</span>] = getpiecechr(move->piece); +<a class="c2html-lineno" name="l92" href="#l92"> 92 </a> <span class="c2html-keyword">int</span> idx = string[<span class="c2html-macroconst">0</span>] ? <span class="c2html-macroconst">1</span> : <span class="c2html-macroconst">0</span>; +<a class="c2html-lineno" name="l93" href="#l93"> 93 </a> +<a class="c2html-lineno" name="l94" href="#l94"> 94 </a> <span class="c2html-comment">/* find out how many source information we do need */</span> +<a class="c2html-lineno" name="l95" href="#l95"> 95 </a> <span class="c2html-type">uint8_t</span> piece = move->piece & <span class="c2html-macroconst">PIECE_MASK</span>; +<a class="c2html-lineno" name="l96" href="#l96"> 96 </a> <span class="c2html-keyword">if</span> (piece == <span class="c2html-macroconst">PAWN</span>) { +<a class="c2html-lineno" name="l97" href="#l97"> 97 </a> <span class="c2html-keyword">if</span> (move->capture) { +<a class="c2html-lineno" name="l98" href="#l98"> 98 </a> string[idx++] = filechr(move->fromfile); +<a class="c2html-lineno" name="l99" href="#l99"> 99 </a> } +<a class="c2html-lineno" name="l100" href="#l100">100 </a> } <span class="c2html-keyword">else</span> <span class="c2html-keyword">if</span> (piece != <span class="c2html-macroconst">KING</span>) { +<a class="c2html-lineno" name="l101" href="#l101">101 </a> Move threats[<span class="c2html-macroconst">16</span>]; +<a class="c2html-lineno" name="l102" href="#l102">102 </a> <span class="c2html-type">uint8_t</span> threatcount; +<a class="c2html-lineno" name="l103" href="#l103">103 </a> get_real_threats(gamestate, move->torow, move->tofile, +<a class="c2html-lineno" name="l104" href="#l104">104 </a> move->piece&<span class="c2html-macroconst">COLOR_MASK</span>, threats, &threatcount); +<a class="c2html-lineno" name="l105" href="#l105">105 </a> <span class="c2html-keyword">if</span> (threatcount > <span class="c2html-macroconst">1</span>) { +<a class="c2html-lineno" name="l106" href="#l106">106 </a> <span class="c2html-keyword">int</span> ambrows = <span class="c2html-macroconst">0</span>, ambfiles = <span class="c2html-macroconst">0</span>; +<a class="c2html-lineno" name="l107" href="#l107">107 </a> <span class="c2html-keyword">for</span> (<span class="c2html-type">uint8_t</span> i = <span class="c2html-macroconst">0</span> ; i < threatcount ; i++) { +<a class="c2html-lineno" name="l108" href="#l108">108 </a> <span class="c2html-keyword">if</span> (threats[i].fromrow == move->fromrow) { +<a class="c2html-lineno" name="l109" href="#l109">109 </a> ambrows++; +<a class="c2html-lineno" name="l110" href="#l110">110 </a> } +<a class="c2html-lineno" name="l111" href="#l111">111 </a> <span class="c2html-keyword">if</span> (threats[i].fromfile == move->fromfile) { +<a class="c2html-lineno" name="l112" href="#l112">112 </a> ambfiles++; +<a class="c2html-lineno" name="l113" href="#l113">113 </a> } +<a class="c2html-lineno" name="l114" href="#l114">114 </a> } +<a class="c2html-lineno" name="l115" href="#l115">115 </a> <span class="c2html-comment">/* ambiguous row, name file */</span> +<a class="c2html-lineno" name="l116" href="#l116">116 </a> <span class="c2html-keyword">if</span> (ambrows > <span class="c2html-macroconst">1</span>) { +<a class="c2html-lineno" name="l117" href="#l117">117 </a> string[idx++] = filechr(move->fromfile); +<a class="c2html-lineno" name="l118" href="#l118">118 </a> } +<a class="c2html-lineno" name="l119" href="#l119">119 </a> <span class="c2html-comment">/* ambiguous file, name row */</span> +<a class="c2html-lineno" name="l120" href="#l120">120 </a> <span class="c2html-keyword">if</span> (ambfiles > <span class="c2html-macroconst">1</span>) { +<a class="c2html-lineno" name="l121" href="#l121">121 </a> string[idx++] = filechr(move->fromrow); +<a class="c2html-lineno" name="l122" href="#l122">122 </a> } +<a class="c2html-lineno" name="l123" href="#l123">123 </a> } +<a class="c2html-lineno" name="l124" href="#l124">124 </a> } +<a class="c2html-lineno" name="l125" href="#l125">125 </a> +<a class="c2html-lineno" name="l126" href="#l126">126 </a> <span class="c2html-comment">/* capturing? */</span> +<a class="c2html-lineno" name="l127" href="#l127">127 </a> <span class="c2html-keyword">if</span> (move->capture) { +<a class="c2html-lineno" name="l128" href="#l128">128 </a> string[idx++] = <span class="c2html-string">'x'</span>; +<a class="c2html-lineno" name="l129" href="#l129">129 </a> } +<a class="c2html-lineno" name="l130" href="#l130">130 </a> +<a class="c2html-lineno" name="l131" href="#l131">131 </a> <span class="c2html-comment">/* destination */</span> +<a class="c2html-lineno" name="l132" href="#l132">132 </a> string[idx++] = filechr(move->tofile); +<a class="c2html-lineno" name="l133" href="#l133">133 </a> string[idx++] = rowchr(move->torow); +<a class="c2html-lineno" name="l134" href="#l134">134 </a> +<a class="c2html-lineno" name="l135" href="#l135">135 </a> <span class="c2html-comment">/* promotion? */</span> +<a class="c2html-lineno" name="l136" href="#l136">136 </a> <span class="c2html-keyword">if</span> (move->promotion) { +<a class="c2html-lineno" name="l137" href="#l137">137 </a> string[idx++] = <span class="c2html-string">'='</span>; +<a class="c2html-lineno" name="l138" href="#l138">138 </a> string[idx++] = getpiecechr(move->promotion); +<a class="c2html-lineno" name="l139" href="#l139">139 </a> } +<a class="c2html-lineno" name="l140" href="#l140">140 </a> +<a class="c2html-lineno" name="l141" href="#l141">141 </a> <span class="c2html-comment">/* check? */</span> +<a class="c2html-lineno" name="l142" href="#l142">142 </a> <span class="c2html-keyword">if</span> (move->check) { +<a class="c2html-lineno" name="l143" href="#l143">143 </a> <span class="c2html-comment">/* works only, if this function is called when applying the move */</span> +<a class="c2html-lineno" name="l144" href="#l144">144 </a> string[idx++] = gamestate->checkmate?<span class="c2html-string">'#'</span>:<span class="c2html-string">'+'</span>; +<a class="c2html-lineno" name="l145" href="#l145">145 </a> } +<a class="c2html-lineno" name="l146" href="#l146">146 </a>} +<a class="c2html-lineno" name="l147" href="#l147">147 </a> +<a class="c2html-lineno" name="l148" href="#l148">148 </a><span class="c2html-keyword">static</span> <span class="c2html-keyword">void</span> addmove(GameState* gamestate, Move *move) { +<a class="c2html-lineno" name="l149" href="#l149">149 </a> MoveList *elem = malloc(<span class="c2html-keyword">sizeof</span>(MoveList)); +<a class="c2html-lineno" name="l150" href="#l150">150 </a> elem->next = <span class="c2html-macroconst">NULL</span>; +<a class="c2html-lineno" name="l151" href="#l151">151 </a> elem->move = *move; +<a class="c2html-lineno" name="l152" href="#l152">152 </a> +<a class="c2html-lineno" name="l153" href="#l153">153 </a> <span class="c2html-keyword">struct</span> timeval curtimestamp; +<a class="c2html-lineno" name="l154" href="#l154">154 </a> gettimeofday(&curtimestamp, <span class="c2html-macroconst">NULL</span>); +<a class="c2html-lineno" name="l155" href="#l155">155 </a> elem->move.timestamp.tv_sec = curtimestamp.tv_sec; +<a class="c2html-lineno" name="l156" href="#l156">156 </a> elem->move.timestamp.tv_usec = curtimestamp.tv_usec; +<a class="c2html-lineno" name="l157" href="#l157">157 </a> +<a class="c2html-lineno" name="l158" href="#l158">158 </a> <span class="c2html-keyword">if</span> (gamestate->lastmove) { +<a class="c2html-lineno" name="l159" href="#l159">159 </a> <span class="c2html-keyword">struct</span> movetimeval *lasttstamp = &(gamestate->lastmove->move.timestamp); +<a class="c2html-lineno" name="l160" href="#l160">160 </a> <span class="c2html-type">uint64_t</span> sec = curtimestamp.tv_sec - lasttstamp->tv_sec; +<a class="c2html-lineno" name="l161" href="#l161">161 </a> <span class="c2html-type">suseconds_t</span> micros; +<a class="c2html-lineno" name="l162" href="#l162">162 </a> <span class="c2html-keyword">if</span> (curtimestamp.tv_usec < lasttstamp->tv_usec) { +<a class="c2html-lineno" name="l163" href="#l163">163 </a> micros = 1e6L-(lasttstamp->tv_usec - curtimestamp.tv_usec); +<a class="c2html-lineno" name="l164" href="#l164">164 </a> sec--; +<a class="c2html-lineno" name="l165" href="#l165">165 </a> } <span class="c2html-keyword">else</span> { +<a class="c2html-lineno" name="l166" href="#l166">166 </a> micros = curtimestamp.tv_usec - lasttstamp->tv_usec; +<a class="c2html-lineno" name="l167" href="#l167">167 </a> } +<a class="c2html-lineno" name="l168" href="#l168">168 </a> +<a class="c2html-lineno" name="l169" href="#l169">169 </a> elem->move.movetime.tv_sec = sec; +<a class="c2html-lineno" name="l170" href="#l170">170 </a> elem->move.movetime.tv_usec = micros; +<a class="c2html-lineno" name="l171" href="#l171">171 </a> +<a class="c2html-lineno" name="l172" href="#l172">172 </a> gamestate->lastmove->next = elem; +<a class="c2html-lineno" name="l173" href="#l173">173 </a> gamestate->lastmove = elem; +<a class="c2html-lineno" name="l174" href="#l174">174 </a> } <span class="c2html-keyword">else</span> { +<a class="c2html-lineno" name="l175" href="#l175">175 </a> elem->move.movetime.tv_usec = <span class="c2html-macroconst">0</span>; +<a class="c2html-lineno" name="l176" href="#l176">176 </a> elem->move.movetime.tv_sec = <span class="c2html-macroconst">0</span>; +<a class="c2html-lineno" name="l177" href="#l177">177 </a> gamestate->movelist = gamestate->lastmove = elem; +<a class="c2html-lineno" name="l178" href="#l178">178 </a> } +<a class="c2html-lineno" name="l179" href="#l179">179 </a>} +<a class="c2html-lineno" name="l180" href="#l180">180 </a> +<a class="c2html-lineno" name="l181" href="#l181">181 </a><span class="c2html-keyword">char</span> getpiecechr(<span class="c2html-type">uint8_t</span> piece) { +<a class="c2html-lineno" name="l182" href="#l182">182 </a> <span class="c2html-keyword">switch</span> (piece & <span class="c2html-macroconst">PIECE_MASK</span>) { +<a class="c2html-lineno" name="l183" href="#l183">183 </a> <span class="c2html-keyword">case</span> <span class="c2html-macroconst">ROOK</span>: <span class="c2html-keyword">return</span> <span class="c2html-string">'R'</span>; +<a class="c2html-lineno" name="l184" href="#l184">184 </a> <span class="c2html-keyword">case</span> <span class="c2html-macroconst">KNIGHT</span>: <span class="c2html-keyword">return</span> <span class="c2html-string">'N'</span>; +<a class="c2html-lineno" name="l185" href="#l185">185 </a> <span class="c2html-keyword">case</span> <span class="c2html-macroconst">BISHOP</span>: <span class="c2html-keyword">return</span> <span class="c2html-string">'B'</span>; +<a class="c2html-lineno" name="l186" href="#l186">186 </a> <span class="c2html-keyword">case</span> <span class="c2html-macroconst">QUEEN</span>: <span class="c2html-keyword">return</span> <span class="c2html-string">'Q'</span>; +<a class="c2html-lineno" name="l187" href="#l187">187 </a> <span class="c2html-keyword">case</span> <span class="c2html-macroconst">KING</span>: <span class="c2html-keyword">return</span> <span class="c2html-string">'K'</span>; +<a class="c2html-lineno" name="l188" href="#l188">188 </a> <span class="c2html-keyword">default</span>: <span class="c2html-keyword">return</span> <span class="c2html-string">'\0'</span>; +<a class="c2html-lineno" name="l189" href="#l189">189 </a> } +<a class="c2html-lineno" name="l190" href="#l190">190 </a>} +<a class="c2html-lineno" name="l191" href="#l191">191 </a> +<a class="c2html-lineno" name="l192" href="#l192">192 </a><span class="c2html-type">uint8_t</span> getpiece(<span class="c2html-keyword">char</span> c) { +<a class="c2html-lineno" name="l193" href="#l193">193 </a> <span class="c2html-keyword">switch</span> (c) { +<a class="c2html-lineno" name="l194" href="#l194">194 </a> <span class="c2html-keyword">case</span> <span class="c2html-string">'R'</span>: <span class="c2html-keyword">return</span> <span class="c2html-macroconst">ROOK</span>; +<a class="c2html-lineno" name="l195" href="#l195">195 </a> <span class="c2html-keyword">case</span> <span class="c2html-string">'N'</span>: <span class="c2html-keyword">return</span> <span class="c2html-macroconst">KNIGHT</span>; +<a class="c2html-lineno" name="l196" href="#l196">196 </a> <span class="c2html-keyword">case</span> <span class="c2html-string">'B'</span>: <span class="c2html-keyword">return</span> <span class="c2html-macroconst">BISHOP</span>; +<a class="c2html-lineno" name="l197" href="#l197">197 </a> <span class="c2html-keyword">case</span> <span class="c2html-string">'Q'</span>: <span class="c2html-keyword">return</span> <span class="c2html-macroconst">QUEEN</span>; +<a class="c2html-lineno" name="l198" href="#l198">198 </a> <span class="c2html-keyword">case</span> <span class="c2html-string">'K'</span>: <span class="c2html-keyword">return</span> <span class="c2html-macroconst">KING</span>; +<a class="c2html-lineno" name="l199" href="#l199">199 </a> <span class="c2html-keyword">default</span>: <span class="c2html-keyword">return</span> <span class="c2html-macroconst">0</span>; +<a class="c2html-lineno" name="l200" href="#l200">200 </a> } +<a class="c2html-lineno" name="l201" href="#l201">201 </a>} +<a class="c2html-lineno" name="l202" href="#l202">202 </a> +<a class="c2html-lineno" name="l203" href="#l203">203 </a><span class="c2html-keyword">static</span> <span class="c2html-keyword">void</span> apply_move_impl(GameState *gamestate, Move *move, _Bool simulate) { +<a class="c2html-lineno" name="l204" href="#l204">204 </a> <span class="c2html-type">uint8_t</span> piece = move->piece & <span class="c2html-macroconst">PIECE_MASK</span>; +<a class="c2html-lineno" name="l205" href="#l205">205 </a> <span class="c2html-type">uint8_t</span> color = move->piece & <span class="c2html-macroconst">COLOR_MASK</span>; +<a class="c2html-lineno" name="l206" href="#l206">206 </a> +<a class="c2html-lineno" name="l207" href="#l207">207 </a> <span class="c2html-comment">/* en passant capture */</span> +<a class="c2html-lineno" name="l208" href="#l208">208 </a> <span class="c2html-keyword">if</span> (move->capture && piece == <span class="c2html-macroconst">PAWN</span> && +<a class="c2html-lineno" name="l209" href="#l209">209 </a> mdst(gamestate->board, move) == <span class="c2html-macroconst">0</span>) { +<a class="c2html-lineno" name="l210" href="#l210">210 </a> gamestate->board[move->fromrow][move->tofile] = <span class="c2html-macroconst">0</span>; +<a class="c2html-lineno" name="l211" href="#l211">211 </a> } +<a class="c2html-lineno" name="l212" href="#l212">212 </a> +<a class="c2html-lineno" name="l213" href="#l213">213 </a> <span class="c2html-comment">/* remove old en passant threats */</span> +<a class="c2html-lineno" name="l214" href="#l214">214 </a> <span class="c2html-keyword">for</span> (<span class="c2html-type">uint8_t</span> file = <span class="c2html-macroconst">0</span> ; file < <span class="c2html-macroconst">8</span> ; file++) { +<a class="c2html-lineno" name="l215" href="#l215">215 </a> gamestate->board[<span class="c2html-macroconst">3</span>][file] &= ~<span class="c2html-macroconst">ENPASSANT_THREAT</span>; +<a class="c2html-lineno" name="l216" href="#l216">216 </a> gamestate->board[<span class="c2html-macroconst">4</span>][file] &= ~<span class="c2html-macroconst">ENPASSANT_THREAT</span>; +<a class="c2html-lineno" name="l217" href="#l217">217 </a> } +<a class="c2html-lineno" name="l218" href="#l218">218 </a> +<a class="c2html-lineno" name="l219" href="#l219">219 </a> <span class="c2html-comment">/* add new en passant threat */</span> +<a class="c2html-lineno" name="l220" href="#l220">220 </a> <span class="c2html-keyword">if</span> (piece == <span class="c2html-macroconst">PAWN</span> && ( +<a class="c2html-lineno" name="l221" href="#l221">221 </a> (move->fromrow == <span class="c2html-macroconst">1</span> && move->torow == <span class="c2html-macroconst">3</span>) || +<a class="c2html-lineno" name="l222" href="#l222">222 </a> (move->fromrow == <span class="c2html-macroconst">6</span> && move->torow == <span class="c2html-macroconst">4</span>))) { +<a class="c2html-lineno" name="l223" href="#l223">223 </a> move->piece |= <span class="c2html-macroconst">ENPASSANT_THREAT</span>; +<a class="c2html-lineno" name="l224" href="#l224">224 </a> } +<a class="c2html-lineno" name="l225" href="#l225">225 </a> +<a class="c2html-lineno" name="l226" href="#l226">226 </a> <span class="c2html-comment">/* move (and maybe capture or promote) */</span> +<a class="c2html-lineno" name="l227" href="#l227">227 </a> msrc(gamestate->board, move) = <span class="c2html-macroconst">0</span>; +<a class="c2html-lineno" name="l228" href="#l228">228 </a> <span class="c2html-keyword">if</span> (move->promotion) { +<a class="c2html-lineno" name="l229" href="#l229">229 </a> mdst(gamestate->board, move) = move->promotion; +<a class="c2html-lineno" name="l230" href="#l230">230 </a> } <span class="c2html-keyword">else</span> { +<a class="c2html-lineno" name="l231" href="#l231">231 </a> mdst(gamestate->board, move) = move->piece; +<a class="c2html-lineno" name="l232" href="#l232">232 </a> } +<a class="c2html-lineno" name="l233" href="#l233">233 </a> +<a class="c2html-lineno" name="l234" href="#l234">234 </a> <span class="c2html-comment">/* castling */</span> +<a class="c2html-lineno" name="l235" href="#l235">235 </a> <span class="c2html-keyword">if</span> (piece == <span class="c2html-macroconst">KING</span> && move->fromfile == fileidx(<span class="c2html-string">'e'</span>)) { +<a class="c2html-lineno" name="l236" href="#l236">236 </a> +<a class="c2html-lineno" name="l237" href="#l237">237 </a> <span class="c2html-keyword">if</span> (move->tofile == fileidx(<span class="c2html-string">'g'</span>)) { +<a class="c2html-lineno" name="l238" href="#l238">238 </a> gamestate->board[move->torow][fileidx(<span class="c2html-string">'h'</span>)] = <span class="c2html-macroconst">0</span>; +<a class="c2html-lineno" name="l239" href="#l239">239 </a> gamestate->board[move->torow][fileidx(<span class="c2html-string">'f'</span>)] = color|<span class="c2html-macroconst">ROOK</span>; +<a class="c2html-lineno" name="l240" href="#l240">240 </a> } <span class="c2html-keyword">else</span> <span class="c2html-keyword">if</span> (move->tofile == fileidx(<span class="c2html-string">'c'</span>)) { +<a class="c2html-lineno" name="l241" href="#l241">241 </a> gamestate->board[move->torow][fileidx(<span class="c2html-string">'a'</span>)] = <span class="c2html-macroconst">0</span>; +<a class="c2html-lineno" name="l242" href="#l242">242 </a> gamestate->board[move->torow][fileidx(<span class="c2html-string">'d'</span>)] = color|<span class="c2html-macroconst">ROOK</span>; +<a class="c2html-lineno" name="l243" href="#l243">243 </a> } +<a class="c2html-lineno" name="l244" href="#l244">244 </a> } +<a class="c2html-lineno" name="l245" href="#l245">245 </a> +<a class="c2html-lineno" name="l246" href="#l246">246 </a> <span class="c2html-keyword">if</span> (!simulate) { +<a class="c2html-lineno" name="l247" href="#l247">247 </a> <span class="c2html-keyword">if</span> (!move->string[<span class="c2html-macroconst">0</span>]) { +<a class="c2html-lineno" name="l248" href="#l248">248 </a> format_move(gamestate, move); +<a class="c2html-lineno" name="l249" href="#l249">249 </a> } +<a class="c2html-lineno" name="l250" href="#l250">250 </a> } +<a class="c2html-lineno" name="l251" href="#l251">251 </a> <span class="c2html-comment">/* add move, even in simulation (checkmate test needs it) */</span> +<a class="c2html-lineno" name="l252" href="#l252">252 </a> addmove(gamestate, move); +<a class="c2html-lineno" name="l253" href="#l253">253 </a>} +<a class="c2html-lineno" name="l254" href="#l254">254 </a> +<a class="c2html-lineno" name="l255" href="#l255">255 </a><span class="c2html-keyword">void</span> apply_move(GameState *gamestate, Move *move) { +<a class="c2html-lineno" name="l256" href="#l256">256 </a> apply_move_impl(gamestate, move, <span class="c2html-macroconst">0</span>); +<a class="c2html-lineno" name="l257" href="#l257">257 </a>} +<a class="c2html-lineno" name="l258" href="#l258">258 </a> +<a class="c2html-lineno" name="l259" href="#l259">259 </a><span class="c2html-keyword">static</span> <span class="c2html-keyword">int</span> validate_move_rules(GameState *gamestate, Move *move) { +<a class="c2html-lineno" name="l260" href="#l260">260 </a> <span class="c2html-comment">/* validate indices (don't trust opponent) */</span> +<a class="c2html-lineno" name="l261" href="#l261">261 </a> <span class="c2html-keyword">if</span> (!chkidx(move)) { +<a class="c2html-lineno" name="l262" href="#l262">262 </a> <span class="c2html-keyword">return</span> <span class="c2html-macroconst">INVALID_POSITION</span>; +<a class="c2html-lineno" name="l263" href="#l263">263 </a> } +<a class="c2html-lineno" name="l264" href="#l264">264 </a> +<a class="c2html-lineno" name="l265" href="#l265">265 </a> <span class="c2html-comment">/* must move */</span> +<a class="c2html-lineno" name="l266" href="#l266">266 </a> <span class="c2html-keyword">if</span> (move->fromfile == move->tofile && move->fromrow == move->torow) { +<a class="c2html-lineno" name="l267" href="#l267">267 </a> <span class="c2html-keyword">return</span> <span class="c2html-macroconst">INVALID_MOVE_SYNTAX</span>; +<a class="c2html-lineno" name="l268" href="#l268">268 </a> } +<a class="c2html-lineno" name="l269" href="#l269">269 </a> +<a class="c2html-lineno" name="l270" href="#l270">270 </a> <span class="c2html-comment">/* does piece exist */</span> +<a class="c2html-lineno" name="l271" href="#l271">271 </a> <span class="c2html-keyword">if</span> ((msrc(gamestate->board, move)&(<span class="c2html-macroconst">PIECE_MASK</span>|<span class="c2html-macroconst">COLOR_MASK</span>)) +<a class="c2html-lineno" name="l272" href="#l272">272 </a> != (move->piece&(<span class="c2html-macroconst">PIECE_MASK</span>|<span class="c2html-macroconst">COLOR_MASK</span>))) { +<a class="c2html-lineno" name="l273" href="#l273">273 </a> <span class="c2html-keyword">return</span> <span class="c2html-macroconst">INVALID_POSITION</span>; +<a class="c2html-lineno" name="l274" href="#l274">274 </a> } +<a class="c2html-lineno" name="l275" href="#l275">275 </a> +<a class="c2html-lineno" name="l276" href="#l276">276 </a> <span class="c2html-comment">/* can't capture own pieces */</span> +<a class="c2html-lineno" name="l277" href="#l277">277 </a> <span class="c2html-keyword">if</span> ((mdst(gamestate->board, move) & <span class="c2html-macroconst">COLOR_MASK</span>) +<a class="c2html-lineno" name="l278" href="#l278">278 </a> == (move->piece & <span class="c2html-macroconst">COLOR_MASK</span>)) { +<a class="c2html-lineno" name="l279" href="#l279">279 </a> <span class="c2html-keyword">return</span> <span class="c2html-macroconst">RULES_VIOLATED</span>; +<a class="c2html-lineno" name="l280" href="#l280">280 </a> } +<a class="c2html-lineno" name="l281" href="#l281">281 </a> +<a class="c2html-lineno" name="l282" href="#l282">282 </a> <span class="c2html-comment">/* must capture, if and only if destination is occupied */</span> +<a class="c2html-lineno" name="l283" href="#l283">283 </a> <span class="c2html-keyword">if</span> ((mdst(gamestate->board, move) == <span class="c2html-macroconst">0</span> && move->capture) || +<a class="c2html-lineno" name="l284" href="#l284">284 </a> (mdst(gamestate->board, move) != <span class="c2html-macroconst">0</span> && !move->capture)) { +<a class="c2html-lineno" name="l285" href="#l285">285 </a> <span class="c2html-keyword">return</span> <span class="c2html-macroconst">INVALID_MOVE_SYNTAX</span>; +<a class="c2html-lineno" name="l286" href="#l286">286 </a> } +<a class="c2html-lineno" name="l287" href="#l287">287 </a> +<a class="c2html-lineno" name="l288" href="#l288">288 </a> <span class="c2html-comment">/* validate individual rules */</span> +<a class="c2html-lineno" name="l289" href="#l289">289 </a> _Bool chkrules; +<a class="c2html-lineno" name="l290" href="#l290">290 </a> <span class="c2html-keyword">switch</span> (move->piece & <span class="c2html-macroconst">PIECE_MASK</span>) { +<a class="c2html-lineno" name="l291" href="#l291">291 </a> <span class="c2html-keyword">case</span> <span class="c2html-macroconst">PAWN</span>: +<a class="c2html-lineno" name="l292" href="#l292">292 </a> chkrules = pawn_chkrules(gamestate, move) && +<a class="c2html-lineno" name="l293" href="#l293">293 </a> !pawn_isblocked(gamestate, move); +<a class="c2html-lineno" name="l294" href="#l294">294 </a> <span class="c2html-keyword">break</span>; +<a class="c2html-lineno" name="l295" href="#l295">295 </a> <span class="c2html-keyword">case</span> <span class="c2html-macroconst">ROOK</span>: +<a class="c2html-lineno" name="l296" href="#l296">296 </a> chkrules = rook_chkrules(move) && +<a class="c2html-lineno" name="l297" href="#l297">297 </a> !rook_isblocked(gamestate, move); +<a class="c2html-lineno" name="l298" href="#l298">298 </a> <span class="c2html-keyword">break</span>; +<a class="c2html-lineno" name="l299" href="#l299">299 </a> <span class="c2html-keyword">case</span> <span class="c2html-macroconst">KNIGHT</span>: +<a class="c2html-lineno" name="l300" href="#l300">300 </a> chkrules = knight_chkrules(move); <span class="c2html-comment">/* knight is never blocked */</span> +<a class="c2html-lineno" name="l301" href="#l301">301 </a> <span class="c2html-keyword">break</span>; +<a class="c2html-lineno" name="l302" href="#l302">302 </a> <span class="c2html-keyword">case</span> <span class="c2html-macroconst">BISHOP</span>: +<a class="c2html-lineno" name="l303" href="#l303">303 </a> chkrules = bishop_chkrules(move) && +<a class="c2html-lineno" name="l304" href="#l304">304 </a> !bishop_isblocked(gamestate, move); +<a class="c2html-lineno" name="l305" href="#l305">305 </a> <span class="c2html-keyword">break</span>; +<a class="c2html-lineno" name="l306" href="#l306">306 </a> <span class="c2html-keyword">case</span> <span class="c2html-macroconst">QUEEN</span>: +<a class="c2html-lineno" name="l307" href="#l307">307 </a> chkrules = queen_chkrules(move) && +<a class="c2html-lineno" name="l308" href="#l308">308 </a> !queen_isblocked(gamestate, move); +<a class="c2html-lineno" name="l309" href="#l309">309 </a> <span class="c2html-keyword">break</span>; +<a class="c2html-lineno" name="l310" href="#l310">310 </a> <span class="c2html-keyword">case</span> <span class="c2html-macroconst">KING</span>: +<a class="c2html-lineno" name="l311" href="#l311">311 </a> chkrules = king_chkrules(gamestate, move) && +<a class="c2html-lineno" name="l312" href="#l312">312 </a> !king_isblocked(gamestate, move); +<a class="c2html-lineno" name="l313" href="#l313">313 </a> <span class="c2html-keyword">break</span>; +<a class="c2html-lineno" name="l314" href="#l314">314 </a> <span class="c2html-keyword">default</span>: +<a class="c2html-lineno" name="l315" href="#l315">315 </a> <span class="c2html-keyword">return</span> <span class="c2html-macroconst">INVALID_MOVE_SYNTAX</span>; +<a class="c2html-lineno" name="l316" href="#l316">316 </a> } +<a class="c2html-lineno" name="l317" href="#l317">317 </a> +<a class="c2html-lineno" name="l318" href="#l318">318 </a> <span class="c2html-keyword">return</span> chkrules ? <span class="c2html-macroconst">VALID_MOVE_SEMANTICS</span> : <span class="c2html-macroconst">RULES_VIOLATED</span>; +<a class="c2html-lineno" name="l319" href="#l319">319 </a>} +<a class="c2html-lineno" name="l320" href="#l320">320 </a> +<a class="c2html-lineno" name="l321" href="#l321">321 </a><span class="c2html-keyword">int</span> validate_move(GameState *gamestate, Move *move) { +<a class="c2html-lineno" name="l322" href="#l322">322 </a> +<a class="c2html-lineno" name="l323" href="#l323">323 </a> <span class="c2html-keyword">int</span> result = validate_move_rules(gamestate, move); +<a class="c2html-lineno" name="l324" href="#l324">324 </a> +<a class="c2html-lineno" name="l325" href="#l325">325 </a> <span class="c2html-comment">/* cancel processing to save resources */</span> +<a class="c2html-lineno" name="l326" href="#l326">326 </a> <span class="c2html-keyword">if</span> (result != <span class="c2html-macroconst">VALID_MOVE_SEMANTICS</span>) { +<a class="c2html-lineno" name="l327" href="#l327">327 </a> <span class="c2html-keyword">return</span> result; +<a class="c2html-lineno" name="l328" href="#l328">328 </a> } +<a class="c2html-lineno" name="l329" href="#l329">329 </a> +<a class="c2html-lineno" name="l330" href="#l330">330 </a> <span class="c2html-comment">/* find kings for check validation */</span> +<a class="c2html-lineno" name="l331" href="#l331">331 </a> <span class="c2html-type">uint8_t</span> piececolor = (move->piece & <span class="c2html-macroconst">COLOR_MASK</span>); +<a class="c2html-lineno" name="l332" href="#l332">332 </a> +<a class="c2html-lineno" name="l333" href="#l333">333 </a> <span class="c2html-type">uint8_t</span> mykingfile = <span class="c2html-macroconst">0</span>, mykingrow = <span class="c2html-macroconst">0</span>, opkingfile = <span class="c2html-macroconst">0</span>, opkingrow = <span class="c2html-macroconst">0</span>; +<a class="c2html-lineno" name="l334" href="#l334">334 </a> <span class="c2html-keyword">for</span> (<span class="c2html-type">uint8_t</span> row = <span class="c2html-macroconst">0</span> ; row < <span class="c2html-macroconst">8</span> ; row++) { +<a class="c2html-lineno" name="l335" href="#l335">335 </a> <span class="c2html-keyword">for</span> (<span class="c2html-type">uint8_t</span> file = <span class="c2html-macroconst">0</span> ; file < <span class="c2html-macroconst">8</span> ; file++) { +<a class="c2html-lineno" name="l336" href="#l336">336 </a> <span class="c2html-keyword">if</span> (gamestate->board[row][file] == +<a class="c2html-lineno" name="l337" href="#l337">337 </a> (piececolor == <span class="c2html-macroconst">WHITE</span>?<span class="c2html-macroconst">WKING</span>:<span class="c2html-macroconst">BKING</span>)) { +<a class="c2html-lineno" name="l338" href="#l338">338 </a> mykingfile = file; +<a class="c2html-lineno" name="l339" href="#l339">339 </a> mykingrow = row; +<a class="c2html-lineno" name="l340" href="#l340">340 </a> } <span class="c2html-keyword">else</span> <span class="c2html-keyword">if</span> (gamestate->board[row][file] == +<a class="c2html-lineno" name="l341" href="#l341">341 </a> (piececolor == <span class="c2html-macroconst">WHITE</span>?<span class="c2html-macroconst">BKING</span>:<span class="c2html-macroconst">WKING</span>)) { +<a class="c2html-lineno" name="l342" href="#l342">342 </a> opkingfile = file; +<a class="c2html-lineno" name="l343" href="#l343">343 </a> opkingrow = row; +<a class="c2html-lineno" name="l344" href="#l344">344 </a> } +<a class="c2html-lineno" name="l345" href="#l345">345 </a> } +<a class="c2html-lineno" name="l346" href="#l346">346 </a> } +<a class="c2html-lineno" name="l347" href="#l347">347 </a> +<a class="c2html-lineno" name="l348" href="#l348">348 </a> <span class="c2html-comment">/* simulate move for check validation */</span> +<a class="c2html-lineno" name="l349" href="#l349">349 </a> GameState simulation = gamestate_copy_sim(gamestate); +<a class="c2html-lineno" name="l350" href="#l350">350 </a> Move simmove = *move; +<a class="c2html-lineno" name="l351" href="#l351">351 </a> apply_move_impl(&simulation, &simmove, <span class="c2html-macroconst">1</span>); +<a class="c2html-lineno" name="l352" href="#l352">352 </a> +<a class="c2html-lineno" name="l353" href="#l353">353 </a> <span class="c2html-comment">/* don't move into or stay in check position */</span> +<a class="c2html-lineno" name="l354" href="#l354">354 </a> <span class="c2html-keyword">if</span> (is_covered(&simulation, mykingrow, mykingfile, +<a class="c2html-lineno" name="l355" href="#l355">355 </a> opponent_color(piececolor))) { +<a class="c2html-lineno" name="l356" href="#l356">356 </a> +<a class="c2html-lineno" name="l357" href="#l357">357 </a> gamestate_cleanup(&simulation); +<a class="c2html-lineno" name="l358" href="#l358">358 </a> <span class="c2html-keyword">if</span> ((move->piece & <span class="c2html-macroconst">PIECE_MASK</span>) == <span class="c2html-macroconst">KING</span>) { +<a class="c2html-lineno" name="l359" href="#l359">359 </a> <span class="c2html-keyword">return</span> <span class="c2html-macroconst">KING_MOVES_INTO_CHECK</span>; +<a class="c2html-lineno" name="l360" href="#l360">360 </a> } <span class="c2html-keyword">else</span> { +<a class="c2html-lineno" name="l361" href="#l361">361 </a> <span class="c2html-comment">/* last move is always not null in this case */</span> +<a class="c2html-lineno" name="l362" href="#l362">362 </a> <span class="c2html-keyword">return</span> gamestate->lastmove->move.check ? +<a class="c2html-lineno" name="l363" href="#l363">363 </a> <span class="c2html-macroconst">KING_IN_CHECK</span> : <span class="c2html-macroconst">PIECE_PINNED</span>; +<a class="c2html-lineno" name="l364" href="#l364">364 </a> } +<a class="c2html-lineno" name="l365" href="#l365">365 </a> } +<a class="c2html-lineno" name="l366" href="#l366">366 </a> +<a class="c2html-lineno" name="l367" href="#l367">367 </a> <span class="c2html-comment">/* correct check and checkmate flags (move is still valid) */</span> +<a class="c2html-lineno" name="l368" href="#l368">368 </a> Move threats[<span class="c2html-macroconst">16</span>]; +<a class="c2html-lineno" name="l369" href="#l369">369 </a> <span class="c2html-type">uint8_t</span> threatcount; +<a class="c2html-lineno" name="l370" href="#l370">370 </a> move->check = get_threats(&simulation, opkingrow, opkingfile, +<a class="c2html-lineno" name="l371" href="#l371">371 </a> piececolor, threats, &threatcount); +<a class="c2html-lineno" name="l372" href="#l372">372 </a> +<a class="c2html-lineno" name="l373" href="#l373">373 </a> <span class="c2html-keyword">if</span> (move->check) { +<a class="c2html-lineno" name="l374" href="#l374">374 </a> <span class="c2html-comment">/* determine possible escape fields */</span> +<a class="c2html-lineno" name="l375" href="#l375">375 </a> _Bool canescape = <span class="c2html-macroconst">0</span>; +<a class="c2html-lineno" name="l376" href="#l376">376 </a> <span class="c2html-keyword">for</span> (<span class="c2html-keyword">int</span> dr = -<span class="c2html-macroconst">1</span> ; dr <= <span class="c2html-macroconst">1</span> && !canescape ; dr++) { +<a class="c2html-lineno" name="l377" href="#l377">377 </a> <span class="c2html-keyword">for</span> (<span class="c2html-keyword">int</span> df = -<span class="c2html-macroconst">1</span> ; df <= <span class="c2html-macroconst">1</span> && !canescape ; df++) { +<a class="c2html-lineno" name="l378" href="#l378">378 </a> <span class="c2html-keyword">if</span> (!(dr == <span class="c2html-macroconst">0</span> && df == <span class="c2html-macroconst">0</span>) && +<a class="c2html-lineno" name="l379" href="#l379">379 </a> isidx(opkingrow + dr) && isidx(opkingfile + df)) { +<a class="c2html-lineno" name="l380" href="#l380">380 </a> +<a class="c2html-lineno" name="l381" href="#l381">381 </a> <span class="c2html-comment">/* escape field neither blocked nor covered */</span> +<a class="c2html-lineno" name="l382" href="#l382">382 </a> <span class="c2html-keyword">if</span> ((simulation.board[opkingrow + dr][opkingfile + df] +<a class="c2html-lineno" name="l383" href="#l383">383 </a> & <span class="c2html-macroconst">COLOR_MASK</span>) != opponent_color(piececolor)) { +<a class="c2html-lineno" name="l384" href="#l384">384 </a> canescape |= !is_covered(&simulation, +<a class="c2html-lineno" name="l385" href="#l385">385 </a> opkingrow + dr, opkingfile + df, piececolor); +<a class="c2html-lineno" name="l386" href="#l386">386 </a> } +<a class="c2html-lineno" name="l387" href="#l387">387 </a> } +<a class="c2html-lineno" name="l388" href="#l388">388 </a> } +<a class="c2html-lineno" name="l389" href="#l389">389 </a> } +<a class="c2html-lineno" name="l390" href="#l390">390 </a> <span class="c2html-comment">/* can't escape, can he capture? */</span> +<a class="c2html-lineno" name="l391" href="#l391">391 </a> <span class="c2html-keyword">if</span> (!canescape && threatcount == <span class="c2html-macroconst">1</span>) { +<a class="c2html-lineno" name="l392" href="#l392">392 </a> canescape = is_attacked(&simulation, threats[<span class="c2html-macroconst">0</span>].fromrow, +<a class="c2html-lineno" name="l393" href="#l393">393 </a> threats[<span class="c2html-macroconst">0</span>].fromfile, opponent_color(piececolor)); +<a class="c2html-lineno" name="l394" href="#l394">394 </a> } +<a class="c2html-lineno" name="l395" href="#l395">395 </a> +<a class="c2html-lineno" name="l396" href="#l396">396 </a> <span class="c2html-comment">/* can't capture, can he block? */</span> +<a class="c2html-lineno" name="l397" href="#l397">397 </a> <span class="c2html-keyword">if</span> (!canescape && threatcount == <span class="c2html-macroconst">1</span>) { +<a class="c2html-lineno" name="l398" href="#l398">398 </a> Move *threat = &(threats[<span class="c2html-macroconst">0</span>]); +<a class="c2html-lineno" name="l399" href="#l399">399 </a> <span class="c2html-type">uint8_t</span> threatpiece = threat->piece & <span class="c2html-macroconst">PIECE_MASK</span>; +<a class="c2html-lineno" name="l400" href="#l400">400 </a> +<a class="c2html-lineno" name="l401" href="#l401">401 </a> <span class="c2html-comment">/* knight, pawns and the king cannot be blocked */</span> +<a class="c2html-lineno" name="l402" href="#l402">402 </a> <span class="c2html-keyword">if</span> (threatpiece == <span class="c2html-macroconst">BISHOP</span> || threatpiece == <span class="c2html-macroconst">ROOK</span> +<a class="c2html-lineno" name="l403" href="#l403">403 </a> || threatpiece == <span class="c2html-macroconst">QUEEN</span>) { +<a class="c2html-lineno" name="l404" href="#l404">404 </a> <span class="c2html-keyword">if</span> (threat->fromrow == threat->torow) { +<a class="c2html-lineno" name="l405" href="#l405">405 </a> <span class="c2html-comment">/* rook aspect (on row) */</span> +<a class="c2html-lineno" name="l406" href="#l406">406 </a> <span class="c2html-keyword">int</span> d = threat->tofile > threat->fromfile ? <span class="c2html-macroconst">1</span> : -<span class="c2html-macroconst">1</span>; +<a class="c2html-lineno" name="l407" href="#l407">407 </a> <span class="c2html-type">uint8_t</span> file = threat->fromfile; +<a class="c2html-lineno" name="l408" href="#l408">408 </a> <span class="c2html-keyword">while</span> (!canescape && file != threat->tofile - d) { +<a class="c2html-lineno" name="l409" href="#l409">409 </a> file += d; +<a class="c2html-lineno" name="l410" href="#l410">410 </a> canescape |= is_protected(&simulation, +<a class="c2html-lineno" name="l411" href="#l411">411 </a> threat->torow, file, opponent_color(piececolor)); +<a class="c2html-lineno" name="l412" href="#l412">412 </a> } +<a class="c2html-lineno" name="l413" href="#l413">413 </a> } <span class="c2html-keyword">else</span> <span class="c2html-keyword">if</span> (threat->fromfile == threat->tofile) { +<a class="c2html-lineno" name="l414" href="#l414">414 </a> <span class="c2html-comment">/* rook aspect (on file) */</span> +<a class="c2html-lineno" name="l415" href="#l415">415 </a> <span class="c2html-keyword">int</span> d = threat->torow > threat->fromrow ? <span class="c2html-macroconst">1</span> : -<span class="c2html-macroconst">1</span>; +<a class="c2html-lineno" name="l416" href="#l416">416 </a> <span class="c2html-type">uint8_t</span> row = threat->fromrow; +<a class="c2html-lineno" name="l417" href="#l417">417 </a> <span class="c2html-keyword">while</span> (!canescape && row != threat->torow - d) { +<a class="c2html-lineno" name="l418" href="#l418">418 </a> row += d; +<a class="c2html-lineno" name="l419" href="#l419">419 </a> canescape |= is_protected(&simulation, +<a class="c2html-lineno" name="l420" href="#l420">420 </a> row, threat->tofile, opponent_color(piececolor)); +<a class="c2html-lineno" name="l421" href="#l421">421 </a> } +<a class="c2html-lineno" name="l422" href="#l422">422 </a> } <span class="c2html-keyword">else</span> { +<a class="c2html-lineno" name="l423" href="#l423">423 </a> <span class="c2html-comment">/* bishop aspect */</span> +<a class="c2html-lineno" name="l424" href="#l424">424 </a> <span class="c2html-keyword">int</span> dr = threat->torow > threat->fromrow ? <span class="c2html-macroconst">1</span> : -<span class="c2html-macroconst">1</span>; +<a class="c2html-lineno" name="l425" href="#l425">425 </a> <span class="c2html-keyword">int</span> df = threat->tofile > threat->fromfile ? <span class="c2html-macroconst">1</span> : -<span class="c2html-macroconst">1</span>; +<a class="c2html-lineno" name="l426" href="#l426">426 </a> +<a class="c2html-lineno" name="l427" href="#l427">427 </a> <span class="c2html-type">uint8_t</span> row = threat->fromrow; +<a class="c2html-lineno" name="l428" href="#l428">428 </a> <span class="c2html-type">uint8_t</span> file = threat->fromfile; +<a class="c2html-lineno" name="l429" href="#l429">429 </a> <span class="c2html-keyword">while</span> (!canescape && file != threat->tofile - df +<a class="c2html-lineno" name="l430" href="#l430">430 </a> && row != threat->torow - dr) { +<a class="c2html-lineno" name="l431" href="#l431">431 </a> row += dr; +<a class="c2html-lineno" name="l432" href="#l432">432 </a> file += df; +<a class="c2html-lineno" name="l433" href="#l433">433 </a> canescape |= is_protected(&simulation, row, file, +<a class="c2html-lineno" name="l434" href="#l434">434 </a> opponent_color(piececolor)); +<a class="c2html-lineno" name="l435" href="#l435">435 </a> } +<a class="c2html-lineno" name="l436" href="#l436">436 </a> } +<a class="c2html-lineno" name="l437" href="#l437">437 </a> } +<a class="c2html-lineno" name="l438" href="#l438">438 </a> } +<a class="c2html-lineno" name="l439" href="#l439">439 </a> +<a class="c2html-lineno" name="l440" href="#l440">440 </a> <span class="c2html-keyword">if</span> (!canescape) { +<a class="c2html-lineno" name="l441" href="#l441">441 </a> gamestate->checkmate = <span class="c2html-macroconst">1</span>; +<a class="c2html-lineno" name="l442" href="#l442">442 </a> } +<a class="c2html-lineno" name="l443" href="#l443">443 </a> } +<a class="c2html-lineno" name="l444" href="#l444">444 </a> +<a class="c2html-lineno" name="l445" href="#l445">445 </a> gamestate_cleanup(&simulation); +<a class="c2html-lineno" name="l446" href="#l446">446 </a> +<a class="c2html-lineno" name="l447" href="#l447">447 </a> <span class="c2html-keyword">return</span> <span class="c2html-macroconst">VALID_MOVE_SEMANTICS</span>; +<a class="c2html-lineno" name="l448" href="#l448">448 </a>} +<a class="c2html-lineno" name="l449" href="#l449">449 </a> +<a class="c2html-lineno" name="l450" href="#l450">450 </a>_Bool get_threats(GameState *gamestate, <span class="c2html-type">uint8_t</span> row, <span class="c2html-type">uint8_t</span> file, +<a class="c2html-lineno" name="l451" href="#l451">451 </a> <span class="c2html-type">uint8_t</span> color, Move *threats, <span class="c2html-type">uint8_t</span> *threatcount) { +<a class="c2html-lineno" name="l452" href="#l452">452 </a> Move candidates[<span class="c2html-macroconst">32</span>]; +<a class="c2html-lineno" name="l453" href="#l453">453 </a> <span class="c2html-keyword">int</span> candidatecount = <span class="c2html-macroconst">0</span>; +<a class="c2html-lineno" name="l454" href="#l454">454 </a> <span class="c2html-keyword">for</span> (<span class="c2html-type">uint8_t</span> r = <span class="c2html-macroconst">0</span> ; r < <span class="c2html-macroconst">8</span> ; r++) { +<a class="c2html-lineno" name="l455" href="#l455">455 </a> <span class="c2html-keyword">for</span> (<span class="c2html-type">uint8_t</span> f = <span class="c2html-macroconst">0</span> ; f < <span class="c2html-macroconst">8</span> ; f++) { +<a class="c2html-lineno" name="l456" href="#l456">456 </a> <span class="c2html-keyword">if</span> ((gamestate->board[r][f] & <span class="c2html-macroconst">COLOR_MASK</span>) == color) { +<a class="c2html-lineno" name="l457" href="#l457">457 </a> <span class="c2html-comment">// non-capturing move</span> +<a class="c2html-lineno" name="l458" href="#l458">458 </a> memset(&(candidates[candidatecount]), <span class="c2html-macroconst">0</span>, <span class="c2html-keyword">sizeof</span>(Move)); +<a class="c2html-lineno" name="l459" href="#l459">459 </a> candidates[candidatecount].piece = gamestate->board[r][f]; +<a class="c2html-lineno" name="l460" href="#l460">460 </a> candidates[candidatecount].fromrow = r; +<a class="c2html-lineno" name="l461" href="#l461">461 </a> candidates[candidatecount].fromfile = f; +<a class="c2html-lineno" name="l462" href="#l462">462 </a> candidates[candidatecount].torow = row; +<a class="c2html-lineno" name="l463" href="#l463">463 </a> candidates[candidatecount].tofile = file; +<a class="c2html-lineno" name="l464" href="#l464">464 </a> candidatecount++; +<a class="c2html-lineno" name="l465" href="#l465">465 </a> +<a class="c2html-lineno" name="l466" href="#l466">466 </a> <span class="c2html-comment">// capturing move</span> +<a class="c2html-lineno" name="l467" href="#l467">467 </a> memcpy(&(candidates[candidatecount]), +<a class="c2html-lineno" name="l468" href="#l468">468 </a> &(candidates[candidatecount-<span class="c2html-macroconst">1</span>]), <span class="c2html-keyword">sizeof</span>(Move)); +<a class="c2html-lineno" name="l469" href="#l469">469 </a> candidates[candidatecount].capture = <span class="c2html-macroconst">1</span>; +<a class="c2html-lineno" name="l470" href="#l470">470 </a> candidatecount++; +<a class="c2html-lineno" name="l471" href="#l471">471 </a> } +<a class="c2html-lineno" name="l472" href="#l472">472 </a> } +<a class="c2html-lineno" name="l473" href="#l473">473 </a> } +<a class="c2html-lineno" name="l474" href="#l474">474 </a> +<a class="c2html-lineno" name="l475" href="#l475">475 </a> <span class="c2html-keyword">if</span> (threatcount) { +<a class="c2html-lineno" name="l476" href="#l476">476 </a> *threatcount = <span class="c2html-macroconst">0</span>; +<a class="c2html-lineno" name="l477" href="#l477">477 </a> } +<a class="c2html-lineno" name="l478" href="#l478">478 </a> +<a class="c2html-lineno" name="l479" href="#l479">479 </a> +<a class="c2html-lineno" name="l480" href="#l480">480 </a> _Bool result = <span class="c2html-macroconst">0</span>; +<a class="c2html-lineno" name="l481" href="#l481">481 </a> +<a class="c2html-lineno" name="l482" href="#l482">482 </a> <span class="c2html-keyword">for</span> (<span class="c2html-keyword">int</span> i = <span class="c2html-macroconst">0</span> ; i < candidatecount ; i++) { +<a class="c2html-lineno" name="l483" href="#l483">483 </a> <span class="c2html-keyword">if</span> (validate_move_rules(gamestate, &(candidates[i])) +<a class="c2html-lineno" name="l484" href="#l484">484 </a> == <span class="c2html-macroconst">VALID_MOVE_SEMANTICS</span>) { +<a class="c2html-lineno" name="l485" href="#l485">485 </a> result = <span class="c2html-macroconst">1</span>; +<a class="c2html-lineno" name="l486" href="#l486">486 </a> <span class="c2html-keyword">if</span> (threats && threatcount) { +<a class="c2html-lineno" name="l487" href="#l487">487 </a> threats[(*threatcount)++] = candidates[i]; +<a class="c2html-lineno" name="l488" href="#l488">488 </a> } +<a class="c2html-lineno" name="l489" href="#l489">489 </a> } +<a class="c2html-lineno" name="l490" href="#l490">490 </a> } +<a class="c2html-lineno" name="l491" href="#l491">491 </a> +<a class="c2html-lineno" name="l492" href="#l492">492 </a> <span class="c2html-keyword">return</span> result; +<a class="c2html-lineno" name="l493" href="#l493">493 </a>} +<a class="c2html-lineno" name="l494" href="#l494">494 </a> +<a class="c2html-lineno" name="l495" href="#l495">495 </a>_Bool is_pinned(GameState *gamestate, Move *move) { +<a class="c2html-lineno" name="l496" href="#l496">496 </a> <span class="c2html-type">uint8_t</span> color = move->piece & <span class="c2html-macroconst">COLOR_MASK</span>; +<a class="c2html-lineno" name="l497" href="#l497">497 </a> +<a class="c2html-lineno" name="l498" href="#l498">498 </a> <span class="c2html-type">uint8_t</span> kingfile = <span class="c2html-macroconst">0</span>, kingrow = <span class="c2html-macroconst">0</span>; +<a class="c2html-lineno" name="l499" href="#l499">499 </a> <span class="c2html-keyword">for</span> (<span class="c2html-type">uint8_t</span> row = <span class="c2html-macroconst">0</span> ; row < <span class="c2html-macroconst">8</span> ; row++) { +<a class="c2html-lineno" name="l500" href="#l500">500 </a> <span class="c2html-keyword">for</span> (<span class="c2html-type">uint8_t</span> file = <span class="c2html-macroconst">0</span> ; file < <span class="c2html-macroconst">8</span> ; file++) { +<a class="c2html-lineno" name="l501" href="#l501">501 </a> <span class="c2html-keyword">if</span> (gamestate->board[row][file] == (color|<span class="c2html-macroconst">KING</span>)) { +<a class="c2html-lineno" name="l502" href="#l502">502 </a> kingfile = file; +<a class="c2html-lineno" name="l503" href="#l503">503 </a> kingrow = row; +<a class="c2html-lineno" name="l504" href="#l504">504 </a> } +<a class="c2html-lineno" name="l505" href="#l505">505 </a> } +<a class="c2html-lineno" name="l506" href="#l506">506 </a> } +<a class="c2html-lineno" name="l507" href="#l507">507 </a> +<a class="c2html-lineno" name="l508" href="#l508">508 </a> GameState simulation = gamestate_copy_sim(gamestate); +<a class="c2html-lineno" name="l509" href="#l509">509 </a> Move simmove = *move; +<a class="c2html-lineno" name="l510" href="#l510">510 </a> apply_move(&simulation, &simmove); +<a class="c2html-lineno" name="l511" href="#l511">511 </a> _Bool covered = is_covered(&simulation, +<a class="c2html-lineno" name="l512" href="#l512">512 </a> kingrow, kingfile, opponent_color(color)); +<a class="c2html-lineno" name="l513" href="#l513">513 </a> gamestate_cleanup(&simulation); +<a class="c2html-lineno" name="l514" href="#l514">514 </a> +<a class="c2html-lineno" name="l515" href="#l515">515 </a> <span class="c2html-keyword">return</span> covered; +<a class="c2html-lineno" name="l516" href="#l516">516 </a>} +<a class="c2html-lineno" name="l517" href="#l517">517 </a> +<a class="c2html-lineno" name="l518" href="#l518">518 </a>_Bool get_real_threats(GameState *gamestate, <span class="c2html-type">uint8_t</span> row, <span class="c2html-type">uint8_t</span> file, +<a class="c2html-lineno" name="l519" href="#l519">519 </a> <span class="c2html-type">uint8_t</span> color, Move *threats, <span class="c2html-type">uint8_t</span> *threatcount) { +<a class="c2html-lineno" name="l520" href="#l520">520 </a> +<a class="c2html-lineno" name="l521" href="#l521">521 </a> <span class="c2html-keyword">if</span> (threatcount) { +<a class="c2html-lineno" name="l522" href="#l522">522 </a> *threatcount = <span class="c2html-macroconst">0</span>; +<a class="c2html-lineno" name="l523" href="#l523">523 </a> } +<a class="c2html-lineno" name="l524" href="#l524">524 </a> +<a class="c2html-lineno" name="l525" href="#l525">525 </a> Move candidates[<span class="c2html-macroconst">16</span>]; +<a class="c2html-lineno" name="l526" href="#l526">526 </a> <span class="c2html-type">uint8_t</span> candidatecount; +<a class="c2html-lineno" name="l527" href="#l527">527 </a> <span class="c2html-keyword">if</span> (get_threats(gamestate, row, file, color, candidates, &candidatecount)) { +<a class="c2html-lineno" name="l528" href="#l528">528 </a> +<a class="c2html-lineno" name="l529" href="#l529">529 </a> _Bool result = <span class="c2html-macroconst">0</span>; +<a class="c2html-lineno" name="l530" href="#l530">530 </a> <span class="c2html-type">uint8_t</span> kingfile = <span class="c2html-macroconst">0</span>, kingrow = <span class="c2html-macroconst">0</span>; +<a class="c2html-lineno" name="l531" href="#l531">531 </a> <span class="c2html-keyword">for</span> (<span class="c2html-type">uint8_t</span> row = <span class="c2html-macroconst">0</span> ; row < <span class="c2html-macroconst">8</span> ; row++) { +<a class="c2html-lineno" name="l532" href="#l532">532 </a> <span class="c2html-keyword">for</span> (<span class="c2html-type">uint8_t</span> file = <span class="c2html-macroconst">0</span> ; file < <span class="c2html-macroconst">8</span> ; file++) { +<a class="c2html-lineno" name="l533" href="#l533">533 </a> <span class="c2html-keyword">if</span> (gamestate->board[row][file] == (color|<span class="c2html-macroconst">KING</span>)) { +<a class="c2html-lineno" name="l534" href="#l534">534 </a> kingfile = file; +<a class="c2html-lineno" name="l535" href="#l535">535 </a> kingrow = row; +<a class="c2html-lineno" name="l536" href="#l536">536 </a> } +<a class="c2html-lineno" name="l537" href="#l537">537 </a> } +<a class="c2html-lineno" name="l538" href="#l538">538 </a> } +<a class="c2html-lineno" name="l539" href="#l539">539 </a> +<a class="c2html-lineno" name="l540" href="#l540">540 </a> <span class="c2html-keyword">for</span> (<span class="c2html-type">uint8_t</span> i = <span class="c2html-macroconst">0</span> ; i < candidatecount ; i++) { +<a class="c2html-lineno" name="l541" href="#l541">541 </a> GameState simulation = gamestate_copy_sim(gamestate); +<a class="c2html-lineno" name="l542" href="#l542">542 </a> Move simmove = candidates[i]; +<a class="c2html-lineno" name="l543" href="#l543">543 </a> apply_move(&simulation, &simmove); +<a class="c2html-lineno" name="l544" href="#l544">544 </a> <span class="c2html-keyword">if</span> (!is_covered(&simulation, kingrow, kingfile, +<a class="c2html-lineno" name="l545" href="#l545">545 </a> opponent_color(color))) { +<a class="c2html-lineno" name="l546" href="#l546">546 </a> result = <span class="c2html-macroconst">1</span>; +<a class="c2html-lineno" name="l547" href="#l547">547 </a> <span class="c2html-keyword">if</span> (threats && threatcount) { +<a class="c2html-lineno" name="l548" href="#l548">548 </a> threats[(*threatcount)++] = candidates[i]; +<a class="c2html-lineno" name="l549" href="#l549">549 </a> } +<a class="c2html-lineno" name="l550" href="#l550">550 </a> } +<a class="c2html-lineno" name="l551" href="#l551">551 </a> } +<a class="c2html-lineno" name="l552" href="#l552">552 </a> +<a class="c2html-lineno" name="l553" href="#l553">553 </a> <span class="c2html-keyword">return</span> result; +<a class="c2html-lineno" name="l554" href="#l554">554 </a> } <span class="c2html-keyword">else</span> { +<a class="c2html-lineno" name="l555" href="#l555">555 </a> <span class="c2html-keyword">return</span> <span class="c2html-macroconst">0</span>; +<a class="c2html-lineno" name="l556" href="#l556">556 </a> } +<a class="c2html-lineno" name="l557" href="#l557">557 </a>} +<a class="c2html-lineno" name="l558" href="#l558">558 </a> +<a class="c2html-lineno" name="l559" href="#l559">559 </a><span class="c2html-keyword">static</span> <span class="c2html-keyword">int</span> getlocation(GameState *gamestate, Move *move) { +<a class="c2html-lineno" name="l560" href="#l560">560 </a> +<a class="c2html-lineno" name="l561" href="#l561">561 </a> <span class="c2html-type">uint8_t</span> color = move->piece & <span class="c2html-macroconst">COLOR_MASK</span>; +<a class="c2html-lineno" name="l562" href="#l562">562 </a> _Bool incheck = gamestate->lastmove?gamestate->lastmove->move.check:<span class="c2html-macroconst">0</span>; +<a class="c2html-lineno" name="l563" href="#l563">563 </a> +<a class="c2html-lineno" name="l564" href="#l564">564 </a> Move threats[<span class="c2html-macroconst">16</span>], *threat = <span class="c2html-macroconst">NULL</span>; +<a class="c2html-lineno" name="l565" href="#l565">565 </a> <span class="c2html-type">uint8_t</span> threatcount; +<a class="c2html-lineno" name="l566" href="#l566">566 </a> +<a class="c2html-lineno" name="l567" href="#l567">567 </a> <span class="c2html-keyword">if</span> (get_threats(gamestate, move->torow, move->tofile, color, +<a class="c2html-lineno" name="l568" href="#l568">568 </a> threats, &threatcount)) { +<a class="c2html-lineno" name="l569" href="#l569">569 </a> +<a class="c2html-lineno" name="l570" href="#l570">570 </a> <span class="c2html-keyword">int</span> reason = <span class="c2html-macroconst">INVALID_POSITION</span>; +<a class="c2html-lineno" name="l571" href="#l571">571 </a> +<a class="c2html-lineno" name="l572" href="#l572">572 </a> <span class="c2html-comment">// find threats for the specified position</span> +<a class="c2html-lineno" name="l573" href="#l573">573 </a> <span class="c2html-keyword">for</span> (<span class="c2html-type">uint8_t</span> i = <span class="c2html-macroconst">0</span> ; i < threatcount ; i++) { +<a class="c2html-lineno" name="l574" href="#l574">574 </a> <span class="c2html-keyword">if</span> ((threats[i].piece & (<span class="c2html-macroconst">PIECE_MASK</span> | <span class="c2html-macroconst">COLOR_MASK</span>)) +<a class="c2html-lineno" name="l575" href="#l575">575 </a> == move->piece && +<a class="c2html-lineno" name="l576" href="#l576">576 </a> (move->fromrow == <span class="c2html-macroconst">POS_UNSPECIFIED</span> || +<a class="c2html-lineno" name="l577" href="#l577">577 </a> move->fromrow == threats[i].fromrow) && +<a class="c2html-lineno" name="l578" href="#l578">578 </a> (move->fromfile == <span class="c2html-macroconst">POS_UNSPECIFIED</span> || +<a class="c2html-lineno" name="l579" href="#l579">579 </a> move->fromfile == threats[i].fromfile)) { +<a class="c2html-lineno" name="l580" href="#l580">580 </a> +<a class="c2html-lineno" name="l581" href="#l581">581 </a> <span class="c2html-keyword">if</span> (threat) { +<a class="c2html-lineno" name="l582" href="#l582">582 </a> <span class="c2html-keyword">return</span> <span class="c2html-macroconst">AMBIGUOUS_MOVE</span>; +<a class="c2html-lineno" name="l583" href="#l583">583 </a> } <span class="c2html-keyword">else</span> { +<a class="c2html-lineno" name="l584" href="#l584">584 </a> <span class="c2html-comment">// found threat is no real threat</span> +<a class="c2html-lineno" name="l585" href="#l585">585 </a> <span class="c2html-keyword">if</span> (is_pinned(gamestate, &(threats[i]))) { +<a class="c2html-lineno" name="l586" href="#l586">586 </a> reason = incheck?<span class="c2html-macroconst">KING_IN_CHECK</span>:<span class="c2html-macroconst">PIECE_PINNED</span>; +<a class="c2html-lineno" name="l587" href="#l587">587 </a> } <span class="c2html-keyword">else</span> { +<a class="c2html-lineno" name="l588" href="#l588">588 </a> threat = &(threats[i]); +<a class="c2html-lineno" name="l589" href="#l589">589 </a> } +<a class="c2html-lineno" name="l590" href="#l590">590 </a> } +<a class="c2html-lineno" name="l591" href="#l591">591 </a> } +<a class="c2html-lineno" name="l592" href="#l592">592 </a> } +<a class="c2html-lineno" name="l593" href="#l593">593 </a> +<a class="c2html-lineno" name="l594" href="#l594">594 </a> <span class="c2html-comment">// can't threaten specified position</span> +<a class="c2html-lineno" name="l595" href="#l595">595 </a> <span class="c2html-keyword">if</span> (!threat) { +<a class="c2html-lineno" name="l596" href="#l596">596 </a> <span class="c2html-keyword">return</span> reason; +<a class="c2html-lineno" name="l597" href="#l597">597 </a> } +<a class="c2html-lineno" name="l598" href="#l598">598 </a> +<a class="c2html-lineno" name="l599" href="#l599">599 </a> memcpy(move, threat, <span class="c2html-keyword">sizeof</span>(Move)); +<a class="c2html-lineno" name="l600" href="#l600">600 </a> <span class="c2html-keyword">return</span> <span class="c2html-macroconst">VALID_MOVE_SYNTAX</span>; +<a class="c2html-lineno" name="l601" href="#l601">601 </a> } <span class="c2html-keyword">else</span> { +<a class="c2html-lineno" name="l602" href="#l602">602 </a> <span class="c2html-keyword">return</span> <span class="c2html-macroconst">INVALID_POSITION</span>; +<a class="c2html-lineno" name="l603" href="#l603">603 </a> } +<a class="c2html-lineno" name="l604" href="#l604">604 </a>} +<a class="c2html-lineno" name="l605" href="#l605">605 </a> +<a class="c2html-lineno" name="l606" href="#l606">606 </a><span class="c2html-keyword">int</span> eval_move(GameState *gamestate, <span class="c2html-keyword">char</span> *mstr, Move *move, <span class="c2html-type">uint8_t</span> color) { +<a class="c2html-lineno" name="l607" href="#l607">607 </a> memset(move, <span class="c2html-macroconst">0</span>, <span class="c2html-keyword">sizeof</span>(Move)); +<a class="c2html-lineno" name="l608" href="#l608">608 </a> move->fromfile = <span class="c2html-macroconst">POS_UNSPECIFIED</span>; +<a class="c2html-lineno" name="l609" href="#l609">609 </a> move->fromrow = <span class="c2html-macroconst">POS_UNSPECIFIED</span>; +<a class="c2html-lineno" name="l610" href="#l610">610 </a> +<a class="c2html-lineno" name="l611" href="#l611">611 </a> <span class="c2html-type">size_t</span> len = strlen(mstr); +<a class="c2html-lineno" name="l612" href="#l612">612 </a> <span class="c2html-keyword">if</span> (len < <span class="c2html-macroconst">1</span> || len > <span class="c2html-macroconst">6</span>) { +<a class="c2html-lineno" name="l613" href="#l613">613 </a> <span class="c2html-keyword">return</span> <span class="c2html-macroconst">INVALID_MOVE_SYNTAX</span>; +<a class="c2html-lineno" name="l614" href="#l614">614 </a> } +<a class="c2html-lineno" name="l615" href="#l615">615 </a> +<a class="c2html-lineno" name="l616" href="#l616">616 </a> <span class="c2html-comment">/* evaluate check/checkmate flags */</span> +<a class="c2html-lineno" name="l617" href="#l617">617 </a> <span class="c2html-keyword">if</span> (mstr[len-<span class="c2html-macroconst">1</span>] == <span class="c2html-string">'+'</span>) { +<a class="c2html-lineno" name="l618" href="#l618">618 </a> len--; mstr[len] = <span class="c2html-string">'\0'</span>; +<a class="c2html-lineno" name="l619" href="#l619">619 </a> move->check = <span class="c2html-macroconst">1</span>; +<a class="c2html-lineno" name="l620" href="#l620">620 </a> } <span class="c2html-keyword">else</span> <span class="c2html-keyword">if</span> (mstr[len-<span class="c2html-macroconst">1</span>] == <span class="c2html-string">'#'</span>) { +<a class="c2html-lineno" name="l621" href="#l621">621 </a> len--; mstr[len] = <span class="c2html-string">'\0'</span>; +<a class="c2html-lineno" name="l622" href="#l622">622 </a> <span class="c2html-comment">/* ignore - validation should set game state */</span> +<a class="c2html-lineno" name="l623" href="#l623">623 </a> } +<a class="c2html-lineno" name="l624" href="#l624">624 </a> +<a class="c2html-lineno" name="l625" href="#l625">625 </a> <span class="c2html-comment">/* evaluate promotion */</span> +<a class="c2html-lineno" name="l626" href="#l626">626 </a> <span class="c2html-keyword">if</span> (len > <span class="c2html-macroconst">3</span> && mstr[len-<span class="c2html-macroconst">2</span>] == <span class="c2html-string">'='</span>) { +<a class="c2html-lineno" name="l627" href="#l627">627 </a> move->promotion = getpiece(mstr[len-<span class="c2html-macroconst">1</span>]); +<a class="c2html-lineno" name="l628" href="#l628">628 </a> <span class="c2html-keyword">if</span> (!move->promotion) { +<a class="c2html-lineno" name="l629" href="#l629">629 </a> <span class="c2html-keyword">return</span> <span class="c2html-macroconst">INVALID_MOVE_SYNTAX</span>; +<a class="c2html-lineno" name="l630" href="#l630">630 </a> } <span class="c2html-keyword">else</span> { +<a class="c2html-lineno" name="l631" href="#l631">631 </a> move->promotion |= color; +<a class="c2html-lineno" name="l632" href="#l632">632 </a> len -= <span class="c2html-macroconst">2</span>; +<a class="c2html-lineno" name="l633" href="#l633">633 </a> mstr[len] = <span class="c2html-macroconst">0</span>; +<a class="c2html-lineno" name="l634" href="#l634">634 </a> } +<a class="c2html-lineno" name="l635" href="#l635">635 </a> } +<a class="c2html-lineno" name="l636" href="#l636">636 </a> +<a class="c2html-lineno" name="l637" href="#l637">637 </a> <span class="c2html-keyword">if</span> (len == <span class="c2html-macroconst">2</span>) { +<a class="c2html-lineno" name="l638" href="#l638">638 </a> <span class="c2html-comment">/* pawn move (e.g. "e4") */</span> +<a class="c2html-lineno" name="l639" href="#l639">639 </a> move->piece = <span class="c2html-macroconst">PAWN</span>; +<a class="c2html-lineno" name="l640" href="#l640">640 </a> move->tofile = fileidx(mstr[<span class="c2html-macroconst">0</span>]); +<a class="c2html-lineno" name="l641" href="#l641">641 </a> move->torow = rowidx(mstr[<span class="c2html-macroconst">1</span>]); +<a class="c2html-lineno" name="l642" href="#l642">642 </a> } <span class="c2html-keyword">else</span> <span class="c2html-keyword">if</span> (len == <span class="c2html-macroconst">3</span>) { +<a class="c2html-lineno" name="l643" href="#l643">643 </a> <span class="c2html-keyword">if</span> (strcmp(mstr, <span class="c2html-string">"O-O"</span>) == <span class="c2html-macroconst">0</span>) { +<a class="c2html-lineno" name="l644" href="#l644">644 </a> <span class="c2html-comment">/* king side castling */</span> +<a class="c2html-lineno" name="l645" href="#l645">645 </a> move->piece = <span class="c2html-macroconst">KING</span>; +<a class="c2html-lineno" name="l646" href="#l646">646 </a> move->fromfile = fileidx(<span class="c2html-string">'e'</span>); +<a class="c2html-lineno" name="l647" href="#l647">647 </a> move->tofile = fileidx(<span class="c2html-string">'g'</span>); +<a class="c2html-lineno" name="l648" href="#l648">648 </a> move->fromrow = move->torow = color == <span class="c2html-macroconst">WHITE</span> ? <span class="c2html-macroconst">0</span> : <span class="c2html-macroconst">7</span>; +<a class="c2html-lineno" name="l649" href="#l649">649 </a> } <span class="c2html-keyword">else</span> { +<a class="c2html-lineno" name="l650" href="#l650">650 </a> <span class="c2html-comment">/* move (e.g. "Nf3") */</span> +<a class="c2html-lineno" name="l651" href="#l651">651 </a> move->piece = getpiece(mstr[<span class="c2html-macroconst">0</span>]); +<a class="c2html-lineno" name="l652" href="#l652">652 </a> move->tofile = fileidx(mstr[<span class="c2html-macroconst">1</span>]); +<a class="c2html-lineno" name="l653" href="#l653">653 </a> move->torow = rowidx(mstr[<span class="c2html-macroconst">2</span>]); +<a class="c2html-lineno" name="l654" href="#l654">654 </a> } +<a class="c2html-lineno" name="l655" href="#l655">655 </a> } <span class="c2html-keyword">else</span> <span class="c2html-keyword">if</span> (len == <span class="c2html-macroconst">4</span>) { +<a class="c2html-lineno" name="l656" href="#l656">656 </a> move->piece = getpiece(mstr[<span class="c2html-macroconst">0</span>]); +<a class="c2html-lineno" name="l657" href="#l657">657 </a> <span class="c2html-keyword">if</span> (!move->piece) { +<a class="c2html-lineno" name="l658" href="#l658">658 </a> move->piece = <span class="c2html-macroconst">PAWN</span>; +<a class="c2html-lineno" name="l659" href="#l659">659 </a> move->fromfile = fileidx(mstr[<span class="c2html-macroconst">0</span>]); +<a class="c2html-lineno" name="l660" href="#l660">660 </a> } +<a class="c2html-lineno" name="l661" href="#l661">661 </a> <span class="c2html-keyword">if</span> (mstr[<span class="c2html-macroconst">1</span>] == <span class="c2html-string">'x'</span>) { +<a class="c2html-lineno" name="l662" href="#l662">662 </a> <span class="c2html-comment">/* capture (e.g. "Nxf3", "dxe5") */</span> +<a class="c2html-lineno" name="l663" href="#l663">663 </a> move->capture = <span class="c2html-macroconst">1</span>; +<a class="c2html-lineno" name="l664" href="#l664">664 </a> } <span class="c2html-keyword">else</span> { +<a class="c2html-lineno" name="l665" href="#l665">665 </a> <span class="c2html-comment">/* move (e.g. "Ndf3", "N2c3", "e2e4") */</span> +<a class="c2html-lineno" name="l666" href="#l666">666 </a> <span class="c2html-keyword">if</span> (isfile(mstr[<span class="c2html-macroconst">1</span>])) { +<a class="c2html-lineno" name="l667" href="#l667">667 </a> move->fromfile = fileidx(mstr[<span class="c2html-macroconst">1</span>]); +<a class="c2html-lineno" name="l668" href="#l668">668 </a> <span class="c2html-keyword">if</span> (move->piece == <span class="c2html-macroconst">PAWN</span>) { +<a class="c2html-lineno" name="l669" href="#l669">669 </a> move->piece = <span class="c2html-macroconst">0</span>; +<a class="c2html-lineno" name="l670" href="#l670">670 </a> } +<a class="c2html-lineno" name="l671" href="#l671">671 </a> } <span class="c2html-keyword">else</span> { +<a class="c2html-lineno" name="l672" href="#l672">672 </a> move->fromrow = rowidx(mstr[<span class="c2html-macroconst">1</span>]); +<a class="c2html-lineno" name="l673" href="#l673">673 </a> } +<a class="c2html-lineno" name="l674" href="#l674">674 </a> } +<a class="c2html-lineno" name="l675" href="#l675">675 </a> move->tofile = fileidx(mstr[<span class="c2html-macroconst">2</span>]); +<a class="c2html-lineno" name="l676" href="#l676">676 </a> move->torow = rowidx(mstr[<span class="c2html-macroconst">3</span>]); +<a class="c2html-lineno" name="l677" href="#l677">677 </a> } <span class="c2html-keyword">else</span> <span class="c2html-keyword">if</span> (len == <span class="c2html-macroconst">5</span>) { +<a class="c2html-lineno" name="l678" href="#l678">678 </a> <span class="c2html-keyword">if</span> (strcmp(mstr, <span class="c2html-string">"O-O-O"</span>) == <span class="c2html-macroconst">0</span>) { +<a class="c2html-lineno" name="l679" href="#l679">679 </a> <span class="c2html-comment">/* queen side castling "O-O-O" */</span> +<a class="c2html-lineno" name="l680" href="#l680">680 </a> move->piece = <span class="c2html-macroconst">KING</span>; +<a class="c2html-lineno" name="l681" href="#l681">681 </a> move->fromfile = fileidx(<span class="c2html-string">'e'</span>); +<a class="c2html-lineno" name="l682" href="#l682">682 </a> move->tofile = fileidx(<span class="c2html-string">'c'</span>); +<a class="c2html-lineno" name="l683" href="#l683">683 </a> move->fromrow = move->torow = color == <span class="c2html-macroconst">WHITE</span> ? <span class="c2html-macroconst">0</span> : <span class="c2html-macroconst">7</span>; +<a class="c2html-lineno" name="l684" href="#l684">684 </a> } <span class="c2html-keyword">else</span> { +<a class="c2html-lineno" name="l685" href="#l685">685 </a> move->piece = getpiece(mstr[<span class="c2html-macroconst">0</span>]); +<a class="c2html-lineno" name="l686" href="#l686">686 </a> <span class="c2html-keyword">if</span> (mstr[<span class="c2html-macroconst">2</span>] == <span class="c2html-string">'x'</span>) { +<a class="c2html-lineno" name="l687" href="#l687">687 </a> move->capture = <span class="c2html-macroconst">1</span>; +<a class="c2html-lineno" name="l688" href="#l688">688 </a> <span class="c2html-keyword">if</span> (move->piece) { +<a class="c2html-lineno" name="l689" href="#l689">689 </a> <span class="c2html-comment">/* capture (e.g. "Ndxf3") */</span> +<a class="c2html-lineno" name="l690" href="#l690">690 </a> move->fromfile = fileidx(mstr[<span class="c2html-macroconst">1</span>]); +<a class="c2html-lineno" name="l691" href="#l691">691 </a> } <span class="c2html-keyword">else</span> { +<a class="c2html-lineno" name="l692" href="#l692">692 </a> <span class="c2html-comment">/* long notation capture (e.g. "e5xf6") */</span> +<a class="c2html-lineno" name="l693" href="#l693">693 </a> move->piece = <span class="c2html-macroconst">PAWN</span>; +<a class="c2html-lineno" name="l694" href="#l694">694 </a> move->fromfile = fileidx(mstr[<span class="c2html-macroconst">0</span>]); +<a class="c2html-lineno" name="l695" href="#l695">695 </a> move->fromrow = rowidx(mstr[<span class="c2html-macroconst">1</span>]); +<a class="c2html-lineno" name="l696" href="#l696">696 </a> } +<a class="c2html-lineno" name="l697" href="#l697">697 </a> } <span class="c2html-keyword">else</span> { +<a class="c2html-lineno" name="l698" href="#l698">698 </a> <span class="c2html-comment">/* long notation move (e.g. "Nc5a4") */</span> +<a class="c2html-lineno" name="l699" href="#l699">699 </a> move->fromfile = fileidx(mstr[<span class="c2html-macroconst">1</span>]); +<a class="c2html-lineno" name="l700" href="#l700">700 </a> move->fromrow = rowidx(mstr[<span class="c2html-macroconst">2</span>]); +<a class="c2html-lineno" name="l701" href="#l701">701 </a> } +<a class="c2html-lineno" name="l702" href="#l702">702 </a> move->tofile = fileidx(mstr[<span class="c2html-macroconst">3</span>]); +<a class="c2html-lineno" name="l703" href="#l703">703 </a> move->torow = rowidx(mstr[<span class="c2html-macroconst">4</span>]); +<a class="c2html-lineno" name="l704" href="#l704">704 </a> } +<a class="c2html-lineno" name="l705" href="#l705">705 </a> } <span class="c2html-keyword">else</span> <span class="c2html-keyword">if</span> (len == <span class="c2html-macroconst">6</span>) { +<a class="c2html-lineno" name="l706" href="#l706">706 </a> <span class="c2html-comment">/* long notation capture (e.g. "Nc5xf3") */</span> +<a class="c2html-lineno" name="l707" href="#l707">707 </a> <span class="c2html-keyword">if</span> (mstr[<span class="c2html-macroconst">3</span>] == <span class="c2html-string">'x'</span>) { +<a class="c2html-lineno" name="l708" href="#l708">708 </a> move->capture = <span class="c2html-macroconst">1</span>; +<a class="c2html-lineno" name="l709" href="#l709">709 </a> move->piece = getpiece(mstr[<span class="c2html-macroconst">0</span>]); +<a class="c2html-lineno" name="l710" href="#l710">710 </a> move->fromfile = fileidx(mstr[<span class="c2html-macroconst">1</span>]); +<a class="c2html-lineno" name="l711" href="#l711">711 </a> move->fromrow = rowidx(mstr[<span class="c2html-macroconst">2</span>]); +<a class="c2html-lineno" name="l712" href="#l712">712 </a> move->tofile = fileidx(mstr[<span class="c2html-macroconst">4</span>]); +<a class="c2html-lineno" name="l713" href="#l713">713 </a> move->torow = rowidx(mstr[<span class="c2html-macroconst">5</span>]); +<a class="c2html-lineno" name="l714" href="#l714">714 </a> } +<a class="c2html-lineno" name="l715" href="#l715">715 </a> } +<a class="c2html-lineno" name="l716" href="#l716">716 </a> +<a class="c2html-lineno" name="l717" href="#l717">717 </a> +<a class="c2html-lineno" name="l718" href="#l718">718 </a> <span class="c2html-keyword">if</span> (move->piece) { +<a class="c2html-lineno" name="l719" href="#l719">719 </a> <span class="c2html-keyword">if</span> (move->piece == <span class="c2html-macroconst">PAWN</span> +<a class="c2html-lineno" name="l720" href="#l720">720 </a> && move->torow == (color==<span class="c2html-macroconst">WHITE</span>?<span class="c2html-macroconst">7</span>:<span class="c2html-macroconst">0</span>) +<a class="c2html-lineno" name="l721" href="#l721">721 </a> && !move->promotion) { +<a class="c2html-lineno" name="l722" href="#l722">722 </a> <span class="c2html-keyword">return</span> <span class="c2html-macroconst">NEED_PROMOTION</span>; +<a class="c2html-lineno" name="l723" href="#l723">723 </a> } +<a class="c2html-lineno" name="l724" href="#l724">724 </a> +<a class="c2html-lineno" name="l725" href="#l725">725 </a> move->piece |= color; +<a class="c2html-lineno" name="l726" href="#l726">726 </a> <span class="c2html-keyword">if</span> (move->fromfile == <span class="c2html-macroconst">POS_UNSPECIFIED</span> +<a class="c2html-lineno" name="l727" href="#l727">727 </a> || move->fromrow == <span class="c2html-macroconst">POS_UNSPECIFIED</span>) { +<a class="c2html-lineno" name="l728" href="#l728">728 </a> <span class="c2html-keyword">return</span> getlocation(gamestate, move); +<a class="c2html-lineno" name="l729" href="#l729">729 </a> } <span class="c2html-keyword">else</span> { +<a class="c2html-lineno" name="l730" href="#l730">730 </a> <span class="c2html-keyword">return</span> chkidx(move) ? <span class="c2html-macroconst">VALID_MOVE_SYNTAX</span> : <span class="c2html-macroconst">INVALID_POSITION</span>; +<a class="c2html-lineno" name="l731" href="#l731">731 </a> } +<a class="c2html-lineno" name="l732" href="#l732">732 </a> } <span class="c2html-keyword">else</span> { +<a class="c2html-lineno" name="l733" href="#l733">733 </a> <span class="c2html-keyword">return</span> <span class="c2html-macroconst">INVALID_MOVE_SYNTAX</span>; +<a class="c2html-lineno" name="l734" href="#l734">734 </a> } +<a class="c2html-lineno" name="l735" href="#l735">735 </a>} +<a class="c2html-lineno" name="l736" href="#l736">736 </a> +<a class="c2html-lineno" name="l737" href="#l737">737 </a>_Bool is_protected(GameState *gamestate, <span class="c2html-type">uint8_t</span> row, <span class="c2html-type">uint8_t</span> file, +<a class="c2html-lineno" name="l738" href="#l738">738 </a> <span class="c2html-type">uint8_t</span> color) { +<a class="c2html-lineno" name="l739" href="#l739">739 </a> +<a class="c2html-lineno" name="l740" href="#l740">740 </a> Move threats[<span class="c2html-macroconst">16</span>]; +<a class="c2html-lineno" name="l741" href="#l741">741 </a> <span class="c2html-type">uint8_t</span> threatcount; +<a class="c2html-lineno" name="l742" href="#l742">742 </a> <span class="c2html-keyword">if</span> (get_real_threats(gamestate, row, file, color, threats, &threatcount)) { +<a class="c2html-lineno" name="l743" href="#l743">743 </a> <span class="c2html-keyword">for</span> (<span class="c2html-keyword">int</span> i = <span class="c2html-macroconst">0</span> ; i < threatcount ; i++) { +<a class="c2html-lineno" name="l744" href="#l744">744 </a> <span class="c2html-keyword">if</span> (threats[i].piece != (color|<span class="c2html-macroconst">KING</span>)) { +<a class="c2html-lineno" name="l745" href="#l745">745 </a> <span class="c2html-keyword">return</span> <span class="c2html-macroconst">1</span>; +<a class="c2html-lineno" name="l746" href="#l746">746 </a> } +<a class="c2html-lineno" name="l747" href="#l747">747 </a> } +<a class="c2html-lineno" name="l748" href="#l748">748 </a> <span class="c2html-keyword">return</span> <span class="c2html-macroconst">0</span>; +<a class="c2html-lineno" name="l749" href="#l749">749 </a> } <span class="c2html-keyword">else</span> { +<a class="c2html-lineno" name="l750" href="#l750">750 </a> <span class="c2html-keyword">return</span> <span class="c2html-macroconst">0</span>; +<a class="c2html-lineno" name="l751" href="#l751">751 </a> } +<a class="c2html-lineno" name="l752" href="#l752">752 </a>} +<a class="c2html-lineno" name="l753" href="#l753">753 </a> +<a class="c2html-lineno" name="l754" href="#l754">754 </a><span class="c2html-type">uint16_t</span> remaining_movetime(GameInfo *gameinfo, GameState *gamestate, +<a class="c2html-lineno" name="l755" href="#l755">755 </a> <span class="c2html-type">uint8_t</span> color) { +<a class="c2html-lineno" name="l756" href="#l756">756 </a> <span class="c2html-keyword">if</span> (!gameinfo->timecontrol) { +<a class="c2html-lineno" name="l757" href="#l757">757 </a> <span class="c2html-keyword">return</span> <span class="c2html-macroconst">0</span>; +<a class="c2html-lineno" name="l758" href="#l758">758 </a> } +<a class="c2html-lineno" name="l759" href="#l759">759 </a> +<a class="c2html-lineno" name="l760" href="#l760">760 </a> <span class="c2html-keyword">if</span> (gamestate->movelist) { +<a class="c2html-lineno" name="l761" href="#l761">761 </a> <span class="c2html-type">uint16_t</span> time = gameinfo->time; +<a class="c2html-lineno" name="l762" href="#l762">762 </a> <span class="c2html-type">suseconds_t</span> micros = <span class="c2html-macroconst">0</span>; +<a class="c2html-lineno" name="l763" href="#l763">763 </a> +<a class="c2html-lineno" name="l764" href="#l764">764 </a> MoveList *movelist = color == <span class="c2html-macroconst">WHITE</span> ? +<a class="c2html-lineno" name="l765" href="#l765">765 </a> gamestate->movelist : gamestate->movelist->next; +<a class="c2html-lineno" name="l766" href="#l766">766 </a> +<a class="c2html-lineno" name="l767" href="#l767">767 </a> <span class="c2html-keyword">while</span> (movelist) { +<a class="c2html-lineno" name="l768" href="#l768">768 </a> time += gameinfo->addtime; +<a class="c2html-lineno" name="l769" href="#l769">769 </a> +<a class="c2html-lineno" name="l770" href="#l770">770 </a> <span class="c2html-keyword">struct</span> movetimeval *movetime = &(movelist->move.movetime); +<a class="c2html-lineno" name="l771" href="#l771">771 </a> <span class="c2html-keyword">if</span> (movetime->tv_sec >= time) { +<a class="c2html-lineno" name="l772" href="#l772">772 </a> <span class="c2html-keyword">return</span> <span class="c2html-macroconst">0</span>; +<a class="c2html-lineno" name="l773" href="#l773">773 </a> } +<a class="c2html-lineno" name="l774" href="#l774">774 </a> +<a class="c2html-lineno" name="l775" href="#l775">775 </a> time -= movetime->tv_sec; +<a class="c2html-lineno" name="l776" href="#l776">776 </a> micros += movetime->tv_usec; +<a class="c2html-lineno" name="l777" href="#l777">777 </a> +<a class="c2html-lineno" name="l778" href="#l778">778 </a> movelist = movelist->next ? movelist->next->next : <span class="c2html-macroconst">NULL</span>; +<a class="c2html-lineno" name="l779" href="#l779">779 </a> } +<a class="c2html-lineno" name="l780" href="#l780">780 </a> +<a class="c2html-lineno" name="l781" href="#l781">781 </a> <span class="c2html-type">time_t</span> sec; +<a class="c2html-lineno" name="l782" href="#l782">782 </a> movelist = gamestate->lastmove; +<a class="c2html-lineno" name="l783" href="#l783">783 </a> <span class="c2html-keyword">if</span> ((movelist->move.piece & <span class="c2html-macroconst">COLOR_MASK</span>) != color) { +<a class="c2html-lineno" name="l784" href="#l784">784 </a> <span class="c2html-keyword">struct</span> movetimeval *lastmovetstamp = &(movelist->move.timestamp); +<a class="c2html-lineno" name="l785" href="#l785">785 </a> <span class="c2html-keyword">struct</span> timeval currenttstamp; +<a class="c2html-lineno" name="l786" href="#l786">786 </a> gettimeofday(¤ttstamp, <span class="c2html-macroconst">NULL</span>); +<a class="c2html-lineno" name="l787" href="#l787">787 </a> micros += currenttstamp.tv_usec - lastmovetstamp->tv_usec; +<a class="c2html-lineno" name="l788" href="#l788">788 </a> sec = currenttstamp.tv_sec - lastmovetstamp->tv_sec; +<a class="c2html-lineno" name="l789" href="#l789">789 </a> <span class="c2html-keyword">if</span> (sec >= time) { +<a class="c2html-lineno" name="l790" href="#l790">790 </a> <span class="c2html-keyword">return</span> <span class="c2html-macroconst">0</span>; +<a class="c2html-lineno" name="l791" href="#l791">791 </a> } +<a class="c2html-lineno" name="l792" href="#l792">792 </a> +<a class="c2html-lineno" name="l793" href="#l793">793 </a> time -= sec; +<a class="c2html-lineno" name="l794" href="#l794">794 </a> } +<a class="c2html-lineno" name="l795" href="#l795">795 </a> +<a class="c2html-lineno" name="l796" href="#l796">796 </a> sec = micros / 1e6L; +<a class="c2html-lineno" name="l797" href="#l797">797 </a> +<a class="c2html-lineno" name="l798" href="#l798">798 </a> <span class="c2html-keyword">if</span> (sec >= time) { +<a class="c2html-lineno" name="l799" href="#l799">799 </a> <span class="c2html-keyword">return</span> <span class="c2html-macroconst">0</span>; +<a class="c2html-lineno" name="l800" href="#l800">800 </a> } +<a class="c2html-lineno" name="l801" href="#l801">801 </a> +<a class="c2html-lineno" name="l802" href="#l802">802 </a> time -= sec; +<a class="c2html-lineno" name="l803" href="#l803">803 </a> +<a class="c2html-lineno" name="l804" href="#l804">804 </a> <span class="c2html-keyword">return</span> time; +<a class="c2html-lineno" name="l805" href="#l805">805 </a> } <span class="c2html-keyword">else</span> { +<a class="c2html-lineno" name="l806" href="#l806">806 </a> <span class="c2html-keyword">return</span> gameinfo->time; +<a class="c2html-lineno" name="l807" href="#l807">807 </a> } +<a class="c2html-lineno" name="l808" href="#l808">808 </a>} +</div> + </body> +</html> +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test/gs/ctest.html Mon Apr 24 20:54:38 2023 +0200 @@ -0,0 +1,448 @@ +<!DOCTYPE html> +<html> + <head> + <title>c2html</title> + <style type="text/css"> + div.c2html-code { + white-space: pre; + font-family: monospace; + } + a.c2html-lineno { + /* as long as user-select isn't widely spread, we throw the bomb */ + -webkit-user-select: none; + -moz-user-select: none; + -ms-user-select: none; + user-select: none; + display: inline-block; + font-style: italic; + text-decoration: none; + color: grey; + } + span.c2html-keyword { + color: blue; + } + span.c2html-macroconst { + color: cornflowerblue; + } + span.c2html-type { + color: cornflowerblue; + } + span.c2html-directive { + color: green; + } + span.c2html-string { + color: darkorange; + } + span.c2html-comment { + color: grey; + } + span.c2html-stdinclude { + color: darkorange; + } + span.c2html-userinclude { + color: darkorange; + } + a.c2html-userinclude { + color: darkorange; + text-decoration: underline; + } + </style> + </head> + <body> + +<div class="c2html-code"> +<a class="c2html-lineno" name="l1" href="#l1"> 1 </a><span class="c2html-comment">/*</span> +<a class="c2html-lineno" name="l2" href="#l2"> 2 </a><span class="c2html-comment"> * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.</span> +<a class="c2html-lineno" name="l3" href="#l3"> 3 </a><span class="c2html-comment"> *</span> +<a class="c2html-lineno" name="l4" href="#l4"> 4 </a><span class="c2html-comment"> * Copyright 2015 Olaf Wintermann. All rights reserved.</span> +<a class="c2html-lineno" name="l5" href="#l5"> 5 </a><span class="c2html-comment"> *</span> +<a class="c2html-lineno" name="l6" href="#l6"> 6 </a><span class="c2html-comment"> * Redistribution and use in source and binary forms, with or without</span> +<a class="c2html-lineno" name="l7" href="#l7"> 7 </a><span class="c2html-comment"> * modification, are permitted provided that the following conditions are met:</span> +<a class="c2html-lineno" name="l8" href="#l8"> 8 </a><span class="c2html-comment"> *</span> +<a class="c2html-lineno" name="l9" href="#l9"> 9 </a><span class="c2html-comment"> * 1. Redistributions of source code must retain the above copyright</span> +<a class="c2html-lineno" name="l10" href="#l10"> 10 </a><span class="c2html-comment"> * notice, this list of conditions and the following disclaimer.</span> +<a class="c2html-lineno" name="l11" href="#l11"> 11 </a><span class="c2html-comment"> *</span> +<a class="c2html-lineno" name="l12" href="#l12"> 12 </a><span class="c2html-comment"> * 2. Redistributions in binary form must reproduce the above copyright</span> +<a class="c2html-lineno" name="l13" href="#l13"> 13 </a><span class="c2html-comment"> * notice, this list of conditions and the following disclaimer in the</span> +<a class="c2html-lineno" name="l14" href="#l14"> 14 </a><span class="c2html-comment"> * documentation and/or other materials provided with the distribution.</span> +<a class="c2html-lineno" name="l15" href="#l15"> 15 </a><span class="c2html-comment"> *</span> +<a class="c2html-lineno" name="l16" href="#l16"> 16 </a><span class="c2html-comment"> * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"</span> +<a class="c2html-lineno" name="l17" href="#l17"> 17 </a><span class="c2html-comment"> * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE</span> +<a class="c2html-lineno" name="l18" href="#l18"> 18 </a><span class="c2html-comment"> * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE</span> +<a class="c2html-lineno" name="l19" href="#l19"> 19 </a><span class="c2html-comment"> * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE</span> +<a class="c2html-lineno" name="l20" href="#l20"> 20 </a><span class="c2html-comment"> * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR</span> +<a class="c2html-lineno" name="l21" href="#l21"> 21 </a><span class="c2html-comment"> * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF</span> +<a class="c2html-lineno" name="l22" href="#l22"> 22 </a><span class="c2html-comment"> * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS</span> +<a class="c2html-lineno" name="l23" href="#l23"> 23 </a><span class="c2html-comment"> * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN</span> +<a class="c2html-lineno" name="l24" href="#l24"> 24 </a><span class="c2html-comment"> * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)</span> +<a class="c2html-lineno" name="l25" href="#l25"> 25 </a><span class="c2html-comment"> * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE</span> +<a class="c2html-lineno" name="l26" href="#l26"> 26 </a><span class="c2html-comment"> * POSSIBILITY OF SUCH DAMAGE.</span> +<a class="c2html-lineno" name="l27" href="#l27"> 27 </a><span class="c2html-comment"> */</span> +<a class="c2html-lineno" name="l28" href="#l28"> 28 </a> +<a class="c2html-lineno" name="l29" href="#l29"> 29 </a><span class="c2html-directive">#include</span> <span class="c2html-stdinclude"><time.h></span> +<a class="c2html-lineno" name="l30" href="#l30"> 30 </a><span class="c2html-directive">#include</span> <span class="c2html-stdinclude"><stdio.h></span> +<a class="c2html-lineno" name="l31" href="#l31"> 31 </a><span class="c2html-directive">#include</span> <span class="c2html-stdinclude"><stdlib.h></span> +<a class="c2html-lineno" name="l32" href="#l32"> 32 </a><span class="c2html-directive">#include</span> <span class="c2html-stdinclude"><string.h></span> +<a class="c2html-lineno" name="l33" href="#l33"> 33 </a><span class="c2html-directive">#include</span> <span class="c2html-stdinclude"><ucx/string.h></span> +<a class="c2html-lineno" name="l34" href="#l34"> 34 </a><span class="c2html-directive">#include</span> <span class="c2html-stdinclude"><ucx/buffer.h></span> +<a class="c2html-lineno" name="l35" href="#l35"> 35 </a><span class="c2html-directive">#include</span> <span class="c2html-stdinclude"><ucx/utils.h></span> +<a class="c2html-lineno" name="l36" href="#l36"> 36 </a><span class="c2html-directive">#include</span> <span class="c2html-stdinclude"><libxml/tree.h></span> +<a class="c2html-lineno" name="l37" href="#l37"> 37 </a><span class="c2html-directive">#include</span> <span class="c2html-stdinclude"><curl/curl.h></span> +<a class="c2html-lineno" name="l38" href="#l38"> 38 </a> +<a class="c2html-lineno" name="l39" href="#l39"> 39 </a><span class="c2html-directive">#include</span> <span class="c2html-stdinclude"><openssl/sha.h></span> +<a class="c2html-lineno" name="l40" href="#l40"> 40 </a><span class="c2html-directive">#include</span> <span class="c2html-stdinclude"><openssl/hmac.h></span> +<a class="c2html-lineno" name="l41" href="#l41"> 41 </a><span class="c2html-directive">#include</span> <span class="c2html-stdinclude"><openssl/evp.h></span> +<a class="c2html-lineno" name="l42" href="#l42"> 42 </a><span class="c2html-directive">#include</span> <span class="c2html-stdinclude"><openssl/bio.h></span> +<a class="c2html-lineno" name="l43" href="#l43"> 43 </a><span class="c2html-directive">#include</span> <span class="c2html-stdinclude"><openssl/buffer.h></span> +<a class="c2html-lineno" name="l44" href="#l44"> 44 </a><span class="c2html-directive">#include</span> <span class="c2html-stdinclude"><openssl/rand.h></span> +<a class="c2html-lineno" name="l45" href="#l45"> 45 </a> +<a class="c2html-lineno" name="l46" href="#l46"> 46 </a><span class="c2html-directive">#include</span> <a class="c2html-userinclude" href="utils.h">"utils.h"</a> +<a class="c2html-lineno" name="l47" href="#l47"> 47 </a><span class="c2html-directive">#include</span> <a class="c2html-userinclude" href="crypto.h">"crypto.h"</a> +<a class="c2html-lineno" name="l48" href="#l48"> 48 </a><span class="c2html-directive">#include</span> <a class="c2html-userinclude" href="webdav.h">"webdav.h"</a> +<a class="c2html-lineno" name="l49" href="#l49"> 49 </a> +<a class="c2html-lineno" name="l50" href="#l50"> 50 </a><span class="c2html-directive">#define</span> <span class="c2html-macroconst">MACRO1337</span> <span class="c2html-macroconst">1337L</span> +<a class="c2html-lineno" name="l51" href="#l51"> 51 </a> +<a class="c2html-lineno" name="l52" href="#l52"> 52 </a><span class="c2html-comment">/* -------------------- This is a testing file. -------------------------- */</span> +<a class="c2html-lineno" name="l53" href="#l53"> 53 </a><span class="c2html-comment">/*</span> +<a class="c2html-lineno" name="l54" href="#l54"> 54 </a><span class="c2html-comment">time_t util_parse_creationdate(char *str) {</span> +<a class="c2html-lineno" name="l55" href="#l55"> 55 </a><span class="c2html-comment"> // example: 2012-11-29T21:35:35Z</span> +<a class="c2html-lineno" name="l56" href="#l56"> 56 </a><span class="c2html-comment"> if(!str) {</span> +<a class="c2html-lineno" name="l57" href="#l57"> 57 </a><span class="c2html-comment"> return 0;</span> +<a class="c2html-lineno" name="l58" href="#l58"> 58 </a><span class="c2html-comment"> }</span> +<a class="c2html-lineno" name="l59" href="#l59"> 59 </a><span class="c2html-comment"> // TODO</span> +<a class="c2html-lineno" name="l60" href="#l60"> 60 </a><span class="c2html-comment"> return 0;</span> +<a class="c2html-lineno" name="l61" href="#l61"> 61 </a><span class="c2html-comment">}</span> +<a class="c2html-lineno" name="l62" href="#l62"> 62 </a><span class="c2html-comment">*/</span> +<a class="c2html-lineno" name="l63" href="#l63"> 63 </a><span class="c2html-type">time_t</span> util_parse_lastmodified(<span class="c2html-keyword">char</span> *str) { +<a class="c2html-lineno" name="l64" href="#l64"> 64 </a> <span class="c2html-comment">// example: Thu, 29 Nov 2012 21:35:35 GMT</span> +<a class="c2html-lineno" name="l65" href="#l65"> 65 </a> <span class="c2html-keyword">if</span>(!str) { +<a class="c2html-lineno" name="l66" href="#l66"> 66 </a> <span class="c2html-keyword">return</span> <span class="c2html-macroconst">0</span>; +<a class="c2html-lineno" name="l67" href="#l67"> 67 </a> } <span class="c2html-keyword">else</span> { +<a class="c2html-lineno" name="l68" href="#l68"> 68 </a> <span class="c2html-keyword">return</span> curl_getdate(str, <span class="c2html-macroconst">NULL</span>); +<a class="c2html-lineno" name="l69" href="#l69"> 69 </a> } +<a class="c2html-lineno" name="l70" href="#l70"> 70 </a>} +<a class="c2html-lineno" name="l71" href="#l71"> 71 </a> +<a class="c2html-lineno" name="l72" href="#l72"> 72 </a><span class="c2html-keyword">int</span> util_getboolean(<span class="c2html-keyword">char</span> *v) { +<a class="c2html-lineno" name="l73" href="#l73"> 73 </a> <span class="c2html-keyword">if</span>(v[<span class="c2html-macroconst">0</span>] == <span class="c2html-string">'T'</span> || v[<span class="c2html-macroconst">0</span>] == <span class="c2html-string">'t'</span>) { +<a class="c2html-lineno" name="l74" href="#l74"> 74 </a> <span class="c2html-keyword">return</span> <span class="c2html-macroconst">1</span>; +<a class="c2html-lineno" name="l75" href="#l75"> 75 </a> } +<a class="c2html-lineno" name="l76" href="#l76"> 76 </a> <span class="c2html-keyword">return</span> <span class="c2html-macroconst">0</span>; +<a class="c2html-lineno" name="l77" href="#l77"> 77 </a>} +<a class="c2html-lineno" name="l78" href="#l78"> 78 </a> +<a class="c2html-lineno" name="l79" href="#l79"> 79 </a><span class="c2html-keyword">int</span> util_strtoint(<span class="c2html-keyword">char</span> *str, <span class="c2html-type">int64_t</span> *value) { +<a class="c2html-lineno" name="l80" href="#l80"> 80 </a> <span class="c2html-keyword">char</span> *end; +<a class="c2html-lineno" name="l81" href="#l81"> 81 </a> <span class="c2html-type">int64_t</span> val = strtoll(str, &end, <span class="c2html-macroconst">0</span>); +<a class="c2html-lineno" name="l82" href="#l82"> 82 </a> <span class="c2html-keyword">if</span>(strlen(end) == <span class="c2html-macroconst">0</span>) { +<a class="c2html-lineno" name="l83" href="#l83"> 83 </a> *value = val; +<a class="c2html-lineno" name="l84" href="#l84"> 84 </a> <span class="c2html-keyword">return</span> <span class="c2html-macroconst">1</span>; +<a class="c2html-lineno" name="l85" href="#l85"> 85 </a> } <span class="c2html-keyword">else</span> { +<a class="c2html-lineno" name="l86" href="#l86"> 86 </a> <span class="c2html-keyword">return</span> <span class="c2html-macroconst">0</span>; +<a class="c2html-lineno" name="l87" href="#l87"> 87 </a> } +<a class="c2html-lineno" name="l88" href="#l88"> 88 </a>} +<a class="c2html-lineno" name="l89" href="#l89"> 89 </a> +<a class="c2html-lineno" name="l90" href="#l90"> 90 </a><span class="c2html-keyword">char</span>* util_url_path(<span class="c2html-keyword">char</span> *url) { +<a class="c2html-lineno" name="l91" href="#l91"> 91 </a> <span class="c2html-keyword">char</span> *path = <span class="c2html-macroconst">NULL</span>; +<a class="c2html-lineno" name="l92" href="#l92"> 92 </a> <span class="c2html-type">size_t</span> len = strlen(url); +<a class="c2html-lineno" name="l93" href="#l93"> 93 </a> <span class="c2html-keyword">int</span> slashcount = <span class="c2html-macroconst">0</span>; +<a class="c2html-lineno" name="l94" href="#l94"> 94 </a> <span class="c2html-keyword">int</span> slmax; +<a class="c2html-lineno" name="l95" href="#l95"> 95 </a> <span class="c2html-keyword">if</span>(len > <span class="c2html-macroconst">7</span> && !strncasecmp(url, <span class="c2html-string">"http://"</span>, <span class="c2html-macroconst">7</span>)) { +<a class="c2html-lineno" name="l96" href="#l96"> 96 </a> slmax = <span class="c2html-macroconst">3</span>; +<a class="c2html-lineno" name="l97" href="#l97"> 97 </a> } <span class="c2html-keyword">else</span> <span class="c2html-keyword">if</span>(len > <span class="c2html-macroconst">8</span> && !strncasecmp(url, <span class="c2html-string">"https://"</span>, <span class="c2html-macroconst">8</span>)) { +<a class="c2html-lineno" name="l98" href="#l98"> 98 </a> slmax = <span class="c2html-macroconst">3</span>; +<a class="c2html-lineno" name="l99" href="#l99"> 99 </a> } <span class="c2html-keyword">else</span> { +<a class="c2html-lineno" name="l100" href="#l100">100 </a> slmax = <span class="c2html-macroconst">1</span>; +<a class="c2html-lineno" name="l101" href="#l101">101 </a> } +<a class="c2html-lineno" name="l102" href="#l102">102 </a> <span class="c2html-keyword">char</span> c; +<a class="c2html-lineno" name="l103" href="#l103">103 </a> <span class="c2html-keyword">for</span>(<span class="c2html-keyword">int</span> i=<span class="c2html-macroconst">0</span>;i<len;i++) { +<a class="c2html-lineno" name="l104" href="#l104">104 </a> c = url[i]; +<a class="c2html-lineno" name="l105" href="#l105">105 </a> <span class="c2html-keyword">if</span>(c == <span class="c2html-string">'/'</span>) { +<a class="c2html-lineno" name="l106" href="#l106">106 </a> slashcount++; +<a class="c2html-lineno" name="l107" href="#l107">107 </a> <span class="c2html-keyword">if</span>(slashcount == slmax) { +<a class="c2html-lineno" name="l108" href="#l108">108 </a> path = url + i; +<a class="c2html-lineno" name="l109" href="#l109">109 </a> <span class="c2html-keyword">break</span>; +<a class="c2html-lineno" name="l110" href="#l110">110 </a> } +<a class="c2html-lineno" name="l111" href="#l111">111 </a> } +<a class="c2html-lineno" name="l112" href="#l112">112 </a> } +<a class="c2html-lineno" name="l113" href="#l113">113 </a> <span class="c2html-keyword">return</span> path; +<a class="c2html-lineno" name="l114" href="#l114">114 </a>} +<a class="c2html-lineno" name="l115" href="#l115">115 </a> +<a class="c2html-lineno" name="l116" href="#l116">116 </a><span class="c2html-keyword">char</span>* util_url_decode(DavSession *sn, <span class="c2html-keyword">char</span> *url) { +<a class="c2html-lineno" name="l117" href="#l117">117 </a> <span class="c2html-keyword">char</span> *unesc = curl_easy_unescape(sn->handle, url, strlen(url), <span class="c2html-macroconst">NULL</span>); +<a class="c2html-lineno" name="l118" href="#l118">118 </a> <span class="c2html-keyword">char</span> *ret = strdup(unesc); +<a class="c2html-lineno" name="l119" href="#l119">119 </a> curl_free(unesc); +<a class="c2html-lineno" name="l120" href="#l120">120 </a> <span class="c2html-keyword">return</span> ret; +<a class="c2html-lineno" name="l121" href="#l121">121 </a>} +<a class="c2html-lineno" name="l122" href="#l122">122 </a> +<a class="c2html-lineno" name="l123" href="#l123">123 </a><span class="c2html-keyword">char</span>* util_resource_name(<span class="c2html-keyword">char</span> *url) { +<a class="c2html-lineno" name="l124" href="#l124">124 </a> <span class="c2html-keyword">int</span> si = <span class="c2html-macroconst">0</span>; +<a class="c2html-lineno" name="l125" href="#l125">125 </a> <span class="c2html-keyword">int</span> osi = <span class="c2html-macroconst">0</span>; +<a class="c2html-lineno" name="l126" href="#l126">126 </a> <span class="c2html-keyword">int</span> i = <span class="c2html-macroconst">0</span>; +<a class="c2html-lineno" name="l127" href="#l127">127 </a> <span class="c2html-keyword">int</span> p = <span class="c2html-macroconst">0</span>; +<a class="c2html-lineno" name="l128" href="#l128">128 </a> <span class="c2html-keyword">char</span> c; +<a class="c2html-lineno" name="l129" href="#l129">129 </a> <span class="c2html-keyword">while</span>((c = url[i]) != <span class="c2html-macroconst">0</span>) { +<a class="c2html-lineno" name="l130" href="#l130">130 </a> <span class="c2html-keyword">if</span>(c == <span class="c2html-string">'/'</span>) { +<a class="c2html-lineno" name="l131" href="#l131">131 </a> osi = si; +<a class="c2html-lineno" name="l132" href="#l132">132 </a> si = i; +<a class="c2html-lineno" name="l133" href="#l133">133 </a> p = <span class="c2html-macroconst">1</span>; +<a class="c2html-lineno" name="l134" href="#l134">134 </a> } +<a class="c2html-lineno" name="l135" href="#l135">135 </a> i++; +<a class="c2html-lineno" name="l136" href="#l136">136 </a> } +<a class="c2html-lineno" name="l137" href="#l137">137 </a> +<a class="c2html-lineno" name="l138" href="#l138">138 </a> <span class="c2html-keyword">char</span> *name = url + si + p; +<a class="c2html-lineno" name="l139" href="#l139">139 </a> <span class="c2html-keyword">if</span>(name[<span class="c2html-macroconst">0</span>] == <span class="c2html-macroconst">0</span>) { +<a class="c2html-lineno" name="l140" href="#l140">140 </a> name = url + osi + p; +<a class="c2html-lineno" name="l141" href="#l141">141 </a> <span class="c2html-keyword">if</span>(name[<span class="c2html-macroconst">0</span>] == <span class="c2html-macroconst">0</span>) { +<a class="c2html-lineno" name="l142" href="#l142">142 </a> <span class="c2html-keyword">return</span> url; +<a class="c2html-lineno" name="l143" href="#l143">143 </a> } +<a class="c2html-lineno" name="l144" href="#l144">144 </a> } +<a class="c2html-lineno" name="l145" href="#l145">145 </a> +<a class="c2html-lineno" name="l146" href="#l146">146 </a> <span class="c2html-keyword">return</span> name; +<a class="c2html-lineno" name="l147" href="#l147">147 </a>} +<a class="c2html-lineno" name="l148" href="#l148">148 </a> +<a class="c2html-lineno" name="l149" href="#l149">149 </a><span class="c2html-keyword">int</span> util_mkdir(<span class="c2html-keyword">char</span> *path, <span class="c2html-type">mode_t</span> mode) { +<a class="c2html-lineno" name="l150" href="#l150">150 </a><span class="c2html-directive">#ifdef</span> <span class="c2html-macroconst">_WIN32</span> +<a class="c2html-lineno" name="l151" href="#l151">151 </a> <span class="c2html-keyword">return</span> mkdir(path); +<a class="c2html-lineno" name="l152" href="#l152">152 </a><span class="c2html-directive">#else</span> +<a class="c2html-lineno" name="l153" href="#l153">153 </a> <span class="c2html-keyword">return</span> mkdir(path, mode); +<a class="c2html-lineno" name="l154" href="#l154">154 </a><span class="c2html-directive">#endif</span> +<a class="c2html-lineno" name="l155" href="#l155">155 </a>} +<a class="c2html-lineno" name="l156" href="#l156">156 </a> +<a class="c2html-lineno" name="l157" href="#l157">157 </a><span class="c2html-keyword">char</span>* util_concat_path(<span class="c2html-keyword">char</span> *url_base, <span class="c2html-keyword">char</span> *p) { +<a class="c2html-lineno" name="l158" href="#l158">158 </a> <span class="c2html-type">sstr_t</span> base = sstr(url_base); +<a class="c2html-lineno" name="l159" href="#l159">159 </a> <span class="c2html-type">sstr_t</span> path; +<a class="c2html-lineno" name="l160" href="#l160">160 </a> <span class="c2html-keyword">if</span>(p) { +<a class="c2html-lineno" name="l161" href="#l161">161 </a> path = sstr(p); +<a class="c2html-lineno" name="l162" href="#l162">162 </a> } <span class="c2html-keyword">else</span> { +<a class="c2html-lineno" name="l163" href="#l163">163 </a> path = sstrn(<span class="c2html-string">""</span>, <span class="c2html-macroconst">0</span>); +<a class="c2html-lineno" name="l164" href="#l164">164 </a> } +<a class="c2html-lineno" name="l165" href="#l165">165 </a> +<a class="c2html-lineno" name="l166" href="#l166">166 </a> <span class="c2html-keyword">int</span> add_separator = <span class="c2html-macroconst">0</span>; +<a class="c2html-lineno" name="l167" href="#l167">167 </a> <span class="c2html-keyword">if</span>(base.ptr[base.length-<span class="c2html-macroconst">1</span>] == <span class="c2html-string">'/'</span>) { +<a class="c2html-lineno" name="l168" href="#l168">168 </a> <span class="c2html-keyword">if</span>(path.ptr[<span class="c2html-macroconst">0</span>] == <span class="c2html-string">'/'</span>) { +<a class="c2html-lineno" name="l169" href="#l169">169 </a> base.length--; +<a class="c2html-lineno" name="l170" href="#l170">170 </a> } +<a class="c2html-lineno" name="l171" href="#l171">171 </a> } <span class="c2html-keyword">else</span> { +<a class="c2html-lineno" name="l172" href="#l172">172 </a> <span class="c2html-keyword">if</span>(path.length == <span class="c2html-macroconst">0</span> || path.ptr[<span class="c2html-macroconst">0</span>] != <span class="c2html-string">'/'</span>) { +<a class="c2html-lineno" name="l173" href="#l173">173 </a> add_separator = <span class="c2html-macroconst">1</span>; +<a class="c2html-lineno" name="l174" href="#l174">174 </a> } +<a class="c2html-lineno" name="l175" href="#l175">175 </a> } +<a class="c2html-lineno" name="l176" href="#l176">176 </a> +<a class="c2html-lineno" name="l177" href="#l177">177 </a> <span class="c2html-type">sstr_t</span> url; +<a class="c2html-lineno" name="l178" href="#l178">178 </a> <span class="c2html-keyword">if</span>(add_separator) { +<a class="c2html-lineno" name="l179" href="#l179">179 </a> url = sstrcat(<span class="c2html-macroconst">3</span>, base, sstr(<span class="c2html-string">"/"</span>), path); +<a class="c2html-lineno" name="l180" href="#l180">180 </a> } <span class="c2html-keyword">else</span> { +<a class="c2html-lineno" name="l181" href="#l181">181 </a> url = sstrcat(<span class="c2html-macroconst">2</span>, base, path); +<a class="c2html-lineno" name="l182" href="#l182">182 </a> } +<a class="c2html-lineno" name="l183" href="#l183">183 </a> +<a class="c2html-lineno" name="l184" href="#l184">184 </a> <span class="c2html-keyword">return</span> url.ptr; +<a class="c2html-lineno" name="l185" href="#l185">185 </a>} +<a class="c2html-lineno" name="l186" href="#l186">186 </a> +<a class="c2html-lineno" name="l187" href="#l187">187 </a><span class="c2html-keyword">void</span> util_set_url(DavSession *sn, <span class="c2html-keyword">char</span> *href) { +<a class="c2html-lineno" name="l188" href="#l188">188 </a> <span class="c2html-type">sstr_t</span> base = sstr(sn->base_url); +<a class="c2html-lineno" name="l189" href="#l189">189 </a> <span class="c2html-type">sstr_t</span> href_str = sstr(href); +<a class="c2html-lineno" name="l190" href="#l190">190 </a> +<a class="c2html-lineno" name="l191" href="#l191">191 </a> <span class="c2html-keyword">char</span> *base_path = util_url_path(sn->base_url); +<a class="c2html-lineno" name="l192" href="#l192">192 </a> base.length -= strlen(base_path); +<a class="c2html-lineno" name="l193" href="#l193">193 </a> +<a class="c2html-lineno" name="l194" href="#l194">194 </a> <span class="c2html-type">sstr_t</span> url = sstrcat(<span class="c2html-macroconst">2</span>, base, href_str); +<a class="c2html-lineno" name="l195" href="#l195">195 </a> +<a class="c2html-lineno" name="l196" href="#l196">196 </a> curl_easy_setopt(sn->handle, <span class="c2html-macroconst">CURLOPT_URL</span>, url.ptr); +<a class="c2html-lineno" name="l197" href="#l197">197 </a> free(url.ptr); +<a class="c2html-lineno" name="l198" href="#l198">198 </a>} +<a class="c2html-lineno" name="l199" href="#l199">199 </a> +<a class="c2html-lineno" name="l200" href="#l200">200 </a><span class="c2html-keyword">char</span>* util_path_to_url(DavSession *sn, <span class="c2html-keyword">char</span> *path) { +<a class="c2html-lineno" name="l201" href="#l201">201 </a> <span class="c2html-keyword">char</span> *space = malloc(<span class="c2html-macroconst">256</span>); +<a class="c2html-lineno" name="l202" href="#l202">202 </a> UcxBuffer *url = ucx_buffer_new(space, <span class="c2html-macroconst">256</span>, <span class="c2html-macroconst">CX_BUFFER_AUTO_EXTEND</span>); +<a class="c2html-lineno" name="l203" href="#l203">203 </a> +<a class="c2html-lineno" name="l204" href="#l204">204 </a> <span class="c2html-comment">// add base url</span> +<a class="c2html-lineno" name="l205" href="#l205">205 </a> ucx_buffer_write(sn->base_url, <span class="c2html-macroconst">1</span>, strlen(sn->base_url), url); +<a class="c2html-lineno" name="l206" href="#l206">206 </a> <span class="c2html-comment">// remove trailing slash</span> +<a class="c2html-lineno" name="l207" href="#l207">207 </a> ucx_buffer_seek(url, -<span class="c2html-macroconst">1</span>, <span class="c2html-macroconst">SEEK_CUR</span>); +<a class="c2html-lineno" name="l208" href="#l208">208 </a> +<a class="c2html-lineno" name="l209" href="#l209">209 </a> <span class="c2html-type">sstr_t</span> p = sstr(path); +<a class="c2html-lineno" name="l210" href="#l210">210 </a> <span class="c2html-type">ssize_t</span> ntk = <span class="c2html-macroconst">0</span>; +<a class="c2html-lineno" name="l211" href="#l211">211 </a> <span class="c2html-type">sstr_t</span> *tks = sstrsplit(p, <span class="c2html-macroconst">S</span>(<span class="c2html-string">"/"</span>), &ntk); +<a class="c2html-lineno" name="l212" href="#l212">212 </a> +<a class="c2html-lineno" name="l213" href="#l213">213 </a> <span class="c2html-keyword">for</span>(<span class="c2html-keyword">int</span> i=<span class="c2html-macroconst">0</span>;i<ntk;i++) { +<a class="c2html-lineno" name="l214" href="#l214">214 </a> <span class="c2html-type">sstr_t</span> node = tks[i]; +<a class="c2html-lineno" name="l215" href="#l215">215 </a> <span class="c2html-keyword">if</span>(node.length > <span class="c2html-macroconst">0</span>) { +<a class="c2html-lineno" name="l216" href="#l216">216 </a> <span class="c2html-keyword">char</span> *esc = curl_easy_escape(sn->handle, node.ptr, node.length); +<a class="c2html-lineno" name="l217" href="#l217">217 </a> ucx_buffer_putc(url, <span class="c2html-string">'/'</span>); +<a class="c2html-lineno" name="l218" href="#l218">218 </a> ucx_buffer_write(esc, <span class="c2html-macroconst">1</span>, strlen(esc), url); +<a class="c2html-lineno" name="l219" href="#l219">219 </a> curl_free(esc); +<a class="c2html-lineno" name="l220" href="#l220">220 </a> } +<a class="c2html-lineno" name="l221" href="#l221">221 </a> free(node.ptr); +<a class="c2html-lineno" name="l222" href="#l222">222 </a> } +<a class="c2html-lineno" name="l223" href="#l223">223 </a> free(tks); +<a class="c2html-lineno" name="l224" href="#l224">224 </a> <span class="c2html-keyword">if</span>(path[p.length-<span class="c2html-macroconst">1</span>] == <span class="c2html-string">'/'</span>) { +<a class="c2html-lineno" name="l225" href="#l225">225 </a> ucx_buffer_putc(url, <span class="c2html-string">'/'</span>); +<a class="c2html-lineno" name="l226" href="#l226">226 </a> } +<a class="c2html-lineno" name="l227" href="#l227">227 </a> ucx_buffer_putc(url, <span class="c2html-macroconst">0</span>); +<a class="c2html-lineno" name="l228" href="#l228">228 </a> +<a class="c2html-lineno" name="l229" href="#l229">229 </a> space = url->space; +<a class="c2html-lineno" name="l230" href="#l230">230 </a> ucx_buffer_free(url); +<a class="c2html-lineno" name="l231" href="#l231">231 </a> +<a class="c2html-lineno" name="l232" href="#l232">232 </a> <span class="c2html-keyword">return</span> space; +<a class="c2html-lineno" name="l233" href="#l233">233 </a>} +<a class="c2html-lineno" name="l234" href="#l234">234 </a> +<a class="c2html-lineno" name="l235" href="#l235">235 </a><span class="c2html-keyword">char</span>* util_parent_path(<span class="c2html-keyword">char</span> *path) { +<a class="c2html-lineno" name="l236" href="#l236">236 </a> <span class="c2html-keyword">char</span> *name = util_resource_name(path); +<a class="c2html-lineno" name="l237" href="#l237">237 </a> <span class="c2html-type">size_t</span> namelen = strlen(name); +<a class="c2html-lineno" name="l238" href="#l238">238 </a> <span class="c2html-type">size_t</span> pathlen = strlen(path); +<a class="c2html-lineno" name="l239" href="#l239">239 </a> <span class="c2html-type">size_t</span> parentlen = pathlen - namelen; +<a class="c2html-lineno" name="l240" href="#l240">240 </a> <span class="c2html-keyword">char</span> *parent = malloc(parentlen + <span class="c2html-macroconst">1</span>); +<a class="c2html-lineno" name="l241" href="#l241">241 </a> memcpy(parent, path, parentlen); +<a class="c2html-lineno" name="l242" href="#l242">242 </a> parent[parentlen] = <span class="c2html-string">'\0'</span>; +<a class="c2html-lineno" name="l243" href="#l243">243 </a> <span class="c2html-keyword">return</span> parent; +<a class="c2html-lineno" name="l244" href="#l244">244 </a>} +<a class="c2html-lineno" name="l245" href="#l245">245 </a> +<a class="c2html-lineno" name="l246" href="#l246">246 </a> +<a class="c2html-lineno" name="l247" href="#l247">247 </a><span class="c2html-keyword">char</span>* util_xml_get_text(xmlNode *elm) { +<a class="c2html-lineno" name="l248" href="#l248">248 </a> xmlNode *node = elm->children; +<a class="c2html-lineno" name="l249" href="#l249">249 </a> <span class="c2html-keyword">while</span>(node) { +<a class="c2html-lineno" name="l250" href="#l250">250 </a> <span class="c2html-keyword">if</span>(node->type == <span class="c2html-macroconst">XML_TEXT_NODE</span>) { +<a class="c2html-lineno" name="l251" href="#l251">251 </a> <span class="c2html-keyword">return</span> (<span class="c2html-keyword">char</span>*)node->content; +<a class="c2html-lineno" name="l252" href="#l252">252 </a> } +<a class="c2html-lineno" name="l253" href="#l253">253 </a> node = node->next; +<a class="c2html-lineno" name="l254" href="#l254">254 </a> } +<a class="c2html-lineno" name="l255" href="#l255">255 </a> <span class="c2html-keyword">return</span> <span class="c2html-macroconst">NULL</span>; +<a class="c2html-lineno" name="l256" href="#l256">256 </a>} +<a class="c2html-lineno" name="l257" href="#l257">257 </a> +<a class="c2html-lineno" name="l258" href="#l258">258 </a> +<a class="c2html-lineno" name="l259" href="#l259">259 </a><span class="c2html-keyword">char</span>* util_base64decode(<span class="c2html-keyword">char</span> *in) { +<a class="c2html-lineno" name="l260" href="#l260">260 </a> <span class="c2html-keyword">int</span> len = <span class="c2html-macroconst">0</span>; +<a class="c2html-lineno" name="l261" href="#l261">261 </a> <span class="c2html-keyword">return</span> util_base64decode_len(in, &len); +<a class="c2html-lineno" name="l262" href="#l262">262 </a>} +<a class="c2html-lineno" name="l263" href="#l263">263 </a> +<a class="c2html-lineno" name="l264" href="#l264">264 </a><span class="c2html-keyword">char</span>* util_base64decode_len(<span class="c2html-keyword">char</span>* in, <span class="c2html-keyword">int</span> *outlen) { +<a class="c2html-lineno" name="l265" href="#l265">265 </a> <span class="c2html-type">size_t</span> len = strlen(in); +<a class="c2html-lineno" name="l266" href="#l266">266 </a> <span class="c2html-keyword">char</span> *out = calloc(<span class="c2html-macroconst">1</span>, len); +<a class="c2html-lineno" name="l267" href="#l267">267 </a> +<a class="c2html-lineno" name="l268" href="#l268">268 </a> <span class="c2html-macroconst">BIO</span>* b = BIO_new_mem_buf(in, len); +<a class="c2html-lineno" name="l269" href="#l269">269 </a> <span class="c2html-macroconst">BIO</span> *d = BIO_new(BIO_f_base64()); +<a class="c2html-lineno" name="l270" href="#l270">270 </a> BIO_set_flags(d, <span class="c2html-macroconst">BIO_FLAGS_BASE64_NO_NL</span>); +<a class="c2html-lineno" name="l271" href="#l271">271 </a> b = BIO_push(d, b); +<a class="c2html-lineno" name="l272" href="#l272">272 </a> +<a class="c2html-lineno" name="l273" href="#l273">273 </a> *outlen = BIO_read(b, out, len); +<a class="c2html-lineno" name="l274" href="#l274">274 </a> BIO_free_all(b); +<a class="c2html-lineno" name="l275" href="#l275">275 </a> +<a class="c2html-lineno" name="l276" href="#l276">276 </a> <span class="c2html-keyword">return</span> out; +<a class="c2html-lineno" name="l277" href="#l277">277 </a>} +<a class="c2html-lineno" name="l278" href="#l278">278 </a> +<a class="c2html-lineno" name="l279" href="#l279">279 </a><span class="c2html-keyword">char</span>* util_base64encode(<span class="c2html-keyword">char</span> *in, <span class="c2html-type">size_t</span> len) { +<a class="c2html-lineno" name="l280" href="#l280">280 </a> <span class="c2html-macroconst">BIO</span> *b; +<a class="c2html-lineno" name="l281" href="#l281">281 </a> <span class="c2html-macroconst">BIO</span> *e; +<a class="c2html-lineno" name="l282" href="#l282">282 </a> <span class="c2html-macroconst">BUF_MEM</span> *mem; +<a class="c2html-lineno" name="l283" href="#l283">283 </a> +<a class="c2html-lineno" name="l284" href="#l284">284 </a> e = BIO_new(BIO_f_base64()); +<a class="c2html-lineno" name="l285" href="#l285">285 </a> b = BIO_new(BIO_s_mem()); +<a class="c2html-lineno" name="l286" href="#l286">286 </a> +<a class="c2html-lineno" name="l287" href="#l287">287 </a> e = BIO_push(e, b); +<a class="c2html-lineno" name="l288" href="#l288">288 </a> BIO_write(e, in, len); +<a class="c2html-lineno" name="l289" href="#l289">289 </a> BIO_flush(e); +<a class="c2html-lineno" name="l290" href="#l290">290 </a> +<a class="c2html-lineno" name="l291" href="#l291">291 </a> BIO_get_mem_ptr(e, &mem); +<a class="c2html-lineno" name="l292" href="#l292">292 </a> <span class="c2html-keyword">char</span> *out = malloc(mem->length); +<a class="c2html-lineno" name="l293" href="#l293">293 </a> memcpy(out, mem->data, mem->length -<span class="c2html-macroconst">1</span>); +<a class="c2html-lineno" name="l294" href="#l294">294 </a> out[mem->length - <span class="c2html-macroconst">1</span>] = <span class="c2html-string">'\0'</span>; +<a class="c2html-lineno" name="l295" href="#l295">295 </a> +<a class="c2html-lineno" name="l296" href="#l296">296 </a> BIO_free_all(e); +<a class="c2html-lineno" name="l297" href="#l297">297 </a> +<a class="c2html-lineno" name="l298" href="#l298">298 </a> <span class="c2html-keyword">return</span> out; +<a class="c2html-lineno" name="l299" href="#l299">299 </a>} +<a class="c2html-lineno" name="l300" href="#l300">300 </a> +<a class="c2html-lineno" name="l301" href="#l301">301 </a><span class="c2html-keyword">char</span>* util_encrypt_str(DavSession *sn, <span class="c2html-keyword">char</span> *str, <span class="c2html-keyword">char</span> *key) { +<a class="c2html-lineno" name="l302" href="#l302">302 </a> DavKey *k = dav_context_get_key(sn->context, key); +<a class="c2html-lineno" name="l303" href="#l303">303 </a> <span class="c2html-keyword">if</span>(!k) { +<a class="c2html-lineno" name="l304" href="#l304">304 </a> <span class="c2html-comment">// TODO: session error</span> +<a class="c2html-lineno" name="l305" href="#l305">305 </a> <span class="c2html-keyword">return</span> <span class="c2html-macroconst">NULL</span>; +<a class="c2html-lineno" name="l306" href="#l306">306 </a> } +<a class="c2html-lineno" name="l307" href="#l307">307 </a> +<a class="c2html-lineno" name="l308" href="#l308">308 </a> <span class="c2html-keyword">char</span> *enc_str = aes_encrypt(str, k); +<a class="c2html-lineno" name="l309" href="#l309">309 </a> <span class="c2html-keyword">char</span> *ret_str = dav_session_strdup(sn, enc_str); +<a class="c2html-lineno" name="l310" href="#l310">310 </a> free(enc_str); +<a class="c2html-lineno" name="l311" href="#l311">311 </a> <span class="c2html-keyword">return</span> ret_str; +<a class="c2html-lineno" name="l312" href="#l312">312 </a>} +<a class="c2html-lineno" name="l313" href="#l313">313 </a> +<a class="c2html-lineno" name="l314" href="#l314">314 </a><span class="c2html-comment">/* commented out for testing reasons */</span> +<a class="c2html-lineno" name="l315" href="#l315">315 </a><span class="c2html-comment">/*</span> +<a class="c2html-lineno" name="l316" href="#l316">316 </a><span class="c2html-comment">char* util_decrypt_str(DavSession *sn, char *str, char *key) {</span> +<a class="c2html-lineno" name="l317" href="#l317">317 </a><span class="c2html-comment"> DavKey *k = dav_context_get_key(sn->context, key);</span> +<a class="c2html-lineno" name="l318" href="#l318">318 </a><span class="c2html-comment"> if(!k) {</span> +<a class="c2html-lineno" name="l319" href="#l319">319 </a><span class="c2html-comment"> // TODO: session error</span> +<a class="c2html-lineno" name="l320" href="#l320">320 </a><span class="c2html-comment"> return NULL;</span> +<a class="c2html-lineno" name="l321" href="#l321">321 </a><span class="c2html-comment"> }</span> +<a class="c2html-lineno" name="l322" href="#l322">322 </a><span class="c2html-comment"> </span> +<a class="c2html-lineno" name="l323" href="#l323">323 </a><span class="c2html-comment"> char *dec_str = aes_decrypt(str, k);</span> +<a class="c2html-lineno" name="l324" href="#l324">324 </a><span class="c2html-comment"> char *ret_str = dav_session_strdup(sn, dec_str);</span> +<a class="c2html-lineno" name="l325" href="#l325">325 </a><span class="c2html-comment"> free(dec_str);</span> +<a class="c2html-lineno" name="l326" href="#l326">326 </a><span class="c2html-comment"> return ret_str;</span> +<a class="c2html-lineno" name="l327" href="#l327">327 </a><span class="c2html-comment">}</span> +<a class="c2html-lineno" name="l328" href="#l328">328 </a><span class="c2html-comment">*/</span> +<a class="c2html-lineno" name="l329" href="#l329">329 </a><span class="c2html-keyword">char</span>* util_random_str() { +<a class="c2html-lineno" name="l330" href="#l330">330 </a> <span class="c2html-keyword">unsigned</span> <span class="c2html-keyword">char</span> *str = malloc(<span class="c2html-macroconst">25</span>); +<a class="c2html-lineno" name="l331" href="#l331">331 </a> str[<span class="c2html-macroconst">24</span>] = <span class="c2html-string">'\0'</span>; +<a class="c2html-lineno" name="l332" href="#l332">332 </a> +<a class="c2html-lineno" name="l333" href="#l333">333 </a> <span class="c2html-type">sstr_t</span> t = <span class="c2html-macroconst">S</span>( +<a class="c2html-lineno" name="l334" href="#l334">334 </a> <span class="c2html-string">"01234567890"</span> +<a class="c2html-lineno" name="l335" href="#l335">335 </a> <span class="c2html-string">"abcdefghijklmnopqrstuvwxyz"</span> +<a class="c2html-lineno" name="l336" href="#l336">336 </a> <span class="c2html-string">"ABCDEFGHIJKLMNOPQRSTUVWXYZ"</span>); +<a class="c2html-lineno" name="l337" href="#l337">337 </a> <span class="c2html-keyword">const</span> <span class="c2html-keyword">unsigned</span> <span class="c2html-keyword">char</span> *table = (<span class="c2html-keyword">const</span> <span class="c2html-keyword">unsigned</span> <span class="c2html-keyword">char</span>*)t.ptr; +<a class="c2html-lineno" name="l338" href="#l338">338 </a> +<a class="c2html-lineno" name="l339" href="#l339">339 </a> RAND_pseudo_bytes(str, <span class="c2html-macroconst">24</span>); +<a class="c2html-lineno" name="l340" href="#l340">340 </a> <span class="c2html-keyword">for</span>(<span class="c2html-keyword">int</span> i=<span class="c2html-macroconst">0</span>;i<<span class="c2html-macroconst">24</span>;i++) { +<a class="c2html-lineno" name="l341" href="#l341">341 </a> <span class="c2html-keyword">int</span> c = str[i] % t.length; +<a class="c2html-lineno" name="l342" href="#l342">342 </a> str[i] = table[c]; +<a class="c2html-lineno" name="l343" href="#l343">343 </a> } +<a class="c2html-lineno" name="l344" href="#l344">344 </a> +<a class="c2html-lineno" name="l345" href="#l345">345 </a> <span class="c2html-keyword">return</span> (<span class="c2html-keyword">char</span>*)str; +<a class="c2html-lineno" name="l346" href="#l346">346 </a>} +<a class="c2html-lineno" name="l347" href="#l347">347 </a> +<a class="c2html-lineno" name="l348" href="#l348">348 </a><span class="c2html-comment">/*</span> +<a class="c2html-lineno" name="l349" href="#l349">349 </a><span class="c2html-comment"> * gets a substring from 0 to the appearance of the token</span> +<a class="c2html-lineno" name="l350" href="#l350">350 </a><span class="c2html-comment"> * tokens are separated by space</span> +<a class="c2html-lineno" name="l351" href="#l351">351 </a><span class="c2html-comment"> * sets sub to the substring and returns the remaining string</span> +<a class="c2html-lineno" name="l352" href="#l352">352 </a><span class="c2html-comment"> */</span> +<a class="c2html-lineno" name="l353" href="#l353">353 </a><span class="c2html-type">sstr_t</span> util_getsubstr_until_token(<span class="c2html-type">sstr_t</span> str, <span class="c2html-type">sstr_t</span> token, <span class="c2html-type">sstr_t</span> *sub) { +<a class="c2html-lineno" name="l354" href="#l354">354 </a> <span class="c2html-keyword">int</span> i; +<a class="c2html-lineno" name="l355" href="#l355">355 </a> <span class="c2html-keyword">int</span> token_start = -<span class="c2html-macroconst">1</span>; +<a class="c2html-lineno" name="l356" href="#l356">356 </a> <span class="c2html-keyword">int</span> token_end = -<span class="c2html-macroconst">1</span>; +<a class="c2html-lineno" name="l357" href="#l357">357 </a> <span class="c2html-keyword">for</span>(i=<span class="c2html-macroconst">0</span>;i<=str.length;i++) { +<a class="c2html-lineno" name="l358" href="#l358">358 </a> <span class="c2html-keyword">int</span> c; +<a class="c2html-lineno" name="l359" href="#l359">359 </a> <span class="c2html-keyword">if</span>(i == str.length) { +<a class="c2html-lineno" name="l360" href="#l360">360 </a> c = <span class="c2html-string">' '</span>; +<a class="c2html-lineno" name="l361" href="#l361">361 </a> } <span class="c2html-keyword">else</span> { +<a class="c2html-lineno" name="l362" href="#l362">362 </a> c = str.ptr[i]; +<a class="c2html-lineno" name="l363" href="#l363">363 </a> } +<a class="c2html-lineno" name="l364" href="#l364">364 </a> <span class="c2html-keyword">if</span>(c < <span class="c2html-macroconst">33</span>) { +<a class="c2html-lineno" name="l365" href="#l365">365 </a> <span class="c2html-keyword">if</span>(token_start != -<span class="c2html-macroconst">1</span>) { +<a class="c2html-lineno" name="l366" href="#l366">366 </a> token_end = i; +<a class="c2html-lineno" name="l367" href="#l367">367 </a> <span class="c2html-type">size_t</span> len = token_end - token_start; +<a class="c2html-lineno" name="l368" href="#l368">368 </a> <span class="c2html-type">sstr_t</span> tk = sstrsubsl(str, token_start, len); +<a class="c2html-lineno" name="l369" href="#l369">369 </a> <span class="c2html-comment">//printf("token: {%.*s}\n", token.length, token.ptr);</span> +<a class="c2html-lineno" name="l370" href="#l370">370 </a> <span class="c2html-keyword">if</span>(!sstrcmp(tk, token)) { +<a class="c2html-lineno" name="l371" href="#l371">371 </a> *sub = sstrtrim(sstrsubsl(str, <span class="c2html-macroconst">0</span>, token_start)); +<a class="c2html-lineno" name="l372" href="#l372">372 </a> <span class="c2html-keyword">break</span>; +<a class="c2html-lineno" name="l373" href="#l373">373 </a> } +<a class="c2html-lineno" name="l374" href="#l374">374 </a> token_start = -<span class="c2html-macroconst">1</span>; +<a class="c2html-lineno" name="l375" href="#l375">375 </a> token_end = -<span class="c2html-macroconst">1</span>; +<a class="c2html-lineno" name="l376" href="#l376">376 </a> } +<a class="c2html-lineno" name="l377" href="#l377">377 </a> } <span class="c2html-keyword">else</span> { +<a class="c2html-lineno" name="l378" href="#l378">378 </a> <span class="c2html-keyword">if</span>(token_start == -<span class="c2html-macroconst">1</span>) { +<a class="c2html-lineno" name="l379" href="#l379">379 </a> token_start = i; +<a class="c2html-lineno" name="l380" href="#l380">380 </a> } +<a class="c2html-lineno" name="l381" href="#l381">381 </a> } +<a class="c2html-lineno" name="l382" href="#l382">382 </a> } +<a class="c2html-lineno" name="l383" href="#l383">383 </a> +<a class="c2html-lineno" name="l384" href="#l384">384 </a> <span class="c2html-keyword">if</span>(i < str.length) { +<a class="c2html-lineno" name="l385" href="#l385">385 </a> <span class="c2html-keyword">return</span> sstrtrim(sstrsubs(str, i)); +<a class="c2html-lineno" name="l386" href="#l386">386 </a> } <span class="c2html-keyword">else</span> { +<a class="c2html-lineno" name="l387" href="#l387">387 </a> str.ptr = <span class="c2html-macroconst">NULL</span>; +<a class="c2html-lineno" name="l388" href="#l388">388 </a> str.length = <span class="c2html-macroconst">0</span>; +<a class="c2html-lineno" name="l389" href="#l389">389 </a> <span class="c2html-keyword">return</span> str; +<a class="c2html-lineno" name="l390" href="#l390">390 </a> } +<a class="c2html-lineno" name="l391" href="#l391">391 </a>} +</div> + </body> +</html> +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test/gs/javatest.html Mon Apr 24 20:54:38 2023 +0200 @@ -0,0 +1,225 @@ +<!DOCTYPE html> +<html> + <head> + <title>c2html</title> + <style type="text/css"> + div.c2html-code { + white-space: pre; + font-family: monospace; + } + a.c2html-lineno { + /* as long as user-select isn't widely spread, we throw the bomb */ + -webkit-user-select: none; + -moz-user-select: none; + -ms-user-select: none; + user-select: none; + display: inline-block; + font-style: italic; + text-decoration: none; + color: grey; + } + span.c2html-keyword { + color: blue; + } + span.c2html-macroconst { + color: cornflowerblue; + } + span.c2html-type { + color: teal; + } + span.c2html-directive { + color: silver; + } + span.c2html-string { + color: darkorange; + } + span.c2html-comment { + color: grey; + } + span.c2html-stdinclude, span.c2html-userinclude, a.c2html-userinclude { + } + </style> + </head> + <body> + +<div class="c2html-code"> +<a class="c2html-lineno" name="l1" href="#l1"> 1 </a><span class="c2html-comment">/*</span> +<a class="c2html-lineno" name="l2" href="#l2"> 2 </a><span class="c2html-comment"> * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.</span> +<a class="c2html-lineno" name="l3" href="#l3"> 3 </a><span class="c2html-comment"> *</span> +<a class="c2html-lineno" name="l4" href="#l4"> 4 </a><span class="c2html-comment"> * Copyright 2014 Mike Becker. All rights reserved.</span> +<a class="c2html-lineno" name="l5" href="#l5"> 5 </a><span class="c2html-comment"> *</span> +<a class="c2html-lineno" name="l6" href="#l6"> 6 </a><span class="c2html-comment"> * Redistribution and use in source and binary forms, with or without</span> +<a class="c2html-lineno" name="l7" href="#l7"> 7 </a><span class="c2html-comment"> * modification, are permitted provided that the following conditions are met:</span> +<a class="c2html-lineno" name="l8" href="#l8"> 8 </a><span class="c2html-comment"> *</span> +<a class="c2html-lineno" name="l9" href="#l9"> 9 </a><span class="c2html-comment"> * 1. Redistributions of source code must retain the above copyright</span> +<a class="c2html-lineno" name="l10" href="#l10"> 10 </a><span class="c2html-comment"> * notice, this list of conditions and the following disclaimer.</span> +<a class="c2html-lineno" name="l11" href="#l11"> 11 </a><span class="c2html-comment"> *</span> +<a class="c2html-lineno" name="l12" href="#l12"> 12 </a><span class="c2html-comment"> * 2. Redistributions in binary form must reproduce the above copyright</span> +<a class="c2html-lineno" name="l13" href="#l13"> 13 </a><span class="c2html-comment"> * notice, this list of conditions and the following disclaimer in the</span> +<a class="c2html-lineno" name="l14" href="#l14"> 14 </a><span class="c2html-comment"> * documentation and/or other materials provided with the distribution.</span> +<a class="c2html-lineno" name="l15" href="#l15"> 15 </a><span class="c2html-comment"> *</span> +<a class="c2html-lineno" name="l16" href="#l16"> 16 </a><span class="c2html-comment"> * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"</span> +<a class="c2html-lineno" name="l17" href="#l17"> 17 </a><span class="c2html-comment"> * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE</span> +<a class="c2html-lineno" name="l18" href="#l18"> 18 </a><span class="c2html-comment"> * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE</span> +<a class="c2html-lineno" name="l19" href="#l19"> 19 </a><span class="c2html-comment"> * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE</span> +<a class="c2html-lineno" name="l20" href="#l20"> 20 </a><span class="c2html-comment"> * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR</span> +<a class="c2html-lineno" name="l21" href="#l21"> 21 </a><span class="c2html-comment"> * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF</span> +<a class="c2html-lineno" name="l22" href="#l22"> 22 </a><span class="c2html-comment"> * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS</span> +<a class="c2html-lineno" name="l23" href="#l23"> 23 </a><span class="c2html-comment"> * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN</span> +<a class="c2html-lineno" name="l24" href="#l24"> 24 </a><span class="c2html-comment"> * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)</span> +<a class="c2html-lineno" name="l25" href="#l25"> 25 </a><span class="c2html-comment"> * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE</span> +<a class="c2html-lineno" name="l26" href="#l26"> 26 </a><span class="c2html-comment"> * POSSIBILITY OF SUCH DAMAGE.</span> +<a class="c2html-lineno" name="l27" href="#l27"> 27 </a><span class="c2html-comment"> *</span> +<a class="c2html-lineno" name="l28" href="#l28"> 28 </a><span class="c2html-comment"> */</span> +<a class="c2html-lineno" name="l29" href="#l29"> 29 </a> +<a class="c2html-lineno" name="l30" href="#l30"> 30 </a><span class="c2html-keyword">package</span> de.uapcore.sigred.doc.base; +<a class="c2html-lineno" name="l31" href="#l31"> 31 </a> +<a class="c2html-lineno" name="l32" href="#l32"> 32 </a><span class="c2html-keyword">import</span> de.uapcore.sigred.doc.<span class="c2html-type">Resources</span>; +<a class="c2html-lineno" name="l33" href="#l33"> 33 </a><span class="c2html-keyword">import</span> de.uapcore.sigrapi.impl.<span class="c2html-type">Digraph</span>; +<a class="c2html-lineno" name="l34" href="#l34"> 34 </a><span class="c2html-keyword">import</span> de.uapcore.sigrapi.impl.<span class="c2html-type">Graph</span>; +<a class="c2html-lineno" name="l35" href="#l35"> 35 </a><span class="c2html-keyword">import</span> de.uapcore.sigrapi.<span class="c2html-type">IGraph</span>; +<a class="c2html-lineno" name="l36" href="#l36"> 36 </a><span class="c2html-keyword">import</span> java.io.<span class="c2html-type">IOException</span>; +<a class="c2html-lineno" name="l37" href="#l37"> 37 </a><span class="c2html-keyword">import</span> java.io.<span class="c2html-type">InputStream</span>; +<a class="c2html-lineno" name="l38" href="#l38"> 38 </a><span class="c2html-keyword">import</span> java.io.<span class="c2html-type">OutputStream</span>; +<a class="c2html-lineno" name="l39" href="#l39"> 39 </a><span class="c2html-keyword">import</span> java.util.concurrent.atomic.<span class="c2html-type">AtomicBoolean</span>; +<a class="c2html-lineno" name="l40" href="#l40"> 40 </a><span class="c2html-keyword">import</span> java.util.concurrent.atomic.<span class="c2html-type">AtomicReference</span>; +<a class="c2html-lineno" name="l41" href="#l41"> 41 </a><span class="c2html-keyword">import</span> org.apache.xerces.impl.<span class="c2html-type">Constants</span>; +<a class="c2html-lineno" name="l42" href="#l42"> 42 </a><span class="c2html-keyword">import</span> org.dom4j.<span class="c2html-type">Document</span>; +<a class="c2html-lineno" name="l43" href="#l43"> 43 </a><span class="c2html-keyword">import</span> org.dom4j.<span class="c2html-type">DocumentException</span>; +<a class="c2html-lineno" name="l44" href="#l44"> 44 </a><span class="c2html-keyword">import</span> org.dom4j.<span class="c2html-type">DocumentHelper</span>; +<a class="c2html-lineno" name="l45" href="#l45"> 45 </a><span class="c2html-keyword">import</span> org.dom4j.<span class="c2html-type">Element</span>; +<a class="c2html-lineno" name="l46" href="#l46"> 46 </a><span class="c2html-keyword">import</span> org.dom4j.<span class="c2html-type">Namespace</span>; +<a class="c2html-lineno" name="l47" href="#l47"> 47 </a><span class="c2html-keyword">import</span> org.dom4j.<span class="c2html-type">QName</span>; +<a class="c2html-lineno" name="l48" href="#l48"> 48 </a><span class="c2html-keyword">import</span> org.dom4j.io.<span class="c2html-type">OutputFormat</span>; +<a class="c2html-lineno" name="l49" href="#l49"> 49 </a><span class="c2html-keyword">import</span> org.dom4j.io.<span class="c2html-type">SAXReader</span>; +<a class="c2html-lineno" name="l50" href="#l50"> 50 </a><span class="c2html-keyword">import</span> org.dom4j.io.<span class="c2html-type">XMLWriter</span>; +<a class="c2html-lineno" name="l51" href="#l51"> 51 </a><span class="c2html-keyword">import</span> org.xml.sax.<span class="c2html-type">ErrorHandler</span>; +<a class="c2html-lineno" name="l52" href="#l52"> 52 </a><span class="c2html-keyword">import</span> org.xml.sax.<span class="c2html-type">SAXException</span>; +<a class="c2html-lineno" name="l53" href="#l53"> 53 </a><span class="c2html-keyword">import</span> org.xml.sax.<span class="c2html-type">SAXParseException</span>; +<a class="c2html-lineno" name="l54" href="#l54"> 54 </a> +<a class="c2html-lineno" name="l55" href="#l55"> 55 </a><span class="c2html-keyword">public</span> <span class="c2html-keyword">abstract</span> <span class="c2html-keyword">class</span> <span class="c2html-type">AbstractGraphDocument</span><<span class="c2html-type">T</span> <span class="c2html-keyword">extends</span> <span class="c2html-type">IGraph</span>> +<a class="c2html-lineno" name="l56" href="#l56"> 56 </a> <span class="c2html-keyword">extends</span> <span class="c2html-type">FileBackedDocument</span> { +<a class="c2html-lineno" name="l57" href="#l57"> 57 </a> +<a class="c2html-lineno" name="l58" href="#l58"> 58 </a> <span class="c2html-keyword">protected</span> <span class="c2html-keyword">static</span> <span class="c2html-keyword">final</span> <span class="c2html-type">Namespace</span> <span class="c2html-type">NAMESPACE</span> = <span class="c2html-type">Namespace</span>.get(<span class="c2html-string">"sigred"</span>, +<a class="c2html-lineno" name="l59" href="#l59"> 59 </a> <span class="c2html-string">"http://develop.uap-core.de/sigred/"</span>); +<a class="c2html-lineno" name="l60" href="#l60"> 60 </a> +<a class="c2html-lineno" name="l61" href="#l61"> 61 </a> <span class="c2html-keyword">private</span> <span class="c2html-keyword">static</span> <span class="c2html-keyword">final</span> +<a class="c2html-lineno" name="l62" href="#l62"> 62 </a> <span class="c2html-type">QName</span> <span class="c2html-type">TAG_GRAPHDOC</span> = <span class="c2html-type">QName</span>.get(<span class="c2html-string">"graph-document"</span>, <span class="c2html-type">NAMESPACE</span>); +<a class="c2html-lineno" name="l63" href="#l63"> 63 </a> <span class="c2html-keyword">private</span> <span class="c2html-keyword">static</span> <span class="c2html-keyword">final</span> +<a class="c2html-lineno" name="l64" href="#l64"> 64 </a> <span class="c2html-type">QName</span> <span class="c2html-type">TAG_GRAPH</span> = <span class="c2html-type">QName</span>.get(<span class="c2html-string">"graph"</span>, <span class="c2html-type">NAMESPACE</span>); +<a class="c2html-lineno" name="l65" href="#l65"> 65 </a> <span class="c2html-keyword">private</span> <span class="c2html-keyword">static</span> <span class="c2html-keyword">final</span> +<a class="c2html-lineno" name="l66" href="#l66"> 66 </a> <span class="c2html-type">QName</span> <span class="c2html-type">TAG_DIGRAPH</span> = <span class="c2html-type">QName</span>.get(<span class="c2html-string">"digraph"</span>, <span class="c2html-type">NAMESPACE</span>); +<a class="c2html-lineno" name="l67" href="#l67"> 67 </a> <span class="c2html-keyword">private</span> <span class="c2html-keyword">static</span> <span class="c2html-keyword">final</span> +<a class="c2html-lineno" name="l68" href="#l68"> 68 </a> <span class="c2html-type">QName</span> <span class="c2html-type">TAG_METADATA</span> = <span class="c2html-type">QName</span>.get(<span class="c2html-string">"metadata"</span>, <span class="c2html-type">NAMESPACE</span>); +<a class="c2html-lineno" name="l69" href="#l69"> 69 </a> +<a class="c2html-lineno" name="l70" href="#l70"> 70 </a> <span class="c2html-keyword">protected</span> <span class="c2html-keyword">final</span> <span class="c2html-type">T</span> graph; +<a class="c2html-lineno" name="l71" href="#l71"> 71 </a> +<a class="c2html-lineno" name="l72" href="#l72"> 72 </a> <span class="c2html-keyword">private</span> <span class="c2html-keyword">final</span> <span class="c2html-type">GraphDocumentMetadata</span> metadata; +<a class="c2html-lineno" name="l73" href="#l73"> 73 </a> +<a class="c2html-lineno" name="l74" href="#l74"> 74 </a> <span class="c2html-keyword">public</span> <span class="c2html-type">AbstractGraphDocument</span>(<span class="c2html-type">Class</span><<span class="c2html-type">T</span>> graphType) { +<a class="c2html-lineno" name="l75" href="#l75"> 75 </a> <span class="c2html-type">T</span> g; +<a class="c2html-lineno" name="l76" href="#l76"> 76 </a> <span class="c2html-keyword">try</span> { +<a class="c2html-lineno" name="l77" href="#l77"> 77 </a> g = graphType.newInstance(); +<a class="c2html-lineno" name="l78" href="#l78"> 78 </a> } <span class="c2html-keyword">catch</span> (<span class="c2html-type">ReflectiveOperationException</span> e) { +<a class="c2html-lineno" name="l79" href="#l79"> 79 </a> <span class="c2html-keyword">assert</span> false; +<a class="c2html-lineno" name="l80" href="#l80"> 80 </a> g = null; <span class="c2html-comment">// for the compiler</span> +<a class="c2html-lineno" name="l81" href="#l81"> 81 </a> } +<a class="c2html-lineno" name="l82" href="#l82"> 82 </a> graph = g; +<a class="c2html-lineno" name="l83" href="#l83"> 83 </a> metadata = <span class="c2html-keyword">new</span> <span class="c2html-type">GraphDocumentMetadata</span>(); +<a class="c2html-lineno" name="l84" href="#l84"> 84 </a> } +<a class="c2html-lineno" name="l85" href="#l85"> 85 </a> +<a class="c2html-lineno" name="l86" href="#l86"> 86 </a> <span class="c2html-keyword">public</span> <span class="c2html-type">T</span> getGraph() { +<a class="c2html-lineno" name="l87" href="#l87"> 87 </a> <span class="c2html-keyword">return</span> graph; +<a class="c2html-lineno" name="l88" href="#l88"> 88 </a> } +<a class="c2html-lineno" name="l89" href="#l89"> 89 </a> +<a class="c2html-lineno" name="l90" href="#l90"> 90 </a> <span class="c2html-keyword">public</span> <span class="c2html-type">GraphDocumentMetadata</span> getMetadata() { +<a class="c2html-lineno" name="l91" href="#l91"> 91 </a> <span class="c2html-keyword">return</span> metadata; +<a class="c2html-lineno" name="l92" href="#l92"> 92 </a> } +<a class="c2html-lineno" name="l93" href="#l93"> 93 </a> +<a class="c2html-lineno" name="l94" href="#l94"> 94 </a> <span class="c2html-keyword">protected</span> <span class="c2html-keyword">abstract</span> <span class="c2html-keyword">void</span> writeGraph(<span class="c2html-type">Element</span> rootNode) <span class="c2html-keyword">throws</span> <span class="c2html-type">IOException</span>; +<a class="c2html-lineno" name="l95" href="#l95"> 95 </a> <span class="c2html-keyword">protected</span> <span class="c2html-keyword">abstract</span> <span class="c2html-keyword">void</span> readGraph(<span class="c2html-type">Element</span> rootNode) <span class="c2html-keyword">throws</span> <span class="c2html-type">IOException</span>; +<a class="c2html-lineno" name="l96" href="#l96"> 96 </a> +<a class="c2html-lineno" name="l97" href="#l97"> 97 </a> <span class="c2html-directive">@Override</span> +<a class="c2html-lineno" name="l98" href="#l98"> 98 </a> <span class="c2html-keyword">public</span> <span class="c2html-keyword">void</span> writeTo(<span class="c2html-type">OutputStream</span> out) <span class="c2html-keyword">throws</span> <span class="c2html-type">IOException</span> { +<a class="c2html-lineno" name="l99" href="#l99"> 99 </a> <span class="c2html-type">Document</span> doc = <span class="c2html-type">DocumentHelper</span>.createDocument(); +<a class="c2html-lineno" name="l100" href="#l100">100 </a> +<a class="c2html-lineno" name="l101" href="#l101">101 </a> <span class="c2html-type">Element</span> rootNode = doc.addElement(<span class="c2html-type">TAG_GRAPHDOC</span>); +<a class="c2html-lineno" name="l102" href="#l102">102 </a> +<a class="c2html-lineno" name="l103" href="#l103">103 </a> <span class="c2html-type">Element</span> metadataNode = rootNode.addElement(<span class="c2html-type">TAG_METADATA</span>); +<a class="c2html-lineno" name="l104" href="#l104">104 </a> +<a class="c2html-lineno" name="l105" href="#l105">105 </a> metadata.write(metadataNode); +<a class="c2html-lineno" name="l106" href="#l106">106 </a> +<a class="c2html-lineno" name="l107" href="#l107">107 </a> <span class="c2html-keyword">if</span> (graph <span class="c2html-keyword">instanceof</span> <span class="c2html-type">Graph</span>) { +<a class="c2html-lineno" name="l108" href="#l108">108 </a> writeGraph(rootNode.addElement(<span class="c2html-type">TAG_GRAPH</span>)); +<a class="c2html-lineno" name="l109" href="#l109">109 </a> } <span class="c2html-keyword">else</span> <span class="c2html-keyword">if</span> (graph <span class="c2html-keyword">instanceof</span> <span class="c2html-type">Digraph</span>) { +<a class="c2html-lineno" name="l110" href="#l110">110 </a> writeGraph(rootNode.addElement(<span class="c2html-type">TAG_DIGRAPH</span>)); +<a class="c2html-lineno" name="l111" href="#l111">111 </a> } <span class="c2html-keyword">else</span> { +<a class="c2html-lineno" name="l112" href="#l112">112 </a> <span class="c2html-keyword">throw</span> <span class="c2html-keyword">new</span> <span class="c2html-type">IOException</span>(<span class="c2html-string">"unsupported graph type"</span>); +<a class="c2html-lineno" name="l113" href="#l113">113 </a> } +<a class="c2html-lineno" name="l114" href="#l114">114 </a> +<a class="c2html-lineno" name="l115" href="#l115">115 </a> <span class="c2html-type">XMLWriter</span> writer = <span class="c2html-keyword">new</span> <span class="c2html-type">XMLWriter</span>(out, <span class="c2html-type">OutputFormat</span>.createPrettyPrint()); +<a class="c2html-lineno" name="l116" href="#l116">116 </a> writer.write(doc); +<a class="c2html-lineno" name="l117" href="#l117">117 </a> writer.flush(); +<a class="c2html-lineno" name="l118" href="#l118">118 </a> } +<a class="c2html-lineno" name="l119" href="#l119">119 </a> +<a class="c2html-lineno" name="l120" href="#l120">120 </a> <span class="c2html-directive">@Override</span> +<a class="c2html-lineno" name="l121" href="#l121">121 </a> <span class="c2html-keyword">public</span> <span class="c2html-keyword">void</span> readFrom(<span class="c2html-type">InputStream</span> in) <span class="c2html-keyword">throws</span> <span class="c2html-type">IOException</span> { +<a class="c2html-lineno" name="l122" href="#l122">122 </a> <span class="c2html-keyword">try</span> { +<a class="c2html-lineno" name="l123" href="#l123">123 </a> <span class="c2html-type">SAXReader</span> reader = <span class="c2html-keyword">new</span> <span class="c2html-type">SAXReader</span>(true); +<a class="c2html-lineno" name="l124" href="#l124">124 </a> reader.setStripWhitespaceText(true); +<a class="c2html-lineno" name="l125" href="#l125">125 </a> +<a class="c2html-lineno" name="l126" href="#l126">126 </a> reader.setFeature(<span class="c2html-type">Constants</span>.<span class="c2html-type">XERCES_FEATURE_PREFIX</span>+ +<a class="c2html-lineno" name="l127" href="#l127">127 </a> <span class="c2html-type">Constants</span>.<span class="c2html-type">SCHEMA_VALIDATION_FEATURE</span>, true); +<a class="c2html-lineno" name="l128" href="#l128">128 </a> reader.setProperty(<span class="c2html-type">Constants</span>.<span class="c2html-type">XERCES_PROPERTY_PREFIX</span> + +<a class="c2html-lineno" name="l129" href="#l129">129 </a> <span class="c2html-type">Constants</span>.<span class="c2html-type">SCHEMA_LOCATION</span>, <span class="c2html-type">String</span>.format(<span class="c2html-string">"%s %s"</span>, +<a class="c2html-lineno" name="l130" href="#l130">130 </a> <span class="c2html-type">NAMESPACE</span>.getURI(), <span class="c2html-type">Resources</span>.<span class="c2html-keyword">class</span>.getResource( +<a class="c2html-lineno" name="l131" href="#l131">131 </a> <span class="c2html-string">"graph-document.xsd"</span>).toExternalForm())); +<a class="c2html-lineno" name="l132" href="#l132">132 </a> +<a class="c2html-lineno" name="l133" href="#l133">133 </a> <span class="c2html-keyword">final</span> <span class="c2html-type">AtomicBoolean</span> passed = <span class="c2html-keyword">new</span> <span class="c2html-type">AtomicBoolean</span>(true); +<a class="c2html-lineno" name="l134" href="#l134">134 </a> <span class="c2html-keyword">final</span> <span class="c2html-type">AtomicReference</span><<span class="c2html-type">SAXParseException</span>> xmlerror = <span class="c2html-keyword">new</span> <span class="c2html-type">AtomicReference</span><>(); +<a class="c2html-lineno" name="l135" href="#l135">135 </a> <span class="c2html-comment">// TODO: we should do more detailed error handling here</span> +<a class="c2html-lineno" name="l136" href="#l136">136 </a> reader.setErrorHandler(<span class="c2html-keyword">new</span> <span class="c2html-type">ErrorHandler</span>() { +<a class="c2html-lineno" name="l137" href="#l137">137 </a> <span class="c2html-directive">@Override</span> +<a class="c2html-lineno" name="l138" href="#l138">138 </a> <span class="c2html-keyword">public</span> <span class="c2html-keyword">void</span> warning(<span class="c2html-type">SAXParseException</span> exception) <span class="c2html-keyword">throws</span> <span class="c2html-type">SAXException</span> { +<a class="c2html-lineno" name="l139" href="#l139">139 </a> } +<a class="c2html-lineno" name="l140" href="#l140">140 </a> +<a class="c2html-lineno" name="l141" href="#l141">141 </a> <span class="c2html-directive">@Override</span> +<a class="c2html-lineno" name="l142" href="#l142">142 </a> <span class="c2html-keyword">public</span> <span class="c2html-keyword">void</span> error(<span class="c2html-type">SAXParseException</span> exception) <span class="c2html-keyword">throws</span> <span class="c2html-type">SAXException</span> { +<a class="c2html-lineno" name="l143" href="#l143">143 </a> xmlerror.set(exception); +<a class="c2html-lineno" name="l144" href="#l144">144 </a> passed.set(false); +<a class="c2html-lineno" name="l145" href="#l145">145 </a> } +<a class="c2html-lineno" name="l146" href="#l146">146 </a> +<a class="c2html-lineno" name="l147" href="#l147">147 </a> <span class="c2html-directive">@Override</span> +<a class="c2html-lineno" name="l148" href="#l148">148 </a> <span class="c2html-keyword">public</span> <span class="c2html-keyword">void</span> fatalError(<span class="c2html-type">SAXParseException</span> exception) <span class="c2html-keyword">throws</span> <span class="c2html-type">SAXException</span> { +<a class="c2html-lineno" name="l149" href="#l149">149 </a> xmlerror.set(exception); +<a class="c2html-lineno" name="l150" href="#l150">150 </a> passed.set(false); +<a class="c2html-lineno" name="l151" href="#l151">151 </a> } +<a class="c2html-lineno" name="l152" href="#l152">152 </a> +<a class="c2html-lineno" name="l153" href="#l153">153 </a> }); +<a class="c2html-lineno" name="l154" href="#l154">154 </a> <span class="c2html-type">Document</span> doc = reader.read(in); +<a class="c2html-lineno" name="l155" href="#l155">155 </a> <span class="c2html-keyword">if</span> (!passed.get()) { +<a class="c2html-lineno" name="l156" href="#l156">156 </a> <span class="c2html-comment">// TODO: provide details (maybe via separate error object?)</span> +<a class="c2html-lineno" name="l157" href="#l157">157 </a> <span class="c2html-keyword">throw</span> xmlerror.get(); +<a class="c2html-lineno" name="l158" href="#l158">158 </a> } +<a class="c2html-lineno" name="l159" href="#l159">159 </a> +<a class="c2html-lineno" name="l160" href="#l160">160 </a> doc.normalize(); +<a class="c2html-lineno" name="l161" href="#l161">161 </a> +<a class="c2html-lineno" name="l162" href="#l162">162 </a> <span class="c2html-type">Element</span> root = doc.getRootElement(); +<a class="c2html-lineno" name="l163" href="#l163">163 </a> metadata.read(root.element(<span class="c2html-type">TAG_METADATA</span>)); +<a class="c2html-lineno" name="l164" href="#l164">164 </a> +<a class="c2html-lineno" name="l165" href="#l165">165 </a> <span class="c2html-keyword">if</span> (graph <span class="c2html-keyword">instanceof</span> <span class="c2html-type">Graph</span>) { +<a class="c2html-lineno" name="l166" href="#l166">166 </a> readGraph(root.element(<span class="c2html-type">TAG_GRAPH</span>)); +<a class="c2html-lineno" name="l167" href="#l167">167 </a> } <span class="c2html-keyword">else</span> <span class="c2html-keyword">if</span> (graph <span class="c2html-keyword">instanceof</span> <span class="c2html-type">Digraph</span>) { +<a class="c2html-lineno" name="l168" href="#l168">168 </a> readGraph(root.element(<span class="c2html-type">TAG_DIGRAPH</span>)); +<a class="c2html-lineno" name="l169" href="#l169">169 </a> } <span class="c2html-keyword">else</span> { +<a class="c2html-lineno" name="l170" href="#l170">170 </a> <span class="c2html-keyword">throw</span> <span class="c2html-keyword">new</span> <span class="c2html-type">IOException</span>(<span class="c2html-string">"unsupported graph type"</span>); +<a class="c2html-lineno" name="l171" href="#l171">171 </a> } +<a class="c2html-lineno" name="l172" href="#l172">172 </a> } <span class="c2html-keyword">catch</span> (<span class="c2html-type">DocumentException</span> | <span class="c2html-type">SAXException</span> ex) { +<a class="c2html-lineno" name="l173" href="#l173">173 </a> <span class="c2html-keyword">throw</span> <span class="c2html-keyword">new</span> <span class="c2html-type">IOException</span>(ex); +<a class="c2html-lineno" name="l174" href="#l174">174 </a> } +<a class="c2html-lineno" name="l175" href="#l175">175 </a> } +<a class="c2html-lineno" name="l176" href="#l176">176 </a>} +</div> + </body> +</html> +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test/gs/plain.html Mon Apr 24 20:54:38 2023 +0200 @@ -0,0 +1,63 @@ +<!DOCTYPE html> +<html> + <head> + <title>c2html</title> + <style type="text/css"> + div.c2html-code { + white-space: pre; + font-family: monospace; + } + a.c2html-lineno { + /* as long as user-select isn't widely spread, we throw the bomb */ + -webkit-user-select: none; + -moz-user-select: none; + -ms-user-select: none; + user-select: none; + display: inline-block; + font-style: italic; + text-decoration: none; + color: grey; + } + span.c2html-keyword { + color: blue; + } + span.c2html-macroconst { + color: cornflowerblue; + } + span.c2html-type { + color: cornflowerblue; + } + span.c2html-directive { + color: green; + } + span.c2html-string { + color: darkorange; + } + span.c2html-comment { + color: grey; + } + span.c2html-stdinclude { + color: darkorange; + } + span.c2html-userinclude { + color: darkorange; + } + a.c2html-userinclude { + color: darkorange; + text-decoration: underline; + } + </style> + </head> + <body> + +<div class="c2html-code"> +<a class="c2html-lineno" name="l1" href="#l1">1 </a></body> +<a class="c2html-lineno" name="l2" href="#l2">2 </a></html> +<a class="c2html-lineno" name="l3" href="#l3">3 </a><!c +<a class="c2html-lineno" name="l4" href="#l4">4 </a>pblock_free(q); +<a class="c2html-lineno" name="l5" href="#l5">5 </a>!> +<a class="c2html-lineno" name="l6" href="#l6">6 </a> +</div> + </body> +</html> +
--- a/test/header.html Thu Nov 10 18:44:48 2016 +0100 +++ b/test/header.html Mon Apr 24 20:54:38 2023 +0200 @@ -3,6 +3,10 @@ <head> <title>c2html</title> <style type="text/css"> + div.c2html-code { + white-space: pre; + font-family: monospace; + } a.c2html-lineno { /* as long as user-select isn't widely spread, we throw the bomb */ -webkit-user-select: none;
--- a/test/jheader.html Thu Nov 10 18:44:48 2016 +0100 +++ b/test/jheader.html Mon Apr 24 20:54:38 2023 +0200 @@ -3,6 +3,10 @@ <head> <title>c2html</title> <style type="text/css"> + div.c2html-code { + white-space: pre; + font-family: monospace; + } a.c2html-lineno { /* as long as user-select isn't widely spread, we throw the bomb */ -webkit-user-select: none;
--- a/test/v2-regression/bigtest.html Thu Nov 10 18:44:48 2016 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,855 +0,0 @@ -<!DOCTYPE html> -<html> - <head> - <title>c2html</title> - <style type="text/css"> - span.c2html-lineno a { - font-style: italic; - text-decoration: none; - color: grey; - } - span.c2html-keyword { - color: blue; - } - span.c2html-macroconst { - color: cornflowerblue; - } - span.c2html-type { - color: cornflowerblue; - } - span.c2html-directive { - color: green; - } - span.c2html-string { - color: darkorange; - } - span.c2html-comment { - color: grey; - } - span.c2html-stdinclude { - color: darkorange; - } - span.c2html-userinclude { - color: darkorange; - } - a.c2html-userinclude { - color: darkorange; - text-decoration: underline; - } - </style> - </head> - <body> - -<pre> -<span class="c2html-lineno"><a name="l1" href="#l1"> 1 </a></span> <span class="c2html-comment">/*</span> -<span class="c2html-lineno"><a name="l2" href="#l2"> 2 </a></span> <span class="c2html-comment"> * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.</span> -<span class="c2html-lineno"><a name="l3" href="#l3"> 3 </a></span> <span class="c2html-comment"> *</span> -<span class="c2html-lineno"><a name="l4" href="#l4"> 4 </a></span> <span class="c2html-comment"> * Copyright 2014 Mike Becker. All rights reserved.</span> -<span class="c2html-lineno"><a name="l5" href="#l5"> 5 </a></span> <span class="c2html-comment"> *</span> -<span class="c2html-lineno"><a name="l6" href="#l6"> 6 </a></span> <span class="c2html-comment"> * Redistribution and use in source and binary forms, with or without</span> -<span class="c2html-lineno"><a name="l7" href="#l7"> 7 </a></span> <span class="c2html-comment"> * modification, are permitted provided that the following conditions are met:</span> -<span class="c2html-lineno"><a name="l8" href="#l8"> 8 </a></span> <span class="c2html-comment"> *</span> -<span class="c2html-lineno"><a name="l9" href="#l9"> 9 </a></span> <span class="c2html-comment"> * 1. Redistributions of source code must retain the above copyright</span> -<span class="c2html-lineno"><a name="l10" href="#l10"> 10 </a></span> <span class="c2html-comment"> * notice, this list of conditions and the following disclaimer.</span> -<span class="c2html-lineno"><a name="l11" href="#l11"> 11 </a></span> <span class="c2html-comment"> *</span> -<span class="c2html-lineno"><a name="l12" href="#l12"> 12 </a></span> <span class="c2html-comment"> * 2. Redistributions in binary form must reproduce the above copyright</span> -<span class="c2html-lineno"><a name="l13" href="#l13"> 13 </a></span> <span class="c2html-comment"> * notice, this list of conditions and the following disclaimer in the</span> -<span class="c2html-lineno"><a name="l14" href="#l14"> 14 </a></span> <span class="c2html-comment"> * documentation and/or other materials provided with the distribution.</span> -<span class="c2html-lineno"><a name="l15" href="#l15"> 15 </a></span> <span class="c2html-comment"> *</span> -<span class="c2html-lineno"><a name="l16" href="#l16"> 16 </a></span> <span class="c2html-comment"> * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"</span> -<span class="c2html-lineno"><a name="l17" href="#l17"> 17 </a></span> <span class="c2html-comment"> * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE</span> -<span class="c2html-lineno"><a name="l18" href="#l18"> 18 </a></span> <span class="c2html-comment"> * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE</span> -<span class="c2html-lineno"><a name="l19" href="#l19"> 19 </a></span> <span class="c2html-comment"> * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE</span> -<span class="c2html-lineno"><a name="l20" href="#l20"> 20 </a></span> <span class="c2html-comment"> * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR</span> -<span class="c2html-lineno"><a name="l21" href="#l21"> 21 </a></span> <span class="c2html-comment"> * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF</span> -<span class="c2html-lineno"><a name="l22" href="#l22"> 22 </a></span> <span class="c2html-comment"> * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS</span> -<span class="c2html-lineno"><a name="l23" href="#l23"> 23 </a></span> <span class="c2html-comment"> * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN</span> -<span class="c2html-lineno"><a name="l24" href="#l24"> 24 </a></span> <span class="c2html-comment"> * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)</span> -<span class="c2html-lineno"><a name="l25" href="#l25"> 25 </a></span> <span class="c2html-comment"> * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE</span> -<span class="c2html-lineno"><a name="l26" href="#l26"> 26 </a></span> <span class="c2html-comment"> * POSSIBILITY OF SUCH DAMAGE.</span> -<span class="c2html-lineno"><a name="l27" href="#l27"> 27 </a></span> <span class="c2html-comment"> *</span> -<span class="c2html-lineno"><a name="l28" href="#l28"> 28 </a></span> <span class="c2html-comment"> */</span> -<span class="c2html-lineno"><a name="l29" href="#l29"> 29 </a></span> -<span class="c2html-lineno"><a name="l30" href="#l30"> 30 </a></span> <span class="c2html-directive">#include</span> <a class="c2html-userinclude" href="rules.h">"rules.h"</a> -<span class="c2html-lineno"><a name="l31" href="#l31"> 31 </a></span> <span class="c2html-directive">#include</span> <a class="c2html-userinclude" href="chess.h">"chess.h"</a> -<span class="c2html-lineno"><a name="l32" href="#l32"> 32 </a></span> <span class="c2html-directive">#include</span> <span class="c2html-stdinclude"><string.h></span> -<span class="c2html-lineno"><a name="l33" href="#l33"> 33 </a></span> <span class="c2html-directive">#include</span> <span class="c2html-stdinclude"><stdlib.h></span> -<span class="c2html-lineno"><a name="l34" href="#l34"> 34 </a></span> <span class="c2html-directive">#include</span> <span class="c2html-stdinclude"><sys/time.h></span> -<span class="c2html-lineno"><a name="l35" href="#l35"> 35 </a></span> -<span class="c2html-lineno"><a name="l36" href="#l36"> 36 </a></span> <span class="c2html-keyword">static</span> GameState gamestate_copy_sim(GameState *gamestate) { -<span class="c2html-lineno"><a name="l37" href="#l37"> 37 </a></span> GameState simulation = *gamestate; -<span class="c2html-lineno"><a name="l38" href="#l38"> 38 </a></span> <span class="c2html-keyword">if</span> (simulation.lastmove) { -<span class="c2html-lineno"><a name="l39" href="#l39"> 39 </a></span> MoveList *lastmovecopy = malloc(<span class="c2html-keyword">sizeof</span>(MoveList)); -<span class="c2html-lineno"><a name="l40" href="#l40"> 40 </a></span> *lastmovecopy = *(simulation.lastmove); -<span class="c2html-lineno"><a name="l41" href="#l41"> 41 </a></span> simulation.movelist = simulation.lastmove = lastmovecopy; -<span class="c2html-lineno"><a name="l42" href="#l42"> 42 </a></span> } -<span class="c2html-lineno"><a name="l43" href="#l43"> 43 </a></span> -<span class="c2html-lineno"><a name="l44" href="#l44"> 44 </a></span> <span class="c2html-keyword">return</span> simulation; -<span class="c2html-lineno"><a name="l45" href="#l45"> 45 </a></span> } -<span class="c2html-lineno"><a name="l46" href="#l46"> 46 </a></span> -<span class="c2html-lineno"><a name="l47" href="#l47"> 47 </a></span> <span class="c2html-keyword">void</span> gamestate_init(GameState *gamestate) { -<span class="c2html-lineno"><a name="l48" href="#l48"> 48 </a></span> memset(gamestate, <span class="c2html-macroconst">0</span>, <span class="c2html-keyword">sizeof</span>(GameState)); -<span class="c2html-lineno"><a name="l49" href="#l49"> 49 </a></span> -<span class="c2html-lineno"><a name="l50" href="#l50"> 50 </a></span> Board initboard = { -<span class="c2html-lineno"><a name="l51" href="#l51"> 51 </a></span> {<span class="c2html-macroconst">WROOK</span>, <span class="c2html-macroconst">WKNIGHT</span>, <span class="c2html-macroconst">WBISHOP</span>, <span class="c2html-macroconst">WQUEEN</span>, <span class="c2html-macroconst">WKING</span>, <span class="c2html-macroconst">WBISHOP</span>, <span class="c2html-macroconst">WKNIGHT</span>, <span class="c2html-macroconst">WROOK</span>}, -<span class="c2html-lineno"><a name="l52" href="#l52"> 52 </a></span> {<span class="c2html-macroconst">WPAWN</span>, <span class="c2html-macroconst">WPAWN</span>, <span class="c2html-macroconst">WPAWN</span>, <span class="c2html-macroconst">WPAWN</span>, <span class="c2html-macroconst">WPAWN</span>, <span class="c2html-macroconst">WPAWN</span>, <span class="c2html-macroconst">WPAWN</span>, <span class="c2html-macroconst">WPAWN</span>}, -<span class="c2html-lineno"><a name="l53" href="#l53"> 53 </a></span> {<span class="c2html-macroconst">0</span>, <span class="c2html-macroconst">0</span>, <span class="c2html-macroconst">0</span>, <span class="c2html-macroconst">0</span>, <span class="c2html-macroconst">0</span>, <span class="c2html-macroconst">0</span>, <span class="c2html-macroconst">0</span>, <span class="c2html-macroconst">0</span>}, -<span class="c2html-lineno"><a name="l54" href="#l54"> 54 </a></span> {<span class="c2html-macroconst">0</span>, <span class="c2html-macroconst">0</span>, <span class="c2html-macroconst">0</span>, <span class="c2html-macroconst">0</span>, <span class="c2html-macroconst">0</span>, <span class="c2html-macroconst">0</span>, <span class="c2html-macroconst">0</span>, <span class="c2html-macroconst">0</span>}, -<span class="c2html-lineno"><a name="l55" href="#l55"> 55 </a></span> {<span class="c2html-macroconst">0</span>, <span class="c2html-macroconst">0</span>, <span class="c2html-macroconst">0</span>, <span class="c2html-macroconst">0</span>, <span class="c2html-macroconst">0</span>, <span class="c2html-macroconst">0</span>, <span class="c2html-macroconst">0</span>, <span class="c2html-macroconst">0</span>}, -<span class="c2html-lineno"><a name="l56" href="#l56"> 56 </a></span> {<span class="c2html-macroconst">0</span>, <span class="c2html-macroconst">0</span>, <span class="c2html-macroconst">0</span>, <span class="c2html-macroconst">0</span>, <span class="c2html-macroconst">0</span>, <span class="c2html-macroconst">0</span>, <span class="c2html-macroconst">0</span>, <span class="c2html-macroconst">0</span>}, -<span class="c2html-lineno"><a name="l57" href="#l57"> 57 </a></span> {<span class="c2html-macroconst">BPAWN</span>, <span class="c2html-macroconst">BPAWN</span>, <span class="c2html-macroconst">BPAWN</span>, <span class="c2html-macroconst">BPAWN</span>, <span class="c2html-macroconst">BPAWN</span>, <span class="c2html-macroconst">BPAWN</span>, <span class="c2html-macroconst">BPAWN</span>, <span class="c2html-macroconst">BPAWN</span>}, -<span class="c2html-lineno"><a name="l58" href="#l58"> 58 </a></span> {<span class="c2html-macroconst">BROOK</span>, <span class="c2html-macroconst">BKNIGHT</span>, <span class="c2html-macroconst">BBISHOP</span>, <span class="c2html-macroconst">BQUEEN</span>, <span class="c2html-macroconst">BKING</span>, <span class="c2html-macroconst">BBISHOP</span>, <span class="c2html-macroconst">BKNIGHT</span>, <span class="c2html-macroconst">BROOK</span>} -<span class="c2html-lineno"><a name="l59" href="#l59"> 59 </a></span> }; -<span class="c2html-lineno"><a name="l60" href="#l60"> 60 </a></span> memcpy(gamestate->board, initboard, <span class="c2html-keyword">sizeof</span>(Board)); -<span class="c2html-lineno"><a name="l61" href="#l61"> 61 </a></span> } -<span class="c2html-lineno"><a name="l62" href="#l62"> 62 </a></span> -<span class="c2html-lineno"><a name="l63" href="#l63"> 63 </a></span> <span class="c2html-keyword">void</span> gamestate_cleanup(GameState *gamestate) { -<span class="c2html-lineno"><a name="l64" href="#l64"> 64 </a></span> MoveList *elem; -<span class="c2html-lineno"><a name="l65" href="#l65"> 65 </a></span> elem = gamestate->movelist; -<span class="c2html-lineno"><a name="l66" href="#l66"> 66 </a></span> <span class="c2html-keyword">while</span> (elem) { -<span class="c2html-lineno"><a name="l67" href="#l67"> 67 </a></span> MoveList *cur = elem; -<span class="c2html-lineno"><a name="l68" href="#l68"> 68 </a></span> elem = elem->next; -<span class="c2html-lineno"><a name="l69" href="#l69"> 69 </a></span> free(cur); -<span class="c2html-lineno"><a name="l70" href="#l70"> 70 </a></span> }; -<span class="c2html-lineno"><a name="l71" href="#l71"> 71 </a></span> } -<span class="c2html-lineno"><a name="l72" href="#l72"> 72 </a></span> -<span class="c2html-lineno"><a name="l73" href="#l73"> 73 </a></span> <span class="c2html-comment">/* MUST be called IMMEDIATLY after applying a move to work correctly */</span> -<span class="c2html-lineno"><a name="l74" href="#l74"> 74 </a></span> <span class="c2html-keyword">static</span> <span class="c2html-keyword">void</span> format_move(GameState *gamestate, Move *move) { -<span class="c2html-lineno"><a name="l75" href="#l75"> 75 </a></span> <span class="c2html-keyword">char</span> *string = move->string; -<span class="c2html-lineno"><a name="l76" href="#l76"> 76 </a></span> -<span class="c2html-lineno"><a name="l77" href="#l77"> 77 </a></span> <span class="c2html-comment">/* at least 8 characters should be available, wipe them out */</span> -<span class="c2html-lineno"><a name="l78" href="#l78"> 78 </a></span> memset(string, <span class="c2html-macroconst">0</span>, <span class="c2html-macroconst">8</span>); -<span class="c2html-lineno"><a name="l79" href="#l79"> 79 </a></span> -<span class="c2html-lineno"><a name="l80" href="#l80"> 80 </a></span> <span class="c2html-comment">/* special formats for castling */</span> -<span class="c2html-lineno"><a name="l81" href="#l81"> 81 </a></span> <span class="c2html-keyword">if</span> ((move->piece&<span class="c2html-macroconst">PIECE_MASK</span>) == <span class="c2html-macroconst">KING</span> && -<span class="c2html-lineno"><a name="l82" href="#l82"> 82 </a></span> abs(move->tofile-move->fromfile) == <span class="c2html-macroconst">2</span>) { -<span class="c2html-lineno"><a name="l83" href="#l83"> 83 </a></span> <span class="c2html-keyword">if</span> (move->tofile==fileidx(<span class="c2html-string">'c'</span>)) { -<span class="c2html-lineno"><a name="l84" href="#l84"> 84 </a></span> memcpy(string, <span class="c2html-string">"O-O-O"</span>, <span class="c2html-macroconst">5</span>); -<span class="c2html-lineno"><a name="l85" href="#l85"> 85 </a></span> } <span class="c2html-keyword">else</span> { -<span class="c2html-lineno"><a name="l86" href="#l86"> 86 </a></span> memcpy(string, <span class="c2html-string">"O-O"</span>, <span class="c2html-macroconst">3</span>); -<span class="c2html-lineno"><a name="l87" href="#l87"> 87 </a></span> } -<span class="c2html-lineno"><a name="l88" href="#l88"> 88 </a></span> } -<span class="c2html-lineno"><a name="l89" href="#l89"> 89 </a></span> -<span class="c2html-lineno"><a name="l90" href="#l90"> 90 </a></span> <span class="c2html-comment">/* start by notating the piece character */</span> -<span class="c2html-lineno"><a name="l91" href="#l91"> 91 </a></span> string[<span class="c2html-macroconst">0</span>] = getpiecechr(move->piece); -<span class="c2html-lineno"><a name="l92" href="#l92"> 92 </a></span> <span class="c2html-keyword">int</span> idx = string[<span class="c2html-macroconst">0</span>] ? <span class="c2html-macroconst">1</span> : <span class="c2html-macroconst">0</span>; -<span class="c2html-lineno"><a name="l93" href="#l93"> 93 </a></span> -<span class="c2html-lineno"><a name="l94" href="#l94"> 94 </a></span> <span class="c2html-comment">/* find out how many source information we do need */</span> -<span class="c2html-lineno"><a name="l95" href="#l95"> 95 </a></span> <span class="c2html-type">uint8_t</span> piece = move->piece & <span class="c2html-macroconst">PIECE_MASK</span>; -<span class="c2html-lineno"><a name="l96" href="#l96"> 96 </a></span> <span class="c2html-keyword">if</span> (piece == <span class="c2html-macroconst">PAWN</span>) { -<span class="c2html-lineno"><a name="l97" href="#l97"> 97 </a></span> <span class="c2html-keyword">if</span> (move->capture) { -<span class="c2html-lineno"><a name="l98" href="#l98"> 98 </a></span> string[idx++] = filechr(move->fromfile); -<span class="c2html-lineno"><a name="l99" href="#l99"> 99 </a></span> } -<span class="c2html-lineno"><a name="l100" href="#l100">100 </a></span> } <span class="c2html-keyword">else</span> <span class="c2html-keyword">if</span> (piece != <span class="c2html-macroconst">KING</span>) { -<span class="c2html-lineno"><a name="l101" href="#l101">101 </a></span> Move threats[<span class="c2html-macroconst">16</span>]; -<span class="c2html-lineno"><a name="l102" href="#l102">102 </a></span> <span class="c2html-type">uint8_t</span> threatcount; -<span class="c2html-lineno"><a name="l103" href="#l103">103 </a></span> get_real_threats(gamestate, move->torow, move->tofile, -<span class="c2html-lineno"><a name="l104" href="#l104">104 </a></span> move->piece&<span class="c2html-macroconst">COLOR_MASK</span>, threats, &threatcount); -<span class="c2html-lineno"><a name="l105" href="#l105">105 </a></span> <span class="c2html-keyword">if</span> (threatcount > <span class="c2html-macroconst">1</span>) { -<span class="c2html-lineno"><a name="l106" href="#l106">106 </a></span> <span class="c2html-keyword">int</span> ambrows = <span class="c2html-macroconst">0</span>, ambfiles = <span class="c2html-macroconst">0</span>; -<span class="c2html-lineno"><a name="l107" href="#l107">107 </a></span> <span class="c2html-keyword">for</span> (<span class="c2html-type">uint8_t</span> i = <span class="c2html-macroconst">0</span> ; i < threatcount ; i++) { -<span class="c2html-lineno"><a name="l108" href="#l108">108 </a></span> <span class="c2html-keyword">if</span> (threats[i].fromrow == move->fromrow) { -<span class="c2html-lineno"><a name="l109" href="#l109">109 </a></span> ambrows++; -<span class="c2html-lineno"><a name="l110" href="#l110">110 </a></span> } -<span class="c2html-lineno"><a name="l111" href="#l111">111 </a></span> <span class="c2html-keyword">if</span> (threats[i].fromfile == move->fromfile) { -<span class="c2html-lineno"><a name="l112" href="#l112">112 </a></span> ambfiles++; -<span class="c2html-lineno"><a name="l113" href="#l113">113 </a></span> } -<span class="c2html-lineno"><a name="l114" href="#l114">114 </a></span> } -<span class="c2html-lineno"><a name="l115" href="#l115">115 </a></span> <span class="c2html-comment">/* ambiguous row, name file */</span> -<span class="c2html-lineno"><a name="l116" href="#l116">116 </a></span> <span class="c2html-keyword">if</span> (ambrows > <span class="c2html-macroconst">1</span>) { -<span class="c2html-lineno"><a name="l117" href="#l117">117 </a></span> string[idx++] = filechr(move->fromfile); -<span class="c2html-lineno"><a name="l118" href="#l118">118 </a></span> } -<span class="c2html-lineno"><a name="l119" href="#l119">119 </a></span> <span class="c2html-comment">/* ambiguous file, name row */</span> -<span class="c2html-lineno"><a name="l120" href="#l120">120 </a></span> <span class="c2html-keyword">if</span> (ambfiles > <span class="c2html-macroconst">1</span>) { -<span class="c2html-lineno"><a name="l121" href="#l121">121 </a></span> string[idx++] = filechr(move->fromrow); -<span class="c2html-lineno"><a name="l122" href="#l122">122 </a></span> } -<span class="c2html-lineno"><a name="l123" href="#l123">123 </a></span> } -<span class="c2html-lineno"><a name="l124" href="#l124">124 </a></span> } -<span class="c2html-lineno"><a name="l125" href="#l125">125 </a></span> -<span class="c2html-lineno"><a name="l126" href="#l126">126 </a></span> <span class="c2html-comment">/* capturing? */</span> -<span class="c2html-lineno"><a name="l127" href="#l127">127 </a></span> <span class="c2html-keyword">if</span> (move->capture) { -<span class="c2html-lineno"><a name="l128" href="#l128">128 </a></span> string[idx++] = <span class="c2html-string">'x'</span>; -<span class="c2html-lineno"><a name="l129" href="#l129">129 </a></span> } -<span class="c2html-lineno"><a name="l130" href="#l130">130 </a></span> -<span class="c2html-lineno"><a name="l131" href="#l131">131 </a></span> <span class="c2html-comment">/* destination */</span> -<span class="c2html-lineno"><a name="l132" href="#l132">132 </a></span> string[idx++] = filechr(move->tofile); -<span class="c2html-lineno"><a name="l133" href="#l133">133 </a></span> string[idx++] = rowchr(move->torow); -<span class="c2html-lineno"><a name="l134" href="#l134">134 </a></span> -<span class="c2html-lineno"><a name="l135" href="#l135">135 </a></span> <span class="c2html-comment">/* promotion? */</span> -<span class="c2html-lineno"><a name="l136" href="#l136">136 </a></span> <span class="c2html-keyword">if</span> (move->promotion) { -<span class="c2html-lineno"><a name="l137" href="#l137">137 </a></span> string[idx++] = <span class="c2html-string">'='</span>; -<span class="c2html-lineno"><a name="l138" href="#l138">138 </a></span> string[idx++] = getpiecechr(move->promotion); -<span class="c2html-lineno"><a name="l139" href="#l139">139 </a></span> } -<span class="c2html-lineno"><a name="l140" href="#l140">140 </a></span> -<span class="c2html-lineno"><a name="l141" href="#l141">141 </a></span> <span class="c2html-comment">/* check? */</span> -<span class="c2html-lineno"><a name="l142" href="#l142">142 </a></span> <span class="c2html-keyword">if</span> (move->check) { -<span class="c2html-lineno"><a name="l143" href="#l143">143 </a></span> <span class="c2html-comment">/* works only, if this function is called when applying the move */</span> -<span class="c2html-lineno"><a name="l144" href="#l144">144 </a></span> string[idx++] = gamestate->checkmate?<span class="c2html-string">'#'</span>:<span class="c2html-string">'+'</span>; -<span class="c2html-lineno"><a name="l145" href="#l145">145 </a></span> } -<span class="c2html-lineno"><a name="l146" href="#l146">146 </a></span> } -<span class="c2html-lineno"><a name="l147" href="#l147">147 </a></span> -<span class="c2html-lineno"><a name="l148" href="#l148">148 </a></span> <span class="c2html-keyword">static</span> <span class="c2html-keyword">void</span> addmove(GameState* gamestate, Move *move) { -<span class="c2html-lineno"><a name="l149" href="#l149">149 </a></span> MoveList *elem = malloc(<span class="c2html-keyword">sizeof</span>(MoveList)); -<span class="c2html-lineno"><a name="l150" href="#l150">150 </a></span> elem->next = <span class="c2html-macroconst">NULL</span>; -<span class="c2html-lineno"><a name="l151" href="#l151">151 </a></span> elem->move = *move; -<span class="c2html-lineno"><a name="l152" href="#l152">152 </a></span> -<span class="c2html-lineno"><a name="l153" href="#l153">153 </a></span> <span class="c2html-keyword">struct</span> timeval curtimestamp; -<span class="c2html-lineno"><a name="l154" href="#l154">154 </a></span> gettimeofday(&curtimestamp, <span class="c2html-macroconst">NULL</span>); -<span class="c2html-lineno"><a name="l155" href="#l155">155 </a></span> elem->move.timestamp.tv_sec = curtimestamp.tv_sec; -<span class="c2html-lineno"><a name="l156" href="#l156">156 </a></span> elem->move.timestamp.tv_usec = curtimestamp.tv_usec; -<span class="c2html-lineno"><a name="l157" href="#l157">157 </a></span> -<span class="c2html-lineno"><a name="l158" href="#l158">158 </a></span> <span class="c2html-keyword">if</span> (gamestate->lastmove) { -<span class="c2html-lineno"><a name="l159" href="#l159">159 </a></span> <span class="c2html-keyword">struct</span> movetimeval *lasttstamp = &(gamestate->lastmove->move.timestamp); -<span class="c2html-lineno"><a name="l160" href="#l160">160 </a></span> <span class="c2html-type">uint64_t</span> sec = curtimestamp.tv_sec - lasttstamp->tv_sec; -<span class="c2html-lineno"><a name="l161" href="#l161">161 </a></span> <span class="c2html-type">suseconds_t</span> micros; -<span class="c2html-lineno"><a name="l162" href="#l162">162 </a></span> <span class="c2html-keyword">if</span> (curtimestamp.tv_usec < lasttstamp->tv_usec) { -<span class="c2html-lineno"><a name="l163" href="#l163">163 </a></span> micros = 1e6L-(lasttstamp->tv_usec - curtimestamp.tv_usec); -<span class="c2html-lineno"><a name="l164" href="#l164">164 </a></span> sec--; -<span class="c2html-lineno"><a name="l165" href="#l165">165 </a></span> } <span class="c2html-keyword">else</span> { -<span class="c2html-lineno"><a name="l166" href="#l166">166 </a></span> micros = curtimestamp.tv_usec - lasttstamp->tv_usec; -<span class="c2html-lineno"><a name="l167" href="#l167">167 </a></span> } -<span class="c2html-lineno"><a name="l168" href="#l168">168 </a></span> -<span class="c2html-lineno"><a name="l169" href="#l169">169 </a></span> elem->move.movetime.tv_sec = sec; -<span class="c2html-lineno"><a name="l170" href="#l170">170 </a></span> elem->move.movetime.tv_usec = micros; -<span class="c2html-lineno"><a name="l171" href="#l171">171 </a></span> -<span class="c2html-lineno"><a name="l172" href="#l172">172 </a></span> gamestate->lastmove->next = elem; -<span class="c2html-lineno"><a name="l173" href="#l173">173 </a></span> gamestate->lastmove = elem; -<span class="c2html-lineno"><a name="l174" href="#l174">174 </a></span> } <span class="c2html-keyword">else</span> { -<span class="c2html-lineno"><a name="l175" href="#l175">175 </a></span> elem->move.movetime.tv_usec = <span class="c2html-macroconst">0</span>; -<span class="c2html-lineno"><a name="l176" href="#l176">176 </a></span> elem->move.movetime.tv_sec = <span class="c2html-macroconst">0</span>; -<span class="c2html-lineno"><a name="l177" href="#l177">177 </a></span> gamestate->movelist = gamestate->lastmove = elem; -<span class="c2html-lineno"><a name="l178" href="#l178">178 </a></span> } -<span class="c2html-lineno"><a name="l179" href="#l179">179 </a></span> } -<span class="c2html-lineno"><a name="l180" href="#l180">180 </a></span> -<span class="c2html-lineno"><a name="l181" href="#l181">181 </a></span> <span class="c2html-keyword">char</span> getpiecechr(<span class="c2html-type">uint8_t</span> piece) { -<span class="c2html-lineno"><a name="l182" href="#l182">182 </a></span> <span class="c2html-keyword">switch</span> (piece & <span class="c2html-macroconst">PIECE_MASK</span>) { -<span class="c2html-lineno"><a name="l183" href="#l183">183 </a></span> <span class="c2html-keyword">case</span> <span class="c2html-macroconst">ROOK</span>: <span class="c2html-keyword">return</span> <span class="c2html-string">'R'</span>; -<span class="c2html-lineno"><a name="l184" href="#l184">184 </a></span> <span class="c2html-keyword">case</span> <span class="c2html-macroconst">KNIGHT</span>: <span class="c2html-keyword">return</span> <span class="c2html-string">'N'</span>; -<span class="c2html-lineno"><a name="l185" href="#l185">185 </a></span> <span class="c2html-keyword">case</span> <span class="c2html-macroconst">BISHOP</span>: <span class="c2html-keyword">return</span> <span class="c2html-string">'B'</span>; -<span class="c2html-lineno"><a name="l186" href="#l186">186 </a></span> <span class="c2html-keyword">case</span> <span class="c2html-macroconst">QUEEN</span>: <span class="c2html-keyword">return</span> <span class="c2html-string">'Q'</span>; -<span class="c2html-lineno"><a name="l187" href="#l187">187 </a></span> <span class="c2html-keyword">case</span> <span class="c2html-macroconst">KING</span>: <span class="c2html-keyword">return</span> <span class="c2html-string">'K'</span>; -<span class="c2html-lineno"><a name="l188" href="#l188">188 </a></span> <span class="c2html-keyword">default</span>: <span class="c2html-keyword">return</span> <span class="c2html-string">'\0'</span>; -<span class="c2html-lineno"><a name="l189" href="#l189">189 </a></span> } -<span class="c2html-lineno"><a name="l190" href="#l190">190 </a></span> } -<span class="c2html-lineno"><a name="l191" href="#l191">191 </a></span> -<span class="c2html-lineno"><a name="l192" href="#l192">192 </a></span> <span class="c2html-type">uint8_t</span> getpiece(<span class="c2html-keyword">char</span> c) { -<span class="c2html-lineno"><a name="l193" href="#l193">193 </a></span> <span class="c2html-keyword">switch</span> (c) { -<span class="c2html-lineno"><a name="l194" href="#l194">194 </a></span> <span class="c2html-keyword">case</span> <span class="c2html-string">'R'</span>: <span class="c2html-keyword">return</span> <span class="c2html-macroconst">ROOK</span>; -<span class="c2html-lineno"><a name="l195" href="#l195">195 </a></span> <span class="c2html-keyword">case</span> <span class="c2html-string">'N'</span>: <span class="c2html-keyword">return</span> <span class="c2html-macroconst">KNIGHT</span>; -<span class="c2html-lineno"><a name="l196" href="#l196">196 </a></span> <span class="c2html-keyword">case</span> <span class="c2html-string">'B'</span>: <span class="c2html-keyword">return</span> <span class="c2html-macroconst">BISHOP</span>; -<span class="c2html-lineno"><a name="l197" href="#l197">197 </a></span> <span class="c2html-keyword">case</span> <span class="c2html-string">'Q'</span>: <span class="c2html-keyword">return</span> <span class="c2html-macroconst">QUEEN</span>; -<span class="c2html-lineno"><a name="l198" href="#l198">198 </a></span> <span class="c2html-keyword">case</span> <span class="c2html-string">'K'</span>: <span class="c2html-keyword">return</span> <span class="c2html-macroconst">KING</span>; -<span class="c2html-lineno"><a name="l199" href="#l199">199 </a></span> <span class="c2html-keyword">default</span>: <span class="c2html-keyword">return</span> <span class="c2html-macroconst">0</span>; -<span class="c2html-lineno"><a name="l200" href="#l200">200 </a></span> } -<span class="c2html-lineno"><a name="l201" href="#l201">201 </a></span> } -<span class="c2html-lineno"><a name="l202" href="#l202">202 </a></span> -<span class="c2html-lineno"><a name="l203" href="#l203">203 </a></span> <span class="c2html-keyword">static</span> <span class="c2html-keyword">void</span> apply_move_impl(GameState *gamestate, Move *move, _Bool simulate) { -<span class="c2html-lineno"><a name="l204" href="#l204">204 </a></span> <span class="c2html-type">uint8_t</span> piece = move->piece & <span class="c2html-macroconst">PIECE_MASK</span>; -<span class="c2html-lineno"><a name="l205" href="#l205">205 </a></span> <span class="c2html-type">uint8_t</span> color = move->piece & <span class="c2html-macroconst">COLOR_MASK</span>; -<span class="c2html-lineno"><a name="l206" href="#l206">206 </a></span> -<span class="c2html-lineno"><a name="l207" href="#l207">207 </a></span> <span class="c2html-comment">/* en passant capture */</span> -<span class="c2html-lineno"><a name="l208" href="#l208">208 </a></span> <span class="c2html-keyword">if</span> (move->capture && piece == <span class="c2html-macroconst">PAWN</span> && -<span class="c2html-lineno"><a name="l209" href="#l209">209 </a></span> mdst(gamestate->board, move) == <span class="c2html-macroconst">0</span>) { -<span class="c2html-lineno"><a name="l210" href="#l210">210 </a></span> gamestate->board[move->fromrow][move->tofile] = <span class="c2html-macroconst">0</span>; -<span class="c2html-lineno"><a name="l211" href="#l211">211 </a></span> } -<span class="c2html-lineno"><a name="l212" href="#l212">212 </a></span> -<span class="c2html-lineno"><a name="l213" href="#l213">213 </a></span> <span class="c2html-comment">/* remove old en passant threats */</span> -<span class="c2html-lineno"><a name="l214" href="#l214">214 </a></span> <span class="c2html-keyword">for</span> (<span class="c2html-type">uint8_t</span> file = <span class="c2html-macroconst">0</span> ; file < <span class="c2html-macroconst">8</span> ; file++) { -<span class="c2html-lineno"><a name="l215" href="#l215">215 </a></span> gamestate->board[<span class="c2html-macroconst">3</span>][file] &= ~<span class="c2html-macroconst">ENPASSANT_THREAT</span>; -<span class="c2html-lineno"><a name="l216" href="#l216">216 </a></span> gamestate->board[<span class="c2html-macroconst">4</span>][file] &= ~<span class="c2html-macroconst">ENPASSANT_THREAT</span>; -<span class="c2html-lineno"><a name="l217" href="#l217">217 </a></span> } -<span class="c2html-lineno"><a name="l218" href="#l218">218 </a></span> -<span class="c2html-lineno"><a name="l219" href="#l219">219 </a></span> <span class="c2html-comment">/* add new en passant threat */</span> -<span class="c2html-lineno"><a name="l220" href="#l220">220 </a></span> <span class="c2html-keyword">if</span> (piece == <span class="c2html-macroconst">PAWN</span> && ( -<span class="c2html-lineno"><a name="l221" href="#l221">221 </a></span> (move->fromrow == <span class="c2html-macroconst">1</span> && move->torow == <span class="c2html-macroconst">3</span>) || -<span class="c2html-lineno"><a name="l222" href="#l222">222 </a></span> (move->fromrow == <span class="c2html-macroconst">6</span> && move->torow == <span class="c2html-macroconst">4</span>))) { -<span class="c2html-lineno"><a name="l223" href="#l223">223 </a></span> move->piece |= <span class="c2html-macroconst">ENPASSANT_THREAT</span>; -<span class="c2html-lineno"><a name="l224" href="#l224">224 </a></span> } -<span class="c2html-lineno"><a name="l225" href="#l225">225 </a></span> -<span class="c2html-lineno"><a name="l226" href="#l226">226 </a></span> <span class="c2html-comment">/* move (and maybe capture or promote) */</span> -<span class="c2html-lineno"><a name="l227" href="#l227">227 </a></span> msrc(gamestate->board, move) = <span class="c2html-macroconst">0</span>; -<span class="c2html-lineno"><a name="l228" href="#l228">228 </a></span> <span class="c2html-keyword">if</span> (move->promotion) { -<span class="c2html-lineno"><a name="l229" href="#l229">229 </a></span> mdst(gamestate->board, move) = move->promotion; -<span class="c2html-lineno"><a name="l230" href="#l230">230 </a></span> } <span class="c2html-keyword">else</span> { -<span class="c2html-lineno"><a name="l231" href="#l231">231 </a></span> mdst(gamestate->board, move) = move->piece; -<span class="c2html-lineno"><a name="l232" href="#l232">232 </a></span> } -<span class="c2html-lineno"><a name="l233" href="#l233">233 </a></span> -<span class="c2html-lineno"><a name="l234" href="#l234">234 </a></span> <span class="c2html-comment">/* castling */</span> -<span class="c2html-lineno"><a name="l235" href="#l235">235 </a></span> <span class="c2html-keyword">if</span> (piece == <span class="c2html-macroconst">KING</span> && move->fromfile == fileidx(<span class="c2html-string">'e'</span>)) { -<span class="c2html-lineno"><a name="l236" href="#l236">236 </a></span> -<span class="c2html-lineno"><a name="l237" href="#l237">237 </a></span> <span class="c2html-keyword">if</span> (move->tofile == fileidx(<span class="c2html-string">'g'</span>)) { -<span class="c2html-lineno"><a name="l238" href="#l238">238 </a></span> gamestate->board[move->torow][fileidx(<span class="c2html-string">'h'</span>)] = <span class="c2html-macroconst">0</span>; -<span class="c2html-lineno"><a name="l239" href="#l239">239 </a></span> gamestate->board[move->torow][fileidx(<span class="c2html-string">'f'</span>)] = color|<span class="c2html-macroconst">ROOK</span>; -<span class="c2html-lineno"><a name="l240" href="#l240">240 </a></span> } <span class="c2html-keyword">else</span> <span class="c2html-keyword">if</span> (move->tofile == fileidx(<span class="c2html-string">'c'</span>)) { -<span class="c2html-lineno"><a name="l241" href="#l241">241 </a></span> gamestate->board[move->torow][fileidx(<span class="c2html-string">'a'</span>)] = <span class="c2html-macroconst">0</span>; -<span class="c2html-lineno"><a name="l242" href="#l242">242 </a></span> gamestate->board[move->torow][fileidx(<span class="c2html-string">'d'</span>)] = color|<span class="c2html-macroconst">ROOK</span>; -<span class="c2html-lineno"><a name="l243" href="#l243">243 </a></span> } -<span class="c2html-lineno"><a name="l244" href="#l244">244 </a></span> } -<span class="c2html-lineno"><a name="l245" href="#l245">245 </a></span> -<span class="c2html-lineno"><a name="l246" href="#l246">246 </a></span> <span class="c2html-keyword">if</span> (!simulate) { -<span class="c2html-lineno"><a name="l247" href="#l247">247 </a></span> <span class="c2html-keyword">if</span> (!move->string[<span class="c2html-macroconst">0</span>]) { -<span class="c2html-lineno"><a name="l248" href="#l248">248 </a></span> format_move(gamestate, move); -<span class="c2html-lineno"><a name="l249" href="#l249">249 </a></span> } -<span class="c2html-lineno"><a name="l250" href="#l250">250 </a></span> } -<span class="c2html-lineno"><a name="l251" href="#l251">251 </a></span> <span class="c2html-comment">/* add move, even in simulation (checkmate test needs it) */</span> -<span class="c2html-lineno"><a name="l252" href="#l252">252 </a></span> addmove(gamestate, move); -<span class="c2html-lineno"><a name="l253" href="#l253">253 </a></span> } -<span class="c2html-lineno"><a name="l254" href="#l254">254 </a></span> -<span class="c2html-lineno"><a name="l255" href="#l255">255 </a></span> <span class="c2html-keyword">void</span> apply_move(GameState *gamestate, Move *move) { -<span class="c2html-lineno"><a name="l256" href="#l256">256 </a></span> apply_move_impl(gamestate, move, <span class="c2html-macroconst">0</span>); -<span class="c2html-lineno"><a name="l257" href="#l257">257 </a></span> } -<span class="c2html-lineno"><a name="l258" href="#l258">258 </a></span> -<span class="c2html-lineno"><a name="l259" href="#l259">259 </a></span> <span class="c2html-keyword">static</span> <span class="c2html-keyword">int</span> validate_move_rules(GameState *gamestate, Move *move) { -<span class="c2html-lineno"><a name="l260" href="#l260">260 </a></span> <span class="c2html-comment">/* validate indices (don't trust opponent) */</span> -<span class="c2html-lineno"><a name="l261" href="#l261">261 </a></span> <span class="c2html-keyword">if</span> (!chkidx(move)) { -<span class="c2html-lineno"><a name="l262" href="#l262">262 </a></span> <span class="c2html-keyword">return</span> <span class="c2html-macroconst">INVALID_POSITION</span>; -<span class="c2html-lineno"><a name="l263" href="#l263">263 </a></span> } -<span class="c2html-lineno"><a name="l264" href="#l264">264 </a></span> -<span class="c2html-lineno"><a name="l265" href="#l265">265 </a></span> <span class="c2html-comment">/* must move */</span> -<span class="c2html-lineno"><a name="l266" href="#l266">266 </a></span> <span class="c2html-keyword">if</span> (move->fromfile == move->tofile && move->fromrow == move->torow) { -<span class="c2html-lineno"><a name="l267" href="#l267">267 </a></span> <span class="c2html-keyword">return</span> <span class="c2html-macroconst">INVALID_MOVE_SYNTAX</span>; -<span class="c2html-lineno"><a name="l268" href="#l268">268 </a></span> } -<span class="c2html-lineno"><a name="l269" href="#l269">269 </a></span> -<span class="c2html-lineno"><a name="l270" href="#l270">270 </a></span> <span class="c2html-comment">/* does piece exist */</span> -<span class="c2html-lineno"><a name="l271" href="#l271">271 </a></span> <span class="c2html-keyword">if</span> ((msrc(gamestate->board, move)&(<span class="c2html-macroconst">PIECE_MASK</span>|<span class="c2html-macroconst">COLOR_MASK</span>)) -<span class="c2html-lineno"><a name="l272" href="#l272">272 </a></span> != (move->piece&(<span class="c2html-macroconst">PIECE_MASK</span>|<span class="c2html-macroconst">COLOR_MASK</span>))) { -<span class="c2html-lineno"><a name="l273" href="#l273">273 </a></span> <span class="c2html-keyword">return</span> <span class="c2html-macroconst">INVALID_POSITION</span>; -<span class="c2html-lineno"><a name="l274" href="#l274">274 </a></span> } -<span class="c2html-lineno"><a name="l275" href="#l275">275 </a></span> -<span class="c2html-lineno"><a name="l276" href="#l276">276 </a></span> <span class="c2html-comment">/* can't capture own pieces */</span> -<span class="c2html-lineno"><a name="l277" href="#l277">277 </a></span> <span class="c2html-keyword">if</span> ((mdst(gamestate->board, move) & <span class="c2html-macroconst">COLOR_MASK</span>) -<span class="c2html-lineno"><a name="l278" href="#l278">278 </a></span> == (move->piece & <span class="c2html-macroconst">COLOR_MASK</span>)) { -<span class="c2html-lineno"><a name="l279" href="#l279">279 </a></span> <span class="c2html-keyword">return</span> <span class="c2html-macroconst">RULES_VIOLATED</span>; -<span class="c2html-lineno"><a name="l280" href="#l280">280 </a></span> } -<span class="c2html-lineno"><a name="l281" href="#l281">281 </a></span> -<span class="c2html-lineno"><a name="l282" href="#l282">282 </a></span> <span class="c2html-comment">/* must capture, if and only if destination is occupied */</span> -<span class="c2html-lineno"><a name="l283" href="#l283">283 </a></span> <span class="c2html-keyword">if</span> ((mdst(gamestate->board, move) == <span class="c2html-macroconst">0</span> && move->capture) || -<span class="c2html-lineno"><a name="l284" href="#l284">284 </a></span> (mdst(gamestate->board, move) != <span class="c2html-macroconst">0</span> && !move->capture)) { -<span class="c2html-lineno"><a name="l285" href="#l285">285 </a></span> <span class="c2html-keyword">return</span> <span class="c2html-macroconst">INVALID_MOVE_SYNTAX</span>; -<span class="c2html-lineno"><a name="l286" href="#l286">286 </a></span> } -<span class="c2html-lineno"><a name="l287" href="#l287">287 </a></span> -<span class="c2html-lineno"><a name="l288" href="#l288">288 </a></span> <span class="c2html-comment">/* validate individual rules */</span> -<span class="c2html-lineno"><a name="l289" href="#l289">289 </a></span> _Bool chkrules; -<span class="c2html-lineno"><a name="l290" href="#l290">290 </a></span> <span class="c2html-keyword">switch</span> (move->piece & <span class="c2html-macroconst">PIECE_MASK</span>) { -<span class="c2html-lineno"><a name="l291" href="#l291">291 </a></span> <span class="c2html-keyword">case</span> <span class="c2html-macroconst">PAWN</span>: -<span class="c2html-lineno"><a name="l292" href="#l292">292 </a></span> chkrules = pawn_chkrules(gamestate, move) && -<span class="c2html-lineno"><a name="l293" href="#l293">293 </a></span> !pawn_isblocked(gamestate, move); -<span class="c2html-lineno"><a name="l294" href="#l294">294 </a></span> <span class="c2html-keyword">break</span>; -<span class="c2html-lineno"><a name="l295" href="#l295">295 </a></span> <span class="c2html-keyword">case</span> <span class="c2html-macroconst">ROOK</span>: -<span class="c2html-lineno"><a name="l296" href="#l296">296 </a></span> chkrules = rook_chkrules(move) && -<span class="c2html-lineno"><a name="l297" href="#l297">297 </a></span> !rook_isblocked(gamestate, move); -<span class="c2html-lineno"><a name="l298" href="#l298">298 </a></span> <span class="c2html-keyword">break</span>; -<span class="c2html-lineno"><a name="l299" href="#l299">299 </a></span> <span class="c2html-keyword">case</span> <span class="c2html-macroconst">KNIGHT</span>: -<span class="c2html-lineno"><a name="l300" href="#l300">300 </a></span> chkrules = knight_chkrules(move); <span class="c2html-comment">/* knight is never blocked */</span> -<span class="c2html-lineno"><a name="l301" href="#l301">301 </a></span> <span class="c2html-keyword">break</span>; -<span class="c2html-lineno"><a name="l302" href="#l302">302 </a></span> <span class="c2html-keyword">case</span> <span class="c2html-macroconst">BISHOP</span>: -<span class="c2html-lineno"><a name="l303" href="#l303">303 </a></span> chkrules = bishop_chkrules(move) && -<span class="c2html-lineno"><a name="l304" href="#l304">304 </a></span> !bishop_isblocked(gamestate, move); -<span class="c2html-lineno"><a name="l305" href="#l305">305 </a></span> <span class="c2html-keyword">break</span>; -<span class="c2html-lineno"><a name="l306" href="#l306">306 </a></span> <span class="c2html-keyword">case</span> <span class="c2html-macroconst">QUEEN</span>: -<span class="c2html-lineno"><a name="l307" href="#l307">307 </a></span> chkrules = queen_chkrules(move) && -<span class="c2html-lineno"><a name="l308" href="#l308">308 </a></span> !queen_isblocked(gamestate, move); -<span class="c2html-lineno"><a name="l309" href="#l309">309 </a></span> <span class="c2html-keyword">break</span>; -<span class="c2html-lineno"><a name="l310" href="#l310">310 </a></span> <span class="c2html-keyword">case</span> <span class="c2html-macroconst">KING</span>: -<span class="c2html-lineno"><a name="l311" href="#l311">311 </a></span> chkrules = king_chkrules(gamestate, move) && -<span class="c2html-lineno"><a name="l312" href="#l312">312 </a></span> !king_isblocked(gamestate, move); -<span class="c2html-lineno"><a name="l313" href="#l313">313 </a></span> <span class="c2html-keyword">break</span>; -<span class="c2html-lineno"><a name="l314" href="#l314">314 </a></span> <span class="c2html-keyword">default</span>: -<span class="c2html-lineno"><a name="l315" href="#l315">315 </a></span> <span class="c2html-keyword">return</span> <span class="c2html-macroconst">INVALID_MOVE_SYNTAX</span>; -<span class="c2html-lineno"><a name="l316" href="#l316">316 </a></span> } -<span class="c2html-lineno"><a name="l317" href="#l317">317 </a></span> -<span class="c2html-lineno"><a name="l318" href="#l318">318 </a></span> <span class="c2html-keyword">return</span> chkrules ? <span class="c2html-macroconst">VALID_MOVE_SEMANTICS</span> : <span class="c2html-macroconst">RULES_VIOLATED</span>; -<span class="c2html-lineno"><a name="l319" href="#l319">319 </a></span> } -<span class="c2html-lineno"><a name="l320" href="#l320">320 </a></span> -<span class="c2html-lineno"><a name="l321" href="#l321">321 </a></span> <span class="c2html-keyword">int</span> validate_move(GameState *gamestate, Move *move) { -<span class="c2html-lineno"><a name="l322" href="#l322">322 </a></span> -<span class="c2html-lineno"><a name="l323" href="#l323">323 </a></span> <span class="c2html-keyword">int</span> result = validate_move_rules(gamestate, move); -<span class="c2html-lineno"><a name="l324" href="#l324">324 </a></span> -<span class="c2html-lineno"><a name="l325" href="#l325">325 </a></span> <span class="c2html-comment">/* cancel processing to save resources */</span> -<span class="c2html-lineno"><a name="l326" href="#l326">326 </a></span> <span class="c2html-keyword">if</span> (result != <span class="c2html-macroconst">VALID_MOVE_SEMANTICS</span>) { -<span class="c2html-lineno"><a name="l327" href="#l327">327 </a></span> <span class="c2html-keyword">return</span> result; -<span class="c2html-lineno"><a name="l328" href="#l328">328 </a></span> } -<span class="c2html-lineno"><a name="l329" href="#l329">329 </a></span> -<span class="c2html-lineno"><a name="l330" href="#l330">330 </a></span> <span class="c2html-comment">/* find kings for check validation */</span> -<span class="c2html-lineno"><a name="l331" href="#l331">331 </a></span> <span class="c2html-type">uint8_t</span> piececolor = (move->piece & <span class="c2html-macroconst">COLOR_MASK</span>); -<span class="c2html-lineno"><a name="l332" href="#l332">332 </a></span> -<span class="c2html-lineno"><a name="l333" href="#l333">333 </a></span> <span class="c2html-type">uint8_t</span> mykingfile = <span class="c2html-macroconst">0</span>, mykingrow = <span class="c2html-macroconst">0</span>, opkingfile = <span class="c2html-macroconst">0</span>, opkingrow = <span class="c2html-macroconst">0</span>; -<span class="c2html-lineno"><a name="l334" href="#l334">334 </a></span> <span class="c2html-keyword">for</span> (<span class="c2html-type">uint8_t</span> row = <span class="c2html-macroconst">0</span> ; row < <span class="c2html-macroconst">8</span> ; row++) { -<span class="c2html-lineno"><a name="l335" href="#l335">335 </a></span> <span class="c2html-keyword">for</span> (<span class="c2html-type">uint8_t</span> file = <span class="c2html-macroconst">0</span> ; file < <span class="c2html-macroconst">8</span> ; file++) { -<span class="c2html-lineno"><a name="l336" href="#l336">336 </a></span> <span class="c2html-keyword">if</span> (gamestate->board[row][file] == -<span class="c2html-lineno"><a name="l337" href="#l337">337 </a></span> (piececolor == <span class="c2html-macroconst">WHITE</span>?<span class="c2html-macroconst">WKING</span>:<span class="c2html-macroconst">BKING</span>)) { -<span class="c2html-lineno"><a name="l338" href="#l338">338 </a></span> mykingfile = file; -<span class="c2html-lineno"><a name="l339" href="#l339">339 </a></span> mykingrow = row; -<span class="c2html-lineno"><a name="l340" href="#l340">340 </a></span> } <span class="c2html-keyword">else</span> <span class="c2html-keyword">if</span> (gamestate->board[row][file] == -<span class="c2html-lineno"><a name="l341" href="#l341">341 </a></span> (piececolor == <span class="c2html-macroconst">WHITE</span>?<span class="c2html-macroconst">BKING</span>:<span class="c2html-macroconst">WKING</span>)) { -<span class="c2html-lineno"><a name="l342" href="#l342">342 </a></span> opkingfile = file; -<span class="c2html-lineno"><a name="l343" href="#l343">343 </a></span> opkingrow = row; -<span class="c2html-lineno"><a name="l344" href="#l344">344 </a></span> } -<span class="c2html-lineno"><a name="l345" href="#l345">345 </a></span> } -<span class="c2html-lineno"><a name="l346" href="#l346">346 </a></span> } -<span class="c2html-lineno"><a name="l347" href="#l347">347 </a></span> -<span class="c2html-lineno"><a name="l348" href="#l348">348 </a></span> <span class="c2html-comment">/* simulate move for check validation */</span> -<span class="c2html-lineno"><a name="l349" href="#l349">349 </a></span> GameState simulation = gamestate_copy_sim(gamestate); -<span class="c2html-lineno"><a name="l350" href="#l350">350 </a></span> Move simmove = *move; -<span class="c2html-lineno"><a name="l351" href="#l351">351 </a></span> apply_move_impl(&simulation, &simmove, <span class="c2html-macroconst">1</span>); -<span class="c2html-lineno"><a name="l352" href="#l352">352 </a></span> -<span class="c2html-lineno"><a name="l353" href="#l353">353 </a></span> <span class="c2html-comment">/* don't move into or stay in check position */</span> -<span class="c2html-lineno"><a name="l354" href="#l354">354 </a></span> <span class="c2html-keyword">if</span> (is_covered(&simulation, mykingrow, mykingfile, -<span class="c2html-lineno"><a name="l355" href="#l355">355 </a></span> opponent_color(piececolor))) { -<span class="c2html-lineno"><a name="l356" href="#l356">356 </a></span> -<span class="c2html-lineno"><a name="l357" href="#l357">357 </a></span> gamestate_cleanup(&simulation); -<span class="c2html-lineno"><a name="l358" href="#l358">358 </a></span> <span class="c2html-keyword">if</span> ((move->piece & <span class="c2html-macroconst">PIECE_MASK</span>) == <span class="c2html-macroconst">KING</span>) { -<span class="c2html-lineno"><a name="l359" href="#l359">359 </a></span> <span class="c2html-keyword">return</span> <span class="c2html-macroconst">KING_MOVES_INTO_CHECK</span>; -<span class="c2html-lineno"><a name="l360" href="#l360">360 </a></span> } <span class="c2html-keyword">else</span> { -<span class="c2html-lineno"><a name="l361" href="#l361">361 </a></span> <span class="c2html-comment">/* last move is always not null in this case */</span> -<span class="c2html-lineno"><a name="l362" href="#l362">362 </a></span> <span class="c2html-keyword">return</span> gamestate->lastmove->move.check ? -<span class="c2html-lineno"><a name="l363" href="#l363">363 </a></span> <span class="c2html-macroconst">KING_IN_CHECK</span> : <span class="c2html-macroconst">PIECE_PINNED</span>; -<span class="c2html-lineno"><a name="l364" href="#l364">364 </a></span> } -<span class="c2html-lineno"><a name="l365" href="#l365">365 </a></span> } -<span class="c2html-lineno"><a name="l366" href="#l366">366 </a></span> -<span class="c2html-lineno"><a name="l367" href="#l367">367 </a></span> <span class="c2html-comment">/* correct check and checkmate flags (move is still valid) */</span> -<span class="c2html-lineno"><a name="l368" href="#l368">368 </a></span> Move threats[<span class="c2html-macroconst">16</span>]; -<span class="c2html-lineno"><a name="l369" href="#l369">369 </a></span> <span class="c2html-type">uint8_t</span> threatcount; -<span class="c2html-lineno"><a name="l370" href="#l370">370 </a></span> move->check = get_threats(&simulation, opkingrow, opkingfile, -<span class="c2html-lineno"><a name="l371" href="#l371">371 </a></span> piececolor, threats, &threatcount); -<span class="c2html-lineno"><a name="l372" href="#l372">372 </a></span> -<span class="c2html-lineno"><a name="l373" href="#l373">373 </a></span> <span class="c2html-keyword">if</span> (move->check) { -<span class="c2html-lineno"><a name="l374" href="#l374">374 </a></span> <span class="c2html-comment">/* determine possible escape fields */</span> -<span class="c2html-lineno"><a name="l375" href="#l375">375 </a></span> _Bool canescape = <span class="c2html-macroconst">0</span>; -<span class="c2html-lineno"><a name="l376" href="#l376">376 </a></span> <span class="c2html-keyword">for</span> (<span class="c2html-keyword">int</span> dr = -<span class="c2html-macroconst">1</span> ; dr <= <span class="c2html-macroconst">1</span> && !canescape ; dr++) { -<span class="c2html-lineno"><a name="l377" href="#l377">377 </a></span> <span class="c2html-keyword">for</span> (<span class="c2html-keyword">int</span> df = -<span class="c2html-macroconst">1</span> ; df <= <span class="c2html-macroconst">1</span> && !canescape ; df++) { -<span class="c2html-lineno"><a name="l378" href="#l378">378 </a></span> <span class="c2html-keyword">if</span> (!(dr == <span class="c2html-macroconst">0</span> && df == <span class="c2html-macroconst">0</span>) && -<span class="c2html-lineno"><a name="l379" href="#l379">379 </a></span> isidx(opkingrow + dr) && isidx(opkingfile + df)) { -<span class="c2html-lineno"><a name="l380" href="#l380">380 </a></span> -<span class="c2html-lineno"><a name="l381" href="#l381">381 </a></span> <span class="c2html-comment">/* escape field neither blocked nor covered */</span> -<span class="c2html-lineno"><a name="l382" href="#l382">382 </a></span> <span class="c2html-keyword">if</span> ((simulation.board[opkingrow + dr][opkingfile + df] -<span class="c2html-lineno"><a name="l383" href="#l383">383 </a></span> & <span class="c2html-macroconst">COLOR_MASK</span>) != opponent_color(piececolor)) { -<span class="c2html-lineno"><a name="l384" href="#l384">384 </a></span> canescape |= !is_covered(&simulation, -<span class="c2html-lineno"><a name="l385" href="#l385">385 </a></span> opkingrow + dr, opkingfile + df, piececolor); -<span class="c2html-lineno"><a name="l386" href="#l386">386 </a></span> } -<span class="c2html-lineno"><a name="l387" href="#l387">387 </a></span> } -<span class="c2html-lineno"><a name="l388" href="#l388">388 </a></span> } -<span class="c2html-lineno"><a name="l389" href="#l389">389 </a></span> } -<span class="c2html-lineno"><a name="l390" href="#l390">390 </a></span> <span class="c2html-comment">/* can't escape, can he capture? */</span> -<span class="c2html-lineno"><a name="l391" href="#l391">391 </a></span> <span class="c2html-keyword">if</span> (!canescape && threatcount == <span class="c2html-macroconst">1</span>) { -<span class="c2html-lineno"><a name="l392" href="#l392">392 </a></span> canescape = is_attacked(&simulation, threats[<span class="c2html-macroconst">0</span>].fromrow, -<span class="c2html-lineno"><a name="l393" href="#l393">393 </a></span> threats[<span class="c2html-macroconst">0</span>].fromfile, opponent_color(piececolor)); -<span class="c2html-lineno"><a name="l394" href="#l394">394 </a></span> } -<span class="c2html-lineno"><a name="l395" href="#l395">395 </a></span> -<span class="c2html-lineno"><a name="l396" href="#l396">396 </a></span> <span class="c2html-comment">/* can't capture, can he block? */</span> -<span class="c2html-lineno"><a name="l397" href="#l397">397 </a></span> <span class="c2html-keyword">if</span> (!canescape && threatcount == <span class="c2html-macroconst">1</span>) { -<span class="c2html-lineno"><a name="l398" href="#l398">398 </a></span> Move *threat = &(threats[<span class="c2html-macroconst">0</span>]); -<span class="c2html-lineno"><a name="l399" href="#l399">399 </a></span> <span class="c2html-type">uint8_t</span> threatpiece = threat->piece & <span class="c2html-macroconst">PIECE_MASK</span>; -<span class="c2html-lineno"><a name="l400" href="#l400">400 </a></span> -<span class="c2html-lineno"><a name="l401" href="#l401">401 </a></span> <span class="c2html-comment">/* knight, pawns and the king cannot be blocked */</span> -<span class="c2html-lineno"><a name="l402" href="#l402">402 </a></span> <span class="c2html-keyword">if</span> (threatpiece == <span class="c2html-macroconst">BISHOP</span> || threatpiece == <span class="c2html-macroconst">ROOK</span> -<span class="c2html-lineno"><a name="l403" href="#l403">403 </a></span> || threatpiece == <span class="c2html-macroconst">QUEEN</span>) { -<span class="c2html-lineno"><a name="l404" href="#l404">404 </a></span> <span class="c2html-keyword">if</span> (threat->fromrow == threat->torow) { -<span class="c2html-lineno"><a name="l405" href="#l405">405 </a></span> <span class="c2html-comment">/* rook aspect (on row) */</span> -<span class="c2html-lineno"><a name="l406" href="#l406">406 </a></span> <span class="c2html-keyword">int</span> d = threat->tofile > threat->fromfile ? <span class="c2html-macroconst">1</span> : -<span class="c2html-macroconst">1</span>; -<span class="c2html-lineno"><a name="l407" href="#l407">407 </a></span> <span class="c2html-type">uint8_t</span> file = threat->fromfile; -<span class="c2html-lineno"><a name="l408" href="#l408">408 </a></span> <span class="c2html-keyword">while</span> (!canescape && file != threat->tofile - d) { -<span class="c2html-lineno"><a name="l409" href="#l409">409 </a></span> file += d; -<span class="c2html-lineno"><a name="l410" href="#l410">410 </a></span> canescape |= is_protected(&simulation, -<span class="c2html-lineno"><a name="l411" href="#l411">411 </a></span> threat->torow, file, opponent_color(piececolor)); -<span class="c2html-lineno"><a name="l412" href="#l412">412 </a></span> } -<span class="c2html-lineno"><a name="l413" href="#l413">413 </a></span> } <span class="c2html-keyword">else</span> <span class="c2html-keyword">if</span> (threat->fromfile == threat->tofile) { -<span class="c2html-lineno"><a name="l414" href="#l414">414 </a></span> <span class="c2html-comment">/* rook aspect (on file) */</span> -<span class="c2html-lineno"><a name="l415" href="#l415">415 </a></span> <span class="c2html-keyword">int</span> d = threat->torow > threat->fromrow ? <span class="c2html-macroconst">1</span> : -<span class="c2html-macroconst">1</span>; -<span class="c2html-lineno"><a name="l416" href="#l416">416 </a></span> <span class="c2html-type">uint8_t</span> row = threat->fromrow; -<span class="c2html-lineno"><a name="l417" href="#l417">417 </a></span> <span class="c2html-keyword">while</span> (!canescape && row != threat->torow - d) { -<span class="c2html-lineno"><a name="l418" href="#l418">418 </a></span> row += d; -<span class="c2html-lineno"><a name="l419" href="#l419">419 </a></span> canescape |= is_protected(&simulation, -<span class="c2html-lineno"><a name="l420" href="#l420">420 </a></span> row, threat->tofile, opponent_color(piececolor)); -<span class="c2html-lineno"><a name="l421" href="#l421">421 </a></span> } -<span class="c2html-lineno"><a name="l422" href="#l422">422 </a></span> } <span class="c2html-keyword">else</span> { -<span class="c2html-lineno"><a name="l423" href="#l423">423 </a></span> <span class="c2html-comment">/* bishop aspect */</span> -<span class="c2html-lineno"><a name="l424" href="#l424">424 </a></span> <span class="c2html-keyword">int</span> dr = threat->torow > threat->fromrow ? <span class="c2html-macroconst">1</span> : -<span class="c2html-macroconst">1</span>; -<span class="c2html-lineno"><a name="l425" href="#l425">425 </a></span> <span class="c2html-keyword">int</span> df = threat->tofile > threat->fromfile ? <span class="c2html-macroconst">1</span> : -<span class="c2html-macroconst">1</span>; -<span class="c2html-lineno"><a name="l426" href="#l426">426 </a></span> -<span class="c2html-lineno"><a name="l427" href="#l427">427 </a></span> <span class="c2html-type">uint8_t</span> row = threat->fromrow; -<span class="c2html-lineno"><a name="l428" href="#l428">428 </a></span> <span class="c2html-type">uint8_t</span> file = threat->fromfile; -<span class="c2html-lineno"><a name="l429" href="#l429">429 </a></span> <span class="c2html-keyword">while</span> (!canescape && file != threat->tofile - df -<span class="c2html-lineno"><a name="l430" href="#l430">430 </a></span> && row != threat->torow - dr) { -<span class="c2html-lineno"><a name="l431" href="#l431">431 </a></span> row += dr; -<span class="c2html-lineno"><a name="l432" href="#l432">432 </a></span> file += df; -<span class="c2html-lineno"><a name="l433" href="#l433">433 </a></span> canescape |= is_protected(&simulation, row, file, -<span class="c2html-lineno"><a name="l434" href="#l434">434 </a></span> opponent_color(piececolor)); -<span class="c2html-lineno"><a name="l435" href="#l435">435 </a></span> } -<span class="c2html-lineno"><a name="l436" href="#l436">436 </a></span> } -<span class="c2html-lineno"><a name="l437" href="#l437">437 </a></span> } -<span class="c2html-lineno"><a name="l438" href="#l438">438 </a></span> } -<span class="c2html-lineno"><a name="l439" href="#l439">439 </a></span> -<span class="c2html-lineno"><a name="l440" href="#l440">440 </a></span> <span class="c2html-keyword">if</span> (!canescape) { -<span class="c2html-lineno"><a name="l441" href="#l441">441 </a></span> gamestate->checkmate = <span class="c2html-macroconst">1</span>; -<span class="c2html-lineno"><a name="l442" href="#l442">442 </a></span> } -<span class="c2html-lineno"><a name="l443" href="#l443">443 </a></span> } -<span class="c2html-lineno"><a name="l444" href="#l444">444 </a></span> -<span class="c2html-lineno"><a name="l445" href="#l445">445 </a></span> gamestate_cleanup(&simulation); -<span class="c2html-lineno"><a name="l446" href="#l446">446 </a></span> -<span class="c2html-lineno"><a name="l447" href="#l447">447 </a></span> <span class="c2html-keyword">return</span> <span class="c2html-macroconst">VALID_MOVE_SEMANTICS</span>; -<span class="c2html-lineno"><a name="l448" href="#l448">448 </a></span> } -<span class="c2html-lineno"><a name="l449" href="#l449">449 </a></span> -<span class="c2html-lineno"><a name="l450" href="#l450">450 </a></span> _Bool get_threats(GameState *gamestate, <span class="c2html-type">uint8_t</span> row, <span class="c2html-type">uint8_t</span> file, -<span class="c2html-lineno"><a name="l451" href="#l451">451 </a></span> <span class="c2html-type">uint8_t</span> color, Move *threats, <span class="c2html-type">uint8_t</span> *threatcount) { -<span class="c2html-lineno"><a name="l452" href="#l452">452 </a></span> Move candidates[<span class="c2html-macroconst">32</span>]; -<span class="c2html-lineno"><a name="l453" href="#l453">453 </a></span> <span class="c2html-keyword">int</span> candidatecount = <span class="c2html-macroconst">0</span>; -<span class="c2html-lineno"><a name="l454" href="#l454">454 </a></span> <span class="c2html-keyword">for</span> (<span class="c2html-type">uint8_t</span> r = <span class="c2html-macroconst">0</span> ; r < <span class="c2html-macroconst">8</span> ; r++) { -<span class="c2html-lineno"><a name="l455" href="#l455">455 </a></span> <span class="c2html-keyword">for</span> (<span class="c2html-type">uint8_t</span> f = <span class="c2html-macroconst">0</span> ; f < <span class="c2html-macroconst">8</span> ; f++) { -<span class="c2html-lineno"><a name="l456" href="#l456">456 </a></span> <span class="c2html-keyword">if</span> ((gamestate->board[r][f] & <span class="c2html-macroconst">COLOR_MASK</span>) == color) { -<span class="c2html-lineno"><a name="l457" href="#l457">457 </a></span> <span class="c2html-comment">// non-capturing move</span> -<span class="c2html-lineno"><a name="l458" href="#l458">458 </a></span> memset(&(candidates[candidatecount]), <span class="c2html-macroconst">0</span>, <span class="c2html-keyword">sizeof</span>(Move)); -<span class="c2html-lineno"><a name="l459" href="#l459">459 </a></span> candidates[candidatecount].piece = gamestate->board[r][f]; -<span class="c2html-lineno"><a name="l460" href="#l460">460 </a></span> candidates[candidatecount].fromrow = r; -<span class="c2html-lineno"><a name="l461" href="#l461">461 </a></span> candidates[candidatecount].fromfile = f; -<span class="c2html-lineno"><a name="l462" href="#l462">462 </a></span> candidates[candidatecount].torow = row; -<span class="c2html-lineno"><a name="l463" href="#l463">463 </a></span> candidates[candidatecount].tofile = file; -<span class="c2html-lineno"><a name="l464" href="#l464">464 </a></span> candidatecount++; -<span class="c2html-lineno"><a name="l465" href="#l465">465 </a></span> -<span class="c2html-lineno"><a name="l466" href="#l466">466 </a></span> <span class="c2html-comment">// capturing move</span> -<span class="c2html-lineno"><a name="l467" href="#l467">467 </a></span> memcpy(&(candidates[candidatecount]), -<span class="c2html-lineno"><a name="l468" href="#l468">468 </a></span> &(candidates[candidatecount-<span class="c2html-macroconst">1</span>]), <span class="c2html-keyword">sizeof</span>(Move)); -<span class="c2html-lineno"><a name="l469" href="#l469">469 </a></span> candidates[candidatecount].capture = <span class="c2html-macroconst">1</span>; -<span class="c2html-lineno"><a name="l470" href="#l470">470 </a></span> candidatecount++; -<span class="c2html-lineno"><a name="l471" href="#l471">471 </a></span> } -<span class="c2html-lineno"><a name="l472" href="#l472">472 </a></span> } -<span class="c2html-lineno"><a name="l473" href="#l473">473 </a></span> } -<span class="c2html-lineno"><a name="l474" href="#l474">474 </a></span> -<span class="c2html-lineno"><a name="l475" href="#l475">475 </a></span> <span class="c2html-keyword">if</span> (threatcount) { -<span class="c2html-lineno"><a name="l476" href="#l476">476 </a></span> *threatcount = <span class="c2html-macroconst">0</span>; -<span class="c2html-lineno"><a name="l477" href="#l477">477 </a></span> } -<span class="c2html-lineno"><a name="l478" href="#l478">478 </a></span> -<span class="c2html-lineno"><a name="l479" href="#l479">479 </a></span> -<span class="c2html-lineno"><a name="l480" href="#l480">480 </a></span> _Bool result = <span class="c2html-macroconst">0</span>; -<span class="c2html-lineno"><a name="l481" href="#l481">481 </a></span> -<span class="c2html-lineno"><a name="l482" href="#l482">482 </a></span> <span class="c2html-keyword">for</span> (<span class="c2html-keyword">int</span> i = <span class="c2html-macroconst">0</span> ; i < candidatecount ; i++) { -<span class="c2html-lineno"><a name="l483" href="#l483">483 </a></span> <span class="c2html-keyword">if</span> (validate_move_rules(gamestate, &(candidates[i])) -<span class="c2html-lineno"><a name="l484" href="#l484">484 </a></span> == <span class="c2html-macroconst">VALID_MOVE_SEMANTICS</span>) { -<span class="c2html-lineno"><a name="l485" href="#l485">485 </a></span> result = <span class="c2html-macroconst">1</span>; -<span class="c2html-lineno"><a name="l486" href="#l486">486 </a></span> <span class="c2html-keyword">if</span> (threats && threatcount) { -<span class="c2html-lineno"><a name="l487" href="#l487">487 </a></span> threats[(*threatcount)++] = candidates[i]; -<span class="c2html-lineno"><a name="l488" href="#l488">488 </a></span> } -<span class="c2html-lineno"><a name="l489" href="#l489">489 </a></span> } -<span class="c2html-lineno"><a name="l490" href="#l490">490 </a></span> } -<span class="c2html-lineno"><a name="l491" href="#l491">491 </a></span> -<span class="c2html-lineno"><a name="l492" href="#l492">492 </a></span> <span class="c2html-keyword">return</span> result; -<span class="c2html-lineno"><a name="l493" href="#l493">493 </a></span> } -<span class="c2html-lineno"><a name="l494" href="#l494">494 </a></span> -<span class="c2html-lineno"><a name="l495" href="#l495">495 </a></span> _Bool is_pinned(GameState *gamestate, Move *move) { -<span class="c2html-lineno"><a name="l496" href="#l496">496 </a></span> <span class="c2html-type">uint8_t</span> color = move->piece & <span class="c2html-macroconst">COLOR_MASK</span>; -<span class="c2html-lineno"><a name="l497" href="#l497">497 </a></span> -<span class="c2html-lineno"><a name="l498" href="#l498">498 </a></span> <span class="c2html-type">uint8_t</span> kingfile = <span class="c2html-macroconst">0</span>, kingrow = <span class="c2html-macroconst">0</span>; -<span class="c2html-lineno"><a name="l499" href="#l499">499 </a></span> <span class="c2html-keyword">for</span> (<span class="c2html-type">uint8_t</span> row = <span class="c2html-macroconst">0</span> ; row < <span class="c2html-macroconst">8</span> ; row++) { -<span class="c2html-lineno"><a name="l500" href="#l500">500 </a></span> <span class="c2html-keyword">for</span> (<span class="c2html-type">uint8_t</span> file = <span class="c2html-macroconst">0</span> ; file < <span class="c2html-macroconst">8</span> ; file++) { -<span class="c2html-lineno"><a name="l501" href="#l501">501 </a></span> <span class="c2html-keyword">if</span> (gamestate->board[row][file] == (color|<span class="c2html-macroconst">KING</span>)) { -<span class="c2html-lineno"><a name="l502" href="#l502">502 </a></span> kingfile = file; -<span class="c2html-lineno"><a name="l503" href="#l503">503 </a></span> kingrow = row; -<span class="c2html-lineno"><a name="l504" href="#l504">504 </a></span> } -<span class="c2html-lineno"><a name="l505" href="#l505">505 </a></span> } -<span class="c2html-lineno"><a name="l506" href="#l506">506 </a></span> } -<span class="c2html-lineno"><a name="l507" href="#l507">507 </a></span> -<span class="c2html-lineno"><a name="l508" href="#l508">508 </a></span> GameState simulation = gamestate_copy_sim(gamestate); -<span class="c2html-lineno"><a name="l509" href="#l509">509 </a></span> Move simmove = *move; -<span class="c2html-lineno"><a name="l510" href="#l510">510 </a></span> apply_move(&simulation, &simmove); -<span class="c2html-lineno"><a name="l511" href="#l511">511 </a></span> _Bool covered = is_covered(&simulation, -<span class="c2html-lineno"><a name="l512" href="#l512">512 </a></span> kingrow, kingfile, opponent_color(color)); -<span class="c2html-lineno"><a name="l513" href="#l513">513 </a></span> gamestate_cleanup(&simulation); -<span class="c2html-lineno"><a name="l514" href="#l514">514 </a></span> -<span class="c2html-lineno"><a name="l515" href="#l515">515 </a></span> <span class="c2html-keyword">return</span> covered; -<span class="c2html-lineno"><a name="l516" href="#l516">516 </a></span> } -<span class="c2html-lineno"><a name="l517" href="#l517">517 </a></span> -<span class="c2html-lineno"><a name="l518" href="#l518">518 </a></span> _Bool get_real_threats(GameState *gamestate, <span class="c2html-type">uint8_t</span> row, <span class="c2html-type">uint8_t</span> file, -<span class="c2html-lineno"><a name="l519" href="#l519">519 </a></span> <span class="c2html-type">uint8_t</span> color, Move *threats, <span class="c2html-type">uint8_t</span> *threatcount) { -<span class="c2html-lineno"><a name="l520" href="#l520">520 </a></span> -<span class="c2html-lineno"><a name="l521" href="#l521">521 </a></span> <span class="c2html-keyword">if</span> (threatcount) { -<span class="c2html-lineno"><a name="l522" href="#l522">522 </a></span> *threatcount = <span class="c2html-macroconst">0</span>; -<span class="c2html-lineno"><a name="l523" href="#l523">523 </a></span> } -<span class="c2html-lineno"><a name="l524" href="#l524">524 </a></span> -<span class="c2html-lineno"><a name="l525" href="#l525">525 </a></span> Move candidates[<span class="c2html-macroconst">16</span>]; -<span class="c2html-lineno"><a name="l526" href="#l526">526 </a></span> <span class="c2html-type">uint8_t</span> candidatecount; -<span class="c2html-lineno"><a name="l527" href="#l527">527 </a></span> <span class="c2html-keyword">if</span> (get_threats(gamestate, row, file, color, candidates, &candidatecount)) { -<span class="c2html-lineno"><a name="l528" href="#l528">528 </a></span> -<span class="c2html-lineno"><a name="l529" href="#l529">529 </a></span> _Bool result = <span class="c2html-macroconst">0</span>; -<span class="c2html-lineno"><a name="l530" href="#l530">530 </a></span> <span class="c2html-type">uint8_t</span> kingfile = <span class="c2html-macroconst">0</span>, kingrow = <span class="c2html-macroconst">0</span>; -<span class="c2html-lineno"><a name="l531" href="#l531">531 </a></span> <span class="c2html-keyword">for</span> (<span class="c2html-type">uint8_t</span> row = <span class="c2html-macroconst">0</span> ; row < <span class="c2html-macroconst">8</span> ; row++) { -<span class="c2html-lineno"><a name="l532" href="#l532">532 </a></span> <span class="c2html-keyword">for</span> (<span class="c2html-type">uint8_t</span> file = <span class="c2html-macroconst">0</span> ; file < <span class="c2html-macroconst">8</span> ; file++) { -<span class="c2html-lineno"><a name="l533" href="#l533">533 </a></span> <span class="c2html-keyword">if</span> (gamestate->board[row][file] == (color|<span class="c2html-macroconst">KING</span>)) { -<span class="c2html-lineno"><a name="l534" href="#l534">534 </a></span> kingfile = file; -<span class="c2html-lineno"><a name="l535" href="#l535">535 </a></span> kingrow = row; -<span class="c2html-lineno"><a name="l536" href="#l536">536 </a></span> } -<span class="c2html-lineno"><a name="l537" href="#l537">537 </a></span> } -<span class="c2html-lineno"><a name="l538" href="#l538">538 </a></span> } -<span class="c2html-lineno"><a name="l539" href="#l539">539 </a></span> -<span class="c2html-lineno"><a name="l540" href="#l540">540 </a></span> <span class="c2html-keyword">for</span> (<span class="c2html-type">uint8_t</span> i = <span class="c2html-macroconst">0</span> ; i < candidatecount ; i++) { -<span class="c2html-lineno"><a name="l541" href="#l541">541 </a></span> GameState simulation = gamestate_copy_sim(gamestate); -<span class="c2html-lineno"><a name="l542" href="#l542">542 </a></span> Move simmove = candidates[i]; -<span class="c2html-lineno"><a name="l543" href="#l543">543 </a></span> apply_move(&simulation, &simmove); -<span class="c2html-lineno"><a name="l544" href="#l544">544 </a></span> <span class="c2html-keyword">if</span> (!is_covered(&simulation, kingrow, kingfile, -<span class="c2html-lineno"><a name="l545" href="#l545">545 </a></span> opponent_color(color))) { -<span class="c2html-lineno"><a name="l546" href="#l546">546 </a></span> result = <span class="c2html-macroconst">1</span>; -<span class="c2html-lineno"><a name="l547" href="#l547">547 </a></span> <span class="c2html-keyword">if</span> (threats && threatcount) { -<span class="c2html-lineno"><a name="l548" href="#l548">548 </a></span> threats[(*threatcount)++] = candidates[i]; -<span class="c2html-lineno"><a name="l549" href="#l549">549 </a></span> } -<span class="c2html-lineno"><a name="l550" href="#l550">550 </a></span> } -<span class="c2html-lineno"><a name="l551" href="#l551">551 </a></span> } -<span class="c2html-lineno"><a name="l552" href="#l552">552 </a></span> -<span class="c2html-lineno"><a name="l553" href="#l553">553 </a></span> <span class="c2html-keyword">return</span> result; -<span class="c2html-lineno"><a name="l554" href="#l554">554 </a></span> } <span class="c2html-keyword">else</span> { -<span class="c2html-lineno"><a name="l555" href="#l555">555 </a></span> <span class="c2html-keyword">return</span> <span class="c2html-macroconst">0</span>; -<span class="c2html-lineno"><a name="l556" href="#l556">556 </a></span> } -<span class="c2html-lineno"><a name="l557" href="#l557">557 </a></span> } -<span class="c2html-lineno"><a name="l558" href="#l558">558 </a></span> -<span class="c2html-lineno"><a name="l559" href="#l559">559 </a></span> <span class="c2html-keyword">static</span> <span class="c2html-keyword">int</span> getlocation(GameState *gamestate, Move *move) { -<span class="c2html-lineno"><a name="l560" href="#l560">560 </a></span> -<span class="c2html-lineno"><a name="l561" href="#l561">561 </a></span> <span class="c2html-type">uint8_t</span> color = move->piece & <span class="c2html-macroconst">COLOR_MASK</span>; -<span class="c2html-lineno"><a name="l562" href="#l562">562 </a></span> _Bool incheck = gamestate->lastmove?gamestate->lastmove->move.check:<span class="c2html-macroconst">0</span>; -<span class="c2html-lineno"><a name="l563" href="#l563">563 </a></span> -<span class="c2html-lineno"><a name="l564" href="#l564">564 </a></span> Move threats[<span class="c2html-macroconst">16</span>], *threat = <span class="c2html-macroconst">NULL</span>; -<span class="c2html-lineno"><a name="l565" href="#l565">565 </a></span> <span class="c2html-type">uint8_t</span> threatcount; -<span class="c2html-lineno"><a name="l566" href="#l566">566 </a></span> -<span class="c2html-lineno"><a name="l567" href="#l567">567 </a></span> <span class="c2html-keyword">if</span> (get_threats(gamestate, move->torow, move->tofile, color, -<span class="c2html-lineno"><a name="l568" href="#l568">568 </a></span> threats, &threatcount)) { -<span class="c2html-lineno"><a name="l569" href="#l569">569 </a></span> -<span class="c2html-lineno"><a name="l570" href="#l570">570 </a></span> <span class="c2html-keyword">int</span> reason = <span class="c2html-macroconst">INVALID_POSITION</span>; -<span class="c2html-lineno"><a name="l571" href="#l571">571 </a></span> -<span class="c2html-lineno"><a name="l572" href="#l572">572 </a></span> <span class="c2html-comment">// find threats for the specified position</span> -<span class="c2html-lineno"><a name="l573" href="#l573">573 </a></span> <span class="c2html-keyword">for</span> (<span class="c2html-type">uint8_t</span> i = <span class="c2html-macroconst">0</span> ; i < threatcount ; i++) { -<span class="c2html-lineno"><a name="l574" href="#l574">574 </a></span> <span class="c2html-keyword">if</span> ((threats[i].piece & (<span class="c2html-macroconst">PIECE_MASK</span> | <span class="c2html-macroconst">COLOR_MASK</span>)) -<span class="c2html-lineno"><a name="l575" href="#l575">575 </a></span> == move->piece && -<span class="c2html-lineno"><a name="l576" href="#l576">576 </a></span> (move->fromrow == <span class="c2html-macroconst">POS_UNSPECIFIED</span> || -<span class="c2html-lineno"><a name="l577" href="#l577">577 </a></span> move->fromrow == threats[i].fromrow) && -<span class="c2html-lineno"><a name="l578" href="#l578">578 </a></span> (move->fromfile == <span class="c2html-macroconst">POS_UNSPECIFIED</span> || -<span class="c2html-lineno"><a name="l579" href="#l579">579 </a></span> move->fromfile == threats[i].fromfile)) { -<span class="c2html-lineno"><a name="l580" href="#l580">580 </a></span> -<span class="c2html-lineno"><a name="l581" href="#l581">581 </a></span> <span class="c2html-keyword">if</span> (threat) { -<span class="c2html-lineno"><a name="l582" href="#l582">582 </a></span> <span class="c2html-keyword">return</span> <span class="c2html-macroconst">AMBIGUOUS_MOVE</span>; -<span class="c2html-lineno"><a name="l583" href="#l583">583 </a></span> } <span class="c2html-keyword">else</span> { -<span class="c2html-lineno"><a name="l584" href="#l584">584 </a></span> <span class="c2html-comment">// found threat is no real threat</span> -<span class="c2html-lineno"><a name="l585" href="#l585">585 </a></span> <span class="c2html-keyword">if</span> (is_pinned(gamestate, &(threats[i]))) { -<span class="c2html-lineno"><a name="l586" href="#l586">586 </a></span> reason = incheck?<span class="c2html-macroconst">KING_IN_CHECK</span>:<span class="c2html-macroconst">PIECE_PINNED</span>; -<span class="c2html-lineno"><a name="l587" href="#l587">587 </a></span> } <span class="c2html-keyword">else</span> { -<span class="c2html-lineno"><a name="l588" href="#l588">588 </a></span> threat = &(threats[i]); -<span class="c2html-lineno"><a name="l589" href="#l589">589 </a></span> } -<span class="c2html-lineno"><a name="l590" href="#l590">590 </a></span> } -<span class="c2html-lineno"><a name="l591" href="#l591">591 </a></span> } -<span class="c2html-lineno"><a name="l592" href="#l592">592 </a></span> } -<span class="c2html-lineno"><a name="l593" href="#l593">593 </a></span> -<span class="c2html-lineno"><a name="l594" href="#l594">594 </a></span> <span class="c2html-comment">// can't threaten specified position</span> -<span class="c2html-lineno"><a name="l595" href="#l595">595 </a></span> <span class="c2html-keyword">if</span> (!threat) { -<span class="c2html-lineno"><a name="l596" href="#l596">596 </a></span> <span class="c2html-keyword">return</span> reason; -<span class="c2html-lineno"><a name="l597" href="#l597">597 </a></span> } -<span class="c2html-lineno"><a name="l598" href="#l598">598 </a></span> -<span class="c2html-lineno"><a name="l599" href="#l599">599 </a></span> memcpy(move, threat, <span class="c2html-keyword">sizeof</span>(Move)); -<span class="c2html-lineno"><a name="l600" href="#l600">600 </a></span> <span class="c2html-keyword">return</span> <span class="c2html-macroconst">VALID_MOVE_SYNTAX</span>; -<span class="c2html-lineno"><a name="l601" href="#l601">601 </a></span> } <span class="c2html-keyword">else</span> { -<span class="c2html-lineno"><a name="l602" href="#l602">602 </a></span> <span class="c2html-keyword">return</span> <span class="c2html-macroconst">INVALID_POSITION</span>; -<span class="c2html-lineno"><a name="l603" href="#l603">603 </a></span> } -<span class="c2html-lineno"><a name="l604" href="#l604">604 </a></span> } -<span class="c2html-lineno"><a name="l605" href="#l605">605 </a></span> -<span class="c2html-lineno"><a name="l606" href="#l606">606 </a></span> <span class="c2html-keyword">int</span> eval_move(GameState *gamestate, <span class="c2html-keyword">char</span> *mstr, Move *move, <span class="c2html-type">uint8_t</span> color) { -<span class="c2html-lineno"><a name="l607" href="#l607">607 </a></span> memset(move, <span class="c2html-macroconst">0</span>, <span class="c2html-keyword">sizeof</span>(Move)); -<span class="c2html-lineno"><a name="l608" href="#l608">608 </a></span> move->fromfile = <span class="c2html-macroconst">POS_UNSPECIFIED</span>; -<span class="c2html-lineno"><a name="l609" href="#l609">609 </a></span> move->fromrow = <span class="c2html-macroconst">POS_UNSPECIFIED</span>; -<span class="c2html-lineno"><a name="l610" href="#l610">610 </a></span> -<span class="c2html-lineno"><a name="l611" href="#l611">611 </a></span> <span class="c2html-type">size_t</span> len = strlen(mstr); -<span class="c2html-lineno"><a name="l612" href="#l612">612 </a></span> <span class="c2html-keyword">if</span> (len < <span class="c2html-macroconst">1</span> || len > <span class="c2html-macroconst">6</span>) { -<span class="c2html-lineno"><a name="l613" href="#l613">613 </a></span> <span class="c2html-keyword">return</span> <span class="c2html-macroconst">INVALID_MOVE_SYNTAX</span>; -<span class="c2html-lineno"><a name="l614" href="#l614">614 </a></span> } -<span class="c2html-lineno"><a name="l615" href="#l615">615 </a></span> -<span class="c2html-lineno"><a name="l616" href="#l616">616 </a></span> <span class="c2html-comment">/* evaluate check/checkmate flags */</span> -<span class="c2html-lineno"><a name="l617" href="#l617">617 </a></span> <span class="c2html-keyword">if</span> (mstr[len-<span class="c2html-macroconst">1</span>] == <span class="c2html-string">'+'</span>) { -<span class="c2html-lineno"><a name="l618" href="#l618">618 </a></span> len--; mstr[len] = <span class="c2html-string">'\0'</span>; -<span class="c2html-lineno"><a name="l619" href="#l619">619 </a></span> move->check = <span class="c2html-macroconst">1</span>; -<span class="c2html-lineno"><a name="l620" href="#l620">620 </a></span> } <span class="c2html-keyword">else</span> <span class="c2html-keyword">if</span> (mstr[len-<span class="c2html-macroconst">1</span>] == <span class="c2html-string">'#'</span>) { -<span class="c2html-lineno"><a name="l621" href="#l621">621 </a></span> len--; mstr[len] = <span class="c2html-string">'\0'</span>; -<span class="c2html-lineno"><a name="l622" href="#l622">622 </a></span> <span class="c2html-comment">/* ignore - validation should set game state */</span> -<span class="c2html-lineno"><a name="l623" href="#l623">623 </a></span> } -<span class="c2html-lineno"><a name="l624" href="#l624">624 </a></span> -<span class="c2html-lineno"><a name="l625" href="#l625">625 </a></span> <span class="c2html-comment">/* evaluate promotion */</span> -<span class="c2html-lineno"><a name="l626" href="#l626">626 </a></span> <span class="c2html-keyword">if</span> (len > <span class="c2html-macroconst">3</span> && mstr[len-<span class="c2html-macroconst">2</span>] == <span class="c2html-string">'='</span>) { -<span class="c2html-lineno"><a name="l627" href="#l627">627 </a></span> move->promotion = getpiece(mstr[len-<span class="c2html-macroconst">1</span>]); -<span class="c2html-lineno"><a name="l628" href="#l628">628 </a></span> <span class="c2html-keyword">if</span> (!move->promotion) { -<span class="c2html-lineno"><a name="l629" href="#l629">629 </a></span> <span class="c2html-keyword">return</span> <span class="c2html-macroconst">INVALID_MOVE_SYNTAX</span>; -<span class="c2html-lineno"><a name="l630" href="#l630">630 </a></span> } <span class="c2html-keyword">else</span> { -<span class="c2html-lineno"><a name="l631" href="#l631">631 </a></span> move->promotion |= color; -<span class="c2html-lineno"><a name="l632" href="#l632">632 </a></span> len -= <span class="c2html-macroconst">2</span>; -<span class="c2html-lineno"><a name="l633" href="#l633">633 </a></span> mstr[len] = <span class="c2html-macroconst">0</span>; -<span class="c2html-lineno"><a name="l634" href="#l634">634 </a></span> } -<span class="c2html-lineno"><a name="l635" href="#l635">635 </a></span> } -<span class="c2html-lineno"><a name="l636" href="#l636">636 </a></span> -<span class="c2html-lineno"><a name="l637" href="#l637">637 </a></span> <span class="c2html-keyword">if</span> (len == <span class="c2html-macroconst">2</span>) { -<span class="c2html-lineno"><a name="l638" href="#l638">638 </a></span> <span class="c2html-comment">/* pawn move (e.g. "e4") */</span> -<span class="c2html-lineno"><a name="l639" href="#l639">639 </a></span> move->piece = <span class="c2html-macroconst">PAWN</span>; -<span class="c2html-lineno"><a name="l640" href="#l640">640 </a></span> move->tofile = fileidx(mstr[<span class="c2html-macroconst">0</span>]); -<span class="c2html-lineno"><a name="l641" href="#l641">641 </a></span> move->torow = rowidx(mstr[<span class="c2html-macroconst">1</span>]); -<span class="c2html-lineno"><a name="l642" href="#l642">642 </a></span> } <span class="c2html-keyword">else</span> <span class="c2html-keyword">if</span> (len == <span class="c2html-macroconst">3</span>) { -<span class="c2html-lineno"><a name="l643" href="#l643">643 </a></span> <span class="c2html-keyword">if</span> (strcmp(mstr, <span class="c2html-string">"O-O"</span>) == <span class="c2html-macroconst">0</span>) { -<span class="c2html-lineno"><a name="l644" href="#l644">644 </a></span> <span class="c2html-comment">/* king side castling */</span> -<span class="c2html-lineno"><a name="l645" href="#l645">645 </a></span> move->piece = <span class="c2html-macroconst">KING</span>; -<span class="c2html-lineno"><a name="l646" href="#l646">646 </a></span> move->fromfile = fileidx(<span class="c2html-string">'e'</span>); -<span class="c2html-lineno"><a name="l647" href="#l647">647 </a></span> move->tofile = fileidx(<span class="c2html-string">'g'</span>); -<span class="c2html-lineno"><a name="l648" href="#l648">648 </a></span> move->fromrow = move->torow = color == <span class="c2html-macroconst">WHITE</span> ? <span class="c2html-macroconst">0</span> : <span class="c2html-macroconst">7</span>; -<span class="c2html-lineno"><a name="l649" href="#l649">649 </a></span> } <span class="c2html-keyword">else</span> { -<span class="c2html-lineno"><a name="l650" href="#l650">650 </a></span> <span class="c2html-comment">/* move (e.g. "Nf3") */</span> -<span class="c2html-lineno"><a name="l651" href="#l651">651 </a></span> move->piece = getpiece(mstr[<span class="c2html-macroconst">0</span>]); -<span class="c2html-lineno"><a name="l652" href="#l652">652 </a></span> move->tofile = fileidx(mstr[<span class="c2html-macroconst">1</span>]); -<span class="c2html-lineno"><a name="l653" href="#l653">653 </a></span> move->torow = rowidx(mstr[<span class="c2html-macroconst">2</span>]); -<span class="c2html-lineno"><a name="l654" href="#l654">654 </a></span> } -<span class="c2html-lineno"><a name="l655" href="#l655">655 </a></span> } <span class="c2html-keyword">else</span> <span class="c2html-keyword">if</span> (len == <span class="c2html-macroconst">4</span>) { -<span class="c2html-lineno"><a name="l656" href="#l656">656 </a></span> move->piece = getpiece(mstr[<span class="c2html-macroconst">0</span>]); -<span class="c2html-lineno"><a name="l657" href="#l657">657 </a></span> <span class="c2html-keyword">if</span> (!move->piece) { -<span class="c2html-lineno"><a name="l658" href="#l658">658 </a></span> move->piece = <span class="c2html-macroconst">PAWN</span>; -<span class="c2html-lineno"><a name="l659" href="#l659">659 </a></span> move->fromfile = fileidx(mstr[<span class="c2html-macroconst">0</span>]); -<span class="c2html-lineno"><a name="l660" href="#l660">660 </a></span> } -<span class="c2html-lineno"><a name="l661" href="#l661">661 </a></span> <span class="c2html-keyword">if</span> (mstr[<span class="c2html-macroconst">1</span>] == <span class="c2html-string">'x'</span>) { -<span class="c2html-lineno"><a name="l662" href="#l662">662 </a></span> <span class="c2html-comment">/* capture (e.g. "Nxf3", "dxe5") */</span> -<span class="c2html-lineno"><a name="l663" href="#l663">663 </a></span> move->capture = <span class="c2html-macroconst">1</span>; -<span class="c2html-lineno"><a name="l664" href="#l664">664 </a></span> } <span class="c2html-keyword">else</span> { -<span class="c2html-lineno"><a name="l665" href="#l665">665 </a></span> <span class="c2html-comment">/* move (e.g. "Ndf3", "N2c3", "e2e4") */</span> -<span class="c2html-lineno"><a name="l666" href="#l666">666 </a></span> <span class="c2html-keyword">if</span> (isfile(mstr[<span class="c2html-macroconst">1</span>])) { -<span class="c2html-lineno"><a name="l667" href="#l667">667 </a></span> move->fromfile = fileidx(mstr[<span class="c2html-macroconst">1</span>]); -<span class="c2html-lineno"><a name="l668" href="#l668">668 </a></span> <span class="c2html-keyword">if</span> (move->piece == <span class="c2html-macroconst">PAWN</span>) { -<span class="c2html-lineno"><a name="l669" href="#l669">669 </a></span> move->piece = <span class="c2html-macroconst">0</span>; -<span class="c2html-lineno"><a name="l670" href="#l670">670 </a></span> } -<span class="c2html-lineno"><a name="l671" href="#l671">671 </a></span> } <span class="c2html-keyword">else</span> { -<span class="c2html-lineno"><a name="l672" href="#l672">672 </a></span> move->fromrow = rowidx(mstr[<span class="c2html-macroconst">1</span>]); -<span class="c2html-lineno"><a name="l673" href="#l673">673 </a></span> } -<span class="c2html-lineno"><a name="l674" href="#l674">674 </a></span> } -<span class="c2html-lineno"><a name="l675" href="#l675">675 </a></span> move->tofile = fileidx(mstr[<span class="c2html-macroconst">2</span>]); -<span class="c2html-lineno"><a name="l676" href="#l676">676 </a></span> move->torow = rowidx(mstr[<span class="c2html-macroconst">3</span>]); -<span class="c2html-lineno"><a name="l677" href="#l677">677 </a></span> } <span class="c2html-keyword">else</span> <span class="c2html-keyword">if</span> (len == <span class="c2html-macroconst">5</span>) { -<span class="c2html-lineno"><a name="l678" href="#l678">678 </a></span> <span class="c2html-keyword">if</span> (strcmp(mstr, <span class="c2html-string">"O-O-O"</span>) == <span class="c2html-macroconst">0</span>) { -<span class="c2html-lineno"><a name="l679" href="#l679">679 </a></span> <span class="c2html-comment">/* queen side castling "O-O-O" */</span> -<span class="c2html-lineno"><a name="l680" href="#l680">680 </a></span> move->piece = <span class="c2html-macroconst">KING</span>; -<span class="c2html-lineno"><a name="l681" href="#l681">681 </a></span> move->fromfile = fileidx(<span class="c2html-string">'e'</span>); -<span class="c2html-lineno"><a name="l682" href="#l682">682 </a></span> move->tofile = fileidx(<span class="c2html-string">'c'</span>); -<span class="c2html-lineno"><a name="l683" href="#l683">683 </a></span> move->fromrow = move->torow = color == <span class="c2html-macroconst">WHITE</span> ? <span class="c2html-macroconst">0</span> : <span class="c2html-macroconst">7</span>; -<span class="c2html-lineno"><a name="l684" href="#l684">684 </a></span> } <span class="c2html-keyword">else</span> { -<span class="c2html-lineno"><a name="l685" href="#l685">685 </a></span> move->piece = getpiece(mstr[<span class="c2html-macroconst">0</span>]); -<span class="c2html-lineno"><a name="l686" href="#l686">686 </a></span> <span class="c2html-keyword">if</span> (mstr[<span class="c2html-macroconst">2</span>] == <span class="c2html-string">'x'</span>) { -<span class="c2html-lineno"><a name="l687" href="#l687">687 </a></span> move->capture = <span class="c2html-macroconst">1</span>; -<span class="c2html-lineno"><a name="l688" href="#l688">688 </a></span> <span class="c2html-keyword">if</span> (move->piece) { -<span class="c2html-lineno"><a name="l689" href="#l689">689 </a></span> <span class="c2html-comment">/* capture (e.g. "Ndxf3") */</span> -<span class="c2html-lineno"><a name="l690" href="#l690">690 </a></span> move->fromfile = fileidx(mstr[<span class="c2html-macroconst">1</span>]); -<span class="c2html-lineno"><a name="l691" href="#l691">691 </a></span> } <span class="c2html-keyword">else</span> { -<span class="c2html-lineno"><a name="l692" href="#l692">692 </a></span> <span class="c2html-comment">/* long notation capture (e.g. "e5xf6") */</span> -<span class="c2html-lineno"><a name="l693" href="#l693">693 </a></span> move->piece = <span class="c2html-macroconst">PAWN</span>; -<span class="c2html-lineno"><a name="l694" href="#l694">694 </a></span> move->fromfile = fileidx(mstr[<span class="c2html-macroconst">0</span>]); -<span class="c2html-lineno"><a name="l695" href="#l695">695 </a></span> move->fromrow = rowidx(mstr[<span class="c2html-macroconst">1</span>]); -<span class="c2html-lineno"><a name="l696" href="#l696">696 </a></span> } -<span class="c2html-lineno"><a name="l697" href="#l697">697 </a></span> } <span class="c2html-keyword">else</span> { -<span class="c2html-lineno"><a name="l698" href="#l698">698 </a></span> <span class="c2html-comment">/* long notation move (e.g. "Nc5a4") */</span> -<span class="c2html-lineno"><a name="l699" href="#l699">699 </a></span> move->fromfile = fileidx(mstr[<span class="c2html-macroconst">1</span>]); -<span class="c2html-lineno"><a name="l700" href="#l700">700 </a></span> move->fromrow = rowidx(mstr[<span class="c2html-macroconst">2</span>]); -<span class="c2html-lineno"><a name="l701" href="#l701">701 </a></span> } -<span class="c2html-lineno"><a name="l702" href="#l702">702 </a></span> move->tofile = fileidx(mstr[<span class="c2html-macroconst">3</span>]); -<span class="c2html-lineno"><a name="l703" href="#l703">703 </a></span> move->torow = rowidx(mstr[<span class="c2html-macroconst">4</span>]); -<span class="c2html-lineno"><a name="l704" href="#l704">704 </a></span> } -<span class="c2html-lineno"><a name="l705" href="#l705">705 </a></span> } <span class="c2html-keyword">else</span> <span class="c2html-keyword">if</span> (len == <span class="c2html-macroconst">6</span>) { -<span class="c2html-lineno"><a name="l706" href="#l706">706 </a></span> <span class="c2html-comment">/* long notation capture (e.g. "Nc5xf3") */</span> -<span class="c2html-lineno"><a name="l707" href="#l707">707 </a></span> <span class="c2html-keyword">if</span> (mstr[<span class="c2html-macroconst">3</span>] == <span class="c2html-string">'x'</span>) { -<span class="c2html-lineno"><a name="l708" href="#l708">708 </a></span> move->capture = <span class="c2html-macroconst">1</span>; -<span class="c2html-lineno"><a name="l709" href="#l709">709 </a></span> move->piece = getpiece(mstr[<span class="c2html-macroconst">0</span>]); -<span class="c2html-lineno"><a name="l710" href="#l710">710 </a></span> move->fromfile = fileidx(mstr[<span class="c2html-macroconst">1</span>]); -<span class="c2html-lineno"><a name="l711" href="#l711">711 </a></span> move->fromrow = rowidx(mstr[<span class="c2html-macroconst">2</span>]); -<span class="c2html-lineno"><a name="l712" href="#l712">712 </a></span> move->tofile = fileidx(mstr[<span class="c2html-macroconst">4</span>]); -<span class="c2html-lineno"><a name="l713" href="#l713">713 </a></span> move->torow = rowidx(mstr[<span class="c2html-macroconst">5</span>]); -<span class="c2html-lineno"><a name="l714" href="#l714">714 </a></span> } -<span class="c2html-lineno"><a name="l715" href="#l715">715 </a></span> } -<span class="c2html-lineno"><a name="l716" href="#l716">716 </a></span> -<span class="c2html-lineno"><a name="l717" href="#l717">717 </a></span> -<span class="c2html-lineno"><a name="l718" href="#l718">718 </a></span> <span class="c2html-keyword">if</span> (move->piece) { -<span class="c2html-lineno"><a name="l719" href="#l719">719 </a></span> <span class="c2html-keyword">if</span> (move->piece == <span class="c2html-macroconst">PAWN</span> -<span class="c2html-lineno"><a name="l720" href="#l720">720 </a></span> && move->torow == (color==<span class="c2html-macroconst">WHITE</span>?<span class="c2html-macroconst">7</span>:<span class="c2html-macroconst">0</span>) -<span class="c2html-lineno"><a name="l721" href="#l721">721 </a></span> && !move->promotion) { -<span class="c2html-lineno"><a name="l722" href="#l722">722 </a></span> <span class="c2html-keyword">return</span> <span class="c2html-macroconst">NEED_PROMOTION</span>; -<span class="c2html-lineno"><a name="l723" href="#l723">723 </a></span> } -<span class="c2html-lineno"><a name="l724" href="#l724">724 </a></span> -<span class="c2html-lineno"><a name="l725" href="#l725">725 </a></span> move->piece |= color; -<span class="c2html-lineno"><a name="l726" href="#l726">726 </a></span> <span class="c2html-keyword">if</span> (move->fromfile == <span class="c2html-macroconst">POS_UNSPECIFIED</span> -<span class="c2html-lineno"><a name="l727" href="#l727">727 </a></span> || move->fromrow == <span class="c2html-macroconst">POS_UNSPECIFIED</span>) { -<span class="c2html-lineno"><a name="l728" href="#l728">728 </a></span> <span class="c2html-keyword">return</span> getlocation(gamestate, move); -<span class="c2html-lineno"><a name="l729" href="#l729">729 </a></span> } <span class="c2html-keyword">else</span> { -<span class="c2html-lineno"><a name="l730" href="#l730">730 </a></span> <span class="c2html-keyword">return</span> chkidx(move) ? <span class="c2html-macroconst">VALID_MOVE_SYNTAX</span> : <span class="c2html-macroconst">INVALID_POSITION</span>; -<span class="c2html-lineno"><a name="l731" href="#l731">731 </a></span> } -<span class="c2html-lineno"><a name="l732" href="#l732">732 </a></span> } <span class="c2html-keyword">else</span> { -<span class="c2html-lineno"><a name="l733" href="#l733">733 </a></span> <span class="c2html-keyword">return</span> <span class="c2html-macroconst">INVALID_MOVE_SYNTAX</span>; -<span class="c2html-lineno"><a name="l734" href="#l734">734 </a></span> } -<span class="c2html-lineno"><a name="l735" href="#l735">735 </a></span> } -<span class="c2html-lineno"><a name="l736" href="#l736">736 </a></span> -<span class="c2html-lineno"><a name="l737" href="#l737">737 </a></span> _Bool is_protected(GameState *gamestate, <span class="c2html-type">uint8_t</span> row, <span class="c2html-type">uint8_t</span> file, -<span class="c2html-lineno"><a name="l738" href="#l738">738 </a></span> <span class="c2html-type">uint8_t</span> color) { -<span class="c2html-lineno"><a name="l739" href="#l739">739 </a></span> -<span class="c2html-lineno"><a name="l740" href="#l740">740 </a></span> Move threats[<span class="c2html-macroconst">16</span>]; -<span class="c2html-lineno"><a name="l741" href="#l741">741 </a></span> <span class="c2html-type">uint8_t</span> threatcount; -<span class="c2html-lineno"><a name="l742" href="#l742">742 </a></span> <span class="c2html-keyword">if</span> (get_real_threats(gamestate, row, file, color, threats, &threatcount)) { -<span class="c2html-lineno"><a name="l743" href="#l743">743 </a></span> <span class="c2html-keyword">for</span> (<span class="c2html-keyword">int</span> i = <span class="c2html-macroconst">0</span> ; i < threatcount ; i++) { -<span class="c2html-lineno"><a name="l744" href="#l744">744 </a></span> <span class="c2html-keyword">if</span> (threats[i].piece != (color|<span class="c2html-macroconst">KING</span>)) { -<span class="c2html-lineno"><a name="l745" href="#l745">745 </a></span> <span class="c2html-keyword">return</span> <span class="c2html-macroconst">1</span>; -<span class="c2html-lineno"><a name="l746" href="#l746">746 </a></span> } -<span class="c2html-lineno"><a name="l747" href="#l747">747 </a></span> } -<span class="c2html-lineno"><a name="l748" href="#l748">748 </a></span> <span class="c2html-keyword">return</span> <span class="c2html-macroconst">0</span>; -<span class="c2html-lineno"><a name="l749" href="#l749">749 </a></span> } <span class="c2html-keyword">else</span> { -<span class="c2html-lineno"><a name="l750" href="#l750">750 </a></span> <span class="c2html-keyword">return</span> <span class="c2html-macroconst">0</span>; -<span class="c2html-lineno"><a name="l751" href="#l751">751 </a></span> } -<span class="c2html-lineno"><a name="l752" href="#l752">752 </a></span> } -<span class="c2html-lineno"><a name="l753" href="#l753">753 </a></span> -<span class="c2html-lineno"><a name="l754" href="#l754">754 </a></span> <span class="c2html-type">uint16_t</span> remaining_movetime(GameInfo *gameinfo, GameState *gamestate, -<span class="c2html-lineno"><a name="l755" href="#l755">755 </a></span> <span class="c2html-type">uint8_t</span> color) { -<span class="c2html-lineno"><a name="l756" href="#l756">756 </a></span> <span class="c2html-keyword">if</span> (!gameinfo->timecontrol) { -<span class="c2html-lineno"><a name="l757" href="#l757">757 </a></span> <span class="c2html-keyword">return</span> <span class="c2html-macroconst">0</span>; -<span class="c2html-lineno"><a name="l758" href="#l758">758 </a></span> } -<span class="c2html-lineno"><a name="l759" href="#l759">759 </a></span> -<span class="c2html-lineno"><a name="l760" href="#l760">760 </a></span> <span class="c2html-keyword">if</span> (gamestate->movelist) { -<span class="c2html-lineno"><a name="l761" href="#l761">761 </a></span> <span class="c2html-type">uint16_t</span> time = gameinfo->time; -<span class="c2html-lineno"><a name="l762" href="#l762">762 </a></span> <span class="c2html-type">suseconds_t</span> micros = <span class="c2html-macroconst">0</span>; -<span class="c2html-lineno"><a name="l763" href="#l763">763 </a></span> -<span class="c2html-lineno"><a name="l764" href="#l764">764 </a></span> MoveList *movelist = color == <span class="c2html-macroconst">WHITE</span> ? -<span class="c2html-lineno"><a name="l765" href="#l765">765 </a></span> gamestate->movelist : gamestate->movelist->next; -<span class="c2html-lineno"><a name="l766" href="#l766">766 </a></span> -<span class="c2html-lineno"><a name="l767" href="#l767">767 </a></span> <span class="c2html-keyword">while</span> (movelist) { -<span class="c2html-lineno"><a name="l768" href="#l768">768 </a></span> time += gameinfo->addtime; -<span class="c2html-lineno"><a name="l769" href="#l769">769 </a></span> -<span class="c2html-lineno"><a name="l770" href="#l770">770 </a></span> <span class="c2html-keyword">struct</span> movetimeval *movetime = &(movelist->move.movetime); -<span class="c2html-lineno"><a name="l771" href="#l771">771 </a></span> <span class="c2html-keyword">if</span> (movetime->tv_sec >= time) { -<span class="c2html-lineno"><a name="l772" href="#l772">772 </a></span> <span class="c2html-keyword">return</span> <span class="c2html-macroconst">0</span>; -<span class="c2html-lineno"><a name="l773" href="#l773">773 </a></span> } -<span class="c2html-lineno"><a name="l774" href="#l774">774 </a></span> -<span class="c2html-lineno"><a name="l775" href="#l775">775 </a></span> time -= movetime->tv_sec; -<span class="c2html-lineno"><a name="l776" href="#l776">776 </a></span> micros += movetime->tv_usec; -<span class="c2html-lineno"><a name="l777" href="#l777">777 </a></span> -<span class="c2html-lineno"><a name="l778" href="#l778">778 </a></span> movelist = movelist->next ? movelist->next->next : <span class="c2html-macroconst">NULL</span>; -<span class="c2html-lineno"><a name="l779" href="#l779">779 </a></span> } -<span class="c2html-lineno"><a name="l780" href="#l780">780 </a></span> -<span class="c2html-lineno"><a name="l781" href="#l781">781 </a></span> <span class="c2html-type">time_t</span> sec; -<span class="c2html-lineno"><a name="l782" href="#l782">782 </a></span> movelist = gamestate->lastmove; -<span class="c2html-lineno"><a name="l783" href="#l783">783 </a></span> <span class="c2html-keyword">if</span> ((movelist->move.piece & <span class="c2html-macroconst">COLOR_MASK</span>) != color) { -<span class="c2html-lineno"><a name="l784" href="#l784">784 </a></span> <span class="c2html-keyword">struct</span> movetimeval *lastmovetstamp = &(movelist->move.timestamp); -<span class="c2html-lineno"><a name="l785" href="#l785">785 </a></span> <span class="c2html-keyword">struct</span> timeval currenttstamp; -<span class="c2html-lineno"><a name="l786" href="#l786">786 </a></span> gettimeofday(¤ttstamp, <span class="c2html-macroconst">NULL</span>); -<span class="c2html-lineno"><a name="l787" href="#l787">787 </a></span> micros += currenttstamp.tv_usec - lastmovetstamp->tv_usec; -<span class="c2html-lineno"><a name="l788" href="#l788">788 </a></span> sec = currenttstamp.tv_sec - lastmovetstamp->tv_sec; -<span class="c2html-lineno"><a name="l789" href="#l789">789 </a></span> <span class="c2html-keyword">if</span> (sec >= time) { -<span class="c2html-lineno"><a name="l790" href="#l790">790 </a></span> <span class="c2html-keyword">return</span> <span class="c2html-macroconst">0</span>; -<span class="c2html-lineno"><a name="l791" href="#l791">791 </a></span> } -<span class="c2html-lineno"><a name="l792" href="#l792">792 </a></span> -<span class="c2html-lineno"><a name="l793" href="#l793">793 </a></span> time -= sec; -<span class="c2html-lineno"><a name="l794" href="#l794">794 </a></span> } -<span class="c2html-lineno"><a name="l795" href="#l795">795 </a></span> -<span class="c2html-lineno"><a name="l796" href="#l796">796 </a></span> sec = micros / 1e6L; -<span class="c2html-lineno"><a name="l797" href="#l797">797 </a></span> -<span class="c2html-lineno"><a name="l798" href="#l798">798 </a></span> <span class="c2html-keyword">if</span> (sec >= time) { -<span class="c2html-lineno"><a name="l799" href="#l799">799 </a></span> <span class="c2html-keyword">return</span> <span class="c2html-macroconst">0</span>; -<span class="c2html-lineno"><a name="l800" href="#l800">800 </a></span> } -<span class="c2html-lineno"><a name="l801" href="#l801">801 </a></span> -<span class="c2html-lineno"><a name="l802" href="#l802">802 </a></span> time -= sec; -<span class="c2html-lineno"><a name="l803" href="#l803">803 </a></span> -<span class="c2html-lineno"><a name="l804" href="#l804">804 </a></span> <span class="c2html-keyword">return</span> time; -<span class="c2html-lineno"><a name="l805" href="#l805">805 </a></span> } <span class="c2html-keyword">else</span> { -<span class="c2html-lineno"><a name="l806" href="#l806">806 </a></span> <span class="c2html-keyword">return</span> gameinfo->time; -<span class="c2html-lineno"><a name="l807" href="#l807">807 </a></span> } -<span class="c2html-lineno"><a name="l808" href="#l808">808 </a></span> } -</pre> - </body> -</html> -
--- a/test/v2-regression/ctest.html Thu Nov 10 18:44:48 2016 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,438 +0,0 @@ -<!DOCTYPE html> -<html> - <head> - <title>c2html</title> - <style type="text/css"> - span.c2html-lineno a { - font-style: italic; - text-decoration: none; - color: grey; - } - span.c2html-keyword { - color: blue; - } - span.c2html-macroconst { - color: cornflowerblue; - } - span.c2html-type { - color: cornflowerblue; - } - span.c2html-directive { - color: green; - } - span.c2html-string { - color: darkorange; - } - span.c2html-comment { - color: grey; - } - span.c2html-stdinclude { - color: darkorange; - } - span.c2html-userinclude { - color: darkorange; - } - a.c2html-userinclude { - color: darkorange; - text-decoration: underline; - } - </style> - </head> - <body> - -<pre> -<span class="c2html-lineno"><a name="l1" href="#l1"> 1 </a></span> <span class="c2html-comment">/*</span> -<span class="c2html-lineno"><a name="l2" href="#l2"> 2 </a></span> <span class="c2html-comment"> * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.</span> -<span class="c2html-lineno"><a name="l3" href="#l3"> 3 </a></span> <span class="c2html-comment"> *</span> -<span class="c2html-lineno"><a name="l4" href="#l4"> 4 </a></span> <span class="c2html-comment"> * Copyright 2015 Olaf Wintermann. All rights reserved.</span> -<span class="c2html-lineno"><a name="l5" href="#l5"> 5 </a></span> <span class="c2html-comment"> *</span> -<span class="c2html-lineno"><a name="l6" href="#l6"> 6 </a></span> <span class="c2html-comment"> * Redistribution and use in source and binary forms, with or without</span> -<span class="c2html-lineno"><a name="l7" href="#l7"> 7 </a></span> <span class="c2html-comment"> * modification, are permitted provided that the following conditions are met:</span> -<span class="c2html-lineno"><a name="l8" href="#l8"> 8 </a></span> <span class="c2html-comment"> *</span> -<span class="c2html-lineno"><a name="l9" href="#l9"> 9 </a></span> <span class="c2html-comment"> * 1. Redistributions of source code must retain the above copyright</span> -<span class="c2html-lineno"><a name="l10" href="#l10"> 10 </a></span> <span class="c2html-comment"> * notice, this list of conditions and the following disclaimer.</span> -<span class="c2html-lineno"><a name="l11" href="#l11"> 11 </a></span> <span class="c2html-comment"> *</span> -<span class="c2html-lineno"><a name="l12" href="#l12"> 12 </a></span> <span class="c2html-comment"> * 2. Redistributions in binary form must reproduce the above copyright</span> -<span class="c2html-lineno"><a name="l13" href="#l13"> 13 </a></span> <span class="c2html-comment"> * notice, this list of conditions and the following disclaimer in the</span> -<span class="c2html-lineno"><a name="l14" href="#l14"> 14 </a></span> <span class="c2html-comment"> * documentation and/or other materials provided with the distribution.</span> -<span class="c2html-lineno"><a name="l15" href="#l15"> 15 </a></span> <span class="c2html-comment"> *</span> -<span class="c2html-lineno"><a name="l16" href="#l16"> 16 </a></span> <span class="c2html-comment"> * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"</span> -<span class="c2html-lineno"><a name="l17" href="#l17"> 17 </a></span> <span class="c2html-comment"> * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE</span> -<span class="c2html-lineno"><a name="l18" href="#l18"> 18 </a></span> <span class="c2html-comment"> * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE</span> -<span class="c2html-lineno"><a name="l19" href="#l19"> 19 </a></span> <span class="c2html-comment"> * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE</span> -<span class="c2html-lineno"><a name="l20" href="#l20"> 20 </a></span> <span class="c2html-comment"> * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR</span> -<span class="c2html-lineno"><a name="l21" href="#l21"> 21 </a></span> <span class="c2html-comment"> * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF</span> -<span class="c2html-lineno"><a name="l22" href="#l22"> 22 </a></span> <span class="c2html-comment"> * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS</span> -<span class="c2html-lineno"><a name="l23" href="#l23"> 23 </a></span> <span class="c2html-comment"> * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN</span> -<span class="c2html-lineno"><a name="l24" href="#l24"> 24 </a></span> <span class="c2html-comment"> * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)</span> -<span class="c2html-lineno"><a name="l25" href="#l25"> 25 </a></span> <span class="c2html-comment"> * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE</span> -<span class="c2html-lineno"><a name="l26" href="#l26"> 26 </a></span> <span class="c2html-comment"> * POSSIBILITY OF SUCH DAMAGE.</span> -<span class="c2html-lineno"><a name="l27" href="#l27"> 27 </a></span> <span class="c2html-comment"> */</span> -<span class="c2html-lineno"><a name="l28" href="#l28"> 28 </a></span> -<span class="c2html-lineno"><a name="l29" href="#l29"> 29 </a></span> <span class="c2html-directive">#include</span> <span class="c2html-stdinclude"><time.h></span> -<span class="c2html-lineno"><a name="l30" href="#l30"> 30 </a></span> <span class="c2html-directive">#include</span> <span class="c2html-stdinclude"><stdio.h></span> -<span class="c2html-lineno"><a name="l31" href="#l31"> 31 </a></span> <span class="c2html-directive">#include</span> <span class="c2html-stdinclude"><stdlib.h></span> -<span class="c2html-lineno"><a name="l32" href="#l32"> 32 </a></span> <span class="c2html-directive">#include</span> <span class="c2html-stdinclude"><string.h></span> -<span class="c2html-lineno"><a name="l33" href="#l33"> 33 </a></span> <span class="c2html-directive">#include</span> <span class="c2html-stdinclude"><ucx/string.h></span> -<span class="c2html-lineno"><a name="l34" href="#l34"> 34 </a></span> <span class="c2html-directive">#include</span> <span class="c2html-stdinclude"><ucx/buffer.h></span> -<span class="c2html-lineno"><a name="l35" href="#l35"> 35 </a></span> <span class="c2html-directive">#include</span> <span class="c2html-stdinclude"><ucx/utils.h></span> -<span class="c2html-lineno"><a name="l36" href="#l36"> 36 </a></span> <span class="c2html-directive">#include</span> <span class="c2html-stdinclude"><libxml/tree.h></span> -<span class="c2html-lineno"><a name="l37" href="#l37"> 37 </a></span> <span class="c2html-directive">#include</span> <span class="c2html-stdinclude"><curl/curl.h></span> -<span class="c2html-lineno"><a name="l38" href="#l38"> 38 </a></span> -<span class="c2html-lineno"><a name="l39" href="#l39"> 39 </a></span> <span class="c2html-directive">#include</span> <span class="c2html-stdinclude"><openssl/sha.h></span> -<span class="c2html-lineno"><a name="l40" href="#l40"> 40 </a></span> <span class="c2html-directive">#include</span> <span class="c2html-stdinclude"><openssl/hmac.h></span> -<span class="c2html-lineno"><a name="l41" href="#l41"> 41 </a></span> <span class="c2html-directive">#include</span> <span class="c2html-stdinclude"><openssl/evp.h></span> -<span class="c2html-lineno"><a name="l42" href="#l42"> 42 </a></span> <span class="c2html-directive">#include</span> <span class="c2html-stdinclude"><openssl/bio.h></span> -<span class="c2html-lineno"><a name="l43" href="#l43"> 43 </a></span> <span class="c2html-directive">#include</span> <span class="c2html-stdinclude"><openssl/buffer.h></span> -<span class="c2html-lineno"><a name="l44" href="#l44"> 44 </a></span> <span class="c2html-directive">#include</span> <span class="c2html-stdinclude"><openssl/rand.h></span> -<span class="c2html-lineno"><a name="l45" href="#l45"> 45 </a></span> -<span class="c2html-lineno"><a name="l46" href="#l46"> 46 </a></span> <span class="c2html-directive">#include</span> <a class="c2html-userinclude" href="utils.h">"utils.h"</a> -<span class="c2html-lineno"><a name="l47" href="#l47"> 47 </a></span> <span class="c2html-directive">#include</span> <a class="c2html-userinclude" href="crypto.h">"crypto.h"</a> -<span class="c2html-lineno"><a name="l48" href="#l48"> 48 </a></span> <span class="c2html-directive">#include</span> <a class="c2html-userinclude" href="webdav.h">"webdav.h"</a> -<span class="c2html-lineno"><a name="l49" href="#l49"> 49 </a></span> -<span class="c2html-lineno"><a name="l50" href="#l50"> 50 </a></span> <span class="c2html-directive">#define</span> <span class="c2html-macroconst">MACRO1337</span> <span class="c2html-macroconst">1337L</span> -<span class="c2html-lineno"><a name="l51" href="#l51"> 51 </a></span> -<span class="c2html-lineno"><a name="l52" href="#l52"> 52 </a></span> <span class="c2html-comment">/* -------------------- This is a testing file. -------------------------- */</span> -<span class="c2html-lineno"><a name="l53" href="#l53"> 53 </a></span> <span class="c2html-comment">/*</span> -<span class="c2html-lineno"><a name="l54" href="#l54"> 54 </a></span> <span class="c2html-comment">time_t util_parse_creationdate(char *str) {</span> -<span class="c2html-lineno"><a name="l55" href="#l55"> 55 </a></span> <span class="c2html-comment"> // example: 2012-11-29T21:35:35Z</span> -<span class="c2html-lineno"><a name="l56" href="#l56"> 56 </a></span> <span class="c2html-comment"> if(!str) {</span> -<span class="c2html-lineno"><a name="l57" href="#l57"> 57 </a></span> <span class="c2html-comment"> return 0;</span> -<span class="c2html-lineno"><a name="l58" href="#l58"> 58 </a></span> <span class="c2html-comment"> }</span> -<span class="c2html-lineno"><a name="l59" href="#l59"> 59 </a></span> <span class="c2html-comment"> // TODO</span> -<span class="c2html-lineno"><a name="l60" href="#l60"> 60 </a></span> <span class="c2html-comment"> return 0;</span> -<span class="c2html-lineno"><a name="l61" href="#l61"> 61 </a></span> <span class="c2html-comment">}</span> -<span class="c2html-lineno"><a name="l62" href="#l62"> 62 </a></span> <span class="c2html-comment">*/</span> -<span class="c2html-lineno"><a name="l63" href="#l63"> 63 </a></span> <span class="c2html-type">time_t</span> util_parse_lastmodified(<span class="c2html-keyword">char</span> *str) { -<span class="c2html-lineno"><a name="l64" href="#l64"> 64 </a></span> <span class="c2html-comment">// example: Thu, 29 Nov 2012 21:35:35 GMT</span> -<span class="c2html-lineno"><a name="l65" href="#l65"> 65 </a></span> <span class="c2html-keyword">if</span>(!str) { -<span class="c2html-lineno"><a name="l66" href="#l66"> 66 </a></span> <span class="c2html-keyword">return</span> <span class="c2html-macroconst">0</span>; -<span class="c2html-lineno"><a name="l67" href="#l67"> 67 </a></span> } <span class="c2html-keyword">else</span> { -<span class="c2html-lineno"><a name="l68" href="#l68"> 68 </a></span> <span class="c2html-keyword">return</span> curl_getdate(str, <span class="c2html-macroconst">NULL</span>); -<span class="c2html-lineno"><a name="l69" href="#l69"> 69 </a></span> } -<span class="c2html-lineno"><a name="l70" href="#l70"> 70 </a></span> } -<span class="c2html-lineno"><a name="l71" href="#l71"> 71 </a></span> -<span class="c2html-lineno"><a name="l72" href="#l72"> 72 </a></span> <span class="c2html-keyword">int</span> util_getboolean(<span class="c2html-keyword">char</span> *v) { -<span class="c2html-lineno"><a name="l73" href="#l73"> 73 </a></span> <span class="c2html-keyword">if</span>(v[<span class="c2html-macroconst">0</span>] == <span class="c2html-string">'T'</span> || v[<span class="c2html-macroconst">0</span>] == <span class="c2html-string">'t'</span>) { -<span class="c2html-lineno"><a name="l74" href="#l74"> 74 </a></span> <span class="c2html-keyword">return</span> <span class="c2html-macroconst">1</span>; -<span class="c2html-lineno"><a name="l75" href="#l75"> 75 </a></span> } -<span class="c2html-lineno"><a name="l76" href="#l76"> 76 </a></span> <span class="c2html-keyword">return</span> <span class="c2html-macroconst">0</span>; -<span class="c2html-lineno"><a name="l77" href="#l77"> 77 </a></span> } -<span class="c2html-lineno"><a name="l78" href="#l78"> 78 </a></span> -<span class="c2html-lineno"><a name="l79" href="#l79"> 79 </a></span> <span class="c2html-keyword">int</span> util_strtoint(<span class="c2html-keyword">char</span> *str, <span class="c2html-type">int64_t</span> *value) { -<span class="c2html-lineno"><a name="l80" href="#l80"> 80 </a></span> <span class="c2html-keyword">char</span> *end; -<span class="c2html-lineno"><a name="l81" href="#l81"> 81 </a></span> <span class="c2html-type">int64_t</span> val = strtoll(str, &end, <span class="c2html-macroconst">0</span>); -<span class="c2html-lineno"><a name="l82" href="#l82"> 82 </a></span> <span class="c2html-keyword">if</span>(strlen(end) == <span class="c2html-macroconst">0</span>) { -<span class="c2html-lineno"><a name="l83" href="#l83"> 83 </a></span> *value = val; -<span class="c2html-lineno"><a name="l84" href="#l84"> 84 </a></span> <span class="c2html-keyword">return</span> <span class="c2html-macroconst">1</span>; -<span class="c2html-lineno"><a name="l85" href="#l85"> 85 </a></span> } <span class="c2html-keyword">else</span> { -<span class="c2html-lineno"><a name="l86" href="#l86"> 86 </a></span> <span class="c2html-keyword">return</span> <span class="c2html-macroconst">0</span>; -<span class="c2html-lineno"><a name="l87" href="#l87"> 87 </a></span> } -<span class="c2html-lineno"><a name="l88" href="#l88"> 88 </a></span> } -<span class="c2html-lineno"><a name="l89" href="#l89"> 89 </a></span> -<span class="c2html-lineno"><a name="l90" href="#l90"> 90 </a></span> <span class="c2html-keyword">char</span>* util_url_path(<span class="c2html-keyword">char</span> *url) { -<span class="c2html-lineno"><a name="l91" href="#l91"> 91 </a></span> <span class="c2html-keyword">char</span> *path = <span class="c2html-macroconst">NULL</span>; -<span class="c2html-lineno"><a name="l92" href="#l92"> 92 </a></span> <span class="c2html-type">size_t</span> len = strlen(url); -<span class="c2html-lineno"><a name="l93" href="#l93"> 93 </a></span> <span class="c2html-keyword">int</span> slashcount = <span class="c2html-macroconst">0</span>; -<span class="c2html-lineno"><a name="l94" href="#l94"> 94 </a></span> <span class="c2html-keyword">int</span> slmax; -<span class="c2html-lineno"><a name="l95" href="#l95"> 95 </a></span> <span class="c2html-keyword">if</span>(len > <span class="c2html-macroconst">7</span> && !strncasecmp(url, <span class="c2html-string">"http://"</span>, <span class="c2html-macroconst">7</span>)) { -<span class="c2html-lineno"><a name="l96" href="#l96"> 96 </a></span> slmax = <span class="c2html-macroconst">3</span>; -<span class="c2html-lineno"><a name="l97" href="#l97"> 97 </a></span> } <span class="c2html-keyword">else</span> <span class="c2html-keyword">if</span>(len > <span class="c2html-macroconst">8</span> && !strncasecmp(url, <span class="c2html-string">"https://"</span>, <span class="c2html-macroconst">8</span>)) { -<span class="c2html-lineno"><a name="l98" href="#l98"> 98 </a></span> slmax = <span class="c2html-macroconst">3</span>; -<span class="c2html-lineno"><a name="l99" href="#l99"> 99 </a></span> } <span class="c2html-keyword">else</span> { -<span class="c2html-lineno"><a name="l100" href="#l100">100 </a></span> slmax = <span class="c2html-macroconst">1</span>; -<span class="c2html-lineno"><a name="l101" href="#l101">101 </a></span> } -<span class="c2html-lineno"><a name="l102" href="#l102">102 </a></span> <span class="c2html-keyword">char</span> c; -<span class="c2html-lineno"><a name="l103" href="#l103">103 </a></span> <span class="c2html-keyword">for</span>(<span class="c2html-keyword">int</span> i=<span class="c2html-macroconst">0</span>;i<len;i++) { -<span class="c2html-lineno"><a name="l104" href="#l104">104 </a></span> c = url[i]; -<span class="c2html-lineno"><a name="l105" href="#l105">105 </a></span> <span class="c2html-keyword">if</span>(c == <span class="c2html-string">'/'</span>) { -<span class="c2html-lineno"><a name="l106" href="#l106">106 </a></span> slashcount++; -<span class="c2html-lineno"><a name="l107" href="#l107">107 </a></span> <span class="c2html-keyword">if</span>(slashcount == slmax) { -<span class="c2html-lineno"><a name="l108" href="#l108">108 </a></span> path = url + i; -<span class="c2html-lineno"><a name="l109" href="#l109">109 </a></span> <span class="c2html-keyword">break</span>; -<span class="c2html-lineno"><a name="l110" href="#l110">110 </a></span> } -<span class="c2html-lineno"><a name="l111" href="#l111">111 </a></span> } -<span class="c2html-lineno"><a name="l112" href="#l112">112 </a></span> } -<span class="c2html-lineno"><a name="l113" href="#l113">113 </a></span> <span class="c2html-keyword">return</span> path; -<span class="c2html-lineno"><a name="l114" href="#l114">114 </a></span> } -<span class="c2html-lineno"><a name="l115" href="#l115">115 </a></span> -<span class="c2html-lineno"><a name="l116" href="#l116">116 </a></span> <span class="c2html-keyword">char</span>* util_url_decode(DavSession *sn, <span class="c2html-keyword">char</span> *url) { -<span class="c2html-lineno"><a name="l117" href="#l117">117 </a></span> <span class="c2html-keyword">char</span> *unesc = curl_easy_unescape(sn->handle, url, strlen(url), <span class="c2html-macroconst">NULL</span>); -<span class="c2html-lineno"><a name="l118" href="#l118">118 </a></span> <span class="c2html-keyword">char</span> *ret = strdup(unesc); -<span class="c2html-lineno"><a name="l119" href="#l119">119 </a></span> curl_free(unesc); -<span class="c2html-lineno"><a name="l120" href="#l120">120 </a></span> <span class="c2html-keyword">return</span> ret; -<span class="c2html-lineno"><a name="l121" href="#l121">121 </a></span> } -<span class="c2html-lineno"><a name="l122" href="#l122">122 </a></span> -<span class="c2html-lineno"><a name="l123" href="#l123">123 </a></span> <span class="c2html-keyword">char</span>* util_resource_name(<span class="c2html-keyword">char</span> *url) { -<span class="c2html-lineno"><a name="l124" href="#l124">124 </a></span> <span class="c2html-keyword">int</span> si = <span class="c2html-macroconst">0</span>; -<span class="c2html-lineno"><a name="l125" href="#l125">125 </a></span> <span class="c2html-keyword">int</span> osi = <span class="c2html-macroconst">0</span>; -<span class="c2html-lineno"><a name="l126" href="#l126">126 </a></span> <span class="c2html-keyword">int</span> i = <span class="c2html-macroconst">0</span>; -<span class="c2html-lineno"><a name="l127" href="#l127">127 </a></span> <span class="c2html-keyword">int</span> p = <span class="c2html-macroconst">0</span>; -<span class="c2html-lineno"><a name="l128" href="#l128">128 </a></span> <span class="c2html-keyword">char</span> c; -<span class="c2html-lineno"><a name="l129" href="#l129">129 </a></span> <span class="c2html-keyword">while</span>((c = url[i]) != <span class="c2html-macroconst">0</span>) { -<span class="c2html-lineno"><a name="l130" href="#l130">130 </a></span> <span class="c2html-keyword">if</span>(c == <span class="c2html-string">'/'</span>) { -<span class="c2html-lineno"><a name="l131" href="#l131">131 </a></span> osi = si; -<span class="c2html-lineno"><a name="l132" href="#l132">132 </a></span> si = i; -<span class="c2html-lineno"><a name="l133" href="#l133">133 </a></span> p = <span class="c2html-macroconst">1</span>; -<span class="c2html-lineno"><a name="l134" href="#l134">134 </a></span> } -<span class="c2html-lineno"><a name="l135" href="#l135">135 </a></span> i++; -<span class="c2html-lineno"><a name="l136" href="#l136">136 </a></span> } -<span class="c2html-lineno"><a name="l137" href="#l137">137 </a></span> -<span class="c2html-lineno"><a name="l138" href="#l138">138 </a></span> <span class="c2html-keyword">char</span> *name = url + si + p; -<span class="c2html-lineno"><a name="l139" href="#l139">139 </a></span> <span class="c2html-keyword">if</span>(name[<span class="c2html-macroconst">0</span>] == <span class="c2html-macroconst">0</span>) { -<span class="c2html-lineno"><a name="l140" href="#l140">140 </a></span> name = url + osi + p; -<span class="c2html-lineno"><a name="l141" href="#l141">141 </a></span> <span class="c2html-keyword">if</span>(name[<span class="c2html-macroconst">0</span>] == <span class="c2html-macroconst">0</span>) { -<span class="c2html-lineno"><a name="l142" href="#l142">142 </a></span> <span class="c2html-keyword">return</span> url; -<span class="c2html-lineno"><a name="l143" href="#l143">143 </a></span> } -<span class="c2html-lineno"><a name="l144" href="#l144">144 </a></span> } -<span class="c2html-lineno"><a name="l145" href="#l145">145 </a></span> -<span class="c2html-lineno"><a name="l146" href="#l146">146 </a></span> <span class="c2html-keyword">return</span> name; -<span class="c2html-lineno"><a name="l147" href="#l147">147 </a></span> } -<span class="c2html-lineno"><a name="l148" href="#l148">148 </a></span> -<span class="c2html-lineno"><a name="l149" href="#l149">149 </a></span> <span class="c2html-keyword">int</span> util_mkdir(<span class="c2html-keyword">char</span> *path, <span class="c2html-type">mode_t</span> mode) { -<span class="c2html-lineno"><a name="l150" href="#l150">150 </a></span> <span class="c2html-directive">#ifdef</span> <span class="c2html-macroconst">_WIN32</span> -<span class="c2html-lineno"><a name="l151" href="#l151">151 </a></span> <span class="c2html-keyword">return</span> mkdir(path); -<span class="c2html-lineno"><a name="l152" href="#l152">152 </a></span> <span class="c2html-directive">#else</span> -<span class="c2html-lineno"><a name="l153" href="#l153">153 </a></span> <span class="c2html-keyword">return</span> mkdir(path, mode); -<span class="c2html-lineno"><a name="l154" href="#l154">154 </a></span> <span class="c2html-directive">#endif</span> -<span class="c2html-lineno"><a name="l155" href="#l155">155 </a></span> } -<span class="c2html-lineno"><a name="l156" href="#l156">156 </a></span> -<span class="c2html-lineno"><a name="l157" href="#l157">157 </a></span> <span class="c2html-keyword">char</span>* util_concat_path(<span class="c2html-keyword">char</span> *url_base, <span class="c2html-keyword">char</span> *p) { -<span class="c2html-lineno"><a name="l158" href="#l158">158 </a></span> <span class="c2html-type">sstr_t</span> base = sstr(url_base); -<span class="c2html-lineno"><a name="l159" href="#l159">159 </a></span> <span class="c2html-type">sstr_t</span> path; -<span class="c2html-lineno"><a name="l160" href="#l160">160 </a></span> <span class="c2html-keyword">if</span>(p) { -<span class="c2html-lineno"><a name="l161" href="#l161">161 </a></span> path = sstr(p); -<span class="c2html-lineno"><a name="l162" href="#l162">162 </a></span> } <span class="c2html-keyword">else</span> { -<span class="c2html-lineno"><a name="l163" href="#l163">163 </a></span> path = sstrn(<span class="c2html-string">""</span>, <span class="c2html-macroconst">0</span>); -<span class="c2html-lineno"><a name="l164" href="#l164">164 </a></span> } -<span class="c2html-lineno"><a name="l165" href="#l165">165 </a></span> -<span class="c2html-lineno"><a name="l166" href="#l166">166 </a></span> <span class="c2html-keyword">int</span> add_separator = <span class="c2html-macroconst">0</span>; -<span class="c2html-lineno"><a name="l167" href="#l167">167 </a></span> <span class="c2html-keyword">if</span>(base.ptr[base.length-<span class="c2html-macroconst">1</span>] == <span class="c2html-string">'/'</span>) { -<span class="c2html-lineno"><a name="l168" href="#l168">168 </a></span> <span class="c2html-keyword">if</span>(path.ptr[<span class="c2html-macroconst">0</span>] == <span class="c2html-string">'/'</span>) { -<span class="c2html-lineno"><a name="l169" href="#l169">169 </a></span> base.length--; -<span class="c2html-lineno"><a name="l170" href="#l170">170 </a></span> } -<span class="c2html-lineno"><a name="l171" href="#l171">171 </a></span> } <span class="c2html-keyword">else</span> { -<span class="c2html-lineno"><a name="l172" href="#l172">172 </a></span> <span class="c2html-keyword">if</span>(path.length == <span class="c2html-macroconst">0</span> || path.ptr[<span class="c2html-macroconst">0</span>] != <span class="c2html-string">'/'</span>) { -<span class="c2html-lineno"><a name="l173" href="#l173">173 </a></span> add_separator = <span class="c2html-macroconst">1</span>; -<span class="c2html-lineno"><a name="l174" href="#l174">174 </a></span> } -<span class="c2html-lineno"><a name="l175" href="#l175">175 </a></span> } -<span class="c2html-lineno"><a name="l176" href="#l176">176 </a></span> -<span class="c2html-lineno"><a name="l177" href="#l177">177 </a></span> <span class="c2html-type">sstr_t</span> url; -<span class="c2html-lineno"><a name="l178" href="#l178">178 </a></span> <span class="c2html-keyword">if</span>(add_separator) { -<span class="c2html-lineno"><a name="l179" href="#l179">179 </a></span> url = sstrcat(<span class="c2html-macroconst">3</span>, base, sstr(<span class="c2html-string">"/"</span>), path); -<span class="c2html-lineno"><a name="l180" href="#l180">180 </a></span> } <span class="c2html-keyword">else</span> { -<span class="c2html-lineno"><a name="l181" href="#l181">181 </a></span> url = sstrcat(<span class="c2html-macroconst">2</span>, base, path); -<span class="c2html-lineno"><a name="l182" href="#l182">182 </a></span> } -<span class="c2html-lineno"><a name="l183" href="#l183">183 </a></span> -<span class="c2html-lineno"><a name="l184" href="#l184">184 </a></span> <span class="c2html-keyword">return</span> url.ptr; -<span class="c2html-lineno"><a name="l185" href="#l185">185 </a></span> } -<span class="c2html-lineno"><a name="l186" href="#l186">186 </a></span> -<span class="c2html-lineno"><a name="l187" href="#l187">187 </a></span> <span class="c2html-keyword">void</span> util_set_url(DavSession *sn, <span class="c2html-keyword">char</span> *href) { -<span class="c2html-lineno"><a name="l188" href="#l188">188 </a></span> <span class="c2html-type">sstr_t</span> base = sstr(sn->base_url); -<span class="c2html-lineno"><a name="l189" href="#l189">189 </a></span> <span class="c2html-type">sstr_t</span> href_str = sstr(href); -<span class="c2html-lineno"><a name="l190" href="#l190">190 </a></span> -<span class="c2html-lineno"><a name="l191" href="#l191">191 </a></span> <span class="c2html-keyword">char</span> *base_path = util_url_path(sn->base_url); -<span class="c2html-lineno"><a name="l192" href="#l192">192 </a></span> base.length -= strlen(base_path); -<span class="c2html-lineno"><a name="l193" href="#l193">193 </a></span> -<span class="c2html-lineno"><a name="l194" href="#l194">194 </a></span> <span class="c2html-type">sstr_t</span> url = sstrcat(<span class="c2html-macroconst">2</span>, base, href_str); -<span class="c2html-lineno"><a name="l195" href="#l195">195 </a></span> -<span class="c2html-lineno"><a name="l196" href="#l196">196 </a></span> curl_easy_setopt(sn->handle, <span class="c2html-macroconst">CURLOPT_URL</span>, url.ptr); -<span class="c2html-lineno"><a name="l197" href="#l197">197 </a></span> free(url.ptr); -<span class="c2html-lineno"><a name="l198" href="#l198">198 </a></span> } -<span class="c2html-lineno"><a name="l199" href="#l199">199 </a></span> -<span class="c2html-lineno"><a name="l200" href="#l200">200 </a></span> <span class="c2html-keyword">char</span>* util_path_to_url(DavSession *sn, <span class="c2html-keyword">char</span> *path) { -<span class="c2html-lineno"><a name="l201" href="#l201">201 </a></span> <span class="c2html-keyword">char</span> *space = malloc(<span class="c2html-macroconst">256</span>); -<span class="c2html-lineno"><a name="l202" href="#l202">202 </a></span> UcxBuffer *url = ucx_buffer_new(space, <span class="c2html-macroconst">256</span>, <span class="c2html-macroconst">UCX_BUFFER_AUTOEXTEND</span>); -<span class="c2html-lineno"><a name="l203" href="#l203">203 </a></span> -<span class="c2html-lineno"><a name="l204" href="#l204">204 </a></span> <span class="c2html-comment">// add base url</span> -<span class="c2html-lineno"><a name="l205" href="#l205">205 </a></span> ucx_buffer_write(sn->base_url, <span class="c2html-macroconst">1</span>, strlen(sn->base_url), url); -<span class="c2html-lineno"><a name="l206" href="#l206">206 </a></span> <span class="c2html-comment">// remove trailing slash</span> -<span class="c2html-lineno"><a name="l207" href="#l207">207 </a></span> ucx_buffer_seek(url, -<span class="c2html-macroconst">1</span>, <span class="c2html-macroconst">SEEK_CUR</span>); -<span class="c2html-lineno"><a name="l208" href="#l208">208 </a></span> -<span class="c2html-lineno"><a name="l209" href="#l209">209 </a></span> <span class="c2html-type">sstr_t</span> p = sstr(path); -<span class="c2html-lineno"><a name="l210" href="#l210">210 </a></span> <span class="c2html-type">ssize_t</span> ntk = <span class="c2html-macroconst">0</span>; -<span class="c2html-lineno"><a name="l211" href="#l211">211 </a></span> <span class="c2html-type">sstr_t</span> *tks = sstrsplit(p, <span class="c2html-macroconst">S</span>(<span class="c2html-string">"/"</span>), &ntk); -<span class="c2html-lineno"><a name="l212" href="#l212">212 </a></span> -<span class="c2html-lineno"><a name="l213" href="#l213">213 </a></span> <span class="c2html-keyword">for</span>(<span class="c2html-keyword">int</span> i=<span class="c2html-macroconst">0</span>;i<ntk;i++) { -<span class="c2html-lineno"><a name="l214" href="#l214">214 </a></span> <span class="c2html-type">sstr_t</span> node = tks[i]; -<span class="c2html-lineno"><a name="l215" href="#l215">215 </a></span> <span class="c2html-keyword">if</span>(node.length > <span class="c2html-macroconst">0</span>) { -<span class="c2html-lineno"><a name="l216" href="#l216">216 </a></span> <span class="c2html-keyword">char</span> *esc = curl_easy_escape(sn->handle, node.ptr, node.length); -<span class="c2html-lineno"><a name="l217" href="#l217">217 </a></span> ucx_buffer_putc(url, <span class="c2html-string">'/'</span>); -<span class="c2html-lineno"><a name="l218" href="#l218">218 </a></span> ucx_buffer_write(esc, <span class="c2html-macroconst">1</span>, strlen(esc), url); -<span class="c2html-lineno"><a name="l219" href="#l219">219 </a></span> curl_free(esc); -<span class="c2html-lineno"><a name="l220" href="#l220">220 </a></span> } -<span class="c2html-lineno"><a name="l221" href="#l221">221 </a></span> free(node.ptr); -<span class="c2html-lineno"><a name="l222" href="#l222">222 </a></span> } -<span class="c2html-lineno"><a name="l223" href="#l223">223 </a></span> free(tks); -<span class="c2html-lineno"><a name="l224" href="#l224">224 </a></span> <span class="c2html-keyword">if</span>(path[p.length-<span class="c2html-macroconst">1</span>] == <span class="c2html-string">'/'</span>) { -<span class="c2html-lineno"><a name="l225" href="#l225">225 </a></span> ucx_buffer_putc(url, <span class="c2html-string">'/'</span>); -<span class="c2html-lineno"><a name="l226" href="#l226">226 </a></span> } -<span class="c2html-lineno"><a name="l227" href="#l227">227 </a></span> ucx_buffer_putc(url, <span class="c2html-macroconst">0</span>); -<span class="c2html-lineno"><a name="l228" href="#l228">228 </a></span> -<span class="c2html-lineno"><a name="l229" href="#l229">229 </a></span> space = url->space; -<span class="c2html-lineno"><a name="l230" href="#l230">230 </a></span> ucx_buffer_free(url); -<span class="c2html-lineno"><a name="l231" href="#l231">231 </a></span> -<span class="c2html-lineno"><a name="l232" href="#l232">232 </a></span> <span class="c2html-keyword">return</span> space; -<span class="c2html-lineno"><a name="l233" href="#l233">233 </a></span> } -<span class="c2html-lineno"><a name="l234" href="#l234">234 </a></span> -<span class="c2html-lineno"><a name="l235" href="#l235">235 </a></span> <span class="c2html-keyword">char</span>* util_parent_path(<span class="c2html-keyword">char</span> *path) { -<span class="c2html-lineno"><a name="l236" href="#l236">236 </a></span> <span class="c2html-keyword">char</span> *name = util_resource_name(path); -<span class="c2html-lineno"><a name="l237" href="#l237">237 </a></span> <span class="c2html-type">size_t</span> namelen = strlen(name); -<span class="c2html-lineno"><a name="l238" href="#l238">238 </a></span> <span class="c2html-type">size_t</span> pathlen = strlen(path); -<span class="c2html-lineno"><a name="l239" href="#l239">239 </a></span> <span class="c2html-type">size_t</span> parentlen = pathlen - namelen; -<span class="c2html-lineno"><a name="l240" href="#l240">240 </a></span> <span class="c2html-keyword">char</span> *parent = malloc(parentlen + <span class="c2html-macroconst">1</span>); -<span class="c2html-lineno"><a name="l241" href="#l241">241 </a></span> memcpy(parent, path, parentlen); -<span class="c2html-lineno"><a name="l242" href="#l242">242 </a></span> parent[parentlen] = <span class="c2html-string">'\0'</span>; -<span class="c2html-lineno"><a name="l243" href="#l243">243 </a></span> <span class="c2html-keyword">return</span> parent; -<span class="c2html-lineno"><a name="l244" href="#l244">244 </a></span> } -<span class="c2html-lineno"><a name="l245" href="#l245">245 </a></span> -<span class="c2html-lineno"><a name="l246" href="#l246">246 </a></span> -<span class="c2html-lineno"><a name="l247" href="#l247">247 </a></span> <span class="c2html-keyword">char</span>* util_xml_get_text(xmlNode *elm) { -<span class="c2html-lineno"><a name="l248" href="#l248">248 </a></span> xmlNode *node = elm->children; -<span class="c2html-lineno"><a name="l249" href="#l249">249 </a></span> <span class="c2html-keyword">while</span>(node) { -<span class="c2html-lineno"><a name="l250" href="#l250">250 </a></span> <span class="c2html-keyword">if</span>(node->type == <span class="c2html-macroconst">XML_TEXT_NODE</span>) { -<span class="c2html-lineno"><a name="l251" href="#l251">251 </a></span> <span class="c2html-keyword">return</span> (<span class="c2html-keyword">char</span>*)node->content; -<span class="c2html-lineno"><a name="l252" href="#l252">252 </a></span> } -<span class="c2html-lineno"><a name="l253" href="#l253">253 </a></span> node = node->next; -<span class="c2html-lineno"><a name="l254" href="#l254">254 </a></span> } -<span class="c2html-lineno"><a name="l255" href="#l255">255 </a></span> <span class="c2html-keyword">return</span> <span class="c2html-macroconst">NULL</span>; -<span class="c2html-lineno"><a name="l256" href="#l256">256 </a></span> } -<span class="c2html-lineno"><a name="l257" href="#l257">257 </a></span> -<span class="c2html-lineno"><a name="l258" href="#l258">258 </a></span> -<span class="c2html-lineno"><a name="l259" href="#l259">259 </a></span> <span class="c2html-keyword">char</span>* util_base64decode(<span class="c2html-keyword">char</span> *in) { -<span class="c2html-lineno"><a name="l260" href="#l260">260 </a></span> <span class="c2html-keyword">int</span> len = <span class="c2html-macroconst">0</span>; -<span class="c2html-lineno"><a name="l261" href="#l261">261 </a></span> <span class="c2html-keyword">return</span> util_base64decode_len(in, &len); -<span class="c2html-lineno"><a name="l262" href="#l262">262 </a></span> } -<span class="c2html-lineno"><a name="l263" href="#l263">263 </a></span> -<span class="c2html-lineno"><a name="l264" href="#l264">264 </a></span> <span class="c2html-keyword">char</span>* util_base64decode_len(<span class="c2html-keyword">char</span>* in, <span class="c2html-keyword">int</span> *outlen) { -<span class="c2html-lineno"><a name="l265" href="#l265">265 </a></span> <span class="c2html-type">size_t</span> len = strlen(in); -<span class="c2html-lineno"><a name="l266" href="#l266">266 </a></span> <span class="c2html-keyword">char</span> *out = calloc(<span class="c2html-macroconst">1</span>, len); -<span class="c2html-lineno"><a name="l267" href="#l267">267 </a></span> -<span class="c2html-lineno"><a name="l268" href="#l268">268 </a></span> <span class="c2html-macroconst">BIO</span>* b = BIO_new_mem_buf(in, len); -<span class="c2html-lineno"><a name="l269" href="#l269">269 </a></span> <span class="c2html-macroconst">BIO</span> *d = BIO_new(BIO_f_base64()); -<span class="c2html-lineno"><a name="l270" href="#l270">270 </a></span> BIO_set_flags(d, <span class="c2html-macroconst">BIO_FLAGS_BASE64_NO_NL</span>); -<span class="c2html-lineno"><a name="l271" href="#l271">271 </a></span> b = BIO_push(d, b); -<span class="c2html-lineno"><a name="l272" href="#l272">272 </a></span> -<span class="c2html-lineno"><a name="l273" href="#l273">273 </a></span> *outlen = BIO_read(b, out, len); -<span class="c2html-lineno"><a name="l274" href="#l274">274 </a></span> BIO_free_all(b); -<span class="c2html-lineno"><a name="l275" href="#l275">275 </a></span> -<span class="c2html-lineno"><a name="l276" href="#l276">276 </a></span> <span class="c2html-keyword">return</span> out; -<span class="c2html-lineno"><a name="l277" href="#l277">277 </a></span> } -<span class="c2html-lineno"><a name="l278" href="#l278">278 </a></span> -<span class="c2html-lineno"><a name="l279" href="#l279">279 </a></span> <span class="c2html-keyword">char</span>* util_base64encode(<span class="c2html-keyword">char</span> *in, <span class="c2html-type">size_t</span> len) { -<span class="c2html-lineno"><a name="l280" href="#l280">280 </a></span> <span class="c2html-macroconst">BIO</span> *b; -<span class="c2html-lineno"><a name="l281" href="#l281">281 </a></span> <span class="c2html-macroconst">BIO</span> *e; -<span class="c2html-lineno"><a name="l282" href="#l282">282 </a></span> <span class="c2html-macroconst">BUF_MEM</span> *mem; -<span class="c2html-lineno"><a name="l283" href="#l283">283 </a></span> -<span class="c2html-lineno"><a name="l284" href="#l284">284 </a></span> e = BIO_new(BIO_f_base64()); -<span class="c2html-lineno"><a name="l285" href="#l285">285 </a></span> b = BIO_new(BIO_s_mem()); -<span class="c2html-lineno"><a name="l286" href="#l286">286 </a></span> -<span class="c2html-lineno"><a name="l287" href="#l287">287 </a></span> e = BIO_push(e, b); -<span class="c2html-lineno"><a name="l288" href="#l288">288 </a></span> BIO_write(e, in, len); -<span class="c2html-lineno"><a name="l289" href="#l289">289 </a></span> BIO_flush(e); -<span class="c2html-lineno"><a name="l290" href="#l290">290 </a></span> -<span class="c2html-lineno"><a name="l291" href="#l291">291 </a></span> BIO_get_mem_ptr(e, &mem); -<span class="c2html-lineno"><a name="l292" href="#l292">292 </a></span> <span class="c2html-keyword">char</span> *out = malloc(mem->length); -<span class="c2html-lineno"><a name="l293" href="#l293">293 </a></span> memcpy(out, mem->data, mem->length -<span class="c2html-macroconst">1</span>); -<span class="c2html-lineno"><a name="l294" href="#l294">294 </a></span> out[mem->length - <span class="c2html-macroconst">1</span>] = <span class="c2html-string">'\0'</span>; -<span class="c2html-lineno"><a name="l295" href="#l295">295 </a></span> -<span class="c2html-lineno"><a name="l296" href="#l296">296 </a></span> BIO_free_all(e); -<span class="c2html-lineno"><a name="l297" href="#l297">297 </a></span> -<span class="c2html-lineno"><a name="l298" href="#l298">298 </a></span> <span class="c2html-keyword">return</span> out; -<span class="c2html-lineno"><a name="l299" href="#l299">299 </a></span> } -<span class="c2html-lineno"><a name="l300" href="#l300">300 </a></span> -<span class="c2html-lineno"><a name="l301" href="#l301">301 </a></span> <span class="c2html-keyword">char</span>* util_encrypt_str(DavSession *sn, <span class="c2html-keyword">char</span> *str, <span class="c2html-keyword">char</span> *key) { -<span class="c2html-lineno"><a name="l302" href="#l302">302 </a></span> DavKey *k = dav_context_get_key(sn->context, key); -<span class="c2html-lineno"><a name="l303" href="#l303">303 </a></span> <span class="c2html-keyword">if</span>(!k) { -<span class="c2html-lineno"><a name="l304" href="#l304">304 </a></span> <span class="c2html-comment">// TODO: session error</span> -<span class="c2html-lineno"><a name="l305" href="#l305">305 </a></span> <span class="c2html-keyword">return</span> <span class="c2html-macroconst">NULL</span>; -<span class="c2html-lineno"><a name="l306" href="#l306">306 </a></span> } -<span class="c2html-lineno"><a name="l307" href="#l307">307 </a></span> -<span class="c2html-lineno"><a name="l308" href="#l308">308 </a></span> <span class="c2html-keyword">char</span> *enc_str = aes_encrypt(str, k); -<span class="c2html-lineno"><a name="l309" href="#l309">309 </a></span> <span class="c2html-keyword">char</span> *ret_str = dav_session_strdup(sn, enc_str); -<span class="c2html-lineno"><a name="l310" href="#l310">310 </a></span> free(enc_str); -<span class="c2html-lineno"><a name="l311" href="#l311">311 </a></span> <span class="c2html-keyword">return</span> ret_str; -<span class="c2html-lineno"><a name="l312" href="#l312">312 </a></span> } -<span class="c2html-lineno"><a name="l313" href="#l313">313 </a></span> -<span class="c2html-lineno"><a name="l314" href="#l314">314 </a></span> <span class="c2html-comment">/* commented out for testing reasons */</span> -<span class="c2html-lineno"><a name="l315" href="#l315">315 </a></span> <span class="c2html-comment">/*</span> -<span class="c2html-lineno"><a name="l316" href="#l316">316 </a></span> <span class="c2html-comment">char* util_decrypt_str(DavSession *sn, char *str, char *key) {</span> -<span class="c2html-lineno"><a name="l317" href="#l317">317 </a></span> <span class="c2html-comment"> DavKey *k = dav_context_get_key(sn->context, key);</span> -<span class="c2html-lineno"><a name="l318" href="#l318">318 </a></span> <span class="c2html-comment"> if(!k) {</span> -<span class="c2html-lineno"><a name="l319" href="#l319">319 </a></span> <span class="c2html-comment"> // TODO: session error</span> -<span class="c2html-lineno"><a name="l320" href="#l320">320 </a></span> <span class="c2html-comment"> return NULL;</span> -<span class="c2html-lineno"><a name="l321" href="#l321">321 </a></span> <span class="c2html-comment"> }</span> -<span class="c2html-lineno"><a name="l322" href="#l322">322 </a></span> <span class="c2html-comment"> </span> -<span class="c2html-lineno"><a name="l323" href="#l323">323 </a></span> <span class="c2html-comment"> char *dec_str = aes_decrypt(str, k);</span> -<span class="c2html-lineno"><a name="l324" href="#l324">324 </a></span> <span class="c2html-comment"> char *ret_str = dav_session_strdup(sn, dec_str);</span> -<span class="c2html-lineno"><a name="l325" href="#l325">325 </a></span> <span class="c2html-comment"> free(dec_str);</span> -<span class="c2html-lineno"><a name="l326" href="#l326">326 </a></span> <span class="c2html-comment"> return ret_str;</span> -<span class="c2html-lineno"><a name="l327" href="#l327">327 </a></span> <span class="c2html-comment">}</span> -<span class="c2html-lineno"><a name="l328" href="#l328">328 </a></span> <span class="c2html-comment">*/</span> -<span class="c2html-lineno"><a name="l329" href="#l329">329 </a></span> <span class="c2html-keyword">char</span>* util_random_str() { -<span class="c2html-lineno"><a name="l330" href="#l330">330 </a></span> <span class="c2html-keyword">unsigned</span> <span class="c2html-keyword">char</span> *str = malloc(<span class="c2html-macroconst">25</span>); -<span class="c2html-lineno"><a name="l331" href="#l331">331 </a></span> str[<span class="c2html-macroconst">24</span>] = <span class="c2html-string">'\0'</span>; -<span class="c2html-lineno"><a name="l332" href="#l332">332 </a></span> -<span class="c2html-lineno"><a name="l333" href="#l333">333 </a></span> <span class="c2html-type">sstr_t</span> t = <span class="c2html-macroconst">S</span>( -<span class="c2html-lineno"><a name="l334" href="#l334">334 </a></span> <span class="c2html-string">"01234567890"</span> -<span class="c2html-lineno"><a name="l335" href="#l335">335 </a></span> <span class="c2html-string">"abcdefghijklmnopqrstuvwxyz"</span> -<span class="c2html-lineno"><a name="l336" href="#l336">336 </a></span> <span class="c2html-string">"ABCDEFGHIJKLMNOPQRSTUVWXYZ"</span>); -<span class="c2html-lineno"><a name="l337" href="#l337">337 </a></span> <span class="c2html-keyword">const</span> <span class="c2html-keyword">unsigned</span> <span class="c2html-keyword">char</span> *table = (<span class="c2html-keyword">const</span> <span class="c2html-keyword">unsigned</span> <span class="c2html-keyword">char</span>*)t.ptr; -<span class="c2html-lineno"><a name="l338" href="#l338">338 </a></span> -<span class="c2html-lineno"><a name="l339" href="#l339">339 </a></span> RAND_pseudo_bytes(str, <span class="c2html-macroconst">24</span>); -<span class="c2html-lineno"><a name="l340" href="#l340">340 </a></span> <span class="c2html-keyword">for</span>(<span class="c2html-keyword">int</span> i=<span class="c2html-macroconst">0</span>;i<<span class="c2html-macroconst">24</span>;i++) { -<span class="c2html-lineno"><a name="l341" href="#l341">341 </a></span> <span class="c2html-keyword">int</span> c = str[i] % t.length; -<span class="c2html-lineno"><a name="l342" href="#l342">342 </a></span> str[i] = table[c]; -<span class="c2html-lineno"><a name="l343" href="#l343">343 </a></span> } -<span class="c2html-lineno"><a name="l344" href="#l344">344 </a></span> -<span class="c2html-lineno"><a name="l345" href="#l345">345 </a></span> <span class="c2html-keyword">return</span> (<span class="c2html-keyword">char</span>*)str; -<span class="c2html-lineno"><a name="l346" href="#l346">346 </a></span> } -<span class="c2html-lineno"><a name="l347" href="#l347">347 </a></span> -<span class="c2html-lineno"><a name="l348" href="#l348">348 </a></span> <span class="c2html-comment">/*</span> -<span class="c2html-lineno"><a name="l349" href="#l349">349 </a></span> <span class="c2html-comment"> * gets a substring from 0 to the appearance of the token</span> -<span class="c2html-lineno"><a name="l350" href="#l350">350 </a></span> <span class="c2html-comment"> * tokens are separated by space</span> -<span class="c2html-lineno"><a name="l351" href="#l351">351 </a></span> <span class="c2html-comment"> * sets sub to the substring and returns the remaining string</span> -<span class="c2html-lineno"><a name="l352" href="#l352">352 </a></span> <span class="c2html-comment"> */</span> -<span class="c2html-lineno"><a name="l353" href="#l353">353 </a></span> <span class="c2html-type">sstr_t</span> util_getsubstr_until_token(<span class="c2html-type">sstr_t</span> str, <span class="c2html-type">sstr_t</span> token, <span class="c2html-type">sstr_t</span> *sub) { -<span class="c2html-lineno"><a name="l354" href="#l354">354 </a></span> <span class="c2html-keyword">int</span> i; -<span class="c2html-lineno"><a name="l355" href="#l355">355 </a></span> <span class="c2html-keyword">int</span> token_start = -<span class="c2html-macroconst">1</span>; -<span class="c2html-lineno"><a name="l356" href="#l356">356 </a></span> <span class="c2html-keyword">int</span> token_end = -<span class="c2html-macroconst">1</span>; -<span class="c2html-lineno"><a name="l357" href="#l357">357 </a></span> <span class="c2html-keyword">for</span>(i=<span class="c2html-macroconst">0</span>;i<=str.length;i++) { -<span class="c2html-lineno"><a name="l358" href="#l358">358 </a></span> <span class="c2html-keyword">int</span> c; -<span class="c2html-lineno"><a name="l359" href="#l359">359 </a></span> <span class="c2html-keyword">if</span>(i == str.length) { -<span class="c2html-lineno"><a name="l360" href="#l360">360 </a></span> c = <span class="c2html-string">' '</span>; -<span class="c2html-lineno"><a name="l361" href="#l361">361 </a></span> } <span class="c2html-keyword">else</span> { -<span class="c2html-lineno"><a name="l362" href="#l362">362 </a></span> c = str.ptr[i]; -<span class="c2html-lineno"><a name="l363" href="#l363">363 </a></span> } -<span class="c2html-lineno"><a name="l364" href="#l364">364 </a></span> <span class="c2html-keyword">if</span>(c < <span class="c2html-macroconst">33</span>) { -<span class="c2html-lineno"><a name="l365" href="#l365">365 </a></span> <span class="c2html-keyword">if</span>(token_start != -<span class="c2html-macroconst">1</span>) { -<span class="c2html-lineno"><a name="l366" href="#l366">366 </a></span> token_end = i; -<span class="c2html-lineno"><a name="l367" href="#l367">367 </a></span> <span class="c2html-type">size_t</span> len = token_end - token_start; -<span class="c2html-lineno"><a name="l368" href="#l368">368 </a></span> <span class="c2html-type">sstr_t</span> tk = sstrsubsl(str, token_start, len); -<span class="c2html-lineno"><a name="l369" href="#l369">369 </a></span> <span class="c2html-comment">//printf("token: {%.*s}\n", token.length, token.ptr);</span> -<span class="c2html-lineno"><a name="l370" href="#l370">370 </a></span> <span class="c2html-keyword">if</span>(!sstrcmp(tk, token)) { -<span class="c2html-lineno"><a name="l371" href="#l371">371 </a></span> *sub = sstrtrim(sstrsubsl(str, <span class="c2html-macroconst">0</span>, token_start)); -<span class="c2html-lineno"><a name="l372" href="#l372">372 </a></span> <span class="c2html-keyword">break</span>; -<span class="c2html-lineno"><a name="l373" href="#l373">373 </a></span> } -<span class="c2html-lineno"><a name="l374" href="#l374">374 </a></span> token_start = -<span class="c2html-macroconst">1</span>; -<span class="c2html-lineno"><a name="l375" href="#l375">375 </a></span> token_end = -<span class="c2html-macroconst">1</span>; -<span class="c2html-lineno"><a name="l376" href="#l376">376 </a></span> } -<span class="c2html-lineno"><a name="l377" href="#l377">377 </a></span> } <span class="c2html-keyword">else</span> { -<span class="c2html-lineno"><a name="l378" href="#l378">378 </a></span> <span class="c2html-keyword">if</span>(token_start == -<span class="c2html-macroconst">1</span>) { -<span class="c2html-lineno"><a name="l379" href="#l379">379 </a></span> token_start = i; -<span class="c2html-lineno"><a name="l380" href="#l380">380 </a></span> } -<span class="c2html-lineno"><a name="l381" href="#l381">381 </a></span> } -<span class="c2html-lineno"><a name="l382" href="#l382">382 </a></span> } -<span class="c2html-lineno"><a name="l383" href="#l383">383 </a></span> -<span class="c2html-lineno"><a name="l384" href="#l384">384 </a></span> <span class="c2html-keyword">if</span>(i < str.length) { -<span class="c2html-lineno"><a name="l385" href="#l385">385 </a></span> <span class="c2html-keyword">return</span> sstrtrim(sstrsubs(str, i)); -<span class="c2html-lineno"><a name="l386" href="#l386">386 </a></span> } <span class="c2html-keyword">else</span> { -<span class="c2html-lineno"><a name="l387" href="#l387">387 </a></span> str.ptr = <span class="c2html-macroconst">NULL</span>; -<span class="c2html-lineno"><a name="l388" href="#l388">388 </a></span> str.length = <span class="c2html-macroconst">0</span>; -<span class="c2html-lineno"><a name="l389" href="#l389">389 </a></span> <span class="c2html-keyword">return</span> str; -<span class="c2html-lineno"><a name="l390" href="#l390">390 </a></span> } -<span class="c2html-lineno"><a name="l391" href="#l391">391 </a></span> } -</pre> - </body> -</html> -
--- a/test/v2-regression/javatest.html Thu Nov 10 18:44:48 2016 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,215 +0,0 @@ -<!DOCTYPE html> -<html> - <head> - <title>c2html</title> - <style type="text/css"> - span.c2html-lineno a { - font-style: italic; - text-decoration: none; - color: grey; - } - span.c2html-keyword { - color: blue; - } - span.c2html-macroconst { - color: cornflowerblue; - } - span.c2html-type { - color: teal; - } - span.c2html-directive { - color: silver; - } - span.c2html-string { - color: darkorange; - } - span.c2html-comment { - color: grey; - } - span.c2html-stdinclude, span.c2html-userinclude, a.c2html-userinclude { - } - </style> - </head> - <body> - -<pre> -<span class="c2html-lineno"><a name="l1" href="#l1"> 1 </a></span> <span class="c2html-comment">/*</span> -<span class="c2html-lineno"><a name="l2" href="#l2"> 2 </a></span> <span class="c2html-comment"> * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.</span> -<span class="c2html-lineno"><a name="l3" href="#l3"> 3 </a></span> <span class="c2html-comment"> *</span> -<span class="c2html-lineno"><a name="l4" href="#l4"> 4 </a></span> <span class="c2html-comment"> * Copyright 2014 Mike Becker. All rights reserved.</span> -<span class="c2html-lineno"><a name="l5" href="#l5"> 5 </a></span> <span class="c2html-comment"> *</span> -<span class="c2html-lineno"><a name="l6" href="#l6"> 6 </a></span> <span class="c2html-comment"> * Redistribution and use in source and binary forms, with or without</span> -<span class="c2html-lineno"><a name="l7" href="#l7"> 7 </a></span> <span class="c2html-comment"> * modification, are permitted provided that the following conditions are met:</span> -<span class="c2html-lineno"><a name="l8" href="#l8"> 8 </a></span> <span class="c2html-comment"> *</span> -<span class="c2html-lineno"><a name="l9" href="#l9"> 9 </a></span> <span class="c2html-comment"> * 1. Redistributions of source code must retain the above copyright</span> -<span class="c2html-lineno"><a name="l10" href="#l10"> 10 </a></span> <span class="c2html-comment"> * notice, this list of conditions and the following disclaimer.</span> -<span class="c2html-lineno"><a name="l11" href="#l11"> 11 </a></span> <span class="c2html-comment"> *</span> -<span class="c2html-lineno"><a name="l12" href="#l12"> 12 </a></span> <span class="c2html-comment"> * 2. Redistributions in binary form must reproduce the above copyright</span> -<span class="c2html-lineno"><a name="l13" href="#l13"> 13 </a></span> <span class="c2html-comment"> * notice, this list of conditions and the following disclaimer in the</span> -<span class="c2html-lineno"><a name="l14" href="#l14"> 14 </a></span> <span class="c2html-comment"> * documentation and/or other materials provided with the distribution.</span> -<span class="c2html-lineno"><a name="l15" href="#l15"> 15 </a></span> <span class="c2html-comment"> *</span> -<span class="c2html-lineno"><a name="l16" href="#l16"> 16 </a></span> <span class="c2html-comment"> * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"</span> -<span class="c2html-lineno"><a name="l17" href="#l17"> 17 </a></span> <span class="c2html-comment"> * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE</span> -<span class="c2html-lineno"><a name="l18" href="#l18"> 18 </a></span> <span class="c2html-comment"> * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE</span> -<span class="c2html-lineno"><a name="l19" href="#l19"> 19 </a></span> <span class="c2html-comment"> * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE</span> -<span class="c2html-lineno"><a name="l20" href="#l20"> 20 </a></span> <span class="c2html-comment"> * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR</span> -<span class="c2html-lineno"><a name="l21" href="#l21"> 21 </a></span> <span class="c2html-comment"> * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF</span> -<span class="c2html-lineno"><a name="l22" href="#l22"> 22 </a></span> <span class="c2html-comment"> * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS</span> -<span class="c2html-lineno"><a name="l23" href="#l23"> 23 </a></span> <span class="c2html-comment"> * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN</span> -<span class="c2html-lineno"><a name="l24" href="#l24"> 24 </a></span> <span class="c2html-comment"> * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)</span> -<span class="c2html-lineno"><a name="l25" href="#l25"> 25 </a></span> <span class="c2html-comment"> * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE</span> -<span class="c2html-lineno"><a name="l26" href="#l26"> 26 </a></span> <span class="c2html-comment"> * POSSIBILITY OF SUCH DAMAGE.</span> -<span class="c2html-lineno"><a name="l27" href="#l27"> 27 </a></span> <span class="c2html-comment"> *</span> -<span class="c2html-lineno"><a name="l28" href="#l28"> 28 </a></span> <span class="c2html-comment"> */</span> -<span class="c2html-lineno"><a name="l29" href="#l29"> 29 </a></span> -<span class="c2html-lineno"><a name="l30" href="#l30"> 30 </a></span> <span class="c2html-keyword">package</span> de.uapcore.sigred.doc.base; -<span class="c2html-lineno"><a name="l31" href="#l31"> 31 </a></span> -<span class="c2html-lineno"><a name="l32" href="#l32"> 32 </a></span> <span class="c2html-keyword">import</span> de.uapcore.sigred.doc.<span class="c2html-type">Resources</span>; -<span class="c2html-lineno"><a name="l33" href="#l33"> 33 </a></span> <span class="c2html-keyword">import</span> de.uapcore.sigrapi.impl.<span class="c2html-type">Digraph</span>; -<span class="c2html-lineno"><a name="l34" href="#l34"> 34 </a></span> <span class="c2html-keyword">import</span> de.uapcore.sigrapi.impl.<span class="c2html-type">Graph</span>; -<span class="c2html-lineno"><a name="l35" href="#l35"> 35 </a></span> <span class="c2html-keyword">import</span> de.uapcore.sigrapi.<span class="c2html-type">IGraph</span>; -<span class="c2html-lineno"><a name="l36" href="#l36"> 36 </a></span> <span class="c2html-keyword">import</span> java.io.<span class="c2html-type">IOException</span>; -<span class="c2html-lineno"><a name="l37" href="#l37"> 37 </a></span> <span class="c2html-keyword">import</span> java.io.<span class="c2html-type">InputStream</span>; -<span class="c2html-lineno"><a name="l38" href="#l38"> 38 </a></span> <span class="c2html-keyword">import</span> java.io.<span class="c2html-type">OutputStream</span>; -<span class="c2html-lineno"><a name="l39" href="#l39"> 39 </a></span> <span class="c2html-keyword">import</span> java.util.concurrent.atomic.<span class="c2html-type">AtomicBoolean</span>; -<span class="c2html-lineno"><a name="l40" href="#l40"> 40 </a></span> <span class="c2html-keyword">import</span> java.util.concurrent.atomic.<span class="c2html-type">AtomicReference</span>; -<span class="c2html-lineno"><a name="l41" href="#l41"> 41 </a></span> <span class="c2html-keyword">import</span> org.apache.xerces.impl.<span class="c2html-type">Constants</span>; -<span class="c2html-lineno"><a name="l42" href="#l42"> 42 </a></span> <span class="c2html-keyword">import</span> org.dom4j.<span class="c2html-type">Document</span>; -<span class="c2html-lineno"><a name="l43" href="#l43"> 43 </a></span> <span class="c2html-keyword">import</span> org.dom4j.<span class="c2html-type">DocumentException</span>; -<span class="c2html-lineno"><a name="l44" href="#l44"> 44 </a></span> <span class="c2html-keyword">import</span> org.dom4j.<span class="c2html-type">DocumentHelper</span>; -<span class="c2html-lineno"><a name="l45" href="#l45"> 45 </a></span> <span class="c2html-keyword">import</span> org.dom4j.<span class="c2html-type">Element</span>; -<span class="c2html-lineno"><a name="l46" href="#l46"> 46 </a></span> <span class="c2html-keyword">import</span> org.dom4j.<span class="c2html-type">Namespace</span>; -<span class="c2html-lineno"><a name="l47" href="#l47"> 47 </a></span> <span class="c2html-keyword">import</span> org.dom4j.<span class="c2html-type">QName</span>; -<span class="c2html-lineno"><a name="l48" href="#l48"> 48 </a></span> <span class="c2html-keyword">import</span> org.dom4j.io.<span class="c2html-type">OutputFormat</span>; -<span class="c2html-lineno"><a name="l49" href="#l49"> 49 </a></span> <span class="c2html-keyword">import</span> org.dom4j.io.<span class="c2html-type">SAXReader</span>; -<span class="c2html-lineno"><a name="l50" href="#l50"> 50 </a></span> <span class="c2html-keyword">import</span> org.dom4j.io.<span class="c2html-type">XMLWriter</span>; -<span class="c2html-lineno"><a name="l51" href="#l51"> 51 </a></span> <span class="c2html-keyword">import</span> org.xml.sax.<span class="c2html-type">ErrorHandler</span>; -<span class="c2html-lineno"><a name="l52" href="#l52"> 52 </a></span> <span class="c2html-keyword">import</span> org.xml.sax.<span class="c2html-type">SAXException</span>; -<span class="c2html-lineno"><a name="l53" href="#l53"> 53 </a></span> <span class="c2html-keyword">import</span> org.xml.sax.<span class="c2html-type">SAXParseException</span>; -<span class="c2html-lineno"><a name="l54" href="#l54"> 54 </a></span> -<span class="c2html-lineno"><a name="l55" href="#l55"> 55 </a></span> <span class="c2html-keyword">public</span> <span class="c2html-keyword">abstract</span> <span class="c2html-keyword">class</span> <span class="c2html-type">AbstractGraphDocument</span><<span class="c2html-type">T</span> <span class="c2html-keyword">extends</span> <span class="c2html-type">IGraph</span>> -<span class="c2html-lineno"><a name="l56" href="#l56"> 56 </a></span> <span class="c2html-keyword">extends</span> <span class="c2html-type">FileBackedDocument</span> { -<span class="c2html-lineno"><a name="l57" href="#l57"> 57 </a></span> -<span class="c2html-lineno"><a name="l58" href="#l58"> 58 </a></span> <span class="c2html-keyword">protected</span> <span class="c2html-keyword">static</span> <span class="c2html-keyword">final</span> <span class="c2html-type">Namespace</span> <span class="c2html-type">NAMESPACE</span> = <span class="c2html-type">Namespace</span>.get(<span class="c2html-string">"sigred"</span>, -<span class="c2html-lineno"><a name="l59" href="#l59"> 59 </a></span> <span class="c2html-string">"http://develop.uap-core.de/sigred/"</span>); -<span class="c2html-lineno"><a name="l60" href="#l60"> 60 </a></span> -<span class="c2html-lineno"><a name="l61" href="#l61"> 61 </a></span> <span class="c2html-keyword">private</span> <span class="c2html-keyword">static</span> <span class="c2html-keyword">final</span> -<span class="c2html-lineno"><a name="l62" href="#l62"> 62 </a></span> <span class="c2html-type">QName</span> <span class="c2html-type">TAG_GRAPHDOC</span> = <span class="c2html-type">QName</span>.get(<span class="c2html-string">"graph-document"</span>, <span class="c2html-type">NAMESPACE</span>); -<span class="c2html-lineno"><a name="l63" href="#l63"> 63 </a></span> <span class="c2html-keyword">private</span> <span class="c2html-keyword">static</span> <span class="c2html-keyword">final</span> -<span class="c2html-lineno"><a name="l64" href="#l64"> 64 </a></span> <span class="c2html-type">QName</span> <span class="c2html-type">TAG_GRAPH</span> = <span class="c2html-type">QName</span>.get(<span class="c2html-string">"graph"</span>, <span class="c2html-type">NAMESPACE</span>); -<span class="c2html-lineno"><a name="l65" href="#l65"> 65 </a></span> <span class="c2html-keyword">private</span> <span class="c2html-keyword">static</span> <span class="c2html-keyword">final</span> -<span class="c2html-lineno"><a name="l66" href="#l66"> 66 </a></span> <span class="c2html-type">QName</span> <span class="c2html-type">TAG_DIGRAPH</span> = <span class="c2html-type">QName</span>.get(<span class="c2html-string">"digraph"</span>, <span class="c2html-type">NAMESPACE</span>); -<span class="c2html-lineno"><a name="l67" href="#l67"> 67 </a></span> <span class="c2html-keyword">private</span> <span class="c2html-keyword">static</span> <span class="c2html-keyword">final</span> -<span class="c2html-lineno"><a name="l68" href="#l68"> 68 </a></span> <span class="c2html-type">QName</span> <span class="c2html-type">TAG_METADATA</span> = <span class="c2html-type">QName</span>.get(<span class="c2html-string">"metadata"</span>, <span class="c2html-type">NAMESPACE</span>); -<span class="c2html-lineno"><a name="l69" href="#l69"> 69 </a></span> -<span class="c2html-lineno"><a name="l70" href="#l70"> 70 </a></span> <span class="c2html-keyword">protected</span> <span class="c2html-keyword">final</span> <span class="c2html-type">T</span> graph; -<span class="c2html-lineno"><a name="l71" href="#l71"> 71 </a></span> -<span class="c2html-lineno"><a name="l72" href="#l72"> 72 </a></span> <span class="c2html-keyword">private</span> <span class="c2html-keyword">final</span> <span class="c2html-type">GraphDocumentMetadata</span> metadata; -<span class="c2html-lineno"><a name="l73" href="#l73"> 73 </a></span> -<span class="c2html-lineno"><a name="l74" href="#l74"> 74 </a></span> <span class="c2html-keyword">public</span> <span class="c2html-type">AbstractGraphDocument</span>(<span class="c2html-type">Class</span><<span class="c2html-type">T</span>> graphType) { -<span class="c2html-lineno"><a name="l75" href="#l75"> 75 </a></span> <span class="c2html-type">T</span> g; -<span class="c2html-lineno"><a name="l76" href="#l76"> 76 </a></span> <span class="c2html-keyword">try</span> { -<span class="c2html-lineno"><a name="l77" href="#l77"> 77 </a></span> g = graphType.newInstance(); -<span class="c2html-lineno"><a name="l78" href="#l78"> 78 </a></span> } <span class="c2html-keyword">catch</span> (<span class="c2html-type">ReflectiveOperationException</span> e) { -<span class="c2html-lineno"><a name="l79" href="#l79"> 79 </a></span> <span class="c2html-keyword">assert</span> false; -<span class="c2html-lineno"><a name="l80" href="#l80"> 80 </a></span> g = null; <span class="c2html-comment">// for the compiler</span> -<span class="c2html-lineno"><a name="l81" href="#l81"> 81 </a></span> } -<span class="c2html-lineno"><a name="l82" href="#l82"> 82 </a></span> graph = g; -<span class="c2html-lineno"><a name="l83" href="#l83"> 83 </a></span> metadata = <span class="c2html-keyword">new</span> <span class="c2html-type">GraphDocumentMetadata</span>(); -<span class="c2html-lineno"><a name="l84" href="#l84"> 84 </a></span> } -<span class="c2html-lineno"><a name="l85" href="#l85"> 85 </a></span> -<span class="c2html-lineno"><a name="l86" href="#l86"> 86 </a></span> <span class="c2html-keyword">public</span> <span class="c2html-type">T</span> getGraph() { -<span class="c2html-lineno"><a name="l87" href="#l87"> 87 </a></span> <span class="c2html-keyword">return</span> graph; -<span class="c2html-lineno"><a name="l88" href="#l88"> 88 </a></span> } -<span class="c2html-lineno"><a name="l89" href="#l89"> 89 </a></span> -<span class="c2html-lineno"><a name="l90" href="#l90"> 90 </a></span> <span class="c2html-keyword">public</span> <span class="c2html-type">GraphDocumentMetadata</span> getMetadata() { -<span class="c2html-lineno"><a name="l91" href="#l91"> 91 </a></span> <span class="c2html-keyword">return</span> metadata; -<span class="c2html-lineno"><a name="l92" href="#l92"> 92 </a></span> } -<span class="c2html-lineno"><a name="l93" href="#l93"> 93 </a></span> -<span class="c2html-lineno"><a name="l94" href="#l94"> 94 </a></span> <span class="c2html-keyword">protected</span> <span class="c2html-keyword">abstract</span> <span class="c2html-keyword">void</span> writeGraph(<span class="c2html-type">Element</span> rootNode) <span class="c2html-keyword">throws</span> <span class="c2html-type">IOException</span>; -<span class="c2html-lineno"><a name="l95" href="#l95"> 95 </a></span> <span class="c2html-keyword">protected</span> <span class="c2html-keyword">abstract</span> <span class="c2html-keyword">void</span> readGraph(<span class="c2html-type">Element</span> rootNode) <span class="c2html-keyword">throws</span> <span class="c2html-type">IOException</span>; -<span class="c2html-lineno"><a name="l96" href="#l96"> 96 </a></span> -<span class="c2html-lineno"><a name="l97" href="#l97"> 97 </a></span> <span class="c2html-directive">@Override</span> -<span class="c2html-lineno"><a name="l98" href="#l98"> 98 </a></span> <span class="c2html-keyword">public</span> <span class="c2html-keyword">void</span> writeTo(<span class="c2html-type">OutputStream</span> out) <span class="c2html-keyword">throws</span> <span class="c2html-type">IOException</span> { -<span class="c2html-lineno"><a name="l99" href="#l99"> 99 </a></span> <span class="c2html-type">Document</span> doc = <span class="c2html-type">DocumentHelper</span>.createDocument(); -<span class="c2html-lineno"><a name="l100" href="#l100">100 </a></span> -<span class="c2html-lineno"><a name="l101" href="#l101">101 </a></span> <span class="c2html-type">Element</span> rootNode = doc.addElement(<span class="c2html-type">TAG_GRAPHDOC</span>); -<span class="c2html-lineno"><a name="l102" href="#l102">102 </a></span> -<span class="c2html-lineno"><a name="l103" href="#l103">103 </a></span> <span class="c2html-type">Element</span> metadataNode = rootNode.addElement(<span class="c2html-type">TAG_METADATA</span>); -<span class="c2html-lineno"><a name="l104" href="#l104">104 </a></span> -<span class="c2html-lineno"><a name="l105" href="#l105">105 </a></span> metadata.write(metadataNode); -<span class="c2html-lineno"><a name="l106" href="#l106">106 </a></span> -<span class="c2html-lineno"><a name="l107" href="#l107">107 </a></span> <span class="c2html-keyword">if</span> (graph <span class="c2html-keyword">instanceof</span> <span class="c2html-type">Graph</span>) { -<span class="c2html-lineno"><a name="l108" href="#l108">108 </a></span> writeGraph(rootNode.addElement(<span class="c2html-type">TAG_GRAPH</span>)); -<span class="c2html-lineno"><a name="l109" href="#l109">109 </a></span> } <span class="c2html-keyword">else</span> <span class="c2html-keyword">if</span> (graph <span class="c2html-keyword">instanceof</span> <span class="c2html-type">Digraph</span>) { -<span class="c2html-lineno"><a name="l110" href="#l110">110 </a></span> writeGraph(rootNode.addElement(<span class="c2html-type">TAG_DIGRAPH</span>)); -<span class="c2html-lineno"><a name="l111" href="#l111">111 </a></span> } <span class="c2html-keyword">else</span> { -<span class="c2html-lineno"><a name="l112" href="#l112">112 </a></span> <span class="c2html-keyword">throw</span> <span class="c2html-keyword">new</span> <span class="c2html-type">IOException</span>(<span class="c2html-string">"unsupported graph type"</span>); -<span class="c2html-lineno"><a name="l113" href="#l113">113 </a></span> } -<span class="c2html-lineno"><a name="l114" href="#l114">114 </a></span> -<span class="c2html-lineno"><a name="l115" href="#l115">115 </a></span> <span class="c2html-type">XMLWriter</span> writer = <span class="c2html-keyword">new</span> <span class="c2html-type">XMLWriter</span>(out, <span class="c2html-type">OutputFormat</span>.createPrettyPrint()); -<span class="c2html-lineno"><a name="l116" href="#l116">116 </a></span> writer.write(doc); -<span class="c2html-lineno"><a name="l117" href="#l117">117 </a></span> writer.flush(); -<span class="c2html-lineno"><a name="l118" href="#l118">118 </a></span> } -<span class="c2html-lineno"><a name="l119" href="#l119">119 </a></span> -<span class="c2html-lineno"><a name="l120" href="#l120">120 </a></span> <span class="c2html-directive">@Override</span> -<span class="c2html-lineno"><a name="l121" href="#l121">121 </a></span> <span class="c2html-keyword">public</span> <span class="c2html-keyword">void</span> readFrom(<span class="c2html-type">InputStream</span> in) <span class="c2html-keyword">throws</span> <span class="c2html-type">IOException</span> { -<span class="c2html-lineno"><a name="l122" href="#l122">122 </a></span> <span class="c2html-keyword">try</span> { -<span class="c2html-lineno"><a name="l123" href="#l123">123 </a></span> <span class="c2html-type">SAXReader</span> reader = <span class="c2html-keyword">new</span> <span class="c2html-type">SAXReader</span>(true); -<span class="c2html-lineno"><a name="l124" href="#l124">124 </a></span> reader.setStripWhitespaceText(true); -<span class="c2html-lineno"><a name="l125" href="#l125">125 </a></span> -<span class="c2html-lineno"><a name="l126" href="#l126">126 </a></span> reader.setFeature(<span class="c2html-type">Constants</span>.<span class="c2html-type">XERCES_FEATURE_PREFIX</span>+ -<span class="c2html-lineno"><a name="l127" href="#l127">127 </a></span> <span class="c2html-type">Constants</span>.<span class="c2html-type">SCHEMA_VALIDATION_FEATURE</span>, true); -<span class="c2html-lineno"><a name="l128" href="#l128">128 </a></span> reader.setProperty(<span class="c2html-type">Constants</span>.<span class="c2html-type">XERCES_PROPERTY_PREFIX</span> + -<span class="c2html-lineno"><a name="l129" href="#l129">129 </a></span> <span class="c2html-type">Constants</span>.<span class="c2html-type">SCHEMA_LOCATION</span>, <span class="c2html-type">String</span>.format(<span class="c2html-string">"%s %s"</span>, -<span class="c2html-lineno"><a name="l130" href="#l130">130 </a></span> <span class="c2html-type">NAMESPACE</span>.getURI(), <span class="c2html-type">Resources</span>.<span class="c2html-keyword">class</span>.getResource( -<span class="c2html-lineno"><a name="l131" href="#l131">131 </a></span> <span class="c2html-string">"graph-document.xsd"</span>).toExternalForm())); -<span class="c2html-lineno"><a name="l132" href="#l132">132 </a></span> -<span class="c2html-lineno"><a name="l133" href="#l133">133 </a></span> <span class="c2html-keyword">final</span> <span class="c2html-type">AtomicBoolean</span> passed = <span class="c2html-keyword">new</span> <span class="c2html-type">AtomicBoolean</span>(true); -<span class="c2html-lineno"><a name="l134" href="#l134">134 </a></span> <span class="c2html-keyword">final</span> <span class="c2html-type">AtomicReference</span><<span class="c2html-type">SAXParseException</span>> xmlerror = <span class="c2html-keyword">new</span> <span class="c2html-type">AtomicReference</span><>(); -<span class="c2html-lineno"><a name="l135" href="#l135">135 </a></span> <span class="c2html-comment">// TODO: we should do more detailed error handling here</span> -<span class="c2html-lineno"><a name="l136" href="#l136">136 </a></span> reader.setErrorHandler(<span class="c2html-keyword">new</span> <span class="c2html-type">ErrorHandler</span>() { -<span class="c2html-lineno"><a name="l137" href="#l137">137 </a></span> <span class="c2html-directive">@Override</span> -<span class="c2html-lineno"><a name="l138" href="#l138">138 </a></span> <span class="c2html-keyword">public</span> <span class="c2html-keyword">void</span> warning(<span class="c2html-type">SAXParseException</span> exception) <span class="c2html-keyword">throws</span> <span class="c2html-type">SAXException</span> { -<span class="c2html-lineno"><a name="l139" href="#l139">139 </a></span> } -<span class="c2html-lineno"><a name="l140" href="#l140">140 </a></span> -<span class="c2html-lineno"><a name="l141" href="#l141">141 </a></span> <span class="c2html-directive">@Override</span> -<span class="c2html-lineno"><a name="l142" href="#l142">142 </a></span> <span class="c2html-keyword">public</span> <span class="c2html-keyword">void</span> error(<span class="c2html-type">SAXParseException</span> exception) <span class="c2html-keyword">throws</span> <span class="c2html-type">SAXException</span> { -<span class="c2html-lineno"><a name="l143" href="#l143">143 </a></span> xmlerror.set(exception); -<span class="c2html-lineno"><a name="l144" href="#l144">144 </a></span> passed.set(false); -<span class="c2html-lineno"><a name="l145" href="#l145">145 </a></span> } -<span class="c2html-lineno"><a name="l146" href="#l146">146 </a></span> -<span class="c2html-lineno"><a name="l147" href="#l147">147 </a></span> <span class="c2html-directive">@Override</span> -<span class="c2html-lineno"><a name="l148" href="#l148">148 </a></span> <span class="c2html-keyword">public</span> <span class="c2html-keyword">void</span> fatalError(<span class="c2html-type">SAXParseException</span> exception) <span class="c2html-keyword">throws</span> <span class="c2html-type">SAXException</span> { -<span class="c2html-lineno"><a name="l149" href="#l149">149 </a></span> xmlerror.set(exception); -<span class="c2html-lineno"><a name="l150" href="#l150">150 </a></span> passed.set(false); -<span class="c2html-lineno"><a name="l151" href="#l151">151 </a></span> } -<span class="c2html-lineno"><a name="l152" href="#l152">152 </a></span> -<span class="c2html-lineno"><a name="l153" href="#l153">153 </a></span> }); -<span class="c2html-lineno"><a name="l154" href="#l154">154 </a></span> <span class="c2html-type">Document</span> doc = reader.read(in); -<span class="c2html-lineno"><a name="l155" href="#l155">155 </a></span> <span class="c2html-keyword">if</span> (!passed.get()) { -<span class="c2html-lineno"><a name="l156" href="#l156">156 </a></span> <span class="c2html-comment">// TODO: provide details (maybe via separate error object?)</span> -<span class="c2html-lineno"><a name="l157" href="#l157">157 </a></span> <span class="c2html-keyword">throw</span> xmlerror.get(); -<span class="c2html-lineno"><a name="l158" href="#l158">158 </a></span> } -<span class="c2html-lineno"><a name="l159" href="#l159">159 </a></span> -<span class="c2html-lineno"><a name="l160" href="#l160">160 </a></span> doc.normalize(); -<span class="c2html-lineno"><a name="l161" href="#l161">161 </a></span> -<span class="c2html-lineno"><a name="l162" href="#l162">162 </a></span> <span class="c2html-type">Element</span> root = doc.getRootElement(); -<span class="c2html-lineno"><a name="l163" href="#l163">163 </a></span> metadata.read(root.element(<span class="c2html-type">TAG_METADATA</span>)); -<span class="c2html-lineno"><a name="l164" href="#l164">164 </a></span> -<span class="c2html-lineno"><a name="l165" href="#l165">165 </a></span> <span class="c2html-keyword">if</span> (graph <span class="c2html-keyword">instanceof</span> <span class="c2html-type">Graph</span>) { -<span class="c2html-lineno"><a name="l166" href="#l166">166 </a></span> readGraph(root.element(<span class="c2html-type">TAG_GRAPH</span>)); -<span class="c2html-lineno"><a name="l167" href="#l167">167 </a></span> } <span class="c2html-keyword">else</span> <span class="c2html-keyword">if</span> (graph <span class="c2html-keyword">instanceof</span> <span class="c2html-type">Digraph</span>) { -<span class="c2html-lineno"><a name="l168" href="#l168">168 </a></span> readGraph(root.element(<span class="c2html-type">TAG_DIGRAPH</span>)); -<span class="c2html-lineno"><a name="l169" href="#l169">169 </a></span> } <span class="c2html-keyword">else</span> { -<span class="c2html-lineno"><a name="l170" href="#l170">170 </a></span> <span class="c2html-keyword">throw</span> <span class="c2html-keyword">new</span> <span class="c2html-type">IOException</span>(<span class="c2html-string">"unsupported graph type"</span>); -<span class="c2html-lineno"><a name="l171" href="#l171">171 </a></span> } -<span class="c2html-lineno"><a name="l172" href="#l172">172 </a></span> } <span class="c2html-keyword">catch</span> (<span class="c2html-type">DocumentException</span> | <span class="c2html-type">SAXException</span> ex) { -<span class="c2html-lineno"><a name="l173" href="#l173">173 </a></span> <span class="c2html-keyword">throw</span> <span class="c2html-keyword">new</span> <span class="c2html-type">IOException</span>(ex); -<span class="c2html-lineno"><a name="l174" href="#l174">174 </a></span> } -<span class="c2html-lineno"><a name="l175" href="#l175">175 </a></span> } -<span class="c2html-lineno"><a name="l176" href="#l176">176 </a></span> } -</pre> - </body> -</html> -
--- a/test/v2-regression/plain.html Thu Nov 10 18:44:48 2016 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,53 +0,0 @@ -<!DOCTYPE html> -<html> - <head> - <title>c2html</title> - <style type="text/css"> - span.c2html-lineno a { - font-style: italic; - text-decoration: none; - color: grey; - } - span.c2html-keyword { - color: blue; - } - span.c2html-macroconst { - color: cornflowerblue; - } - span.c2html-type { - color: cornflowerblue; - } - span.c2html-directive { - color: green; - } - span.c2html-string { - color: darkorange; - } - span.c2html-comment { - color: grey; - } - span.c2html-stdinclude { - color: darkorange; - } - span.c2html-userinclude { - color: darkorange; - } - a.c2html-userinclude { - color: darkorange; - text-decoration: underline; - } - </style> - </head> - <body> - -<pre> -<span class="c2html-lineno"><a name="l1" href="#l1">1 </a></span> </body> -<span class="c2html-lineno"><a name="l2" href="#l2">2 </a></span> </html> -<span class="c2html-lineno"><a name="l3" href="#l3">3 </a></span> <!c -<span class="c2html-lineno"><a name="l4" href="#l4">4 </a></span> pblock_free(q); -<span class="c2html-lineno"><a name="l5" href="#l5">5 </a></span> !> -<span class="c2html-lineno"><a name="l6" href="#l6">6 </a></span> -</pre> - </body> -</html> -