Mon, 24 Apr 2023 20:54:38 +0200
upgrade to ucx 3.0
1.1 --- a/.hgignore Thu Nov 10 18:44:48 2016 +0100 1.2 +++ b/.hgignore Mon Apr 24 20:54:38 2023 +0200 1.3 @@ -5,3 +5,4 @@ 1.4 \.conflict\~$ 1.5 ^build/.*$ 1.6 ^nbproject/.*$ 1.7 +^.idea/.*$
2.1 --- a/Makefile Thu Nov 10 18:44:48 2016 +0100 2.2 +++ b/Makefile Mon Apr 24 20:54:38 2023 +0200 2.3 @@ -34,25 +34,17 @@ 2.4 SRC = frontend.c 2.5 SRC += c2html.c 2.6 SRC += highlighter.c 2.7 -SRC += ucx/allocator.c 2.8 -SRC += ucx/buffer.c 2.9 -SRC += ucx/list.c 2.10 -SRC += ucx/string.c 2.11 -SRC += ucx/utils.c 2.12 2.13 OBJ = $(SRC:%.c=build/%$(OBJ_EXT)) 2.14 2.15 -all: build/ucx build/$(BIN) 2.16 +all: build build/$(BIN) 2.17 2.18 build/$(BIN): $(OBJ) 2.19 $(LD) -o $@ $^ $(LDFLAGS) 2.20 2.21 build/%$(OBJ_EXT): src/%.c 2.22 $(CC) -o $@ $(CFLAGS) -c $< 2.23 - 2.24 -build/ucx: build 2.25 - $(MKDIR) $@ 2.26 - 2.27 + 2.28 build: 2.29 $(MKDIR) $@ 2.30 2.31 @@ -65,12 +57,10 @@ 2.32 -H test/header.html -F test/footer.html 2.33 ./build/$(BIN) -p test/plain.csp -o build/plain.html \ 2.34 -H test/header.html -F test/footer.html 2.35 - 2.36 -test-v2-regression: test 2.37 - diff build/ctest.html test/v2-regression/ctest.html && \ 2.38 - diff build/javatest.html test/v2-regression/javatest.html && \ 2.39 - diff build/bigtest.html test/v2-regression/bigtest.html && \ 2.40 - diff build/plain.html test/v2-regression/plain.html 2.41 + diff build/ctest.html test/gs/ctest.html && \ 2.42 + diff build/javatest.html test/gs/javatest.html && \ 2.43 + diff build/bigtest.html test/gs/bigtest.html && \ 2.44 + diff build/plain.html test/gs/plain.html 2.45 2.46 clean: 2.47 $(RM) $(RMFLAGS) build
3.1 --- a/clang.mk Thu Nov 10 18:44:48 2016 +0100 3.2 +++ b/clang.mk Mon Apr 24 20:54:38 2023 +0200 3.3 @@ -36,5 +36,5 @@ 3.4 CC = clang 3.5 CFLAGS = -O2 3.6 LD = clang 3.7 -LDFLAGS = 3.8 +LDFLAGS = -lucx 3.9 OBJ_EXT = .o
4.1 --- a/gcc.mk Thu Nov 10 18:44:48 2016 +0100 4.2 +++ b/gcc.mk Mon Apr 24 20:54:38 2023 +0200 4.3 @@ -36,5 +36,5 @@ 4.4 CC = gcc 4.5 CFLAGS = -O2 -std=gnu99 4.6 LD = gcc 4.7 -LDFLAGS = 4.8 +LDFLAGS = -lucx 4.9 OBJ_EXT = .o
5.1 --- a/src/c2html.c Thu Nov 10 18:44:48 2016 +0100 5.2 +++ b/src/c2html.c Mon Apr 24 20:54:38 2023 +0200 5.3 @@ -29,131 +29,93 @@ 5.4 5.5 #include "c2html.h" 5.6 5.7 -#include "ucx/list.h" 5.8 -#include "ucx/utils.h" 5.9 +#include <cx/array_list.h> 5.10 +#include <cx/printf.h> 5.11 5.12 -#define try_write(wfnc, str, n, buf, written, maxlen) \ 5.13 - { \ 5.14 - size_t m = maxlen-written; \ 5.15 - written += wfnc(str, 1, n > m ? m : n, buf); \ 5.16 +size_t c2html_format( 5.17 + CxList const *lines, 5.18 + void *outbuf, 5.19 + cx_write_func wfnc, 5.20 + c2html_highlighter_func highlighter, 5.21 + int showln 5.22 +) { 5.23 + /* total written bytes */ 5.24 + size_t written = 0; 5.25 + 5.26 + /* compute width of line numbering */ 5.27 + int lnw = 0; 5.28 + if (showln) { 5.29 + size_t no_lines = cxListSize(lines); 5.30 + for (size_t p = 1; p < no_lines; p *= 10) lnw++; 5.31 } 5.32 5.33 -static size_t formatlines(c2html_highlighter_func highlighter, UcxList *in, 5.34 - void *outbuf, write_func wfnc, size_t maxlen, int showlineno) { 5.35 - /* total written bytes */ 5.36 - size_t written = 0; 5.37 - 5.38 - /* compute width of line numbering */ 5.39 - int lnw = 0; 5.40 - if (showlineno) { 5.41 - size_t lines = ucx_list_size(in); 5.42 - for (size_t p = 1; p < lines ; p*=10) lnw++; 5.43 - } 5.44 - 5.45 - /* start monospace formatting */ 5.46 - try_write(wfnc, "<pre>\n", 6, outbuf, written, maxlen); 5.47 + /* start code formatting */ 5.48 + written += wfnc("<div class=\"c2html-code\">\n", 1, 26, outbuf); 5.49 5.50 /* process lines */ 5.51 - size_t lineno = 0; 5.52 - c2html_highlighter_data* hd = malloc(sizeof(c2html_highlighter_data)); 5.53 - hd->multiline_comment = 0; 5.54 - hd->primary_buffer = ucx_buffer_new(NULL, 256, UCX_BUFFER_AUTOEXTEND); 5.55 - hd->secondary_buffer = ucx_buffer_new(NULL, 32, UCX_BUFFER_AUTOEXTEND); 5.56 - UcxBuffer *line = ucx_buffer_new(NULL, 1024, UCX_BUFFER_AUTOEXTEND); 5.57 - 5.58 - UCX_FOREACH(sourceline, in) { 5.59 + int lineno = 0; 5.60 + c2html_highlighter_data hd; 5.61 + hd.multiline_comment = 0; 5.62 + cxBufferInit(&hd.primary_buffer, NULL, 256, NULL, CX_BUFFER_AUTO_EXTEND); 5.63 + cxBufferInit(&hd.secondary_buffer, NULL, 32, NULL, CX_BUFFER_AUTO_EXTEND); 5.64 + CxBuffer out_line; 5.65 + cxBufferInit(&out_line, NULL, 128, NULL, CX_BUFFER_AUTO_EXTEND); 5.66 + 5.67 + CxIterator in_lines = cxListIterator(lines); 5.68 + cx_foreach(char*, in_line, in_lines) { 5.69 /* increase line number and clean line buffer */ 5.70 lineno++; 5.71 - ucx_buffer_clear(line); 5.72 - 5.73 + cxBufferClear(&out_line); 5.74 + 5.75 /* write line number */ 5.76 - if (showlineno) { 5.77 - ucx_bprintf(line, "<a class=\"c2html-lineno\" name=\"l%d\" " 5.78 - "href=\"#l%d\">%*d </a>", 5.79 - lineno, lineno, lnw, lineno); 5.80 + if (showln) { 5.81 + cx_bprintf(&out_line, "<a class=\"c2html-lineno\" name=\"l%d\" " 5.82 + "href=\"#l%d\">%*d </a>", 5.83 + lineno, lineno, lnw, lineno); 5.84 } 5.85 - 5.86 + 5.87 /* process code line */ 5.88 - highlighter(sourceline->data, line, hd); 5.89 - 5.90 + highlighter(in_line, &out_line, &hd); 5.91 + 5.92 /* write code line */ 5.93 - try_write(wfnc, line->space, line->size, outbuf, written, maxlen); 5.94 - 5.95 - if (written == maxlen) break; 5.96 + written += wfnc(out_line.space, 1, out_line.size, outbuf); 5.97 } 5.98 - 5.99 - /* end monospace formatting */ 5.100 - try_write(wfnc, "</pre>\n", 7, outbuf, written, maxlen); 5.101 - 5.102 + 5.103 + /* end code formatting */ 5.104 + written += wfnc("</div>\n", 1, 7, outbuf); 5.105 + 5.106 /* cleanup and return */ 5.107 - ucx_buffer_free(hd->primary_buffer); 5.108 - ucx_buffer_free(hd->secondary_buffer); 5.109 - free(hd); 5.110 - ucx_buffer_free(line); 5.111 - 5.112 + cxBufferDestroy(&hd.primary_buffer); 5.113 + cxBufferDestroy(&hd.secondary_buffer); 5.114 + cxBufferDestroy(&out_line); 5.115 + 5.116 return written; 5.117 } 5.118 5.119 -size_t c2html_formatn(void* inputbuffer, read_func rfnc, 5.120 - char* ibuf, size_t ibuflen, void* outputbuffer, write_func wfnc, 5.121 - size_t maxlen, c2html_highlighter_func hltr, int showln) { 5.122 - 5.123 - UcxBuffer *content = ucx_buffer_new(NULL, ibuflen*2, UCX_BUFFER_AUTOEXTEND); 5.124 - ucx_stream_copy(inputbuffer, content, rfnc, (write_func) ucx_buffer_write, 5.125 - ibuf, ibuflen, (size_t)-1); 5.126 +size_t c2html_bformat( 5.127 + char const *inputbuffer, 5.128 + size_t inputbuflen, 5.129 + void *outbuf, 5.130 + cx_write_func wfnc, 5.131 + c2html_highlighter_func highlighter, 5.132 + int showln 5.133 +) { 5.134 + /* a rough estimate for the number of lines */ 5.135 + size_t est_cap = 16 + inputbuflen / 40; 5.136 5.137 - size_t n = c2html_bformatn(content->space, content->size, 5.138 - outputbuffer, wfnc, maxlen, hltr, showln); 5.139 - 5.140 - ucx_buffer_free(content); 5.141 - return n; 5.142 -} 5.143 - 5.144 -size_t c2html_format(void* inputbuffer, read_func rfnc, 5.145 - char* ibuf, size_t ibuflen, void* outputbuffer, write_func wfnc, 5.146 - c2html_highlighter_func hltr, int showln) { 5.147 - return c2html_formatn(inputbuffer, rfnc, ibuf, ibuflen, 5.148 - outputbuffer, wfnc, (size_t)-1, hltr, showln); 5.149 -} 5.150 - 5.151 -size_t c2html_fformat(FILE* inputfile, char *ibuf, size_t ibuflen, 5.152 - void* outputbuffer, write_func wfnc, 5.153 - c2html_highlighter_func hltr, int showln) { 5.154 - return c2html_format(inputfile, (read_func) fread, ibuf, ibuflen, 5.155 - outputbuffer, wfnc, hltr, showln); 5.156 -} 5.157 - 5.158 -void c2html_fformatf(FILE *inputfile, char *ibuf, size_t ibuflen, 5.159 - FILE* outputfile, c2html_highlighter_func hltr, int showln) { 5.160 - c2html_format(inputfile, (read_func) fread, ibuf, ibuflen, 5.161 - outputfile, (write_func) fwrite, hltr, showln); 5.162 -} 5.163 - 5.164 -size_t c2html_bformatn(const char* inputbuffer, size_t inputbuflen, 5.165 - void* outputbuffer, write_func wfnc, 5.166 - size_t maxlen, c2html_highlighter_func hltr, int showln) { 5.167 - UcxList *lines = ucx_list_append(NULL, (char*)inputbuffer); 5.168 - for (size_t i = 1 ; i < inputbuflen ; i++) { 5.169 - if (inputbuffer[i] == '\n' && i+1 < inputbuflen) { 5.170 - ucx_list_append(lines, (char*)inputbuffer+i+1); 5.171 + /* create the line pointer array */ 5.172 + CxList *lines = cxArrayListCreateSimple(CX_STORE_POINTERS, est_cap); 5.173 + cxListAdd(lines, inputbuffer); 5.174 + for (size_t i = 1; i < inputbuflen; i++) { 5.175 + if (inputbuffer[i] == '\n' && i + 1 < inputbuflen) { 5.176 + cxListAdd(lines, inputbuffer + i + 1); 5.177 } 5.178 } 5.179 - 5.180 - size_t n = formatlines(hltr, lines, outputbuffer, wfnc, maxlen, showln); 5.181 - 5.182 - ucx_list_free(lines); 5.183 + 5.184 + /* invoke the other function */ 5.185 + size_t n = c2html_format(lines, outbuf, wfnc, highlighter, showln); 5.186 + 5.187 + /* cleanup and return */ 5.188 + cxListDestroy(lines); 5.189 return n; 5.190 -} 5.191 - 5.192 -size_t c2html_bformat(const char* inputbuffer, size_t inputbuflen, 5.193 - void* outputbuffer, write_func wfnc, 5.194 - c2html_highlighter_func hltr, int showln) { 5.195 - return c2html_bformatn(inputbuffer, inputbuflen, outputbuffer, wfnc, 5.196 - (size_t)-1, hltr, showln); 5.197 -} 5.198 - 5.199 -void c2html_bformatf(const char* inputbuffer, size_t inputbuflen, 5.200 - FILE* outputfile, c2html_highlighter_func hltr, int showln) { 5.201 - c2html_bformatn(inputbuffer, inputbuflen, outputfile, 5.202 - (write_func) fwrite, (size_t)-1, hltr, showln); 5.203 -} 5.204 +} 5.205 \ No newline at end of file
6.1 --- a/src/c2html.h Thu Nov 10 18:44:48 2016 +0100 6.2 +++ b/src/c2html.h Mon Apr 24 20:54:38 2023 +0200 6.3 @@ -31,61 +31,23 @@ 6.4 #define C2HTML_H 6.5 6.6 #include <stdio.h> 6.7 +#include <cx/list.h> 6.8 #include "highlighter.h" 6.9 6.10 #ifdef __cplusplus 6.11 extern "C" { 6.12 #endif 6.13 6.14 -#define VERSION_MAJOR 2 6.15 +#define VERSION_MAJOR 3 6.16 #define VERSION_MINOR 0 6.17 #define VERSION_DEVELOP 0 /* set this to zero for release version */ 6.18 6.19 /** 6.20 - * Reads a source file from the input buffer and writes at most 6.21 - * <code>maxlen</code> bytes of the formatted output to the output buffer. 6.22 + * Writes the formatted source data to the output buffer. 6.23 * 6.24 - * The output buffer must either be large enough to hold <code>maxlen</code> 6.25 - * bytes or the write function must trigger an automatic extension of the 6.26 - * buffer. 6.27 - * 6.28 - * The input is copied via an intermediate buffer to an internal buffer. 6.29 - * 6.30 - * @param inputbuffer the input buffer 6.31 - * @param rfnc a read function operating for the input buffer 6.32 - * @param ibuf intermediate processing buffer 6.33 - * @param ibuflen length of intermediate processing buffer 6.34 - * @param outputbuffer the output buffer 6.35 - * @param wfnc a write function for the output buffer 6.36 - * @param maxlen the maximum amount bytes which will be written to the 6.37 - * output buffer 6.38 - * @param hltr the highlighter function 6.39 - * @param showln zero, if line numbers shall be omitted, nonzero otherwise 6.40 - * 6.41 - * @return total amount of bytes written to the output buffer 6.42 - * 6.43 - * @see c2html_plain_highlighter() 6.44 - * @see c2html_c_highlighter() 6.45 - * @see c2html_java_highlighter() 6.46 - */ 6.47 -size_t c2html_formatn(void* inputbuffer, read_func rfnc, 6.48 - char* ibuf, size_t ibuflen, void* outputbuffer, write_func wfnc, 6.49 - size_t maxlen, c2html_highlighter_func hltr, int showln); 6.50 - 6.51 -/** 6.52 - * Reads a source file from the input buffer and writes the formatted output 6.53 - * to an output buffer. 6.54 - * 6.55 - * The output buffer must either be large enough to hold the formatted data 6.56 - * or the write function must trigger an automatic extension of the buffer. 6.57 - * 6.58 - * The input is copied via an intermediate buffer to an internal buffer. 6.59 - * 6.60 - * @param inputbuffer the input buffer 6.61 - * @param rfnc a read function operating for the input buffer 6.62 - * @param ibuf intermediate processing buffer 6.63 - * @param ibuflen length of intermediate processing buffer 6.64 - * @param outputbuffer the output buffer 6.65 + * @param inputbuffer the source file data as string 6.66 + * @param inputbuflen the length of the source file 6.67 + * @param outbuf the output buffer 6.68 * @param wfnc a write function for the output buffer 6.69 * @param hltr the highlighter function 6.70 * @param showln zero, if line numbers shall be omitted, nonzero otherwise 6.71 @@ -96,121 +58,32 @@ 6.72 * @see c2html_c_highlighter() 6.73 * @see c2html_java_highlighter() 6.74 */ 6.75 -size_t c2html_format(void* inputbuffer, read_func rfnc, 6.76 - char* ibuf, size_t ibuflen, void* outputbuffer, write_func wfnc, 6.77 - c2html_highlighter_func hltr, int showln); 6.78 +size_t c2html_bformat(char const* inputbuffer, size_t inputbuflen, 6.79 + void* outbuf, cx_write_func wfnc, 6.80 + c2html_highlighter_func hltr, int showln); 6.81 6.82 /** 6.83 - * Reads a source file from the specified input file stream and writes the 6.84 - * formatted output to an output buffer. 6.85 - * 6.86 - * The output buffer must either be large enough to hold the formatted data 6.87 - * or the write function must trigger an automatic extension of the buffer. 6.88 - * 6.89 - * The input is copied via an intermediate buffer to an internal buffer. 6.90 - * For files, the recommended intermediate buffer length is the file system 6.91 - * block size. 6.92 - * 6.93 - * @param inputfile the input file stream 6.94 - * @param ibuf intermediate processing buffer 6.95 - * @param ibuflen length of intermediate processing buffer 6.96 - * @param outputbuffer the output buffer 6.97 + * Writes the formatted source data to the output buffer. 6.98 + * 6.99 + * This function takes a list of \c char* that point to the beginning of each 6.100 + * line. These pointers may point directly into the source text and the strings 6.101 + * do not need to be zero-terminated, but the line-breaks must be included. 6.102 + * 6.103 + * @param lines a list of pointers to the beginning of each line 6.104 + * @param outbuf the output buffer 6.105 * @param wfnc a write function for the output buffer 6.106 * @param hltr the highlighter function 6.107 * @param showln zero, if line numbers shall be omitted, nonzero otherwise 6.108 - * 6.109 + * 6.110 * @return total amount of bytes written to the output buffer 6.111 - * 6.112 + * 6.113 * @see c2html_plain_highlighter() 6.114 * @see c2html_c_highlighter() 6.115 * @see c2html_java_highlighter() 6.116 */ 6.117 -size_t c2html_fformat(FILE* inputfile, char *ibuf, size_t ibuflen, 6.118 - void* outputbuffer, write_func wfnc, 6.119 - c2html_highlighter_func hltr, int showln); 6.120 - 6.121 -/** 6.122 - * Reads a source file from the specified input file stream and directly writes 6.123 - * the formatted output to the output file stream. 6.124 - * 6.125 - * For files, the recommended intermediate buffer length is the file system 6.126 - * block size. 6.127 - * 6.128 - * @param inputfile the input file stream 6.129 - * @param ibuf intermediate processing buffer 6.130 - * @param ibuflen length of intermediate processing buffer (recommended: 4 KB) 6.131 - * @param outputfile the output file stream 6.132 - * @param hltr the highlighter function 6.133 - * @param showln zero, if line numbers shall be omitted, nonzero otherwise 6.134 - * 6.135 - * @see c2html_plain_highlighter() 6.136 - * @see c2html_c_highlighter() 6.137 - * @see c2html_java_highlighter() 6.138 - */ 6.139 -void c2html_fformatf(FILE *inputfile, char *ibuf, size_t ibuflen, 6.140 - FILE* outputfile, c2html_highlighter_func hltr, int showln); 6.141 - 6.142 - 6.143 -/** 6.144 - * Writes at most <code>maxlen</code> bytes of formatted source data to the 6.145 - * output buffer. 6.146 - * 6.147 - * @param inputbuffer the source file data as string 6.148 - * @param inputbuflen the length of the source file 6.149 - * @param outputbuffer the output buffer 6.150 - * @param wfnc a write function for the output buffer 6.151 - * @param maxlen the maximum amount bytes which will be written to the 6.152 - * output buffer 6.153 - * @param hltr the highlighter function 6.154 - * @param showln zero, if line numbers shall be omitted, nonzero otherwise 6.155 - * 6.156 - * @return total amount of bytes written to the output buffer 6.157 - * 6.158 - * @see c2html_plain_highlighter() 6.159 - * @see c2html_c_highlighter() 6.160 - * @see c2html_java_highlighter() 6.161 - */ 6.162 -size_t c2html_bformatn(const char* inputbuffer, size_t inputbuflen, 6.163 - void* outputbuffer, write_func wfnc, 6.164 - size_t maxlen, c2html_highlighter_func hltr, int showln); 6.165 - 6.166 - 6.167 -/** 6.168 - * Writes the formatted source data to the output buffer. 6.169 - * 6.170 - * @param inputbuffer the source file data as string 6.171 - * @param inputbuflen the length of the source file 6.172 - * @param outputbuffer the output buffer 6.173 - * @param wfnc a write function for the output buffer 6.174 - * @param hltr the highlighter function 6.175 - * @param showln zero, if line numbers shall be omitted, nonzero otherwise 6.176 - * 6.177 - * @return total amount of bytes written to the output buffer 6.178 - * 6.179 - * @see c2html_plain_highlighter() 6.180 - * @see c2html_c_highlighter() 6.181 - * @see c2html_java_highlighter() 6.182 - */ 6.183 -size_t c2html_bformat(const char* inputbuffer, size_t inputbuflen, 6.184 - void* outputbuffer, write_func wfnc, 6.185 - c2html_highlighter_func hltr, int showln); 6.186 - 6.187 - 6.188 -/** 6.189 - * Writes the formatted source data directly to the specified file stream. 6.190 - * 6.191 - * @param inputbuffer the source file data as string 6.192 - * @param inputbuflen the length of the source file 6.193 - * @param outputfile the output file stream 6.194 - * @param hltr the highlighter function 6.195 - * @param showln zero, if line numbers shall be omitted, nonzero otherwise 6.196 - * 6.197 - * @see c2html_plain_highlighter() 6.198 - * @see c2html_c_highlighter() 6.199 - * @see c2html_java_highlighter() 6.200 - */ 6.201 -void c2html_bformatf(const char* inputbuffer, size_t inputbuflen, 6.202 - FILE* outputfile, c2html_highlighter_func hltr, int showln); 6.203 +size_t c2html_format(CxList const* lines, 6.204 + void* outbuf, cx_write_func wfnc, 6.205 + c2html_highlighter_func hltr, int showln); 6.206 6.207 #ifdef __cplusplus 6.208 }
7.1 --- a/src/frontend.c Thu Nov 10 18:44:48 2016 +0100 7.2 +++ b/src/frontend.c Mon Apr 24 20:54:38 2023 +0200 7.3 @@ -33,9 +33,7 @@ 7.4 #include <string.h> 7.5 7.6 #include "c2html.h" 7.7 -#include "ucx/utils.h" 7.8 - 7.9 -#define FILEBUF_SIZE 4096 7.10 +#include <cx/utils.h> 7.11 7.12 typedef struct { 7.13 char* outfilename; 7.14 @@ -45,20 +43,17 @@ 7.15 int showlinenumbers; 7.16 } Settings; 7.17 7.18 -static int appendfile(const char *filename, FILE *fout, 7.19 - char *copybuf, size_t copybuflen, const char *errmsg) { 7.20 - FILE *headerfile = fopen(filename, "r"); 7.21 - if (!headerfile) { 7.22 +static int appendfile(const char *filename, FILE *fout, const char *errmsg) { 7.23 + FILE *fin = fopen(filename, "r"); 7.24 + if (!fin) { 7.25 perror(errmsg); 7.26 if (fout != stdout) { 7.27 fclose(fout); 7.28 } 7.29 return 1; 7.30 } 7.31 - ucx_stream_copy(headerfile, fout, 7.32 - (read_func) fread, (write_func) fwrite, 7.33 - copybuf, copybuflen, (size_t)-1); 7.34 - fclose(headerfile); 7.35 + cx_stream_copy(fin, fout, (cx_read_func) fread, (cx_write_func) fwrite); 7.36 + fclose(fin); 7.37 return 0; 7.38 } 7.39 7.40 @@ -86,7 +81,7 @@ 7.41 c2html_highlighter_func hltr = c2html_c_highlighter; 7.42 7.43 /* Parse command line */ 7.44 - char optc; 7.45 + int optc; 7.46 while ((optc = getopt(argc, argv, "hljo:pH:F:vV")) != -1) { 7.47 switch (optc) { 7.48 case 'o': 7.49 @@ -142,15 +137,8 @@ 7.50 fout = stdout; 7.51 } 7.52 7.53 - /* Allocate file buffer */ 7.54 - char *filebuf = malloc(FILEBUF_SIZE); 7.55 - if (!filebuf) { 7.56 - perror("Error allocating file buffer"); 7.57 - return EXIT_FAILURE; 7.58 - } 7.59 - 7.60 /* Prepend header file */ 7.61 - if (appendfile(settings.headerfile, fout, filebuf, FILEBUF_SIZE, 7.62 + if (appendfile(settings.headerfile, fout, 7.63 "Error opening header file")) { 7.64 return EXIT_FAILURE; 7.65 } 7.66 @@ -158,11 +146,17 @@ 7.67 /* Process input file */ 7.68 FILE *inputfile = fopen(settings.infilename, "r"); 7.69 if (inputfile) { 7.70 - c2html_fformatf( 7.71 - inputfile, filebuf, FILEBUF_SIZE, 7.72 - fout, hltr, settings.showlinenumbers 7.73 + CxBuffer fbuf; 7.74 + cxBufferInit(&fbuf, NULL, 4096, NULL, CX_BUFFER_AUTO_EXTEND); 7.75 + cx_stream_copy(inputfile, &fbuf, (cx_read_func) fread, 7.76 + (cx_write_func) cxBufferWrite); 7.77 + fclose(inputfile); 7.78 + c2html_bformat( 7.79 + fbuf.space, fbuf.size, 7.80 + fout, (cx_write_func ) fwrite, hltr, 7.81 + settings.showlinenumbers 7.82 ); 7.83 - fclose(inputfile); 7.84 + cxBufferDestroy(&fbuf); 7.85 } else { 7.86 perror("Error opening input file"); 7.87 if (fout != stdout) { 7.88 @@ -172,12 +166,10 @@ 7.89 } 7.90 7.91 /* Append footer file */ 7.92 - if (appendfile(settings.footerfile, fout, filebuf, FILEBUF_SIZE, 7.93 + if (appendfile(settings.footerfile, fout, 7.94 "Error opening footer file")) { 7.95 return EXIT_FAILURE; 7.96 } 7.97 - 7.98 - free(filebuf); 7.99 7.100 return EXIT_SUCCESS; 7.101 }
8.1 --- a/src/highlighter.c Thu Nov 10 18:44:48 2016 +0100 8.2 +++ b/src/highlighter.c Mon Apr 24 20:54:38 2023 +0200 8.3 @@ -33,35 +33,35 @@ 8.4 #include <stdio.h> 8.5 #include <string.h> 8.6 #include <ctype.h> 8.7 -#include "ucx/string.h" 8.8 -#include "ucx/utils.h" 8.9 8.10 -static void put_htmlescaped(UcxBuffer *dest, char c) { 8.11 +#include <cx/string.h> 8.12 + 8.13 +static void put_htmlescaped(CxBuffer *dest, char c) { 8.14 if (c == '>') { 8.15 - ucx_buffer_puts(dest, ">"); 8.16 + cxBufferPutString(dest, ">"); 8.17 } else if (c == '<') { 8.18 - ucx_buffer_puts(dest, "<"); 8.19 + cxBufferPutString(dest, "<"); 8.20 } else if (c) { 8.21 - ucx_buffer_putc(dest, c); 8.22 + cxBufferPut(dest, c); 8.23 } 8.24 } 8.25 8.26 -static void put_htmlescapedstr(UcxBuffer *dest, sstr_t s) { 8.27 +static void put_htmlescapedstr(CxBuffer *dest, cxstring s) { 8.28 for (int i = 0 ; i < s.length ; i++) { 8.29 put_htmlescaped(dest, s.ptr[i]); 8.30 } 8.31 } 8.32 8.33 -static int check_keyword(sstr_t word, const char** keywords) { 8.34 +static int check_keyword(cxstring word, const char** keywords) { 8.35 for (int i = 0 ; keywords[i] ; i++) { 8.36 - if (sstrcmp(word, sstr((char*)keywords[i])) == 0) { 8.37 + if (cx_strcmp(word, cx_str(keywords[i])) == 0) { 8.38 return 1; 8.39 } 8.40 } 8.41 return 0; 8.42 } 8.43 8.44 -static int check_capsonly(sstr_t word) { 8.45 +static int check_capsonly(cxstring word) { 8.46 for (size_t i = 0 ; i < word.length ; i++) { 8.47 if (!isupper(word.ptr[i]) && !isdigit(word.ptr[i]) 8.48 && word.ptr[i] != '_') { 8.49 @@ -73,7 +73,7 @@ 8.50 8.51 /* Plaintext Highlighter */ 8.52 8.53 -void c2html_plain_highlighter(char *src, UcxBuffer *dest, 8.54 +void c2html_plain_highlighter(char const *src, CxBuffer *dest, 8.55 c2html_highlighter_data *hd) { 8.56 while (*src && *src != '\n') { 8.57 if (*src != '\r') { 8.58 @@ -81,7 +81,7 @@ 8.59 } 8.60 src++; 8.61 } 8.62 - ucx_buffer_putc(dest, '\n'); 8.63 + cxBufferPut(dest, '\n'); 8.64 } 8.65 8.66 /* C Highlighter */ 8.67 @@ -94,15 +94,15 @@ 8.68 "while", NULL 8.69 }; 8.70 8.71 -void c2html_c_highlighter(char *src, UcxBuffer *dest, 8.72 +void c2html_c_highlighter(char const *src, CxBuffer *dest, 8.73 c2html_highlighter_data *hd) { 8.74 /* reset buffers without clearing them */ 8.75 - hd->primary_buffer->size = hd->primary_buffer->pos = 0; 8.76 - hd->secondary_buffer->size = hd->secondary_buffer->pos = 0; 8.77 + hd->primary_buffer.size = hd->primary_buffer.pos = 0; 8.78 + hd->secondary_buffer.size = hd->secondary_buffer.pos = 0; 8.79 8.80 /* alias the buffers for better handling */ 8.81 - UcxBuffer *wbuf = hd->primary_buffer; 8.82 - UcxBuffer *ifilebuf = hd->secondary_buffer; 8.83 + CxBuffer *wbuf = &hd->primary_buffer; 8.84 + CxBuffer *ifilebuf = &hd->secondary_buffer; 8.85 8.86 /* local information */ 8.87 size_t sp = (size_t)-1; 8.88 @@ -113,7 +113,7 @@ 8.89 /* continue a multi line comment highlighting */ 8.90 if (hd->multiline_comment) { 8.91 iscomment = 1; 8.92 - ucx_buffer_puts(dest, "<span class=\"c2html-comment\">"); 8.93 + cxBufferPutString(dest, "<span class=\"c2html-comment\">"); 8.94 } 8.95 8.96 char c; 8.97 @@ -126,42 +126,42 @@ 8.98 if (hd->multiline_comment && sp > 0 && src[sp-1] == '*') { 8.99 iscomment = 0; 8.100 hd->multiline_comment = 0; 8.101 - ucx_buffer_puts(dest, "/</span>"); 8.102 + cxBufferPutString(dest, "/</span>"); 8.103 continue; 8.104 } else if (!iscomment && (src[sp+1] == '/' || src[sp+1] == '*')) { 8.105 iscomment = 1; 8.106 hd->multiline_comment = (src[sp+1] == '*'); 8.107 - ucx_buffer_puts(dest, "<span class=\"c2html-comment\">"); 8.108 + cxBufferPutString(dest, "<span class=\"c2html-comment\">"); 8.109 } 8.110 } 8.111 8.112 if (iscomment) { 8.113 if (c == '\n') { 8.114 - ucx_buffer_puts(dest, "</span>\n"); 8.115 + cxBufferPutString(dest, "</span>\n"); 8.116 } else { 8.117 put_htmlescaped(dest, c); 8.118 } 8.119 } else if (isinclude) { 8.120 if (c == '<') { 8.121 - ucx_buffer_puts(dest, 8.122 + cxBufferPutString(dest, 8.123 "<span class=\"c2html-stdinclude\"><"); 8.124 } else if (c == '\"') { 8.125 if (parseinclude) { 8.126 - ucx_buffer_puts(dest, "\">"); 8.127 - ucx_buffer_write(ifilebuf->space, 1, ifilebuf->size, dest); 8.128 - ucx_buffer_puts(dest, "\"</a>"); 8.129 + cxBufferPutString(dest, "\">"); 8.130 + cxBufferWrite(ifilebuf->space, 1, ifilebuf->size, dest); 8.131 + cxBufferPutString(dest, "\"</a>"); 8.132 parseinclude = 0; 8.133 } else { 8.134 - ucx_buffer_puts(dest, 8.135 + cxBufferPutString(dest, 8.136 "<a class=\"c2html-userinclude\" href=\""); 8.137 - ucx_buffer_putc(ifilebuf, '\"'); 8.138 + cxBufferPut(ifilebuf, '\"'); 8.139 parseinclude = 1; 8.140 } 8.141 } else if (c == '>') { 8.142 - ucx_buffer_puts(dest, "></span>"); 8.143 + cxBufferPutString(dest, "></span>"); 8.144 } else { 8.145 if (parseinclude) { 8.146 - ucx_buffer_putc(ifilebuf, c); 8.147 + cxBufferPut(ifilebuf, c); 8.148 } 8.149 put_htmlescaped(dest, c); 8.150 } 8.151 @@ -172,14 +172,14 @@ 8.152 put_htmlescaped(dest, c); 8.153 if (c == quote) { 8.154 isstring = 0; 8.155 - ucx_buffer_puts(dest, "</span>"); 8.156 + cxBufferPutString(dest, "</span>"); 8.157 } else { 8.158 put_htmlescaped(dest, c); 8.159 } 8.160 } else { 8.161 isstring = 1; 8.162 quote = c; 8.163 - ucx_buffer_puts(dest, "<span class=\"c2html-string\">"); 8.164 + cxBufferPutString(dest, "<span class=\"c2html-string\">"); 8.165 put_htmlescaped(dest, c); 8.166 } 8.167 } else { 8.168 @@ -187,32 +187,32 @@ 8.169 put_htmlescaped(dest, c); 8.170 } else if (isalnum(c) || c == '_' || c == '#') { 8.171 /* buffer the current word */ 8.172 - ucx_buffer_putc(wbuf, c); 8.173 + cxBufferPut(wbuf, c); 8.174 } else { 8.175 /* write buffered word, if any */ 8.176 if (wbuf->size > 0) { 8.177 - sstr_t word = sstrn(wbuf->space, wbuf->size); 8.178 + cxstring word = cx_strn(wbuf->space, wbuf->size); 8.179 int closespan = 1; 8.180 - sstr_t typesuffix = ST("_t"); 8.181 + cxstring typesuffix = CX_STR("_t"); 8.182 if (check_keyword(word, ckeywords)) { 8.183 - ucx_buffer_puts(dest, 8.184 + cxBufferPutString(dest, 8.185 "<span class=\"c2html-keyword\">"); 8.186 - } else if (sstrsuffix(word, typesuffix)) { 8.187 - ucx_buffer_puts(dest, 8.188 + } else if (cx_strsuffix(word, typesuffix)) { 8.189 + cxBufferPutString(dest, 8.190 "<span class=\"c2html-type\">"); 8.191 } else if (word.ptr[0] == '#') { 8.192 - isinclude = !sstrcmp(word, S("#include")); 8.193 - ucx_buffer_puts(dest, 8.194 + isinclude = !cx_strcmp(word, CX_STR("#include")); 8.195 + cxBufferPutString(dest, 8.196 "<span class=\"c2html-directive\">"); 8.197 } else if (check_capsonly(word)) { 8.198 - ucx_buffer_puts(dest, 8.199 + cxBufferPutString(dest, 8.200 "<span class=\"c2html-macroconst\">"); 8.201 } else { 8.202 closespan = 0; 8.203 } 8.204 put_htmlescapedstr(dest, word); 8.205 if (closespan) { 8.206 - ucx_buffer_puts(dest, "</span>"); 8.207 + cxBufferPutString(dest, "</span>"); 8.208 } 8.209 } 8.210 wbuf->pos = wbuf->size = 0; /* reset word buffer */ 8.211 @@ -239,14 +239,14 @@ 8.212 "volatile", "const", "float", "native", "super", "while", NULL 8.213 }; 8.214 8.215 -void c2html_java_highlighter(char *src, UcxBuffer *dest, 8.216 +void c2html_java_highlighter(char const *src, CxBuffer *dest, 8.217 c2html_highlighter_data *hd) { 8.218 /* reset buffers without clearing them */ 8.219 - hd->primary_buffer->size = hd->primary_buffer->pos = 0; 8.220 - hd->secondary_buffer->size = hd->secondary_buffer->pos = 0; 8.221 + hd->primary_buffer.size = hd->primary_buffer.pos = 0; 8.222 + hd->secondary_buffer.size = hd->secondary_buffer.pos = 0; 8.223 8.224 /* alias the buffers for better handling */ 8.225 - UcxBuffer *wbuf = hd->primary_buffer; 8.226 + CxBuffer *wbuf = &hd->primary_buffer; 8.227 8.228 /* local information */ 8.229 size_t sp = (size_t)-1; 8.230 @@ -256,7 +256,7 @@ 8.231 8.232 if (hd->multiline_comment) { 8.233 iscomment = 1; 8.234 - ucx_buffer_puts(dest, "<span class=\"c2html-comment\">"); 8.235 + cxBufferPutString(dest, "<span class=\"c2html-comment\">"); 8.236 } 8.237 8.238 char c; 8.239 @@ -269,18 +269,18 @@ 8.240 if (hd->multiline_comment && sp > 0 && src[sp-1] == '*') { 8.241 iscomment = 0; 8.242 hd->multiline_comment = 0; 8.243 - ucx_buffer_puts(dest, "/</span>"); 8.244 + cxBufferPutString(dest, "/</span>"); 8.245 continue; 8.246 } else if (!iscomment && (src[sp+1] == '/' || src[sp+1] == '*')) { 8.247 iscomment = 1; 8.248 hd->multiline_comment = (src[sp+1] == '*'); 8.249 - ucx_buffer_puts(dest, "<span class=\"c2html-comment\">"); 8.250 + cxBufferPutString(dest, "<span class=\"c2html-comment\">"); 8.251 } 8.252 } 8.253 8.254 if (iscomment) { 8.255 if (c == '\n') { 8.256 - ucx_buffer_puts(dest, "</span>\n"); 8.257 + cxBufferPutString(dest, "</span>\n"); 8.258 } else { 8.259 put_htmlescaped(dest, c); 8.260 } 8.261 @@ -293,14 +293,14 @@ 8.262 put_htmlescaped(dest, c); 8.263 if (c == quote) { 8.264 isstring = 0; 8.265 - ucx_buffer_puts(dest, "</span>"); 8.266 + cxBufferPutString(dest, "</span>"); 8.267 } else { 8.268 put_htmlescaped(dest, c); 8.269 } 8.270 } else { 8.271 isstring = 1; 8.272 quote = c; 8.273 - ucx_buffer_puts(dest, 8.274 + cxBufferPutString(dest, 8.275 "<span class=\"c2html-string\">"); 8.276 put_htmlescaped(dest, c); 8.277 } 8.278 @@ -309,23 +309,23 @@ 8.279 put_htmlescaped(dest, c); 8.280 } else if (isalnum(c) || c == '_' || c == '@') { 8.281 /* buffer the current word */ 8.282 - ucx_buffer_putc(wbuf, c); 8.283 + cxBufferPut(wbuf, c); 8.284 } else { 8.285 /* write buffered word, if any */ 8.286 if (wbuf->size > 0) { 8.287 - sstr_t word = sstrn(wbuf->space, wbuf->size); 8.288 + cxstring word = cx_strn(wbuf->space, wbuf->size); 8.289 int closespan = 1; 8.290 if (check_keyword(word, jkeywords)) { 8.291 - ucx_buffer_puts(dest, 8.292 + cxBufferPutString(dest, 8.293 "<span class=\"c2html-keyword\">"); 8.294 } else if (isupper(word.ptr[0])) { 8.295 - ucx_buffer_puts(dest, 8.296 + cxBufferPutString(dest, 8.297 "<span class=\"c2html-type\">"); 8.298 } else if (word.ptr[0] == '@') { 8.299 - ucx_buffer_puts(dest, 8.300 + cxBufferPutString(dest, 8.301 "<span class=\"c2html-directive\">"); 8.302 } else if (check_capsonly(word)) { 8.303 - ucx_buffer_puts(dest, 8.304 + cxBufferPutString(dest, 8.305 "<span class=\"c2html-macroconst\">"); 8.306 } else { 8.307 closespan = 0; 8.308 @@ -333,7 +333,7 @@ 8.309 put_htmlescapedstr(dest, word); 8.310 8.311 if (closespan) { 8.312 - ucx_buffer_puts(dest, "</span>"); 8.313 + cxBufferPutString(dest, "</span>"); 8.314 } 8.315 } 8.316 wbuf->pos = wbuf->size = 0; /* reset buffer */
9.1 --- a/src/highlighter.h Thu Nov 10 18:44:48 2016 +0100 9.2 +++ b/src/highlighter.h Mon Apr 24 20:54:38 2023 +0200 9.3 @@ -30,7 +30,7 @@ 9.4 #ifndef C2HTML_HIGHLIGHTER_H 9.5 #define C2HTML_HIGHLIGHTER_H 9.6 9.7 -#include "ucx/buffer.h" 9.8 +#include <cx/buffer.h> 9.9 9.10 #ifdef __cplusplus 9.11 extern "C" { 9.12 @@ -38,12 +38,12 @@ 9.13 9.14 typedef struct { 9.15 int multiline_comment; 9.16 - UcxBuffer* primary_buffer; 9.17 - UcxBuffer* secondary_buffer; 9.18 + CxBuffer primary_buffer; 9.19 + CxBuffer secondary_buffer; 9.20 } c2html_highlighter_data; 9.21 9.22 #define C2HTML_HIGHLIGHTER_SIGNATURE \ 9.23 -char*,UcxBuffer*, c2html_highlighter_data* 9.24 +char const*, CxBuffer*, c2html_highlighter_data* 9.25 9.26 typedef void(*c2html_highlighter_func)(C2HTML_HIGHLIGHTER_SIGNATURE); 9.27
10.1 --- a/src/ucx/allocator.c Thu Nov 10 18:44:48 2016 +0100 10.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 10.3 @@ -1,59 +0,0 @@ 10.4 -/* 10.5 - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 10.6 - * 10.7 - * Copyright 2015 Olaf Wintermann. All rights reserved. 10.8 - * 10.9 - * Redistribution and use in source and binary forms, with or without 10.10 - * modification, are permitted provided that the following conditions are met: 10.11 - * 10.12 - * 1. Redistributions of source code must retain the above copyright 10.13 - * notice, this list of conditions and the following disclaimer. 10.14 - * 10.15 - * 2. Redistributions in binary form must reproduce the above copyright 10.16 - * notice, this list of conditions and the following disclaimer in the 10.17 - * documentation and/or other materials provided with the distribution. 10.18 - * 10.19 - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 10.20 - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 10.21 - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 10.22 - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 10.23 - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 10.24 - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 10.25 - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 10.26 - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 10.27 - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 10.28 - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 10.29 - * POSSIBILITY OF SUCH DAMAGE. 10.30 - */ 10.31 - 10.32 -#include <stdlib.h> 10.33 -#include "allocator.h" 10.34 - 10.35 -UcxAllocator default_allocator = { 10.36 - NULL, 10.37 - ucx_default_malloc, 10.38 - ucx_default_calloc, 10.39 - ucx_default_realloc, 10.40 - ucx_default_free 10.41 -}; 10.42 - 10.43 -UcxAllocator *ucx_default_allocator() { 10.44 - UcxAllocator *allocator = &default_allocator; 10.45 - return allocator; 10.46 -} 10.47 - 10.48 -void *ucx_default_malloc(void *ignore, size_t n) { 10.49 - return malloc(n); 10.50 -} 10.51 - 10.52 -void *ucx_default_calloc(void *ignore, size_t n, size_t size) { 10.53 - return calloc(n, size); 10.54 -} 10.55 - 10.56 -void *ucx_default_realloc(void *ignore, void *data, size_t n) { 10.57 - return realloc(data, n); 10.58 -} 10.59 - 10.60 -void ucx_default_free(void *ignore, void *data) { 10.61 - free(data); 10.62 -}
11.1 --- a/src/ucx/allocator.h Thu Nov 10 18:44:48 2016 +0100 11.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 11.3 @@ -1,206 +0,0 @@ 11.4 -/* 11.5 - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 11.6 - * 11.7 - * Copyright 2015 Olaf Wintermann. All rights reserved. 11.8 - * 11.9 - * Redistribution and use in source and binary forms, with or without 11.10 - * modification, are permitted provided that the following conditions are met: 11.11 - * 11.12 - * 1. Redistributions of source code must retain the above copyright 11.13 - * notice, this list of conditions and the following disclaimer. 11.14 - * 11.15 - * 2. Redistributions in binary form must reproduce the above copyright 11.16 - * notice, this list of conditions and the following disclaimer in the 11.17 - * documentation and/or other materials provided with the distribution. 11.18 - * 11.19 - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 11.20 - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 11.21 - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 11.22 - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 11.23 - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 11.24 - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 11.25 - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 11.26 - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 11.27 - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 11.28 - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 11.29 - * POSSIBILITY OF SUCH DAMAGE. 11.30 - */ 11.31 -/** 11.32 - * Allocator for custom memory management. 11.33 - * 11.34 - * An UCX allocator consists of a pointer to the memory area / pool and four 11.35 - * function pointers to memory management functions operating on this memory 11.36 - * area / pool. These functions shall behave equivalent to the standard libc 11.37 - * functions <code>malloc(), calloc(), realloc()</code> and <code>free()</code>. 11.38 - * 11.39 - * The signature of the memory management functions is based on the signature 11.40 - * of the respective libc function but each of them takes the pointer to the 11.41 - * memory area / pool as first argument. 11.42 - * 11.43 - * As the pointer to the memory area / pool can be arbitrarily chosen, any data 11.44 - * can be provided to the memory management functions. An UcxMempool is just 11.45 - * one example. 11.46 - * 11.47 - * @see mempool.h 11.48 - * @see UcxMap 11.49 - * 11.50 - * @file allocator.h 11.51 - * @author Mike Becker 11.52 - * @author Olaf Wintermann 11.53 - */ 11.54 - 11.55 -#ifndef UCX_ALLOCATOR_H 11.56 -#define UCX_ALLOCATOR_H 11.57 - 11.58 -#include "ucx.h" 11.59 - 11.60 -#ifdef __cplusplus 11.61 -extern "C" { 11.62 -#endif 11.63 - 11.64 -/** 11.65 - * A function pointer to the allocators <code>malloc()</code> function. 11.66 - * @see UcxAllocator 11.67 - */ 11.68 -typedef void*(*ucx_allocator_malloc)(void *pool, size_t n); 11.69 - 11.70 -/** 11.71 - * A function pointer to the allocators <code>calloc()</code> function. 11.72 - * @see UcxAllocator 11.73 - */ 11.74 -typedef void*(*ucx_allocator_calloc)(void *pool, size_t n, size_t size); 11.75 - 11.76 -/** 11.77 - * A function pointer to the allocators <code>realloc()</code> function. 11.78 - * @see UcxAllocator 11.79 - */ 11.80 -typedef void*(*ucx_allocator_realloc)(void *pool, void *data, size_t n); 11.81 - 11.82 -/** 11.83 - * A function pointer to the allocators <code>free()</code> function. 11.84 - * @see UcxAllocator 11.85 - */ 11.86 -typedef void(*ucx_allocator_free)(void *pool, void *data); 11.87 - 11.88 -/** 11.89 - * UCX allocator data structure containing memory management functions. 11.90 - */ 11.91 -typedef struct { 11.92 - /** Pointer to an area of memory or a complex memory pool. 11.93 - * This pointer will be passed to any memory management function as first 11.94 - * argument. 11.95 - */ 11.96 - void *pool; 11.97 - /** 11.98 - * The <code>malloc()</code> function for this allocator. 11.99 - */ 11.100 - ucx_allocator_malloc malloc; 11.101 - /** 11.102 - * The <code>calloc()</code> function for this allocator. 11.103 - */ 11.104 - ucx_allocator_calloc calloc; 11.105 - /** 11.106 - * The <code>realloc()</code> function for this allocator. 11.107 - */ 11.108 - ucx_allocator_realloc realloc; 11.109 - /** 11.110 - * The <code>free()</code> function for this allocator. 11.111 - */ 11.112 - ucx_allocator_free free; 11.113 -} UcxAllocator; 11.114 - 11.115 -/** 11.116 - * Returns a pointer to the default allocator. 11.117 - * 11.118 - * The default allocator contains wrappers to the standard libc memory 11.119 - * management functions. Use this function to get a pointer to a globally 11.120 - * available allocator. You may also define an own UcxAllocator by assigning 11.121 - * #UCX_ALLOCATOR_DEFAULT to a variable and pass the address of this variable 11.122 - * to any function that takes an UcxAllocator as argument. Note that using 11.123 - * this function is the recommended way of passing a default allocator, thus 11.124 - * it never runs out of scope. 11.125 - * 11.126 - * @return a pointer to the default allocator 11.127 - * 11.128 - * @see UCX_ALLOCATOR_DEFAULT 11.129 - */ 11.130 -UcxAllocator *ucx_default_allocator(); 11.131 - 11.132 -/** 11.133 - * A wrapper for the standard libc <code>malloc()</code> function. 11.134 - * @param ignore ignored (may be used by allocators for pooled memory) 11.135 - * @param n argument passed to <code>malloc()</code> 11.136 - * @return return value of <code>malloc()</code> 11.137 - */ 11.138 -void *ucx_default_malloc(void *ignore, size_t n); 11.139 -/** 11.140 - * A wrapper for the standard libc <code>calloc()</code> function. 11.141 - * @param ignore ignored (may be used by allocators for pooled memory) 11.142 - * @param n argument passed to <code>calloc()</code> 11.143 - * @param size argument passed to <code>calloc()</code> 11.144 - * @return return value of <code>calloc()</code> 11.145 - */ 11.146 -void *ucx_default_calloc(void *ignore, size_t n, size_t size); 11.147 -/** 11.148 - * A wrapper for the standard libc <code>realloc()</code> function. 11.149 - * @param ignore ignored (may be used by allocators for pooled memory) 11.150 - * @param data argumend passed to <code>realloc()</code> 11.151 - * @param n argument passed to <code>realloc()</code> 11.152 - * @return return value of <code>realloc()</code> 11.153 - */ 11.154 -void *ucx_default_realloc(void *ignore, void *data, size_t n); 11.155 -/** 11.156 - * A wrapper for the standard libc <code>free()</code> function. 11.157 - * @param ignore ignored (may be used by allocators for pooled memory) 11.158 - * @param data argument passed to <code>free()</code> 11.159 - */ 11.160 -void ucx_default_free(void *ignore, void *data); 11.161 - 11.162 -/** 11.163 - * Shorthand for calling an allocators malloc function. 11.164 - * @param allocator the allocator to use 11.165 - * @param n size of space to allocate 11.166 - * @return a pointer to the allocated memory area 11.167 - */ 11.168 -#define almalloc(allocator, n) ((allocator)->malloc((allocator)->pool, n)) 11.169 - 11.170 -/** 11.171 - * Shorthand for calling an allocators calloc function. 11.172 - * @param allocator the allocator to use 11.173 - * @param n the count of elements the space should be allocated for 11.174 - * @param size the size of each element 11.175 - * @return a pointer to the allocated memory area 11.176 - */ 11.177 -#define alcalloc(allocator, n, size) \ 11.178 - ((allocator)->calloc((allocator)->pool, n, size)) 11.179 - 11.180 -/** 11.181 - * Shorthand for calling an allocators realloc function. 11.182 - * @param allocator the allocator to use 11.183 - * @param ptr the pointer to the memory area that shall be reallocated 11.184 - * @param n the new size of the allocated memory area 11.185 - * @return a pointer to the reallocated memory area 11.186 - */ 11.187 -#define alrealloc(allocator, ptr, n) \ 11.188 - ((allocator)->realloc((allocator)->pool, ptr, n)) 11.189 - 11.190 -/** 11.191 - * Shorthand for calling an allocators free function. 11.192 - * @param allocator the allocator to use 11.193 - * @param ptr the pointer to the memory area that shall be freed 11.194 - */ 11.195 -#define alfree(allocator, ptr) ((allocator)->free((allocator)->pool, ptr)) 11.196 - 11.197 -/** 11.198 - * Convenient macro for a default allocator <code>struct</code> definition. 11.199 - */ 11.200 -#define UCX_ALLOCATOR_DEFAULT {NULL, \ 11.201 - ucx_default_malloc, ucx_default_calloc, ucx_default_realloc, \ 11.202 - ucx_default_free } 11.203 - 11.204 -#ifdef __cplusplus 11.205 -} 11.206 -#endif 11.207 - 11.208 -#endif /* UCX_ALLOCATOR_H */ 11.209 -
12.1 --- a/src/ucx/buffer.c Thu Nov 10 18:44:48 2016 +0100 12.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 12.3 @@ -1,234 +0,0 @@ 12.4 -/* 12.5 - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 12.6 - * 12.7 - * Copyright 2015 Olaf Wintermann. All rights reserved. 12.8 - * 12.9 - * Redistribution and use in source and binary forms, with or without 12.10 - * modification, are permitted provided that the following conditions are met: 12.11 - * 12.12 - * 1. Redistributions of source code must retain the above copyright 12.13 - * notice, this list of conditions and the following disclaimer. 12.14 - * 12.15 - * 2. Redistributions in binary form must reproduce the above copyright 12.16 - * notice, this list of conditions and the following disclaimer in the 12.17 - * documentation and/or other materials provided with the distribution. 12.18 - * 12.19 - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 12.20 - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 12.21 - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 12.22 - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 12.23 - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 12.24 - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 12.25 - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 12.26 - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 12.27 - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 12.28 - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 12.29 - * POSSIBILITY OF SUCH DAMAGE. 12.30 - */ 12.31 - 12.32 -#include "buffer.h" 12.33 -#include <stdarg.h> 12.34 -#include <stdlib.h> 12.35 -#include <string.h> 12.36 - 12.37 -UcxBuffer *ucx_buffer_new(void *space, size_t capacity, int flags) { 12.38 - UcxBuffer *buffer = (UcxBuffer*) malloc(sizeof(UcxBuffer)); 12.39 - if (buffer) { 12.40 - buffer->flags = flags; 12.41 - if (!space) { 12.42 - buffer->space = (char*)malloc(capacity); 12.43 - if (!buffer->space) { 12.44 - free(buffer); 12.45 - return NULL; 12.46 - } 12.47 - memset(buffer->space, 0, capacity); 12.48 - buffer->flags |= UCX_BUFFER_AUTOFREE; 12.49 - } else { 12.50 - buffer->space = (char*)space; 12.51 - } 12.52 - buffer->capacity = capacity; 12.53 - buffer->size = 0; 12.54 - 12.55 - buffer->pos = 0; 12.56 - } 12.57 - 12.58 - return buffer; 12.59 -} 12.60 - 12.61 -void ucx_buffer_free(UcxBuffer *buffer) { 12.62 - if ((buffer->flags & UCX_BUFFER_AUTOFREE) == UCX_BUFFER_AUTOFREE) { 12.63 - free(buffer->space); 12.64 - } 12.65 - free(buffer); 12.66 -} 12.67 - 12.68 -UcxBuffer* ucx_buffer_extract( 12.69 - UcxBuffer *src, size_t start, size_t length, int flags) { 12.70 - 12.71 - if (src->size == 0 || length == 0 || start+length > src->capacity) { 12.72 - return NULL; 12.73 - } 12.74 - 12.75 - UcxBuffer *dst = (UcxBuffer*) malloc(sizeof(UcxBuffer)); 12.76 - if (dst) { 12.77 - dst->space = (char*)malloc(length); 12.78 - if (!dst->space) { 12.79 - free(dst); 12.80 - return NULL; 12.81 - } 12.82 - dst->capacity = length; 12.83 - dst->size = length; 12.84 - dst->flags = flags | UCX_BUFFER_AUTOFREE; 12.85 - dst->pos = 0; 12.86 - memcpy(dst->space, src->space+start, length); 12.87 - } 12.88 - return dst; 12.89 -} 12.90 - 12.91 -int ucx_buffer_seek(UcxBuffer *buffer, off_t offset, int whence) { 12.92 - size_t npos; 12.93 - switch (whence) { 12.94 - case SEEK_CUR: 12.95 - npos = buffer->pos; 12.96 - break; 12.97 - case SEEK_END: 12.98 - npos = buffer->size; 12.99 - break; 12.100 - case SEEK_SET: 12.101 - npos = 0; 12.102 - break; 12.103 - default: 12.104 - return -1; 12.105 - } 12.106 - 12.107 - size_t opos = npos; 12.108 - npos += offset; 12.109 - 12.110 - if ((offset > 0 && npos < opos) || (offset < 0 && npos > opos)) { 12.111 - return -1; 12.112 - } 12.113 - 12.114 - if (npos >= buffer->size) { 12.115 - return -1; 12.116 - } else { 12.117 - buffer->pos = npos; 12.118 - return 0; 12.119 - } 12.120 - 12.121 -} 12.122 - 12.123 -int ucx_buffer_eof(UcxBuffer *buffer) { 12.124 - return buffer->pos >= buffer->size; 12.125 -} 12.126 - 12.127 -int ucx_buffer_extend(UcxBuffer *buffer, size_t len) { 12.128 - size_t newcap = buffer->capacity; 12.129 - 12.130 - if (buffer->capacity + len < buffer->capacity) { 12.131 - return -1; 12.132 - } 12.133 - 12.134 - while (buffer->capacity + len > newcap) { 12.135 - newcap <<= 1; 12.136 - if (newcap < buffer->capacity) { 12.137 - return -1; 12.138 - } 12.139 - } 12.140 - 12.141 - char *newspace = (char*)realloc(buffer->space, newcap); 12.142 - if (newspace) { 12.143 - memset(newspace+buffer->size, 0, newcap-buffer->size); 12.144 - buffer->space = newspace; 12.145 - buffer->capacity = newcap; 12.146 - } else { 12.147 - return -1; 12.148 - } 12.149 - 12.150 - return 0; 12.151 -} 12.152 - 12.153 -size_t ucx_buffer_write(const void *ptr, size_t size, size_t nitems, 12.154 - UcxBuffer *buffer) { 12.155 - size_t len = size * nitems; 12.156 - size_t required = buffer->pos + len; 12.157 - if (buffer->pos > required) { 12.158 - return 0; 12.159 - } 12.160 - 12.161 - if (required > buffer->capacity) { 12.162 - if ((buffer->flags & UCX_BUFFER_AUTOEXTEND) == UCX_BUFFER_AUTOEXTEND) { 12.163 - if (ucx_buffer_extend(buffer, required - buffer->capacity)) { 12.164 - return 0; 12.165 - } 12.166 - } else { 12.167 - len = buffer->capacity - buffer->pos; 12.168 - if (size > 1) { 12.169 - len -= len%size; 12.170 - } 12.171 - } 12.172 - } 12.173 - 12.174 - if (len == 0) { 12.175 - return len; 12.176 - } 12.177 - 12.178 - memcpy(buffer->space + buffer->pos, ptr, len); 12.179 - buffer->pos += len; 12.180 - if(buffer->pos > buffer->size) { 12.181 - buffer->size = buffer->pos; 12.182 - } 12.183 - 12.184 - return len / size; 12.185 -} 12.186 - 12.187 -size_t ucx_buffer_read(void *ptr, size_t size, size_t nitems, 12.188 - UcxBuffer *buffer) { 12.189 - size_t len = size * nitems; 12.190 - if (buffer->pos + len > buffer->size) { 12.191 - len = buffer->size - buffer->pos; 12.192 - if (size > 1) len -= len%size; 12.193 - } 12.194 - 12.195 - if (len <= 0) { 12.196 - return len; 12.197 - } 12.198 - 12.199 - memcpy(ptr, buffer->space + buffer->pos, len); 12.200 - buffer->pos += len; 12.201 - 12.202 - return len / size; 12.203 -} 12.204 - 12.205 -int ucx_buffer_putc(UcxBuffer *buffer, int c) { 12.206 - if(buffer->pos >= buffer->capacity) { 12.207 - if ((buffer->flags & UCX_BUFFER_AUTOEXTEND) == UCX_BUFFER_AUTOEXTEND) { 12.208 - if(ucx_buffer_extend(buffer, 1)) { 12.209 - return EOF; 12.210 - } 12.211 - } else { 12.212 - return EOF; 12.213 - } 12.214 - } 12.215 - 12.216 - c &= 0xFF; 12.217 - buffer->space[buffer->pos] = (char) c; 12.218 - buffer->pos++; 12.219 - if(buffer->pos > buffer->size) { 12.220 - buffer->size = buffer->pos; 12.221 - } 12.222 - return c; 12.223 -} 12.224 - 12.225 -int ucx_buffer_getc(UcxBuffer *buffer) { 12.226 - if (ucx_buffer_eof(buffer)) { 12.227 - return EOF; 12.228 - } else { 12.229 - int c = buffer->space[buffer->pos]; 12.230 - buffer->pos++; 12.231 - return c; 12.232 - } 12.233 -} 12.234 - 12.235 -size_t ucx_buffer_puts(UcxBuffer *buffer, char *str) { 12.236 - return ucx_buffer_write((const void*)str, 1, strlen(str), buffer); 12.237 -}
13.1 --- a/src/ucx/buffer.h Thu Nov 10 18:44:48 2016 +0100 13.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 13.3 @@ -1,270 +0,0 @@ 13.4 -/* 13.5 - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 13.6 - * 13.7 - * Copyright 2015 Olaf Wintermann. All rights reserved. 13.8 - * 13.9 - * Redistribution and use in source and binary forms, with or without 13.10 - * modification, are permitted provided that the following conditions are met: 13.11 - * 13.12 - * 1. Redistributions of source code must retain the above copyright 13.13 - * notice, this list of conditions and the following disclaimer. 13.14 - * 13.15 - * 2. Redistributions in binary form must reproduce the above copyright 13.16 - * notice, this list of conditions and the following disclaimer in the 13.17 - * documentation and/or other materials provided with the distribution. 13.18 - * 13.19 - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 13.20 - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 13.21 - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 13.22 - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 13.23 - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 13.24 - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 13.25 - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 13.26 - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 13.27 - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 13.28 - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 13.29 - * POSSIBILITY OF SUCH DAMAGE. 13.30 - */ 13.31 - 13.32 -/** 13.33 - * @file buffer.h 13.34 - * 13.35 - * Advanced buffer implementation. 13.36 - * 13.37 - * Instances of UcxBuffer can be used to read from or to write to like one 13.38 - * would do with a stream. This allows the use of ucx_stream_copy() to copy 13.39 - * contents from one buffer to another. 13.40 - * 13.41 - * Some features for convenient use of the buffer 13.42 - * can be enabled. See the documentation of the macro constants for more 13.43 - * information. 13.44 - * 13.45 - * @author Mike Becker 13.46 - * @author Olaf Wintermann 13.47 - */ 13.48 - 13.49 -#ifndef UCX_BUFFER_H 13.50 -#define UCX_BUFFER_H 13.51 - 13.52 -#include "ucx.h" 13.53 -#include <sys/types.h> 13.54 -#include <stdio.h> 13.55 - 13.56 -#ifdef __cplusplus 13.57 -extern "C" { 13.58 -#endif 13.59 - 13.60 -/** 13.61 - * No buffer features enabled (all flags cleared). 13.62 - */ 13.63 -#define UCX_BUFFER_DEFAULT 0x00 13.64 - 13.65 -/** 13.66 - * If this flag is enabled, the buffer will automatically free its contents. 13.67 - */ 13.68 -#define UCX_BUFFER_AUTOFREE 0x01 13.69 - 13.70 -/** 13.71 - * If this flag is enabled, the buffer will automatically extends its capacity. 13.72 - */ 13.73 -#define UCX_BUFFER_AUTOEXTEND 0x02 13.74 - 13.75 -/** UCX Buffer. */ 13.76 -typedef struct { 13.77 - /** A pointer to the buffer contents. */ 13.78 - char *space; 13.79 - /** Current position of the buffer. */ 13.80 - size_t pos; 13.81 - /** Current capacity (i.e. maximum size) of the buffer. */ 13.82 - size_t capacity; 13.83 - /** Current size of the buffer content. */ 13.84 - size_t size; 13.85 - /** 13.86 - * Flag register for buffer features. 13.87 - * @see #UCX_BUFFER_DEFAULT 13.88 - * @see #UCX_BUFFER_AUTOFREE 13.89 - * @see #UCX_BUFFER_AUTOEXTEND 13.90 - */ 13.91 - int flags; 13.92 -} UcxBuffer; 13.93 - 13.94 -/** 13.95 - * Creates a new buffer. 13.96 - * 13.97 - * <b>Note:</b> you may provide <code>NULL</code> as argument for 13.98 - * <code>space</code>. Then this function will allocate the space and enforce 13.99 - * the #UCX_BUFFER_AUTOFREE flag. 13.100 - * 13.101 - * @param space pointer to the memory area, or <code>NULL</code> to allocate 13.102 - * new memory 13.103 - * @param capacity the capacity of the buffer 13.104 - * @param flags buffer features (see UcxBuffer.flags) 13.105 - * @return the new buffer 13.106 - */ 13.107 -UcxBuffer *ucx_buffer_new(void *space, size_t capacity, int flags); 13.108 - 13.109 -/** 13.110 - * Destroys a buffer. 13.111 - * 13.112 - * If the #UCX_BUFFER_AUTOFREE feature is enabled, the contents of the buffer 13.113 - * are also freed. 13.114 - * 13.115 - * @param buffer the buffer to destroy 13.116 - */ 13.117 -void ucx_buffer_free(UcxBuffer* buffer); 13.118 - 13.119 -/** 13.120 - * Creates a new buffer and fills it with extracted content from another buffer. 13.121 - * 13.122 - * <b>Note:</b> the #UCX_BUFFER_AUTOFREE feature is enforced for the new buffer. 13.123 - * 13.124 - * @param src the source buffer 13.125 - * @param start the start position of extraction 13.126 - * @param length the count of bytes to extract (must not be zero) 13.127 - * @param flags feature mask for the new buffer 13.128 - * @return a new buffer containing the extraction 13.129 - */ 13.130 -UcxBuffer* ucx_buffer_extract(UcxBuffer *src, 13.131 - size_t start, size_t length, int flags); 13.132 - 13.133 -/** 13.134 - * A shorthand macro for the full extraction of the buffer. 13.135 - * 13.136 - * @param src the source buffer 13.137 - * @param flags feature mask for the new buffer 13.138 - * @return a new buffer with the extracted content 13.139 - */ 13.140 -#define ucx_buffer_clone(src,flags) \ 13.141 - ucx_buffer_extract(src, 0, (src)->capacity, flags) 13.142 - 13.143 -/** 13.144 - * Moves the position of the buffer. 13.145 - * 13.146 - * The new position is relative to the <code>whence</code> argument. 13.147 - * 13.148 - * SEEK_SET marks the start of the buffer. 13.149 - * SEEK_CUR marks the current position. 13.150 - * SEEK_END marks the end of the buffer. 13.151 - * 13.152 - * With an offset of zero, this function sets the buffer position to zero 13.153 - * (SEEK_SET), the buffer size (SEEK_END) or leaves the buffer position 13.154 - * unchanged (SEEK_CUR). 13.155 - * 13.156 - * @param buffer 13.157 - * @param offset position offset relative to <code>whence</code> 13.158 - * @param whence one of SEEK_SET, SEEK_CUR or SEEK_END 13.159 - * @return 0 on success, non-zero if the position is invalid 13.160 - * 13.161 - */ 13.162 -int ucx_buffer_seek(UcxBuffer *buffer, off_t offset, int whence); 13.163 - 13.164 -/** 13.165 - * Clears the buffer by resetting the position and deleting the data. 13.166 - * 13.167 - * The data is deleted by a zeroing it with call to <code>memset()</code>. 13.168 - * 13.169 - * @param buffer the buffer to be cleared 13.170 - */ 13.171 -#define ucx_buffer_clear(buffer) memset(buffer->space, 0, buffer->size); \ 13.172 - buffer->size = 0; buffer->pos = 0; 13.173 - 13.174 -/** 13.175 - * Tests, if the buffer position has exceeded the buffer capacity. 13.176 - * 13.177 - * @param buffer the buffer to test 13.178 - * @return non-zero, if the current buffer position has exceeded the last 13.179 - * available byte of the buffer. 13.180 - */ 13.181 -int ucx_buffer_eof(UcxBuffer *buffer); 13.182 - 13.183 - 13.184 -/** 13.185 - * Extends the capacity of the buffer. 13.186 - * 13.187 - * <b>Note:</b> The buffer capacity increased by a power of two. I.e. 13.188 - * the buffer capacity is doubled, as long as it would not hold the current 13.189 - * content plus the additional required bytes. 13.190 - * 13.191 - * <b>Attention:</b> the argument provided is the number of <i>additional</i> 13.192 - * bytes the buffer shall hold. It is <b>NOT</b> the total number of bytes the 13.193 - * buffer shall hold. 13.194 - * 13.195 - * @param buffer the buffer to extend 13.196 - * @param additional_bytes the number of additional bytes the buffer shall 13.197 - * <i>at least</i> hold 13.198 - * @return 0 on success or a non-zero value on failure 13.199 - */ 13.200 -int ucx_buffer_extend(UcxBuffer *buffer, size_t additional_bytes); 13.201 - 13.202 -/** 13.203 - * Writes data to an UcxBuffer. 13.204 - * 13.205 - * The position of the buffer is increased by the number of bytes written. 13.206 - * 13.207 - * @param ptr a pointer to the memory area containing the bytes to be written 13.208 - * @param size the length of one element 13.209 - * @param nitems the element count 13.210 - * @param buffer the UcxBuffer to write to 13.211 - * @return the total count of bytes written 13.212 - */ 13.213 -size_t ucx_buffer_write(const void *ptr, size_t size, size_t nitems, 13.214 - UcxBuffer *buffer); 13.215 - 13.216 -/** 13.217 - * Reads data from an UcxBuffer. 13.218 - * 13.219 - * The position of the buffer is increased by the number of bytes read. 13.220 - * 13.221 - * @param ptr a pointer to the memory area where to store the read data 13.222 - * @param size the length of one element 13.223 - * @param nitems the element count 13.224 - * @param buffer the UcxBuffer to read from 13.225 - * @return the total number of elements read 13.226 - */ 13.227 -size_t ucx_buffer_read(void *ptr, size_t size, size_t nitems, 13.228 - UcxBuffer *buffer); 13.229 - 13.230 -/** 13.231 - * Writes a character to a buffer. 13.232 - * 13.233 - * The least significant byte of the argument is written to the buffer. If the 13.234 - * end of the buffer is reached and #UCX_BUFFER_AUTOEXTEND feature is enabled, 13.235 - * the buffer capacity is extended by ucx_buffer_extend(). If the feature is 13.236 - * disabled or buffer extension fails, <code>EOF</code> is returned. 13.237 - * 13.238 - * On successful write the position of the buffer is increased. 13.239 - * 13.240 - * @param buffer the buffer to write to 13.241 - * @param c the character to write as <code>int</code> value 13.242 - * @return the byte that has bean written as <code>int</code> value or 13.243 - * <code>EOF</code> when the end of the stream is reached and automatic 13.244 - * extension is not enabled or not possible 13.245 - */ 13.246 -int ucx_buffer_putc(UcxBuffer *buffer, int c); 13.247 - 13.248 -/** 13.249 - * Gets a character from a buffer. 13.250 - * 13.251 - * The current position of the buffer is increased after a successful read. 13.252 - * 13.253 - * @param buffer the buffer to read from 13.254 - * @return the character as <code>int</code> value or <code>EOF</code>, if the 13.255 - * end of the buffer is reached 13.256 - */ 13.257 -int ucx_buffer_getc(UcxBuffer *buffer); 13.258 - 13.259 -/** 13.260 - * Writes a string to a buffer. 13.261 - * 13.262 - * @param buffer the buffer 13.263 - * @param str the string 13.264 - * @return the number of bytes written 13.265 - */ 13.266 -size_t ucx_buffer_puts(UcxBuffer *buffer, char *str); 13.267 - 13.268 -#ifdef __cplusplus 13.269 -} 13.270 -#endif 13.271 - 13.272 -#endif /* UCX_BUFFER_H */ 13.273 -
14.1 --- a/src/ucx/list.c Thu Nov 10 18:44:48 2016 +0100 14.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 14.3 @@ -1,335 +0,0 @@ 14.4 -/* 14.5 - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 14.6 - * 14.7 - * Copyright 2015 Olaf Wintermann. All rights reserved. 14.8 - * 14.9 - * Redistribution and use in source and binary forms, with or without 14.10 - * modification, are permitted provided that the following conditions are met: 14.11 - * 14.12 - * 1. Redistributions of source code must retain the above copyright 14.13 - * notice, this list of conditions and the following disclaimer. 14.14 - * 14.15 - * 2. Redistributions in binary form must reproduce the above copyright 14.16 - * notice, this list of conditions and the following disclaimer in the 14.17 - * documentation and/or other materials provided with the distribution. 14.18 - * 14.19 - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 14.20 - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 14.21 - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 14.22 - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 14.23 - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 14.24 - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 14.25 - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 14.26 - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 14.27 - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 14.28 - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 14.29 - * POSSIBILITY OF SUCH DAMAGE. 14.30 - */ 14.31 - 14.32 -#include "list.h" 14.33 - 14.34 -UcxList *ucx_list_clone(UcxList *l, copy_func fnc, void *data) { 14.35 - return ucx_list_clone_a(ucx_default_allocator(), l, fnc, data); 14.36 -} 14.37 - 14.38 -UcxList *ucx_list_clone_a(UcxAllocator *alloc, UcxList *l, 14.39 - copy_func fnc, void *data) { 14.40 - UcxList *ret = NULL; 14.41 - while (l) { 14.42 - if (fnc) { 14.43 - ret = ucx_list_append_a(alloc, ret, fnc(l->data, data)); 14.44 - } else { 14.45 - ret = ucx_list_append_a(alloc, ret, l->data); 14.46 - } 14.47 - l = l->next; 14.48 - } 14.49 - return ret; 14.50 -} 14.51 - 14.52 -int ucx_list_equals(const UcxList *l1, const UcxList *l2, 14.53 - cmp_func fnc, void* data) { 14.54 - if (l1 == l2) return 1; 14.55 - 14.56 - while (l1 != NULL && l2 != NULL) { 14.57 - if (fnc == NULL) { 14.58 - if (l1->data != l2->data) return 0; 14.59 - } else { 14.60 - if (fnc(l1->data, l2->data, data) != 0) return 0; 14.61 - } 14.62 - l1 = l1->next; 14.63 - l2 = l2->next; 14.64 - } 14.65 - 14.66 - return (l1 == NULL && l2 == NULL); 14.67 -} 14.68 - 14.69 -void ucx_list_free(UcxList *l) { 14.70 - ucx_list_free_a(ucx_default_allocator(), l); 14.71 -} 14.72 - 14.73 -void ucx_list_free_a(UcxAllocator *alloc, UcxList *l) { 14.74 - UcxList *e = l, *f; 14.75 - while (e != NULL) { 14.76 - f = e; 14.77 - e = e->next; 14.78 - alfree(alloc, f); 14.79 - } 14.80 -} 14.81 - 14.82 -void ucx_list_free_content(UcxList* list, ucx_destructor destr) { 14.83 - while (list != NULL) { 14.84 - destr(list->data); 14.85 - list = list->next; 14.86 - } 14.87 -} 14.88 - 14.89 -UcxList *ucx_list_append(UcxList *l, void *data) { 14.90 - return ucx_list_append_a(ucx_default_allocator(), l, data); 14.91 -} 14.92 - 14.93 -UcxList *ucx_list_append_a(UcxAllocator *alloc, UcxList *l, void *data) { 14.94 - UcxList *nl = (UcxList*) almalloc(alloc, sizeof(UcxList)); 14.95 - if (!nl) { 14.96 - return NULL; 14.97 - } 14.98 - 14.99 - nl->data = data; 14.100 - nl->next = NULL; 14.101 - if (l) { 14.102 - UcxList *t = ucx_list_last(l); 14.103 - t->next = nl; 14.104 - nl->prev = t; 14.105 - return l; 14.106 - } else { 14.107 - nl->prev = NULL; 14.108 - return nl; 14.109 - } 14.110 -} 14.111 - 14.112 -UcxList *ucx_list_prepend(UcxList *l, void *data) { 14.113 - return ucx_list_prepend_a(ucx_default_allocator(), l, data); 14.114 -} 14.115 - 14.116 -UcxList *ucx_list_prepend_a(UcxAllocator *alloc, UcxList *l, void *data) { 14.117 - UcxList *nl = ucx_list_append_a(alloc, NULL, data); 14.118 - if (!nl) { 14.119 - return NULL; 14.120 - } 14.121 - l = ucx_list_first(l); 14.122 - 14.123 - if (l) { 14.124 - nl->next = l; 14.125 - l->prev = nl; 14.126 - } 14.127 - return nl; 14.128 -} 14.129 - 14.130 -UcxList *ucx_list_concat(UcxList *l1, UcxList *l2) { 14.131 - if (l1) { 14.132 - UcxList *last = ucx_list_last(l1); 14.133 - last->next = l2; 14.134 - if (l2) { 14.135 - l2->prev = last; 14.136 - } 14.137 - return l1; 14.138 - } else { 14.139 - return l2; 14.140 - } 14.141 -} 14.142 - 14.143 -UcxList *ucx_list_last(const UcxList *l) { 14.144 - if (l == NULL) return NULL; 14.145 - 14.146 - const UcxList *e = l; 14.147 - while (e->next != NULL) { 14.148 - e = e->next; 14.149 - } 14.150 - return (UcxList*)e; 14.151 -} 14.152 - 14.153 -ssize_t ucx_list_indexof(const UcxList *list, const UcxList *elem) { 14.154 - ssize_t index = 0; 14.155 - while (list) { 14.156 - if (list == elem) { 14.157 - return index; 14.158 - } 14.159 - list = list->next; 14.160 - index++; 14.161 - } 14.162 - return -1; 14.163 -} 14.164 - 14.165 -UcxList *ucx_list_get(const UcxList *l, size_t index) { 14.166 - if (l == NULL) return NULL; 14.167 - 14.168 - const UcxList *e = l; 14.169 - while (e->next && index > 0) { 14.170 - e = e->next; 14.171 - index--; 14.172 - } 14.173 - 14.174 - return (UcxList*)(index == 0 ? e : NULL); 14.175 -} 14.176 - 14.177 -ssize_t ucx_list_find(UcxList *l, void *elem, cmp_func fnc, void *cmpdata) { 14.178 - ssize_t index = 0; 14.179 - UCX_FOREACH(e, l) { 14.180 - if (fnc) { 14.181 - if (fnc(elem, e->data, cmpdata) == 0) { 14.182 - return index; 14.183 - } 14.184 - } else { 14.185 - if (elem == e->data) { 14.186 - return index; 14.187 - } 14.188 - } 14.189 - index++; 14.190 - } 14.191 - return -1; 14.192 -} 14.193 - 14.194 -int ucx_list_contains(UcxList *l, void *elem, cmp_func fnc, void *cmpdata) { 14.195 - return ucx_list_find(l, elem, fnc, cmpdata) > -1; 14.196 -} 14.197 - 14.198 -size_t ucx_list_size(const UcxList *l) { 14.199 - if (l == NULL) return 0; 14.200 - 14.201 - const UcxList *e = l; 14.202 - size_t s = 1; 14.203 - while (e->next != NULL) { 14.204 - e = e->next; 14.205 - s++; 14.206 - } 14.207 - 14.208 - return s; 14.209 -} 14.210 - 14.211 -static UcxList *ucx_list_sort_merge(int length, 14.212 - UcxList* restrict ls, UcxList* restrict le, UcxList* restrict re, 14.213 - cmp_func fnc, void* data) { 14.214 - 14.215 - UcxList** sorted = (UcxList**) malloc(sizeof(UcxList*)*length); 14.216 - UcxList *rc, *lc; 14.217 - 14.218 - lc = ls; rc = le; 14.219 - int n = 0; 14.220 - while (lc && lc != le && rc != re) { 14.221 - if (fnc(lc->data, rc->data, data) <= 0) { 14.222 - sorted[n] = lc; 14.223 - lc = lc->next; 14.224 - } else { 14.225 - sorted[n] = rc; 14.226 - rc = rc->next; 14.227 - } 14.228 - n++; 14.229 - } 14.230 - while (lc && lc != le) { 14.231 - sorted[n] = lc; 14.232 - lc = lc->next; 14.233 - n++; 14.234 - } 14.235 - while (rc && rc != re) { 14.236 - sorted[n] = rc; 14.237 - rc = rc->next; 14.238 - n++; 14.239 - } 14.240 - 14.241 - // Update pointer 14.242 - sorted[0]->prev = NULL; 14.243 - for (int i = 0 ; i < length-1 ; i++) { 14.244 - sorted[i]->next = sorted[i+1]; 14.245 - sorted[i+1]->prev = sorted[i]; 14.246 - } 14.247 - sorted[length-1]->next = NULL; 14.248 - 14.249 - UcxList *ret = sorted[0]; 14.250 - free(sorted); 14.251 - return ret; 14.252 -} 14.253 - 14.254 -UcxList *ucx_list_sort(UcxList *l, cmp_func fnc, void *data) { 14.255 - if (l == NULL) { 14.256 - return NULL; 14.257 - } 14.258 - 14.259 - UcxList *lc; 14.260 - int ln = 1; 14.261 - 14.262 - UcxList *restrict ls = l, *restrict le, *restrict re; 14.263 - 14.264 - // check how many elements are already sorted 14.265 - lc = ls; 14.266 - while (lc->next != NULL && fnc(lc->next->data, lc->data, data) > 0) { 14.267 - lc = lc->next; 14.268 - ln++; 14.269 - } 14.270 - le = lc->next; 14.271 - 14.272 - if (le == NULL) { 14.273 - return l; // this list is already sorted :) 14.274 - } else { 14.275 - UcxList *rc; 14.276 - int rn = 1; 14.277 - rc = le; 14.278 - // skip already sorted elements 14.279 - while (rc->next != NULL && fnc(rc->next->data, rc->data, data) > 0) { 14.280 - rc = rc->next; 14.281 - rn++; 14.282 - } 14.283 - re = rc->next; 14.284 - 14.285 - // {ls,...,le->prev} and {rs,...,re->prev} are sorted - merge them 14.286 - UcxList *sorted = ucx_list_sort_merge(ln+rn, 14.287 - ls, le, re, 14.288 - fnc, data); 14.289 - 14.290 - // Something left? Sort it! 14.291 - size_t remainder_length = ucx_list_size(re); 14.292 - if (remainder_length > 0) { 14.293 - UcxList *remainder = ucx_list_sort(re, fnc, data); 14.294 - 14.295 - // merge sorted list with (also sorted) remainder 14.296 - l = ucx_list_sort_merge(ln+rn+remainder_length, 14.297 - sorted, remainder, NULL, fnc, data); 14.298 - } else { 14.299 - // no remainder - we've got our sorted list 14.300 - l = sorted; 14.301 - } 14.302 - 14.303 - return l; 14.304 - } 14.305 -} 14.306 - 14.307 -UcxList *ucx_list_first(const UcxList *l) { 14.308 - if (!l) { 14.309 - return NULL; 14.310 - } 14.311 - 14.312 - const UcxList *e = l; 14.313 - while (e->prev) { 14.314 - e = e->prev; 14.315 - } 14.316 - return (UcxList *)e; 14.317 -} 14.318 - 14.319 -UcxList *ucx_list_remove(UcxList *l, UcxList *e) { 14.320 - return ucx_list_remove_a(ucx_default_allocator(), l, e); 14.321 -} 14.322 - 14.323 -UcxList *ucx_list_remove_a(UcxAllocator *alloc, UcxList *l, UcxList *e) { 14.324 - if (l == e) { 14.325 - l = e->next; 14.326 - } 14.327 - 14.328 - if (e->next) { 14.329 - e->next->prev = e->prev; 14.330 - } 14.331 - 14.332 - if (e->prev) { 14.333 - e->prev->next = e->next; 14.334 - } 14.335 - 14.336 - alfree(alloc, e); 14.337 - return l; 14.338 -}
15.1 --- a/src/ucx/list.h Thu Nov 10 18:44:48 2016 +0100 15.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 15.3 @@ -1,395 +0,0 @@ 15.4 -/* 15.5 - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 15.6 - * 15.7 - * Copyright 2015 Olaf Wintermann. All rights reserved. 15.8 - * 15.9 - * Redistribution and use in source and binary forms, with or without 15.10 - * modification, are permitted provided that the following conditions are met: 15.11 - * 15.12 - * 1. Redistributions of source code must retain the above copyright 15.13 - * notice, this list of conditions and the following disclaimer. 15.14 - * 15.15 - * 2. Redistributions in binary form must reproduce the above copyright 15.16 - * notice, this list of conditions and the following disclaimer in the 15.17 - * documentation and/or other materials provided with the distribution. 15.18 - * 15.19 - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 15.20 - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 15.21 - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 15.22 - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 15.23 - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 15.24 - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 15.25 - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 15.26 - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 15.27 - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 15.28 - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 15.29 - * POSSIBILITY OF SUCH DAMAGE. 15.30 - */ 15.31 -/** 15.32 - * Doubly linked list implementation. 15.33 - * 15.34 - * @file list.h 15.35 - * @author Mike Becker 15.36 - * @author Olaf Wintermann 15.37 - */ 15.38 - 15.39 -#ifndef UCX_LIST_H 15.40 -#define UCX_LIST_H 15.41 - 15.42 -#include "ucx.h" 15.43 -#include "allocator.h" 15.44 - 15.45 -#ifdef __cplusplus 15.46 -extern "C" { 15.47 -#endif 15.48 - 15.49 -/** 15.50 - * Loop statement for UCX lists. 15.51 - * 15.52 - * The first argument is the name of the iteration variable. The scope of 15.53 - * this variable is limited to the <code>UCX_FOREACH</code> statement. 15.54 - * 15.55 - * The second argument is a pointer to the list. In most cases this will be the 15.56 - * pointer to the first element of the list, but it may also be an arbitrary 15.57 - * element of the list. The iteration will then start with that element. 15.58 - * 15.59 - * 15.60 - * @param list The first element of the list 15.61 - * @param elem The variable name of the element 15.62 - */ 15.63 -#define UCX_FOREACH(elem,list) \ 15.64 - for (UcxList* elem = list ; elem != NULL ; elem = elem->next) 15.65 - 15.66 -/** 15.67 - * UCX list type. 15.68 - * @see UcxList 15.69 - */ 15.70 -typedef struct UcxList UcxList; 15.71 - 15.72 -/** 15.73 - * UCX list structure. 15.74 - */ 15.75 -struct UcxList { 15.76 - /** 15.77 - * List element payload. 15.78 - */ 15.79 - void *data; 15.80 - /** 15.81 - * Pointer to the next list element or <code>NULL</code>, if this is the 15.82 - * last element. 15.83 - */ 15.84 - UcxList *next; 15.85 - /** 15.86 - * Pointer to the previous list element or <code>NULL</code>, if this is 15.87 - * the first element. 15.88 - */ 15.89 - UcxList *prev; 15.90 -}; 15.91 - 15.92 -/** 15.93 - * Creates an element-wise copy of a list. 15.94 - * 15.95 - * This function clones the specified list by creating new list elements and 15.96 - * copying the data with the specified copy_func(). If no copy_func() is 15.97 - * specified, a shallow copy is created and the new list will reference the 15.98 - * same data as the source list. 15.99 - * 15.100 - * @param list the list to copy 15.101 - * @param cpyfnc a pointer to the function that shall copy an element (may be 15.102 - * <code>NULL</code>) 15.103 - * @param data additional data for the copy_func() 15.104 - * @return a pointer to the copy 15.105 - */ 15.106 -UcxList *ucx_list_clone(UcxList *list, copy_func cpyfnc, void* data); 15.107 - 15.108 -/** 15.109 - * Creates an element-wise copy of a list using an UcxAllocator. 15.110 - * 15.111 - * See ucx_list_clone() for details. 15.112 - * 15.113 - * Keep in mind, that you might want to pass the allocator as an (part of the) 15.114 - * argument for the <code>data</code> parameter, if you want the copy_func() to 15.115 - * make use of the allocator. 15.116 - * 15.117 - * @param allocator the allocator to use 15.118 - * @param list the list to copy 15.119 - * @param cpyfnc a pointer to the function that shall copy an element (may be 15.120 - * <code>NULL</code>) 15.121 - * @param data additional data for the copy_func() 15.122 - * @return a pointer to the copy 15.123 - * @see ucx_list_clone() 15.124 - */ 15.125 -UcxList *ucx_list_clone_a(UcxAllocator *allocator, UcxList *list, 15.126 - copy_func cpyfnc, void* data); 15.127 - 15.128 -/** 15.129 - * Compares two UCX lists element-wise by using a compare function. 15.130 - * 15.131 - * Each element of the two specified lists are compared by using the specified 15.132 - * compare function and the additional data. The type and content of this 15.133 - * additional data depends on the cmp_func() used. 15.134 - * 15.135 - * If the list pointers denote elements within a list, the lists are compared 15.136 - * starting with the denoted elements. Thus any previous elements are not taken 15.137 - * into account. This might be useful to check, if certain list tails match 15.138 - * each other. 15.139 - * 15.140 - * @param list1 the first list 15.141 - * @param list2 the second list 15.142 - * @param cmpfnc the compare function 15.143 - * @param data additional data for the compare function 15.144 - * @return 1, if and only if the two lists equal element-wise, 0 otherwise 15.145 - */ 15.146 -int ucx_list_equals(const UcxList *list1, const UcxList *list2, 15.147 - cmp_func cmpfnc, void* data); 15.148 - 15.149 -/** 15.150 - * Destroys the entire list. 15.151 - * 15.152 - * The members of the list are not automatically freed, so ensure they are 15.153 - * otherwise referenced or destroyed by ucx_list_free_contents(). 15.154 - * Otherwise, a memory leak is likely to occur. 15.155 - * 15.156 - * <b>Caution:</b> the argument <b>MUST</b> denote an entire list (i.e. a call 15.157 - * to ucx_list_first() on the argument must return the argument itself) 15.158 - * 15.159 - * @param list the list to free 15.160 - * @see ucx_list_free_contents() 15.161 - */ 15.162 -void ucx_list_free(UcxList *list); 15.163 - 15.164 -/** 15.165 - * Destroys the entire list using an UcxAllocator. 15.166 - * 15.167 - * See ucx_list_free() for details. 15.168 - * 15.169 - * @param allocator the allocator to use 15.170 - * @param list the list to free 15.171 - * @see ucx_list_free() 15.172 - */ 15.173 -void ucx_list_free_a(UcxAllocator *allocator, UcxList *list); 15.174 - 15.175 -/** 15.176 - * Destroys the contents of the specified list by calling the specified 15.177 - * destructor on each of them. 15.178 - * 15.179 - * Note, that the contents are not usable afterwards and the list should be 15.180 - * destroyed with ucx_list_free(). 15.181 - * 15.182 - * @param list the list for which the contents shall be freed 15.183 - * @param destr the destructor function (e.g. stdlib free()) 15.184 - * @see ucx_list_free() 15.185 - */ 15.186 -void ucx_list_free_content(UcxList* list, ucx_destructor destr); 15.187 - 15.188 - 15.189 -/** 15.190 - * Inserts an element at the end of the list. 15.191 - * 15.192 - * This is generally an O(n) operation, as the end of the list is retrieved with 15.193 - * ucx_list_last(). 15.194 - * 15.195 - * @param list the list where to append the data, or <code>NULL</code> to 15.196 - * create a new list 15.197 - * @param data the data to insert 15.198 - * @return <code>list</code>, if it is not <code>NULL</code> or a pointer to 15.199 - * the newly created list otherwise 15.200 - */ 15.201 -UcxList *ucx_list_append(UcxList *list, void *data); 15.202 - 15.203 -/** 15.204 - * Inserts an element at the end of the list using an UcxAllocator. 15.205 - * 15.206 - * See ucx_list_append() for details. 15.207 - * 15.208 - * @param allocator the allocator to use 15.209 - * @param list the list where to append the data, or <code>NULL</code> to 15.210 - * create a new list 15.211 - * @param data the data to insert 15.212 - * @return <code>list</code>, if it is not <code>NULL</code> or a pointer to 15.213 - * the newly created list otherwise 15.214 - * @see ucx_list_append() 15.215 - */ 15.216 -UcxList *ucx_list_append_a(UcxAllocator *allocator, UcxList *list, void *data); 15.217 - 15.218 -/** 15.219 - * Inserts an element at the beginning of the list. 15.220 - * 15.221 - * You <i>should</i> overwrite the old list pointer by calling 15.222 - * <code>mylist = ucx_list_prepend(mylist, mydata);</code>. However, you may 15.223 - * also perform successive calls of ucx_list_prepend() on the same list pointer, 15.224 - * as this function always searchs for the head of the list with 15.225 - * ucx_list_first(). 15.226 - * 15.227 - * @param list the list where to insert the data or <code>NULL</code> to create 15.228 - * a new list 15.229 - * @param data the data to insert 15.230 - * @return a pointer to the new list head 15.231 - */ 15.232 -UcxList *ucx_list_prepend(UcxList *list, void *data); 15.233 - 15.234 -/** 15.235 - * Inserts an element at the beginning of the list using an UcxAllocator. 15.236 - * 15.237 - * See ucx_list_prepend() for details. 15.238 - * 15.239 - * @param allocator the allocator to use 15.240 - * @param list the list where to insert the data or <code>NULL</code> to create 15.241 - * a new list 15.242 - * @param data the data to insert 15.243 - * @return a pointer to the new list head 15.244 - * @see ucx_list_prepend() 15.245 - */ 15.246 -UcxList *ucx_list_prepend_a(UcxAllocator *allocator, UcxList *list, void *data); 15.247 - 15.248 -/** 15.249 - * Concatenates two lists. 15.250 - * 15.251 - * Either of the two arguments may be <code>NULL</code>. 15.252 - * 15.253 - * This function modifies the references to the next/previous element of 15.254 - * the last/first element of <code>list1</code>/<code> 15.255 - * list2</code>. 15.256 - * 15.257 - * @param list1 first list 15.258 - * @param list2 second list 15.259 - * @return if <code>list1</code> is <code>NULL</code>, <code>list2</code> is 15.260 - * returned, otherwise <code>list1</code> is returned 15.261 - */ 15.262 -UcxList *ucx_list_concat(UcxList *list1, UcxList *list2); 15.263 - 15.264 -/** 15.265 - * Returns the first element of a list. 15.266 - * 15.267 - * If the argument is the list pointer, it is directly returned. Otherwise 15.268 - * this function traverses to the first element of the list and returns the 15.269 - * list pointer. 15.270 - * 15.271 - * @param elem one element of the list 15.272 - * @return the first element of the list, the specified element is a member of 15.273 - */ 15.274 -UcxList *ucx_list_first(const UcxList *elem); 15.275 - 15.276 -/** 15.277 - * Returns the last element of a list. 15.278 - * 15.279 - * If the argument has no successor, it is the last element and therefore 15.280 - * directly returned. Otherwise this function traverses to the last element of 15.281 - * the list and returns it. 15.282 - * 15.283 - * @param elem one element of the list 15.284 - * @return the last element of the list, the specified element is a member of 15.285 - */ 15.286 -UcxList *ucx_list_last(const UcxList *elem); 15.287 - 15.288 -/** 15.289 - * Returns the list element at the specified index. 15.290 - * 15.291 - * @param list the list to retrieve the element from 15.292 - * @param index index of the element to return 15.293 - * @return the element at the specified index or <code>NULL</code>, if the 15.294 - * index is greater than the list size 15.295 - */ 15.296 -UcxList *ucx_list_get(const UcxList *list, size_t index); 15.297 - 15.298 -/** 15.299 - * Returns the index of an element. 15.300 - * 15.301 - * @param list the list where to search for the element 15.302 - * @param elem the element to find 15.303 - * @return the index of the element or -1 if the list does not contain the 15.304 - * element 15.305 - */ 15.306 -ssize_t ucx_list_indexof(const UcxList *list, const UcxList *elem); 15.307 - 15.308 -/** 15.309 - * Returns the element count of the list. 15.310 - * 15.311 - * @param list the list whose elements are counted 15.312 - * @return the element count 15.313 - */ 15.314 -size_t ucx_list_size(const UcxList *list); 15.315 - 15.316 -/** 15.317 - * Returns the index of an element containing the specified data. 15.318 - * 15.319 - * This function uses a cmp_func() to compare the data of each list element 15.320 - * with the specified data. If no cmp_func is provided, the pointers are 15.321 - * compared. 15.322 - * 15.323 - * If the list contains the data more than once, the index of the first 15.324 - * occurrence is returned. 15.325 - * 15.326 - * @param list the list where to search for the data 15.327 - * @param elem the element data 15.328 - * @param cmpfnc the compare function 15.329 - * @param data additional data for the compare function 15.330 - * @return the index of the element containing the specified data or -1 if the 15.331 - * data is not found in this list 15.332 - */ 15.333 -ssize_t ucx_list_find(UcxList *list, void *elem, cmp_func cmpfnc, void *data); 15.334 - 15.335 -/** 15.336 - * Checks, if a list contains a specific element. 15.337 - * 15.338 - * An element is found, if ucx_list_find() returns a value greater than -1. 15.339 - * 15.340 - * @param list the list where to search for the data 15.341 - * @param elem the element data 15.342 - * @param cmpfnc the compare function 15.343 - * @param data additional data for the compare function 15.344 - * @return 1, if and only if the list contains the specified element data 15.345 - * @see ucx_list_find() 15.346 - */ 15.347 -int ucx_list_contains(UcxList *list, void *elem, cmp_func cmpfnc, void *data); 15.348 - 15.349 -/** 15.350 - * Sorts an UcxList with natural merge sort. 15.351 - * 15.352 - * This function uses O(n) additional temporary memory for merge operations 15.353 - * that is automatically freed after each merge. 15.354 - * 15.355 - * As the head of the list might change, you <b>MUST</b> call this function 15.356 - * as follows: <code>mylist = ucx_list_sort(mylist, mycmpfnc, mydata);</code>. 15.357 - * 15.358 - * @param list the list to sort 15.359 - * @param cmpfnc the function that shall be used to compare the element data 15.360 - * @param data additional data for the cmp_func() 15.361 - * @return the sorted list 15.362 - */ 15.363 -UcxList *ucx_list_sort(UcxList *list, cmp_func cmpfnc, void *data); 15.364 - 15.365 -/** 15.366 - * Removes an element from the list. 15.367 - * 15.368 - * If the first element is removed, the list pointer changes. So it is 15.369 - * <i>highly recommended</i> to <i>always</i> update the pointer by calling 15.370 - * <code>mylist = ucx_list_remove(mylist, myelem);</code>. 15.371 - * 15.372 - * @param list the list from which the element shall be removed 15.373 - * @param element the element to remove 15.374 - * @return returns the updated list pointer or <code>NULL</code>, if the list 15.375 - * is now empty 15.376 - */ 15.377 -UcxList *ucx_list_remove(UcxList *list, UcxList *element); 15.378 - 15.379 -/** 15.380 - * Removes an element from the list using an UcxAllocator. 15.381 - * 15.382 - * See ucx_list_remove() for details. 15.383 - * 15.384 - * @param allocator the allocator to use 15.385 - * @param list the list from which the element shall be removed 15.386 - * @param element the element to remove 15.387 - * @return returns the updated list pointer or <code>NULL</code>, if the list 15.388 - * @see ucx_list_remove() 15.389 - */ 15.390 -UcxList *ucx_list_remove_a(UcxAllocator *allocator, UcxList *list, 15.391 - UcxList *element); 15.392 - 15.393 -#ifdef __cplusplus 15.394 -} 15.395 -#endif 15.396 - 15.397 -#endif /* UCX_LIST_H */ 15.398 -
16.1 --- a/src/ucx/string.c Thu Nov 10 18:44:48 2016 +0100 16.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 16.3 @@ -1,381 +0,0 @@ 16.4 -/* 16.5 - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 16.6 - * 16.7 - * Copyright 2015 Olaf Wintermann. All rights reserved. 16.8 - * 16.9 - * Redistribution and use in source and binary forms, with or without 16.10 - * modification, are permitted provided that the following conditions are met: 16.11 - * 16.12 - * 1. Redistributions of source code must retain the above copyright 16.13 - * notice, this list of conditions and the following disclaimer. 16.14 - * 16.15 - * 2. Redistributions in binary form must reproduce the above copyright 16.16 - * notice, this list of conditions and the following disclaimer in the 16.17 - * documentation and/or other materials provided with the distribution. 16.18 - * 16.19 - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 16.20 - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16.21 - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 16.22 - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 16.23 - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 16.24 - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 16.25 - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 16.26 - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 16.27 - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 16.28 - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 16.29 - * POSSIBILITY OF SUCH DAMAGE. 16.30 - */ 16.31 - 16.32 -#include <stdlib.h> 16.33 -#include <string.h> 16.34 -#include <stdarg.h> 16.35 -#include <ctype.h> 16.36 - 16.37 -#include "string.h" 16.38 -#include "allocator.h" 16.39 - 16.40 -sstr_t sstr(char *cstring) { 16.41 - sstr_t string; 16.42 - string.ptr = cstring; 16.43 - string.length = strlen(cstring); 16.44 - return string; 16.45 -} 16.46 - 16.47 -sstr_t sstrn(char *cstring, size_t length) { 16.48 - sstr_t string; 16.49 - string.ptr = cstring; 16.50 - string.length = length; 16.51 - return string; 16.52 -} 16.53 - 16.54 -size_t sstrnlen(size_t n, sstr_t s, ...) { 16.55 - va_list ap; 16.56 - size_t size = s.length; 16.57 - va_start(ap, s); 16.58 - 16.59 - for (size_t i = 1 ; i < n ; i++) { 16.60 - sstr_t str = va_arg(ap, sstr_t); 16.61 - size += str.length; 16.62 - } 16.63 - va_end(ap); 16.64 - 16.65 - return size; 16.66 -} 16.67 - 16.68 -static sstr_t sstrvcat_a( 16.69 - UcxAllocator *a, 16.70 - size_t count, 16.71 - sstr_t s1, 16.72 - sstr_t s2, 16.73 - va_list ap) { 16.74 - sstr_t str; 16.75 - str.ptr = NULL; 16.76 - str.length = 0; 16.77 - if(count < 2) { 16.78 - return str; 16.79 - } 16.80 - 16.81 - sstr_t *strings = (sstr_t*) calloc(count, sizeof(sstr_t)); 16.82 - if(!strings) { 16.83 - return str; 16.84 - } 16.85 - 16.86 - // get all args and overall length 16.87 - strings[0] = s1; 16.88 - strings[1] = s2; 16.89 - size_t strlen = s1.length + s2.length; 16.90 - for (size_t i=2;i<count;i++) { 16.91 - sstr_t s = va_arg (ap, sstr_t); 16.92 - strings[i] = s; 16.93 - strlen += s.length; 16.94 - } 16.95 - 16.96 - // create new string 16.97 - str.ptr = (char*) almalloc(a, strlen + 1); 16.98 - str.length = strlen; 16.99 - if(!str.ptr) { 16.100 - free(strings); 16.101 - str.length = 0; 16.102 - return str; 16.103 - } 16.104 - 16.105 - // concatenate strings 16.106 - size_t pos = 0; 16.107 - for (size_t i=0;i<count;i++) { 16.108 - sstr_t s = strings[i]; 16.109 - memcpy(str.ptr + pos, s.ptr, s.length); 16.110 - pos += s.length; 16.111 - } 16.112 - 16.113 - str.ptr[str.length] = '\0'; 16.114 - 16.115 - free(strings); 16.116 - 16.117 - return str; 16.118 -} 16.119 - 16.120 -sstr_t sstrcat(size_t count, sstr_t s1, sstr_t s2, ...) { 16.121 - va_list ap; 16.122 - va_start(ap, s2); 16.123 - sstr_t s = sstrvcat_a(ucx_default_allocator(), count, s1, s2, ap); 16.124 - va_end(ap); 16.125 - return s; 16.126 -} 16.127 - 16.128 -sstr_t sstrcat_a(UcxAllocator *a, size_t count, sstr_t s1, sstr_t s2, ...) { 16.129 - va_list ap; 16.130 - va_start(ap, s2); 16.131 - sstr_t s = sstrvcat_a(a, count, s1, s2, ap); 16.132 - va_end(ap); 16.133 - return s; 16.134 -} 16.135 - 16.136 -sstr_t sstrsubs(sstr_t s, size_t start) { 16.137 - return sstrsubsl (s, start, s.length-start); 16.138 -} 16.139 - 16.140 -sstr_t sstrsubsl(sstr_t s, size_t start, size_t length) { 16.141 - sstr_t new_sstr; 16.142 - if (start >= s.length) { 16.143 - new_sstr.ptr = NULL; 16.144 - new_sstr.length = 0; 16.145 - } else { 16.146 - if (length > s.length-start) { 16.147 - length = s.length-start; 16.148 - } 16.149 - new_sstr.ptr = &s.ptr[start]; 16.150 - new_sstr.length = length; 16.151 - } 16.152 - return new_sstr; 16.153 -} 16.154 - 16.155 -sstr_t sstrchr(sstr_t s, int c) { 16.156 - for(size_t i=0;i<s.length;i++) { 16.157 - if(s.ptr[i] == c) { 16.158 - return sstrsubs(s, i); 16.159 - } 16.160 - } 16.161 - sstr_t n; 16.162 - n.ptr = NULL; 16.163 - n.length = 0; 16.164 - return n; 16.165 -} 16.166 - 16.167 -sstr_t sstrrchr(sstr_t s, int c) { 16.168 - if (s.length > 0) { 16.169 - for(size_t i=s.length;i>0;i--) { 16.170 - if(s.ptr[i-1] == c) { 16.171 - return sstrsubs(s, i-1); 16.172 - } 16.173 - } 16.174 - } 16.175 - sstr_t n; 16.176 - n.ptr = NULL; 16.177 - n.length = 0; 16.178 - return n; 16.179 -} 16.180 - 16.181 -sstr_t sstrstr(sstr_t string, sstr_t match) { 16.182 - if (match.length == 0) { 16.183 - return string; 16.184 - } 16.185 - 16.186 - for (size_t i = 0 ; i < string.length ; i++) { 16.187 - sstr_t substr = sstrsubs(string, i); 16.188 - if (sstrprefix(substr, match)) { 16.189 - return substr; 16.190 - } 16.191 - } 16.192 - 16.193 - sstr_t emptystr; 16.194 - emptystr.length = 0; 16.195 - emptystr.ptr = NULL; 16.196 - return emptystr; 16.197 -} 16.198 - 16.199 -sstr_t* sstrsplit(sstr_t s, sstr_t d, ssize_t *n) { 16.200 - return sstrsplit_a(ucx_default_allocator(), s, d, n); 16.201 -} 16.202 - 16.203 -sstr_t* sstrsplit_a(UcxAllocator *allocator, sstr_t s, sstr_t d, ssize_t *n) { 16.204 - if (s.length == 0 || d.length == 0) { 16.205 - *n = -1; 16.206 - return NULL; 16.207 - } 16.208 - 16.209 - sstr_t* result; 16.210 - ssize_t nmax = *n; 16.211 - *n = 1; 16.212 - 16.213 - /* special case: exact match - no processing needed */ 16.214 - if (sstrcmp(s, d) == 0) { 16.215 - *n = 0; 16.216 - return NULL; 16.217 - } 16.218 - sstr_t sv = sstrdup(s); 16.219 - if (sv.length == 0) { 16.220 - *n = -2; 16.221 - return NULL; 16.222 - } 16.223 - 16.224 - for (size_t i = 0 ; i < s.length ; i++) { 16.225 - sstr_t substr = sstrsubs(sv, i); 16.226 - if (sstrprefix(substr, d)) { 16.227 - (*n)++; 16.228 - for (size_t j = 0 ; j < d.length ; j++) { 16.229 - sv.ptr[i+j] = 0; 16.230 - } 16.231 - i += d.length - 1; // -1, because the loop will do a i++ 16.232 - } 16.233 - if ((*n) == nmax) break; 16.234 - } 16.235 - result = (sstr_t*) almalloc(allocator, sizeof(sstr_t)*(*n)); 16.236 - 16.237 - if (result) { 16.238 - char *pptr = sv.ptr; 16.239 - for (ssize_t i = 0 ; i < *n ; i++) { 16.240 - size_t l = strlen(pptr); 16.241 - char* ptr = (char*) almalloc(allocator, l + 1); 16.242 - if (ptr) { 16.243 - memcpy(ptr, pptr, l); 16.244 - ptr[l] = 0; 16.245 - 16.246 - result[i] = sstrn(ptr, l); 16.247 - pptr += l + d.length; 16.248 - } else { 16.249 - for (ssize_t j = i-1 ; j >= 0 ; j--) { 16.250 - alfree(allocator, result[j].ptr); 16.251 - } 16.252 - alfree(allocator, result); 16.253 - *n = -2; 16.254 - break; 16.255 - } 16.256 - } 16.257 - } else { 16.258 - *n = -2; 16.259 - } 16.260 - 16.261 - free(sv.ptr); 16.262 - 16.263 - return result; 16.264 -} 16.265 - 16.266 -int sstrcmp(sstr_t s1, sstr_t s2) { 16.267 - if (s1.length == s2.length) { 16.268 - return memcmp(s1.ptr, s2.ptr, s1.length); 16.269 - } else if (s1.length > s2.length) { 16.270 - return 1; 16.271 - } else { 16.272 - return -1; 16.273 - } 16.274 -} 16.275 - 16.276 -int sstrcasecmp(sstr_t s1, sstr_t s2) { 16.277 - if (s1.length == s2.length) { 16.278 -#ifdef _WIN32 16.279 - return _strnicmp(s1.ptr, s2.ptr, s1.length); 16.280 -#else 16.281 - return strncasecmp(s1.ptr, s2.ptr, s1.length); 16.282 -#endif 16.283 - } else if (s1.length > s2.length) { 16.284 - return 1; 16.285 - } else { 16.286 - return -1; 16.287 - } 16.288 -} 16.289 - 16.290 -sstr_t sstrdup(sstr_t s) { 16.291 - return sstrdup_a(ucx_default_allocator(), s); 16.292 -} 16.293 - 16.294 -sstr_t sstrdup_a(UcxAllocator *allocator, sstr_t s) { 16.295 - sstr_t newstring; 16.296 - newstring.ptr = (char*)almalloc(allocator, s.length + 1); 16.297 - if (newstring.ptr) { 16.298 - newstring.length = s.length; 16.299 - newstring.ptr[newstring.length] = 0; 16.300 - 16.301 - memcpy(newstring.ptr, s.ptr, s.length); 16.302 - } else { 16.303 - newstring.length = 0; 16.304 - } 16.305 - 16.306 - return newstring; 16.307 -} 16.308 - 16.309 -sstr_t sstrtrim(sstr_t string) { 16.310 - sstr_t newstr = string; 16.311 - 16.312 - while (newstr.length > 0 && isspace(*newstr.ptr)) { 16.313 - newstr.ptr++; 16.314 - newstr.length--; 16.315 - } 16.316 - while (newstr.length > 0 && isspace(newstr.ptr[newstr.length-1])) { 16.317 - newstr.length--; 16.318 - } 16.319 - 16.320 - return newstr; 16.321 -} 16.322 - 16.323 -int sstrprefix(sstr_t string, sstr_t prefix) { 16.324 - if (string.length == 0) { 16.325 - return prefix.length == 0; 16.326 - } 16.327 - if (prefix.length == 0) { 16.328 - return 1; 16.329 - } 16.330 - 16.331 - if (prefix.length > string.length) { 16.332 - return 0; 16.333 - } else { 16.334 - return memcmp(string.ptr, prefix.ptr, prefix.length) == 0; 16.335 - } 16.336 -} 16.337 - 16.338 -int sstrsuffix(sstr_t string, sstr_t suffix) { 16.339 - if (string.length == 0) { 16.340 - return suffix.length == 0; 16.341 - } 16.342 - if (suffix.length == 0) { 16.343 - return 1; 16.344 - } 16.345 - 16.346 - if (suffix.length > string.length) { 16.347 - return 0; 16.348 - } else { 16.349 - return memcmp(string.ptr+string.length-suffix.length, 16.350 - suffix.ptr, suffix.length) == 0; 16.351 - } 16.352 -} 16.353 - 16.354 -sstr_t sstrlower(sstr_t string) { 16.355 - sstr_t ret = sstrdup(string); 16.356 - for (size_t i = 0; i < ret.length ; i++) { 16.357 - ret.ptr[i] = tolower(ret.ptr[i]); 16.358 - } 16.359 - return ret; 16.360 -} 16.361 - 16.362 -sstr_t sstrlower_a(UcxAllocator *allocator, sstr_t string) { 16.363 - sstr_t ret = sstrdup_a(allocator, string); 16.364 - for (size_t i = 0; i < ret.length ; i++) { 16.365 - ret.ptr[i] = tolower(ret.ptr[i]); 16.366 - } 16.367 - return ret; 16.368 -} 16.369 - 16.370 -sstr_t sstrupper(sstr_t string) { 16.371 - sstr_t ret = sstrdup(string); 16.372 - for (size_t i = 0; i < ret.length ; i++) { 16.373 - ret.ptr[i] = toupper(ret.ptr[i]); 16.374 - } 16.375 - return ret; 16.376 -} 16.377 - 16.378 -sstr_t sstrupper_a(UcxAllocator *allocator, sstr_t string) { 16.379 - sstr_t ret = sstrdup_a(allocator, string); 16.380 - for (size_t i = 0; i < ret.length ; i++) { 16.381 - ret.ptr[i] = toupper(ret.ptr[i]); 16.382 - } 16.383 - return ret; 16.384 -}
17.1 --- a/src/ucx/string.h Thu Nov 10 18:44:48 2016 +0100 17.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 17.3 @@ -1,457 +0,0 @@ 17.4 -/* 17.5 - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 17.6 - * 17.7 - * Copyright 2015 Olaf Wintermann. All rights reserved. 17.8 - * 17.9 - * Redistribution and use in source and binary forms, with or without 17.10 - * modification, are permitted provided that the following conditions are met: 17.11 - * 17.12 - * 1. Redistributions of source code must retain the above copyright 17.13 - * notice, this list of conditions and the following disclaimer. 17.14 - * 17.15 - * 2. Redistributions in binary form must reproduce the above copyright 17.16 - * notice, this list of conditions and the following disclaimer in the 17.17 - * documentation and/or other materials provided with the distribution. 17.18 - * 17.19 - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 17.20 - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17.21 - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17.22 - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 17.23 - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 17.24 - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 17.25 - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 17.26 - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 17.27 - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 17.28 - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 17.29 - * POSSIBILITY OF SUCH DAMAGE. 17.30 - */ 17.31 -/** 17.32 - * Bounded string implementation. 17.33 - * 17.34 - * The UCX strings (<code>sstr_t</code>) provide an alternative to C strings. 17.35 - * The main difference to C strings is, that <code>sstr_t</code> does <b>not 17.36 - * need to be <code>NULL</code>-terminated</b>. Instead the length is stored 17.37 - * within the structure. 17.38 - * 17.39 - * When using <code>sstr_t</code>, developers must be full aware of what type 17.40 - * of string (<code>NULL</code>-terminated) or not) they are using, when 17.41 - * accessing the <code>char* ptr</code> directly. 17.42 - * 17.43 - * The UCX string module provides some common string functions, known from 17.44 - * standard libc, working with <code>sstr_t</code>. 17.45 - * 17.46 - * @file string.h 17.47 - * @author Mike Becker 17.48 - * @author Olaf Wintermann 17.49 - */ 17.50 - 17.51 -#ifndef UCX_STRING_H 17.52 -#define UCX_STRING_H 17.53 - 17.54 -#include "ucx.h" 17.55 -#include "allocator.h" 17.56 -#include <stddef.h> 17.57 - 17.58 -/** Shortcut for a <code>sstr_t struct</code> literal. */ 17.59 -#define ST(s) { (char*)s, sizeof(s)-1 } 17.60 - 17.61 -/** Shortcut for the conversion of a C string to a <code>sstr_t</code>. */ 17.62 -#define S(s) sstrn((char*)s, sizeof(s)-1) 17.63 - 17.64 -#ifdef __cplusplus 17.65 -extern "C" { 17.66 -#endif 17.67 - 17.68 -/** 17.69 - * The UCX string structure. 17.70 - */ 17.71 -typedef struct { 17.72 - /** A reference to the string (<b>not necessarily <code>NULL</code> 17.73 - * -terminated</b>) */ 17.74 - char *ptr; 17.75 - /** The length of the string */ 17.76 - size_t length; 17.77 -} sstr_t; 17.78 - 17.79 -/** 17.80 - * Creates a new sstr_t based on a C string. 17.81 - * 17.82 - * The length is implicitly inferred by using a call to <code>strlen()</code>. 17.83 - * 17.84 - * <b>Note:</b> the sstr_t will hold a <i>reference</i> to the C string. If you 17.85 - * do want a copy, use sstrdup() on the return value of this function. 17.86 - * 17.87 - * @param cstring the C string to wrap 17.88 - * @return a new sstr_t containing the C string 17.89 - * 17.90 - * @see sstrn() 17.91 - */ 17.92 -sstr_t sstr(char *cstring); 17.93 - 17.94 -/** 17.95 - * Creates a new sstr_t of the specified length based on a C string. 17.96 - * 17.97 - * <b>Note:</b> the sstr_t will hold a <i>reference</i> to the C string. If you 17.98 - * do want a copy, use sstrdup() on the return value of this function. 17.99 - * 17.100 - * @param cstring the C string to wrap 17.101 - * @param length the length of the string 17.102 - * @return a new sstr_t containing the C string 17.103 - * 17.104 - * @see sstr() 17.105 - * @see S() 17.106 - */ 17.107 -sstr_t sstrn(char *cstring, size_t length); 17.108 - 17.109 - 17.110 -/** 17.111 - * Returns the cumulated length of all specified strings. 17.112 - * 17.113 - * At least one string must be specified. 17.114 - * 17.115 - * <b>Attention:</b> if the count argument does not match the count of the 17.116 - * specified strings, the behavior is undefined. 17.117 - * 17.118 - * @param count the total number of specified strings (so at least 1) 17.119 - * @param string the first string 17.120 - * @param ... all other strings 17.121 - * @return the cumulated length of all strings 17.122 - */ 17.123 -size_t sstrnlen(size_t count, sstr_t string, ...); 17.124 - 17.125 -/** 17.126 - * Concatenates two or more strings. 17.127 - * 17.128 - * The resulting string will be allocated by standard <code>malloc()</code>. 17.129 - * So developers <b>MUST</b> pass the sstr_t.ptr to <code>free()</code>. 17.130 - * 17.131 - * The sstr_t.ptr of the return value will <i>always</i> be <code>NULL</code>- 17.132 - * terminated. 17.133 - * 17.134 - * @param count the total number of strings to concatenate 17.135 - * @param s1 first string 17.136 - * @param s2 second string 17.137 - * @param ... all remaining strings 17.138 - * @return the concatenated string 17.139 - */ 17.140 -sstr_t sstrcat(size_t count, sstr_t s1, sstr_t s2, ...); 17.141 - 17.142 -/** 17.143 - * Concatenates two or more strings using an UcxAllocator. 17.144 - * 17.145 - * See sstrcat() for details. 17.146 - * 17.147 - * @param a the allocator to use 17.148 - * @param count the total number of strings to concatenate 17.149 - * @param s1 first string 17.150 - * @param s2 second string 17.151 - * @param ... all remaining strings 17.152 - * @return the concatenated string 17.153 - */ 17.154 -sstr_t sstrcat_a(UcxAllocator *a, size_t count, sstr_t s1, sstr_t s2, ...); 17.155 - 17.156 - 17.157 -/** 17.158 - * Returns a substring starting at the specified location. 17.159 - * 17.160 - * <b>Attention:</b> the new string references the same memory area as the 17.161 - * input string and will <b>NOT</b> be <code>NULL</code>-terminated. 17.162 - * Use sstrdup() to get a copy. 17.163 - * 17.164 - * @param string input string 17.165 - * @param start start location of the substring 17.166 - * @return a substring of <code>string</code> starting at <code>start</code> 17.167 - * 17.168 - * @see sstrsubsl() 17.169 - * @see sstrchr() 17.170 - */ 17.171 -sstr_t sstrsubs(sstr_t string, size_t start); 17.172 - 17.173 -/** 17.174 - * Returns a substring with a maximum length starting at the specified location. 17.175 - * 17.176 - * <b>Attention:</b> the new string references the same memory area as the 17.177 - * input string and will <b>NOT</b> be <code>NULL</code>-terminated. 17.178 - * Use sstrdup() to get a copy. 17.179 - * 17.180 - * @param string input string 17.181 - * @param start start location of the substring 17.182 - * @param length the maximum length of the substring 17.183 - * @return a substring of <code>string</code> starting at <code>start</code> 17.184 - * with a maximum length of <code>length</code> 17.185 - * 17.186 - * @see sstrsubs() 17.187 - * @see sstrchr() 17.188 - */ 17.189 -sstr_t sstrsubsl(sstr_t string, size_t start, size_t length); 17.190 - 17.191 -/** 17.192 - * Returns a substring starting at the location of the first occurrence of the 17.193 - * specified character. 17.194 - * 17.195 - * If the string does not contain the character, an empty string is returned. 17.196 - * 17.197 - * @param string the string where to locate the character 17.198 - * @param chr the character to locate 17.199 - * @return a substring starting at the first location of <code>chr</code> 17.200 - * 17.201 - * @see sstrsubs() 17.202 - */ 17.203 -sstr_t sstrchr(sstr_t string, int chr); 17.204 - 17.205 -/** 17.206 - * Returns a substring starting at the location of the last occurrence of the 17.207 - * specified character. 17.208 - * 17.209 - * If the string does not contain the character, an empty string is returned. 17.210 - * 17.211 - * @param string the string where to locate the character 17.212 - * @param chr the character to locate 17.213 - * @return a substring starting at the last location of <code>chr</code> 17.214 - * 17.215 - * @see sstrsubs() 17.216 - */ 17.217 -sstr_t sstrrchr(sstr_t string, int chr); 17.218 - 17.219 -/** 17.220 - * Returns a substring starting at the location of the first occurrence of the 17.221 - * specified string. 17.222 - * 17.223 - * If the string does not contain the other string, an empty string is returned. 17.224 - * 17.225 - * If <code>match</code> is an empty string, the complete <code>string</code> is 17.226 - * returned. 17.227 - * 17.228 - * @param string the string to be scanned 17.229 - * @param match string containing the sequence of characters to match 17.230 - * @return a substring starting at the first occurrence of 17.231 - * <code>match</code>, or an empty string, if the sequence is not 17.232 - * present in <code>string</code> 17.233 - */ 17.234 -sstr_t sstrstr(sstr_t string, sstr_t match); 17.235 - 17.236 -/** 17.237 - * Splits a string into parts by using a delimiter string. 17.238 - * 17.239 - * This function will return <code>NULL</code>, if one of the following happens: 17.240 - * <ul> 17.241 - * <li>the string length is zero</li> 17.242 - * <li>the delimeter length is zero</li> 17.243 - * <li>the string equals the delimeter</li> 17.244 - * <li>memory allocation fails</li> 17.245 - * </ul> 17.246 - * 17.247 - * The integer referenced by <code>count</code> is used as input and determines 17.248 - * the maximum size of the resulting array, i.e. the maximum count of splits to 17.249 - * perform + 1. 17.250 - * 17.251 - * The integer referenced by <code>count</code> is also used as output and is 17.252 - * set to 17.253 - * <ul> 17.254 - * <li>-2, on memory allocation errors</li> 17.255 - * <li>-1, if either the string or the delimiter is an empty string</li> 17.256 - * <li>0, if the string equals the delimiter</li> 17.257 - * <li>1, if the string does not contain the delimiter</li> 17.258 - * <li>the count of array items, otherwise</li> 17.259 - * </ul> 17.260 - * 17.261 - * If the string starts with the delimiter, the first item of the resulting 17.262 - * array will be an empty string. 17.263 - * 17.264 - * If the string ends with the delimiter and the maximum list size is not 17.265 - * exceeded, the last array item will be an empty string. 17.266 - * 17.267 - * <b>Attention:</b> The array pointer <b>AND</b> all sstr_t.ptr of the array 17.268 - * items must be manually passed to <code>free()</code>. Use sstrsplit_a() with 17.269 - * an allocator to managed memory, to avoid this. 17.270 - * 17.271 - * @param string the string to split 17.272 - * @param delim the delimiter string 17.273 - * @param count IN: the maximum size of the resulting array (0 = no limit), 17.274 - * OUT: the actual size of the array 17.275 - * @return a sstr_t array containing the split strings or 17.276 - * <code>NULL</code> on error 17.277 - * 17.278 - * @see sstrsplit_a() 17.279 - */ 17.280 -sstr_t* sstrsplit(sstr_t string, sstr_t delim, ssize_t *count); 17.281 - 17.282 -/** 17.283 - * Performing sstrsplit() using an UcxAllocator. 17.284 - * 17.285 - * <i>Read the description of sstrsplit() for details.</i> 17.286 - * 17.287 - * The memory for the sstr_t.ptr pointers of the array items and the memory for 17.288 - * the sstr_t array itself are allocated by using the UcxAllocator.malloc() 17.289 - * function. 17.290 - * 17.291 - * <b>Note:</b> the allocator is not used for memory that is freed within the 17.292 - * same call of this function (locally scoped variables). 17.293 - * 17.294 - * @param allocator the UcxAllocator used for allocating memory 17.295 - * @param string the string to split 17.296 - * @param delim the delimiter string 17.297 - * @param count IN: the maximum size of the resulting array (0 = no limit), 17.298 - * OUT: the actual size of the array 17.299 - * @return a sstr_t array containing the split strings or 17.300 - * <code>NULL</code> on error 17.301 - * 17.302 - * @see sstrsplit() 17.303 - */ 17.304 -sstr_t* sstrsplit_a(UcxAllocator *allocator, sstr_t string, sstr_t delim, 17.305 - ssize_t *count); 17.306 - 17.307 -/** 17.308 - * Compares two UCX strings with standard <code>memcmp()</code>. 17.309 - * 17.310 - * At first it compares the sstr_t.length attribute of the two strings. The 17.311 - * <code>memcmp()</code> function is called, if and only if the lengths match. 17.312 - * 17.313 - * @param s1 the first string 17.314 - * @param s2 the second string 17.315 - * @return -1, if the length of s1 is less than the length of s2 or 1, if the 17.316 - * length of s1 is greater than the length of s2 or the result of 17.317 - * <code>memcmp()</code> otherwise (i.e. 0 if the strings match) 17.318 - */ 17.319 -int sstrcmp(sstr_t s1, sstr_t s2); 17.320 - 17.321 -/** 17.322 - * Compares two UCX strings ignoring the case. 17.323 - * 17.324 - * At first it compares the sstr_t.length attribute of the two strings. If and 17.325 - * only if the lengths match, both strings are compared char by char ignoring 17.326 - * the case. 17.327 - * 17.328 - * @param s1 the first string 17.329 - * @param s2 the second string 17.330 - * @return -1, if the length of s1 is less than the length of s2 or 1, if the 17.331 - * length of s1 is greater than the length of s2 or the difference between the 17.332 - * first two differing characters otherwise (i.e. 0 if the strings match and 17.333 - * no characters differ) 17.334 - */ 17.335 -int sstrcasecmp(sstr_t s1, sstr_t s2); 17.336 - 17.337 -/** 17.338 - * Creates a duplicate of the specified string. 17.339 - * 17.340 - * The new sstr_t will contain a copy allocated by standard 17.341 - * <code>malloc()</code>. So developers <b>MUST</b> pass the sstr_t.ptr to 17.342 - * <code>free()</code>. 17.343 - * 17.344 - * The sstr_t.ptr of the return value will <i>always</i> be <code>NULL</code>- 17.345 - * terminated. 17.346 - * 17.347 - * @param string the string to duplicate 17.348 - * @return a duplicate of the string 17.349 - * @see sstrdup_a() 17.350 - */ 17.351 -sstr_t sstrdup(sstr_t string); 17.352 - 17.353 -/** 17.354 - * Creates a duplicate of the specified string using an UcxAllocator. 17.355 - * 17.356 - * The new sstr_t will contain a copy allocated by the allocators 17.357 - * ucx_allocator_malloc function. So it is implementation depended, whether the 17.358 - * returned sstr_t.ptr pointer must be passed to the allocators 17.359 - * ucx_allocator_free function manually. 17.360 - * 17.361 - * The sstr_t.ptr of the return value will <i>always</i> be <code>NULL</code>- 17.362 - * terminated. 17.363 - * 17.364 - * @param allocator a valid instance of an UcxAllocator 17.365 - * @param string the string to duplicate 17.366 - * @return a duplicate of the string 17.367 - * @see sstrdup() 17.368 - */ 17.369 -sstr_t sstrdup_a(UcxAllocator *allocator, sstr_t string); 17.370 - 17.371 -/** 17.372 - * Omits leading and trailing spaces. 17.373 - * 17.374 - * This function returns a new sstr_t containing a trimmed version of the 17.375 - * specified string. 17.376 - * 17.377 - * <b>Note:</b> the new sstr_t references the same memory, thus you 17.378 - * <b>MUST NOT</b> pass the sstr_t.ptr of the return value to 17.379 - * <code>free()</code>. It is also highly recommended to avoid assignments like 17.380 - * <code>mystr = sstrtrim(mystr);</code> as you lose the reference to the 17.381 - * source string. Assignments of this type are only permitted, if the 17.382 - * sstr_t.ptr of the source string does not need to be freed or if another 17.383 - * reference to the source string exists. 17.384 - * 17.385 - * @param string the string that shall be trimmed 17.386 - * @return a new sstr_t containing the trimmed string 17.387 - */ 17.388 -sstr_t sstrtrim(sstr_t string); 17.389 - 17.390 -/** 17.391 - * Checks, if a string has a specific prefix. 17.392 - * @param string the string to check 17.393 - * @param prefix the prefix the string should have 17.394 - * @return 1, if and only if the string has the specified prefix, 0 otherwise 17.395 - */ 17.396 -int sstrprefix(sstr_t string, sstr_t prefix); 17.397 - 17.398 -/** 17.399 - * Checks, if a string has a specific suffix. 17.400 - * @param string the string to check 17.401 - * @param suffix the suffix the string should have 17.402 - * @return 1, if and only if the string has the specified suffix, 0 otherwise 17.403 - */ 17.404 -int sstrsuffix(sstr_t string, sstr_t suffix); 17.405 - 17.406 -/** 17.407 - * Returns a lower case version of a string. 17.408 - * 17.409 - * This function creates a duplicate of the input string, first. See the 17.410 - * documentation of sstrdup() for the implications. 17.411 - * 17.412 - * @param string the input string 17.413 - * @return the resulting lower case string 17.414 - * @see sstrdup() 17.415 - */ 17.416 -sstr_t sstrlower(sstr_t string); 17.417 - 17.418 -/** 17.419 - * Returns a lower case version of a string. 17.420 - * 17.421 - * This function creates a duplicate of the input string, first. See the 17.422 - * documentation of sstrdup_a() for the implications. 17.423 - * 17.424 - * @param allocator the allocator used for duplicating the string 17.425 - * @param string the input string 17.426 - * @return the resulting lower case string 17.427 - * @see sstrdup_a() 17.428 - */ 17.429 -sstr_t sstrlower_a(UcxAllocator *allocator, sstr_t string); 17.430 - 17.431 -/** 17.432 - * Returns a upper case version of a string. 17.433 - * 17.434 - * This function creates a duplicate of the input string, first. See the 17.435 - * documentation of sstrdup() for the implications. 17.436 - * 17.437 - * @param string the input string 17.438 - * @return the resulting upper case string 17.439 - * @see sstrdup() 17.440 - */ 17.441 -sstr_t sstrupper(sstr_t string); 17.442 - 17.443 -/** 17.444 - * Returns a upper case version of a string. 17.445 - * 17.446 - * This function creates a duplicate of the input string, first. See the 17.447 - * documentation of sstrdup_a() for the implications. 17.448 - * 17.449 - * @param allocator the allocator used for duplicating the string 17.450 - * @param string the input string 17.451 - * @return the resulting upper case string 17.452 - * @see sstrdup_a() 17.453 - */ 17.454 -sstr_t sstrupper_a(UcxAllocator *allocator, sstr_t string); 17.455 - 17.456 -#ifdef __cplusplus 17.457 -} 17.458 -#endif 17.459 - 17.460 -#endif /* UCX_STRING_H */
18.1 --- a/src/ucx/ucx.h Thu Nov 10 18:44:48 2016 +0100 18.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 18.3 @@ -1,138 +0,0 @@ 18.4 -/* 18.5 - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 18.6 - * 18.7 - * Copyright 2015 Olaf Wintermann. All rights reserved. 18.8 - * 18.9 - * Redistribution and use in source and binary forms, with or without 18.10 - * modification, are permitted provided that the following conditions are met: 18.11 - * 18.12 - * 1. Redistributions of source code must retain the above copyright 18.13 - * notice, this list of conditions and the following disclaimer. 18.14 - * 18.15 - * 2. Redistributions in binary form must reproduce the above copyright 18.16 - * notice, this list of conditions and the following disclaimer in the 18.17 - * documentation and/or other materials provided with the distribution. 18.18 - * 18.19 - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 18.20 - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18.21 - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18.22 - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 18.23 - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 18.24 - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 18.25 - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 18.26 - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 18.27 - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 18.28 - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 18.29 - * POSSIBILITY OF SUCH DAMAGE. 18.30 - */ 18.31 -/** 18.32 - * Main UCX Header providing most common definitions. 18.33 - * 18.34 - * @file ucx.h 18.35 - * @author Mike Becker 18.36 - * @author Olaf Wintermann 18.37 - */ 18.38 - 18.39 -#ifndef UCX_H 18.40 -#define UCX_H 18.41 - 18.42 -/** Major UCX version as integer constant. */ 18.43 -#define UCX_VERSION_MAJOR 0 18.44 - 18.45 -/** Minor UCX version as integer constant. */ 18.46 -#define UCX_VERSION_MINOR 9 18.47 - 18.48 -/** The UCX version in format [major].[minor] */ 18.49 -#define UCX_VERSION UCX_VERSION_MAJOR.UCX_VERSION_MINOR 18.50 - 18.51 -#include <stdlib.h> 18.52 - 18.53 -#ifdef _WIN32 18.54 -#if !(defined __ssize_t_defined || defined _SSIZE_T_) 18.55 -#include <BaseTsd.h> 18.56 -typedef SSIZE_T ssize_t; 18.57 -#define __ssize_t_defined 18.58 -#define _SSIZE_T_ 18.59 -#endif /* __ssize_t_defined and _SSIZE_T */ 18.60 -#else /* !_WIN32 */ 18.61 -#include <sys/types.h> 18.62 -#endif /* _WIN32 */ 18.63 - 18.64 -#ifdef __cplusplus 18.65 -#ifndef _Bool 18.66 -#define _Bool bool 18.67 -#define restrict 18.68 -#endif 18.69 -/** Use C naming even when compiling with C++. */ 18.70 -#define UCX_EXTERN extern "C" 18.71 -extern "C" { 18.72 -#else 18.73 -/** Pointless in C. */ 18.74 -#define UCX_EXTERN 18.75 -#endif 18.76 - 18.77 - 18.78 -/** 18.79 - * A function pointer to a destructor function. 18.80 - * @see ucx_mempool_setdestr() 18.81 - * @see ucx_mempool_regdestr() 18.82 - */ 18.83 -typedef void(*ucx_destructor)(void*); 18.84 - 18.85 -/** 18.86 - * Function pointer to a compare function. 18.87 - * 18.88 - * The compare function shall take three arguments: the two values that shall be 18.89 - * compared and optional additional data. 18.90 - * The function shall then return -1 if the first argument is less than the 18.91 - * second argument, 1 if the first argument is greater than the second argument 18.92 - * and 0 if both arguments are equal. If the third argument is 18.93 - * <code>NULL</code>, it shall be ignored. 18.94 - */ 18.95 -typedef int(*cmp_func)(void*,void*,void*); 18.96 - 18.97 -/** 18.98 - * Function pointer to a copy function. 18.99 - * 18.100 - * The copy function shall create a copy of the first argument and may use 18.101 - * additional data provided by the second argument. If the second argument is 18.102 - * <code>NULL</code>, it shall be ignored. 18.103 - 18.104 - * <b>Attention:</b> if pointers returned by functions of this type may be 18.105 - * passed to <code>free()</code> depends on the implementation of the 18.106 - * respective <code>copy_func</code>. 18.107 - */ 18.108 -typedef void*(*copy_func)(void*,void*); 18.109 - 18.110 -/** 18.111 - * Function pointer to a write function. 18.112 - * 18.113 - * The signature of the write function shall be compatible to the signature 18.114 - * of standard <code>fwrite</code>, though it may use arbitrary data types for 18.115 - * source and destination. 18.116 - * 18.117 - * The arguments shall contain (in ascending order): a pointer to the source, 18.118 - * the length of one element, the element count and a pointer to the 18.119 - * destination. 18.120 - */ 18.121 -typedef size_t(*write_func)(const void*, size_t, size_t, void*); 18.122 - 18.123 -/** 18.124 - * Function pointer to a read function. 18.125 - * 18.126 - * The signature of the read function shall be compatible to the signature 18.127 - * of standard <code>fread</code>, though it may use arbitrary data types for 18.128 - * source and destination. 18.129 - * 18.130 - * The arguments shall contain (in ascending order): a pointer to the 18.131 - * destination, the length of one element, the element count and a pointer to 18.132 - * the source. 18.133 - */ 18.134 -typedef size_t(*read_func)(void*, size_t, size_t, void*); 18.135 - 18.136 -#ifdef __cplusplus 18.137 -} 18.138 -#endif 18.139 - 18.140 -#endif /* UCX_H */ 18.141 -
19.1 --- a/src/ucx/utils.c Thu Nov 10 18:44:48 2016 +0100 19.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 19.3 @@ -1,259 +0,0 @@ 19.4 -/* 19.5 - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 19.6 - * 19.7 - * Copyright 2015 Olaf Wintermann. All rights reserved. 19.8 - * 19.9 - * Redistribution and use in source and binary forms, with or without 19.10 - * modification, are permitted provided that the following conditions are met: 19.11 - * 19.12 - * 1. Redistributions of source code must retain the above copyright 19.13 - * notice, this list of conditions and the following disclaimer. 19.14 - * 19.15 - * 2. Redistributions in binary form must reproduce the above copyright 19.16 - * notice, this list of conditions and the following disclaimer in the 19.17 - * documentation and/or other materials provided with the distribution. 19.18 - * 19.19 - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 19.20 - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19.21 - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19.22 - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 19.23 - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 19.24 - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 19.25 - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 19.26 - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 19.27 - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 19.28 - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 19.29 - * POSSIBILITY OF SUCH DAMAGE. 19.30 - */ 19.31 - 19.32 -#include "utils.h" 19.33 -#include <math.h> 19.34 -#include <stdio.h> 19.35 -#include <limits.h> 19.36 -#include <errno.h> 19.37 - 19.38 -/* COPY FUCNTIONS */ 19.39 -void* ucx_strcpy(void* s, void* data) { 19.40 - char *str = (char*) s; 19.41 - size_t n = 1+strlen(str); 19.42 - char *cpy = (char*) malloc(n); 19.43 - memcpy(cpy, str, n); 19.44 - return cpy; 19.45 -} 19.46 - 19.47 -void* ucx_memcpy(void* m, void* n) { 19.48 - size_t k = *((size_t*)n); 19.49 - void *cpy = malloc(k); 19.50 - memcpy(cpy, m, k); 19.51 - return cpy; 19.52 -} 19.53 - 19.54 -size_t ucx_stream_copy(void *src, void *dest, read_func readfnc, 19.55 - write_func writefnc, char* buf, size_t bufsize, size_t n) { 19.56 - if(n == 0 || bufsize == 0) { 19.57 - return 0; 19.58 - } 19.59 - 19.60 - char *lbuf; 19.61 - size_t ncp = 0; 19.62 - 19.63 - if(buf) { 19.64 - lbuf = buf; 19.65 - } else { 19.66 - lbuf = (char*)malloc(bufsize); 19.67 - if(lbuf == NULL) { 19.68 - return 0; 19.69 - } 19.70 - } 19.71 - 19.72 - size_t r; 19.73 - size_t rn = bufsize > n ? n : bufsize; 19.74 - while((r = readfnc(lbuf, 1, rn, src)) != 0) { 19.75 - r = writefnc(lbuf, 1, r, dest); 19.76 - ncp += r; 19.77 - n -= r; 19.78 - rn = bufsize > n ? n : bufsize; 19.79 - if(r == 0 || n == 0) { 19.80 - break; 19.81 - } 19.82 - } 19.83 - 19.84 - if (lbuf != buf) { 19.85 - free(lbuf); 19.86 - } 19.87 - 19.88 - return ncp; 19.89 -} 19.90 - 19.91 -/* COMPARE FUNCTIONS */ 19.92 - 19.93 -int ucx_strcmp(void *s1, void *s2, void *data) { 19.94 - return strcmp((char*)s1, (char*)s2); 19.95 -} 19.96 - 19.97 -int ucx_strncmp(void *s1, void *s2, void *n) { 19.98 - return strncmp((char*)s1, (char*)s2, *((size_t*) n)); 19.99 -} 19.100 - 19.101 -int ucx_intcmp(void *i1, void *i2, void *data) { 19.102 - int a = *((int*) i1); 19.103 - int b = *((int*) i2); 19.104 - if (a == b) { 19.105 - return 0; 19.106 - } else { 19.107 - return a < b ? -1 : 1; 19.108 - } 19.109 -} 19.110 - 19.111 -int ucx_floatcmp(void *f1, void *f2, void *epsilon) { 19.112 - float a = *((float*) f1); 19.113 - float b = *((float*) f2); 19.114 - float e = !epsilon ? 1e-6f : *((float*)epsilon); 19.115 - if (fabsf(a - b) < e) { 19.116 - return 0; 19.117 - } else { 19.118 - return a < b ? -1 : 1; 19.119 - } 19.120 -} 19.121 - 19.122 -int ucx_doublecmp(void *d1, void *d2, void *epsilon) { 19.123 - double a = *((float*) d1); 19.124 - double b = *((float*) d2); 19.125 - double e = !epsilon ? 1e-14 : *((double*)epsilon); 19.126 - if (fabs(a - b) < e) { 19.127 - return 0; 19.128 - } else { 19.129 - return a < b ? -1 : 1; 19.130 - } 19.131 -} 19.132 - 19.133 -int ucx_ptrcmp(void *ptr1, void *ptr2, void *data) { 19.134 - intptr_t p1 = (intptr_t) ptr1; 19.135 - intptr_t p2 = (intptr_t) ptr2; 19.136 - if (p1 == p2) { 19.137 - return 0; 19.138 - } else { 19.139 - return p1 < p2 ? -1 : 1; 19.140 - } 19.141 -} 19.142 - 19.143 -int ucx_memcmp(void *ptr1, void *ptr2, void *n) { 19.144 - return memcmp(ptr1, ptr2, *((size_t*)n)); 19.145 -} 19.146 - 19.147 -/* PRINTF FUNCTIONS */ 19.148 - 19.149 -#ifdef va_copy 19.150 -#define UCX_PRINTF_BUFSIZE 256 19.151 -#else 19.152 -#pragma message("WARNING: C99 va_copy macro not supported by this platform" \ 19.153 - " - limiting ucx_*printf to 2 KiB") 19.154 -#define UCX_PRINTF_BUFSIZE 0x800 19.155 -#endif 19.156 - 19.157 -int ucx_fprintf(void *stream, write_func wfc, const char *fmt, ...) { 19.158 - int ret; 19.159 - va_list ap; 19.160 - va_start(ap, fmt); 19.161 - ret = ucx_vfprintf(stream, wfc, fmt, ap); 19.162 - va_end(ap); 19.163 - return ret; 19.164 -} 19.165 - 19.166 -int ucx_vfprintf(void *stream, write_func wfc, const char *fmt, va_list ap) { 19.167 - char buf[UCX_PRINTF_BUFSIZE]; 19.168 -#ifdef va_copy 19.169 - va_list ap2; 19.170 - va_copy(ap2, ap); 19.171 - int ret = vsnprintf(buf, UCX_PRINTF_BUFSIZE, fmt, ap); 19.172 - if (ret < 0) { 19.173 - return ret; 19.174 - } else if (ret < UCX_PRINTF_BUFSIZE) { 19.175 - return (int)wfc(buf, 1, ret, stream); 19.176 - } else { 19.177 - if (ret == INT_MAX) { 19.178 - errno = ENOMEM; 19.179 - return -1; 19.180 - } 19.181 - 19.182 - int len = ret + 1; 19.183 - char *newbuf = (char*)malloc(len); 19.184 - if (!newbuf) { 19.185 - return -1; 19.186 - } 19.187 - 19.188 - ret = vsnprintf(newbuf, len, fmt, ap2); 19.189 - if (ret > 0) { 19.190 - ret = (int)wfc(newbuf, 1, ret, stream); 19.191 - } 19.192 - free(newbuf); 19.193 - } 19.194 - return ret; 19.195 -#else 19.196 - int ret = vsnprintf(buf, UCX_PRINTF_BUFSIZE, fmt, ap); 19.197 - if (ret < 0) { 19.198 - return ret; 19.199 - } else if (ret < UCX_PRINTF_BUFSIZE) { 19.200 - return (int)wfc(buf, 1, ret, stream); 19.201 - } else { 19.202 - errno = ENOMEM; 19.203 - return -1; 19.204 - } 19.205 -#endif 19.206 -} 19.207 - 19.208 -sstr_t ucx_asprintf(UcxAllocator *allocator, const char *fmt, ...) { 19.209 - va_list ap; 19.210 - sstr_t ret; 19.211 - va_start(ap, fmt); 19.212 - ret = ucx_vasprintf(allocator, fmt, ap); 19.213 - va_end(ap); 19.214 - return ret; 19.215 -} 19.216 - 19.217 -sstr_t ucx_vasprintf(UcxAllocator *a, const char *fmt, va_list ap) { 19.218 - sstr_t s; 19.219 - s.ptr = NULL; 19.220 - s.length = 0; 19.221 - char buf[UCX_PRINTF_BUFSIZE]; 19.222 -#ifdef va_copy 19.223 - va_list ap2; 19.224 - va_copy(ap2, ap); 19.225 - int ret = vsnprintf(buf, UCX_PRINTF_BUFSIZE, fmt, ap); 19.226 - if (ret > 0 && ret < UCX_PRINTF_BUFSIZE) { 19.227 - s.ptr = (char*)almalloc(a, ret + 1); 19.228 - if (s.ptr) { 19.229 - s.length = (size_t)ret; 19.230 - memcpy(s.ptr, buf, ret); 19.231 - s.ptr[s.length] = '\0'; 19.232 - } 19.233 - } else if (ret == INT_MAX) { 19.234 - errno = ENOMEM; 19.235 - } else { 19.236 - int len = ret + 1; 19.237 - s.ptr = (char*)almalloc(a, len); 19.238 - if (s.ptr) { 19.239 - ret = vsnprintf(s.ptr, len, fmt, ap2); 19.240 - if (ret < 0) { 19.241 - free(s.ptr); 19.242 - s.ptr = NULL; 19.243 - } else { 19.244 - s.length = (size_t)ret; 19.245 - } 19.246 - } 19.247 - } 19.248 -#else 19.249 - int ret = vsnprintf(buf, UCX_PRINTF_BUFSIZE, fmt, ap); 19.250 - if (ret > 0 && ret < UCX_PRINTF_BUFSIZE) { 19.251 - s.ptr = (char*)almalloc(a, ret + 1); 19.252 - if (s.ptr) { 19.253 - s.length = (size_t)ret; 19.254 - memcpy(s.ptr, buf, ret); 19.255 - s.ptr[s.length] = '\0'; 19.256 - } 19.257 - } else { 19.258 - errno = ENOMEM; 19.259 - } 19.260 -#endif 19.261 - return s; 19.262 -}
20.1 --- a/src/ucx/utils.h Thu Nov 10 18:44:48 2016 +0100 20.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 20.3 @@ -1,254 +0,0 @@ 20.4 -/* 20.5 - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 20.6 - * 20.7 - * Copyright 2015 Olaf Wintermann. All rights reserved. 20.8 - * 20.9 - * Redistribution and use in source and binary forms, with or without 20.10 - * modification, are permitted provided that the following conditions are met: 20.11 - * 20.12 - * 1. Redistributions of source code must retain the above copyright 20.13 - * notice, this list of conditions and the following disclaimer. 20.14 - * 20.15 - * 2. Redistributions in binary form must reproduce the above copyright 20.16 - * notice, this list of conditions and the following disclaimer in the 20.17 - * documentation and/or other materials provided with the distribution. 20.18 - * 20.19 - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 20.20 - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20.21 - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20.22 - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 20.23 - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 20.24 - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 20.25 - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 20.26 - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 20.27 - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 20.28 - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 20.29 - * POSSIBILITY OF SUCH DAMAGE. 20.30 - */ 20.31 - 20.32 -/** 20.33 - * @file utils.h 20.34 - * 20.35 - * Compare, copy and printf functions. 20.36 - * 20.37 - * @author Mike Becker 20.38 - * @author Olaf Wintermann 20.39 - */ 20.40 - 20.41 -#ifndef UCX_UTILS_H 20.42 -#define UCX_UTILS_H 20.43 - 20.44 -#ifdef __cplusplus 20.45 -extern "C" { 20.46 -#endif 20.47 - 20.48 -#include "ucx.h" 20.49 -#include "string.h" 20.50 -#include "allocator.h" 20.51 -#include <inttypes.h> 20.52 -#include <string.h> 20.53 -#include <stdarg.h> 20.54 - 20.55 -/** 20.56 - * Copies a string. 20.57 - * @param s the string to copy 20.58 - * @param data omitted 20.59 - * @return a pointer to a copy of s1 that can be passed to free(void*) 20.60 - */ 20.61 -void *ucx_strcpy(void *s, void *data); 20.62 - 20.63 -/** 20.64 - * Copies a memory area. 20.65 - * @param m a pointer to the memory area 20.66 - * @param n a pointer to the size_t containing the size of the memory area 20.67 - * @return a pointer to a copy of the specified memory area that can 20.68 - * be passed to free(void*) 20.69 - */ 20.70 -void *ucx_memcpy(void *m, void *n); 20.71 - 20.72 - 20.73 -/** 20.74 - * Reads data from a stream and writes it to another stream. 20.75 - * 20.76 - * @param src the source stream 20.77 - * @param dest the destination stream 20.78 - * @param rfnc the read function 20.79 - * @param wfnc the write function 20.80 - * @param buf a pointer to the copy buffer or <code>NULL</code> if a buffer 20.81 - * shall be implicitly created on the heap 20.82 - * @param bufsize the size of the copy buffer - if <code>NULL</code> was 20.83 - * provided for <code>buf</code>, this is the size of the buffer that shall be 20.84 - * implicitly created 20.85 - * @param n the maximum number of bytes that shall be copied 20.86 - * @return the total number of bytes copied 20.87 - */ 20.88 -size_t ucx_stream_copy(void *src, void *dest, read_func rfnc, write_func wfnc, 20.89 - char* buf, size_t bufsize, size_t n); 20.90 - 20.91 -/** 20.92 - * Shorthand for ucx_stream_copy using the default copy buffer. 20.93 - * 20.94 - * @param src the source stream 20.95 - * @param dest the destination stream 20.96 - * @param rfnc the read function 20.97 - * @param wfnc the write function 20.98 - * @return total number of bytes copied 20.99 - */ 20.100 -#define ucx_stream_hcopy(src,dest,rfnc,wfnc) ucx_stream_copy(\ 20.101 - src, dest, (read_func)rfnc, (write_func)wfnc, NULL, 0x100, (size_t)-1) 20.102 - 20.103 -/** 20.104 - * Shorthand for ucx_stream_copy using the default copy buffer and a copy limit. 20.105 - * 20.106 - * @param src the source stream 20.107 - * @param dest the destination stream 20.108 - * @param rfnc the read function 20.109 - * @param wfnc the write function 20.110 - * @param n maximum number of bytes that shall be copied 20.111 - * @return total number of bytes copied 20.112 - */ 20.113 -#define ucx_stream_ncopy(src,dest,rfnc,wfnc, n) ucx_stream_copy(\ 20.114 - src, dest, (read_func)rfnc, (write_func)wfnc, NULL, 0x100, n) 20.115 - 20.116 -/** 20.117 - * Wraps the strcmp function. 20.118 - * @param s1 string one 20.119 - * @param s2 string two 20.120 - * @param data omitted 20.121 - * @return the result of strcmp(s1, s2) 20.122 - */ 20.123 -int ucx_strcmp(void *s1, void *s2, void *data); 20.124 - 20.125 -/** 20.126 - * Wraps the strncmp function. 20.127 - * @param s1 string one 20.128 - * @param s2 string two 20.129 - * @param n a pointer to the size_t containing the third strncmp parameter 20.130 - * @return the result of strncmp(s1, s2, *n) 20.131 - */ 20.132 -int ucx_strncmp(void *s1, void *s2, void *n); 20.133 - 20.134 -/** 20.135 - * Compares two integers of type int. 20.136 - * @param i1 pointer to integer one 20.137 - * @param i2 pointer to integer two 20.138 - * @param data omitted 20.139 - * @return -1, if *i1 is less than *i2, 0 if both are equal, 20.140 - * 1 if *i1 is greater than *i2 20.141 - */ 20.142 -int ucx_intcmp(void *i1, void *i2, void *data); 20.143 - 20.144 -/** 20.145 - * Compares two real numbers of type float. 20.146 - * @param f1 pointer to float one 20.147 - * @param f2 pointer to float two 20.148 - * @param data if provided: a pointer to precision (default: 1e-6f) 20.149 - * @return -1, if *f1 is less than *f2, 0 if both are equal, 20.150 - * 1 if *f1 is greater than *f2 20.151 - */ 20.152 - 20.153 -int ucx_floatcmp(void *f1, void *f2, void *data); 20.154 - 20.155 -/** 20.156 - * Compares two real numbers of type double. 20.157 - * @param d1 pointer to double one 20.158 - * @param d2 pointer to double two 20.159 - * @param data if provided: a pointer to precision (default: 1e-14) 20.160 - * @return -1, if *d1 is less than *d2, 0 if both are equal, 20.161 - * 1 if *d1 is greater than *d2 20.162 - */ 20.163 -int ucx_doublecmp(void *d1, void *d2, void *data); 20.164 - 20.165 -/** 20.166 - * Compares two pointers. 20.167 - * @param ptr1 pointer one 20.168 - * @param ptr2 pointer two 20.169 - * @param data omitted 20.170 - * @return -1 if ptr1 is less than ptr2, 0 if both are equal, 20.171 - * 1 if ptr1 is greater than ptr2 20.172 - */ 20.173 -int ucx_ptrcmp(void *ptr1, void *ptr2, void *data); 20.174 - 20.175 -/** 20.176 - * Compares two memory areas. 20.177 - * @param ptr1 pointer one 20.178 - * @param ptr2 pointer two 20.179 - * @param n a pointer to the size_t containing the third parameter for memcmp 20.180 - * @return the result of memcmp(ptr1, ptr2, *n) 20.181 - */ 20.182 -int ucx_memcmp(void *ptr1, void *ptr2, void *n); 20.183 - 20.184 -/** 20.185 - * A <code>printf()</code> like function which writes the output to a stream by 20.186 - * using a write_func(). 20.187 - * @param stream the stream the data is written to 20.188 - * @param wfc the write function 20.189 - * @param fmt format string 20.190 - * @param ... additional arguments 20.191 - * @return the total number of bytes written 20.192 - */ 20.193 -int ucx_fprintf(void *stream, write_func wfc, const char *fmt, ...); 20.194 - 20.195 -/** 20.196 - * <code>va_list</code> version of ucx_fprintf(). 20.197 - * @param stream the stream the data is written to 20.198 - * @param wfc the write function 20.199 - * @param fmt format string 20.200 - * @param ap argument list 20.201 - * @return the total number of bytes written 20.202 - * @see ucx_fprintf() 20.203 - */ 20.204 -int ucx_vfprintf(void *stream, write_func wfc, const char *fmt, va_list ap); 20.205 - 20.206 -/** 20.207 - * A <code>printf()</code> like function which allocates space for a sstr_t 20.208 - * the result is written to. 20.209 - * 20.210 - * <b>Attention</b>: The sstr_t data is allocated with the allocators 20.211 - * ucx_allocator_malloc() function. So it is implementation dependent, if 20.212 - * the returned sstr_t.ptr pointer must be passed to the allocators 20.213 - * ucx_allocator_free() function manually. 20.214 - * 20.215 - * <b>Note</b>: The sstr_t.ptr of the return value will <i>always</i> be 20.216 - * <code>NULL</code>-terminated. 20.217 - * 20.218 - * @param allocator the UcxAllocator used for allocating the result sstr_t 20.219 - * @param fmt format string 20.220 - * @param ... additional arguments 20.221 - * @return a sstr_t containing the formatted string 20.222 - */ 20.223 -sstr_t ucx_asprintf(UcxAllocator *allocator, const char *fmt, ...); 20.224 - 20.225 -/** Shortcut for ucx_asprintf() with default allocator. */ 20.226 -#define ucx_sprintf(fmt, ...) \ 20.227 - ucx_asprintf(ucx_default_allocator(), fmt, __VA_ARGS__) 20.228 - 20.229 -/** 20.230 - * <code>va_list</code> version of ucx_asprintf(). 20.231 - * 20.232 - * @param allocator the UcxAllocator used for allocating the result sstr_t 20.233 - * @param fmt format string 20.234 - * @param ap argument list 20.235 - * @return a sstr_t containing the formatted string 20.236 - * @see ucx_asprintf() 20.237 - */ 20.238 -sstr_t ucx_vasprintf(UcxAllocator *allocator, const char *fmt, va_list ap); 20.239 - 20.240 -/** 20.241 - * A <code>printf()</code> like function which writes the output to an 20.242 - * UcxBuffer. 20.243 - * 20.244 - * @param buffer the buffer the data is written to 20.245 - * @param ... format string and additional arguments 20.246 - * @return the total number of bytes written 20.247 - * @see ucx_fprintf() 20.248 - */ 20.249 -#define ucx_bprintf(buffer, ...) ucx_fprintf((UcxBuffer*)buffer, \ 20.250 - (write_func)ucx_buffer_write, __VA_ARGS__) 20.251 - 20.252 -#ifdef __cplusplus 20.253 -} 20.254 -#endif 20.255 - 20.256 -#endif /* UCX_UTILS_H */ 20.257 -
21.1 --- a/test/ctestfile.c Thu Nov 10 18:44:48 2016 +0100 21.2 +++ b/test/ctestfile.c Mon Apr 24 20:54:38 2023 +0200 21.3 @@ -199,7 +199,7 @@ 21.4 21.5 char* util_path_to_url(DavSession *sn, char *path) { 21.6 char *space = malloc(256); 21.7 - UcxBuffer *url = ucx_buffer_new(space, 256, UCX_BUFFER_AUTOEXTEND); 21.8 + UcxBuffer *url = ucx_buffer_new(space, 256, CX_BUFFER_AUTO_EXTEND); 21.9 21.10 // add base url 21.11 ucx_buffer_write(sn->base_url, 1, strlen(sn->base_url), url);
22.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 22.2 +++ b/test/gs/bigtest.html Mon Apr 24 20:54:38 2023 +0200 22.3 @@ -0,0 +1,865 @@ 22.4 +<!DOCTYPE html> 22.5 +<html> 22.6 + <head> 22.7 + <title>c2html</title> 22.8 + <style type="text/css"> 22.9 + div.c2html-code { 22.10 + white-space: pre; 22.11 + font-family: monospace; 22.12 + } 22.13 + a.c2html-lineno { 22.14 + /* as long as user-select isn't widely spread, we throw the bomb */ 22.15 + -webkit-user-select: none; 22.16 + -moz-user-select: none; 22.17 + -ms-user-select: none; 22.18 + user-select: none; 22.19 + display: inline-block; 22.20 + font-style: italic; 22.21 + text-decoration: none; 22.22 + color: grey; 22.23 + } 22.24 + span.c2html-keyword { 22.25 + color: blue; 22.26 + } 22.27 + span.c2html-macroconst { 22.28 + color: cornflowerblue; 22.29 + } 22.30 + span.c2html-type { 22.31 + color: cornflowerblue; 22.32 + } 22.33 + span.c2html-directive { 22.34 + color: green; 22.35 + } 22.36 + span.c2html-string { 22.37 + color: darkorange; 22.38 + } 22.39 + span.c2html-comment { 22.40 + color: grey; 22.41 + } 22.42 + span.c2html-stdinclude { 22.43 + color: darkorange; 22.44 + } 22.45 + span.c2html-userinclude { 22.46 + color: darkorange; 22.47 + } 22.48 + a.c2html-userinclude { 22.49 + color: darkorange; 22.50 + text-decoration: underline; 22.51 + } 22.52 + </style> 22.53 + </head> 22.54 + <body> 22.55 + 22.56 +<div class="c2html-code"> 22.57 +<a class="c2html-lineno" name="l1" href="#l1"> 1 </a><span class="c2html-comment">/*</span> 22.58 +<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> 22.59 +<a class="c2html-lineno" name="l3" href="#l3"> 3 </a><span class="c2html-comment"> *</span> 22.60 +<a class="c2html-lineno" name="l4" href="#l4"> 4 </a><span class="c2html-comment"> * Copyright 2014 Mike Becker. All rights reserved.</span> 22.61 +<a class="c2html-lineno" name="l5" href="#l5"> 5 </a><span class="c2html-comment"> *</span> 22.62 +<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> 22.63 +<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> 22.64 +<a class="c2html-lineno" name="l8" href="#l8"> 8 </a><span class="c2html-comment"> *</span> 22.65 +<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> 22.66 +<a class="c2html-lineno" name="l10" href="#l10"> 10 </a><span class="c2html-comment"> * notice, this list of conditions and the following disclaimer.</span> 22.67 +<a class="c2html-lineno" name="l11" href="#l11"> 11 </a><span class="c2html-comment"> *</span> 22.68 +<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> 22.69 +<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> 22.70 +<a class="c2html-lineno" name="l14" href="#l14"> 14 </a><span class="c2html-comment"> * documentation and/or other materials provided with the distribution.</span> 22.71 +<a class="c2html-lineno" name="l15" href="#l15"> 15 </a><span class="c2html-comment"> *</span> 22.72 +<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> 22.73 +<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> 22.74 +<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> 22.75 +<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> 22.76 +<a class="c2html-lineno" name="l20" href="#l20"> 20 </a><span class="c2html-comment"> * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR</span> 22.77 +<a class="c2html-lineno" name="l21" href="#l21"> 21 </a><span class="c2html-comment"> * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF</span> 22.78 +<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> 22.79 +<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> 22.80 +<a class="c2html-lineno" name="l24" href="#l24"> 24 </a><span class="c2html-comment"> * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)</span> 22.81 +<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> 22.82 +<a class="c2html-lineno" name="l26" href="#l26"> 26 </a><span class="c2html-comment"> * POSSIBILITY OF SUCH DAMAGE.</span> 22.83 +<a class="c2html-lineno" name="l27" href="#l27"> 27 </a><span class="c2html-comment"> *</span> 22.84 +<a class="c2html-lineno" name="l28" href="#l28"> 28 </a><span class="c2html-comment"> */</span> 22.85 +<a class="c2html-lineno" name="l29" href="#l29"> 29 </a> 22.86 +<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> 22.87 +<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> 22.88 +<a class="c2html-lineno" name="l32" href="#l32"> 32 </a><span class="c2html-directive">#include</span> <span class="c2html-stdinclude"><string.h></span> 22.89 +<a class="c2html-lineno" name="l33" href="#l33"> 33 </a><span class="c2html-directive">#include</span> <span class="c2html-stdinclude"><stdlib.h></span> 22.90 +<a class="c2html-lineno" name="l34" href="#l34"> 34 </a><span class="c2html-directive">#include</span> <span class="c2html-stdinclude"><sys/time.h></span> 22.91 +<a class="c2html-lineno" name="l35" href="#l35"> 35 </a> 22.92 +<a class="c2html-lineno" name="l36" href="#l36"> 36 </a><span class="c2html-keyword">static</span> GameState gamestate_copy_sim(GameState *gamestate) { 22.93 +<a class="c2html-lineno" name="l37" href="#l37"> 37 </a> GameState simulation = *gamestate; 22.94 +<a class="c2html-lineno" name="l38" href="#l38"> 38 </a> <span class="c2html-keyword">if</span> (simulation.lastmove) { 22.95 +<a class="c2html-lineno" name="l39" href="#l39"> 39 </a> MoveList *lastmovecopy = malloc(<span class="c2html-keyword">sizeof</span>(MoveList)); 22.96 +<a class="c2html-lineno" name="l40" href="#l40"> 40 </a> *lastmovecopy = *(simulation.lastmove); 22.97 +<a class="c2html-lineno" name="l41" href="#l41"> 41 </a> simulation.movelist = simulation.lastmove = lastmovecopy; 22.98 +<a class="c2html-lineno" name="l42" href="#l42"> 42 </a> } 22.99 +<a class="c2html-lineno" name="l43" href="#l43"> 43 </a> 22.100 +<a class="c2html-lineno" name="l44" href="#l44"> 44 </a> <span class="c2html-keyword">return</span> simulation; 22.101 +<a class="c2html-lineno" name="l45" href="#l45"> 45 </a>} 22.102 +<a class="c2html-lineno" name="l46" href="#l46"> 46 </a> 22.103 +<a class="c2html-lineno" name="l47" href="#l47"> 47 </a><span class="c2html-keyword">void</span> gamestate_init(GameState *gamestate) { 22.104 +<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)); 22.105 +<a class="c2html-lineno" name="l49" href="#l49"> 49 </a> 22.106 +<a class="c2html-lineno" name="l50" href="#l50"> 50 </a> Board initboard = { 22.107 +<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>}, 22.108 +<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>}, 22.109 +<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>}, 22.110 +<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>}, 22.111 +<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>}, 22.112 +<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>}, 22.113 +<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>}, 22.114 +<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>} 22.115 +<a class="c2html-lineno" name="l59" href="#l59"> 59 </a> }; 22.116 +<a class="c2html-lineno" name="l60" href="#l60"> 60 </a> memcpy(gamestate->board, initboard, <span class="c2html-keyword">sizeof</span>(Board)); 22.117 +<a class="c2html-lineno" name="l61" href="#l61"> 61 </a>} 22.118 +<a class="c2html-lineno" name="l62" href="#l62"> 62 </a> 22.119 +<a class="c2html-lineno" name="l63" href="#l63"> 63 </a><span class="c2html-keyword">void</span> gamestate_cleanup(GameState *gamestate) { 22.120 +<a class="c2html-lineno" name="l64" href="#l64"> 64 </a> MoveList *elem; 22.121 +<a class="c2html-lineno" name="l65" href="#l65"> 65 </a> elem = gamestate->movelist; 22.122 +<a class="c2html-lineno" name="l66" href="#l66"> 66 </a> <span class="c2html-keyword">while</span> (elem) { 22.123 +<a class="c2html-lineno" name="l67" href="#l67"> 67 </a> MoveList *cur = elem; 22.124 +<a class="c2html-lineno" name="l68" href="#l68"> 68 </a> elem = elem->next; 22.125 +<a class="c2html-lineno" name="l69" href="#l69"> 69 </a> free(cur); 22.126 +<a class="c2html-lineno" name="l70" href="#l70"> 70 </a> }; 22.127 +<a class="c2html-lineno" name="l71" href="#l71"> 71 </a>} 22.128 +<a class="c2html-lineno" name="l72" href="#l72"> 72 </a> 22.129 +<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> 22.130 +<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) { 22.131 +<a class="c2html-lineno" name="l75" href="#l75"> 75 </a> <span class="c2html-keyword">char</span> *string = move->string; 22.132 +<a class="c2html-lineno" name="l76" href="#l76"> 76 </a> 22.133 +<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> 22.134 +<a class="c2html-lineno" name="l78" href="#l78"> 78 </a> memset(string, <span class="c2html-macroconst">0</span>, <span class="c2html-macroconst">8</span>); 22.135 +<a class="c2html-lineno" name="l79" href="#l79"> 79 </a> 22.136 +<a class="c2html-lineno" name="l80" href="#l80"> 80 </a> <span class="c2html-comment">/* special formats for castling */</span> 22.137 +<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> && 22.138 +<a class="c2html-lineno" name="l82" href="#l82"> 82 </a> abs(move->tofile-move->fromfile) == <span class="c2html-macroconst">2</span>) { 22.139 +<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>)) { 22.140 +<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>); 22.141 +<a class="c2html-lineno" name="l85" href="#l85"> 85 </a> } <span class="c2html-keyword">else</span> { 22.142 +<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>); 22.143 +<a class="c2html-lineno" name="l87" href="#l87"> 87 </a> } 22.144 +<a class="c2html-lineno" name="l88" href="#l88"> 88 </a> } 22.145 +<a class="c2html-lineno" name="l89" href="#l89"> 89 </a> 22.146 +<a class="c2html-lineno" name="l90" href="#l90"> 90 </a> <span class="c2html-comment">/* start by notating the piece character */</span> 22.147 +<a class="c2html-lineno" name="l91" href="#l91"> 91 </a> string[<span class="c2html-macroconst">0</span>] = getpiecechr(move->piece); 22.148 +<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>; 22.149 +<a class="c2html-lineno" name="l93" href="#l93"> 93 </a> 22.150 +<a class="c2html-lineno" name="l94" href="#l94"> 94 </a> <span class="c2html-comment">/* find out how many source information we do need */</span> 22.151 +<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>; 22.152 +<a class="c2html-lineno" name="l96" href="#l96"> 96 </a> <span class="c2html-keyword">if</span> (piece == <span class="c2html-macroconst">PAWN</span>) { 22.153 +<a class="c2html-lineno" name="l97" href="#l97"> 97 </a> <span class="c2html-keyword">if</span> (move->capture) { 22.154 +<a class="c2html-lineno" name="l98" href="#l98"> 98 </a> string[idx++] = filechr(move->fromfile); 22.155 +<a class="c2html-lineno" name="l99" href="#l99"> 99 </a> } 22.156 +<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>) { 22.157 +<a class="c2html-lineno" name="l101" href="#l101">101 </a> Move threats[<span class="c2html-macroconst">16</span>]; 22.158 +<a class="c2html-lineno" name="l102" href="#l102">102 </a> <span class="c2html-type">uint8_t</span> threatcount; 22.159 +<a class="c2html-lineno" name="l103" href="#l103">103 </a> get_real_threats(gamestate, move->torow, move->tofile, 22.160 +<a class="c2html-lineno" name="l104" href="#l104">104 </a> move->piece&<span class="c2html-macroconst">COLOR_MASK</span>, threats, &threatcount); 22.161 +<a class="c2html-lineno" name="l105" href="#l105">105 </a> <span class="c2html-keyword">if</span> (threatcount > <span class="c2html-macroconst">1</span>) { 22.162 +<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>; 22.163 +<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++) { 22.164 +<a class="c2html-lineno" name="l108" href="#l108">108 </a> <span class="c2html-keyword">if</span> (threats[i].fromrow == move->fromrow) { 22.165 +<a class="c2html-lineno" name="l109" href="#l109">109 </a> ambrows++; 22.166 +<a class="c2html-lineno" name="l110" href="#l110">110 </a> } 22.167 +<a class="c2html-lineno" name="l111" href="#l111">111 </a> <span class="c2html-keyword">if</span> (threats[i].fromfile == move->fromfile) { 22.168 +<a class="c2html-lineno" name="l112" href="#l112">112 </a> ambfiles++; 22.169 +<a class="c2html-lineno" name="l113" href="#l113">113 </a> } 22.170 +<a class="c2html-lineno" name="l114" href="#l114">114 </a> } 22.171 +<a class="c2html-lineno" name="l115" href="#l115">115 </a> <span class="c2html-comment">/* ambiguous row, name file */</span> 22.172 +<a class="c2html-lineno" name="l116" href="#l116">116 </a> <span class="c2html-keyword">if</span> (ambrows > <span class="c2html-macroconst">1</span>) { 22.173 +<a class="c2html-lineno" name="l117" href="#l117">117 </a> string[idx++] = filechr(move->fromfile); 22.174 +<a class="c2html-lineno" name="l118" href="#l118">118 </a> } 22.175 +<a class="c2html-lineno" name="l119" href="#l119">119 </a> <span class="c2html-comment">/* ambiguous file, name row */</span> 22.176 +<a class="c2html-lineno" name="l120" href="#l120">120 </a> <span class="c2html-keyword">if</span> (ambfiles > <span class="c2html-macroconst">1</span>) { 22.177 +<a class="c2html-lineno" name="l121" href="#l121">121 </a> string[idx++] = filechr(move->fromrow); 22.178 +<a class="c2html-lineno" name="l122" href="#l122">122 </a> } 22.179 +<a class="c2html-lineno" name="l123" href="#l123">123 </a> } 22.180 +<a class="c2html-lineno" name="l124" href="#l124">124 </a> } 22.181 +<a class="c2html-lineno" name="l125" href="#l125">125 </a> 22.182 +<a class="c2html-lineno" name="l126" href="#l126">126 </a> <span class="c2html-comment">/* capturing? */</span> 22.183 +<a class="c2html-lineno" name="l127" href="#l127">127 </a> <span class="c2html-keyword">if</span> (move->capture) { 22.184 +<a class="c2html-lineno" name="l128" href="#l128">128 </a> string[idx++] = <span class="c2html-string">'x'</span>; 22.185 +<a class="c2html-lineno" name="l129" href="#l129">129 </a> } 22.186 +<a class="c2html-lineno" name="l130" href="#l130">130 </a> 22.187 +<a class="c2html-lineno" name="l131" href="#l131">131 </a> <span class="c2html-comment">/* destination */</span> 22.188 +<a class="c2html-lineno" name="l132" href="#l132">132 </a> string[idx++] = filechr(move->tofile); 22.189 +<a class="c2html-lineno" name="l133" href="#l133">133 </a> string[idx++] = rowchr(move->torow); 22.190 +<a class="c2html-lineno" name="l134" href="#l134">134 </a> 22.191 +<a class="c2html-lineno" name="l135" href="#l135">135 </a> <span class="c2html-comment">/* promotion? */</span> 22.192 +<a class="c2html-lineno" name="l136" href="#l136">136 </a> <span class="c2html-keyword">if</span> (move->promotion) { 22.193 +<a class="c2html-lineno" name="l137" href="#l137">137 </a> string[idx++] = <span class="c2html-string">'='</span>; 22.194 +<a class="c2html-lineno" name="l138" href="#l138">138 </a> string[idx++] = getpiecechr(move->promotion); 22.195 +<a class="c2html-lineno" name="l139" href="#l139">139 </a> } 22.196 +<a class="c2html-lineno" name="l140" href="#l140">140 </a> 22.197 +<a class="c2html-lineno" name="l141" href="#l141">141 </a> <span class="c2html-comment">/* check? */</span> 22.198 +<a class="c2html-lineno" name="l142" href="#l142">142 </a> <span class="c2html-keyword">if</span> (move->check) { 22.199 +<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> 22.200 +<a class="c2html-lineno" name="l144" href="#l144">144 </a> string[idx++] = gamestate->checkmate?<span class="c2html-string">'#'</span>:<span class="c2html-string">'+'</span>; 22.201 +<a class="c2html-lineno" name="l145" href="#l145">145 </a> } 22.202 +<a class="c2html-lineno" name="l146" href="#l146">146 </a>} 22.203 +<a class="c2html-lineno" name="l147" href="#l147">147 </a> 22.204 +<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) { 22.205 +<a class="c2html-lineno" name="l149" href="#l149">149 </a> MoveList *elem = malloc(<span class="c2html-keyword">sizeof</span>(MoveList)); 22.206 +<a class="c2html-lineno" name="l150" href="#l150">150 </a> elem->next = <span class="c2html-macroconst">NULL</span>; 22.207 +<a class="c2html-lineno" name="l151" href="#l151">151 </a> elem->move = *move; 22.208 +<a class="c2html-lineno" name="l152" href="#l152">152 </a> 22.209 +<a class="c2html-lineno" name="l153" href="#l153">153 </a> <span class="c2html-keyword">struct</span> timeval curtimestamp; 22.210 +<a class="c2html-lineno" name="l154" href="#l154">154 </a> gettimeofday(&curtimestamp, <span class="c2html-macroconst">NULL</span>); 22.211 +<a class="c2html-lineno" name="l155" href="#l155">155 </a> elem->move.timestamp.tv_sec = curtimestamp.tv_sec; 22.212 +<a class="c2html-lineno" name="l156" href="#l156">156 </a> elem->move.timestamp.tv_usec = curtimestamp.tv_usec; 22.213 +<a class="c2html-lineno" name="l157" href="#l157">157 </a> 22.214 +<a class="c2html-lineno" name="l158" href="#l158">158 </a> <span class="c2html-keyword">if</span> (gamestate->lastmove) { 22.215 +<a class="c2html-lineno" name="l159" href="#l159">159 </a> <span class="c2html-keyword">struct</span> movetimeval *lasttstamp = &(gamestate->lastmove->move.timestamp); 22.216 +<a class="c2html-lineno" name="l160" href="#l160">160 </a> <span class="c2html-type">uint64_t</span> sec = curtimestamp.tv_sec - lasttstamp->tv_sec; 22.217 +<a class="c2html-lineno" name="l161" href="#l161">161 </a> <span class="c2html-type">suseconds_t</span> micros; 22.218 +<a class="c2html-lineno" name="l162" href="#l162">162 </a> <span class="c2html-keyword">if</span> (curtimestamp.tv_usec < lasttstamp->tv_usec) { 22.219 +<a class="c2html-lineno" name="l163" href="#l163">163 </a> micros = 1e6L-(lasttstamp->tv_usec - curtimestamp.tv_usec); 22.220 +<a class="c2html-lineno" name="l164" href="#l164">164 </a> sec--; 22.221 +<a class="c2html-lineno" name="l165" href="#l165">165 </a> } <span class="c2html-keyword">else</span> { 22.222 +<a class="c2html-lineno" name="l166" href="#l166">166 </a> micros = curtimestamp.tv_usec - lasttstamp->tv_usec; 22.223 +<a class="c2html-lineno" name="l167" href="#l167">167 </a> } 22.224 +<a class="c2html-lineno" name="l168" href="#l168">168 </a> 22.225 +<a class="c2html-lineno" name="l169" href="#l169">169 </a> elem->move.movetime.tv_sec = sec; 22.226 +<a class="c2html-lineno" name="l170" href="#l170">170 </a> elem->move.movetime.tv_usec = micros; 22.227 +<a class="c2html-lineno" name="l171" href="#l171">171 </a> 22.228 +<a class="c2html-lineno" name="l172" href="#l172">172 </a> gamestate->lastmove->next = elem; 22.229 +<a class="c2html-lineno" name="l173" href="#l173">173 </a> gamestate->lastmove = elem; 22.230 +<a class="c2html-lineno" name="l174" href="#l174">174 </a> } <span class="c2html-keyword">else</span> { 22.231 +<a class="c2html-lineno" name="l175" href="#l175">175 </a> elem->move.movetime.tv_usec = <span class="c2html-macroconst">0</span>; 22.232 +<a class="c2html-lineno" name="l176" href="#l176">176 </a> elem->move.movetime.tv_sec = <span class="c2html-macroconst">0</span>; 22.233 +<a class="c2html-lineno" name="l177" href="#l177">177 </a> gamestate->movelist = gamestate->lastmove = elem; 22.234 +<a class="c2html-lineno" name="l178" href="#l178">178 </a> } 22.235 +<a class="c2html-lineno" name="l179" href="#l179">179 </a>} 22.236 +<a class="c2html-lineno" name="l180" href="#l180">180 </a> 22.237 +<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) { 22.238 +<a class="c2html-lineno" name="l182" href="#l182">182 </a> <span class="c2html-keyword">switch</span> (piece & <span class="c2html-macroconst">PIECE_MASK</span>) { 22.239 +<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>; 22.240 +<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>; 22.241 +<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>; 22.242 +<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>; 22.243 +<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>; 22.244 +<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>; 22.245 +<a class="c2html-lineno" name="l189" href="#l189">189 </a> } 22.246 +<a class="c2html-lineno" name="l190" href="#l190">190 </a>} 22.247 +<a class="c2html-lineno" name="l191" href="#l191">191 </a> 22.248 +<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) { 22.249 +<a class="c2html-lineno" name="l193" href="#l193">193 </a> <span class="c2html-keyword">switch</span> (c) { 22.250 +<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>; 22.251 +<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>; 22.252 +<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>; 22.253 +<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>; 22.254 +<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>; 22.255 +<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>; 22.256 +<a class="c2html-lineno" name="l200" href="#l200">200 </a> } 22.257 +<a class="c2html-lineno" name="l201" href="#l201">201 </a>} 22.258 +<a class="c2html-lineno" name="l202" href="#l202">202 </a> 22.259 +<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) { 22.260 +<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>; 22.261 +<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>; 22.262 +<a class="c2html-lineno" name="l206" href="#l206">206 </a> 22.263 +<a class="c2html-lineno" name="l207" href="#l207">207 </a> <span class="c2html-comment">/* en passant capture */</span> 22.264 +<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> && 22.265 +<a class="c2html-lineno" name="l209" href="#l209">209 </a> mdst(gamestate->board, move) == <span class="c2html-macroconst">0</span>) { 22.266 +<a class="c2html-lineno" name="l210" href="#l210">210 </a> gamestate->board[move->fromrow][move->tofile] = <span class="c2html-macroconst">0</span>; 22.267 +<a class="c2html-lineno" name="l211" href="#l211">211 </a> } 22.268 +<a class="c2html-lineno" name="l212" href="#l212">212 </a> 22.269 +<a class="c2html-lineno" name="l213" href="#l213">213 </a> <span class="c2html-comment">/* remove old en passant threats */</span> 22.270 +<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++) { 22.271 +<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>; 22.272 +<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>; 22.273 +<a class="c2html-lineno" name="l217" href="#l217">217 </a> } 22.274 +<a class="c2html-lineno" name="l218" href="#l218">218 </a> 22.275 +<a class="c2html-lineno" name="l219" href="#l219">219 </a> <span class="c2html-comment">/* add new en passant threat */</span> 22.276 +<a class="c2html-lineno" name="l220" href="#l220">220 </a> <span class="c2html-keyword">if</span> (piece == <span class="c2html-macroconst">PAWN</span> && ( 22.277 +<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>) || 22.278 +<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>))) { 22.279 +<a class="c2html-lineno" name="l223" href="#l223">223 </a> move->piece |= <span class="c2html-macroconst">ENPASSANT_THREAT</span>; 22.280 +<a class="c2html-lineno" name="l224" href="#l224">224 </a> } 22.281 +<a class="c2html-lineno" name="l225" href="#l225">225 </a> 22.282 +<a class="c2html-lineno" name="l226" href="#l226">226 </a> <span class="c2html-comment">/* move (and maybe capture or promote) */</span> 22.283 +<a class="c2html-lineno" name="l227" href="#l227">227 </a> msrc(gamestate->board, move) = <span class="c2html-macroconst">0</span>; 22.284 +<a class="c2html-lineno" name="l228" href="#l228">228 </a> <span class="c2html-keyword">if</span> (move->promotion) { 22.285 +<a class="c2html-lineno" name="l229" href="#l229">229 </a> mdst(gamestate->board, move) = move->promotion; 22.286 +<a class="c2html-lineno" name="l230" href="#l230">230 </a> } <span class="c2html-keyword">else</span> { 22.287 +<a class="c2html-lineno" name="l231" href="#l231">231 </a> mdst(gamestate->board, move) = move->piece; 22.288 +<a class="c2html-lineno" name="l232" href="#l232">232 </a> } 22.289 +<a class="c2html-lineno" name="l233" href="#l233">233 </a> 22.290 +<a class="c2html-lineno" name="l234" href="#l234">234 </a> <span class="c2html-comment">/* castling */</span> 22.291 +<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>)) { 22.292 +<a class="c2html-lineno" name="l236" href="#l236">236 </a> 22.293 +<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>)) { 22.294 +<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>; 22.295 +<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>; 22.296 +<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>)) { 22.297 +<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>; 22.298 +<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>; 22.299 +<a class="c2html-lineno" name="l243" href="#l243">243 </a> } 22.300 +<a class="c2html-lineno" name="l244" href="#l244">244 </a> } 22.301 +<a class="c2html-lineno" name="l245" href="#l245">245 </a> 22.302 +<a class="c2html-lineno" name="l246" href="#l246">246 </a> <span class="c2html-keyword">if</span> (!simulate) { 22.303 +<a class="c2html-lineno" name="l247" href="#l247">247 </a> <span class="c2html-keyword">if</span> (!move->string[<span class="c2html-macroconst">0</span>]) { 22.304 +<a class="c2html-lineno" name="l248" href="#l248">248 </a> format_move(gamestate, move); 22.305 +<a class="c2html-lineno" name="l249" href="#l249">249 </a> } 22.306 +<a class="c2html-lineno" name="l250" href="#l250">250 </a> } 22.307 +<a class="c2html-lineno" name="l251" href="#l251">251 </a> <span class="c2html-comment">/* add move, even in simulation (checkmate test needs it) */</span> 22.308 +<a class="c2html-lineno" name="l252" href="#l252">252 </a> addmove(gamestate, move); 22.309 +<a class="c2html-lineno" name="l253" href="#l253">253 </a>} 22.310 +<a class="c2html-lineno" name="l254" href="#l254">254 </a> 22.311 +<a class="c2html-lineno" name="l255" href="#l255">255 </a><span class="c2html-keyword">void</span> apply_move(GameState *gamestate, Move *move) { 22.312 +<a class="c2html-lineno" name="l256" href="#l256">256 </a> apply_move_impl(gamestate, move, <span class="c2html-macroconst">0</span>); 22.313 +<a class="c2html-lineno" name="l257" href="#l257">257 </a>} 22.314 +<a class="c2html-lineno" name="l258" href="#l258">258 </a> 22.315 +<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) { 22.316 +<a class="c2html-lineno" name="l260" href="#l260">260 </a> <span class="c2html-comment">/* validate indices (don't trust opponent) */</span> 22.317 +<a class="c2html-lineno" name="l261" href="#l261">261 </a> <span class="c2html-keyword">if</span> (!chkidx(move)) { 22.318 +<a class="c2html-lineno" name="l262" href="#l262">262 </a> <span class="c2html-keyword">return</span> <span class="c2html-macroconst">INVALID_POSITION</span>; 22.319 +<a class="c2html-lineno" name="l263" href="#l263">263 </a> } 22.320 +<a class="c2html-lineno" name="l264" href="#l264">264 </a> 22.321 +<a class="c2html-lineno" name="l265" href="#l265">265 </a> <span class="c2html-comment">/* must move */</span> 22.322 +<a class="c2html-lineno" name="l266" href="#l266">266 </a> <span class="c2html-keyword">if</span> (move->fromfile == move->tofile && move->fromrow == move->torow) { 22.323 +<a class="c2html-lineno" name="l267" href="#l267">267 </a> <span class="c2html-keyword">return</span> <span class="c2html-macroconst">INVALID_MOVE_SYNTAX</span>; 22.324 +<a class="c2html-lineno" name="l268" href="#l268">268 </a> } 22.325 +<a class="c2html-lineno" name="l269" href="#l269">269 </a> 22.326 +<a class="c2html-lineno" name="l270" href="#l270">270 </a> <span class="c2html-comment">/* does piece exist */</span> 22.327 +<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>)) 22.328 +<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>))) { 22.329 +<a class="c2html-lineno" name="l273" href="#l273">273 </a> <span class="c2html-keyword">return</span> <span class="c2html-macroconst">INVALID_POSITION</span>; 22.330 +<a class="c2html-lineno" name="l274" href="#l274">274 </a> } 22.331 +<a class="c2html-lineno" name="l275" href="#l275">275 </a> 22.332 +<a class="c2html-lineno" name="l276" href="#l276">276 </a> <span class="c2html-comment">/* can't capture own pieces */</span> 22.333 +<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>) 22.334 +<a class="c2html-lineno" name="l278" href="#l278">278 </a> == (move->piece & <span class="c2html-macroconst">COLOR_MASK</span>)) { 22.335 +<a class="c2html-lineno" name="l279" href="#l279">279 </a> <span class="c2html-keyword">return</span> <span class="c2html-macroconst">RULES_VIOLATED</span>; 22.336 +<a class="c2html-lineno" name="l280" href="#l280">280 </a> } 22.337 +<a class="c2html-lineno" name="l281" href="#l281">281 </a> 22.338 +<a class="c2html-lineno" name="l282" href="#l282">282 </a> <span class="c2html-comment">/* must capture, if and only if destination is occupied */</span> 22.339 +<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) || 22.340 +<a class="c2html-lineno" name="l284" href="#l284">284 </a> (mdst(gamestate->board, move) != <span class="c2html-macroconst">0</span> && !move->capture)) { 22.341 +<a class="c2html-lineno" name="l285" href="#l285">285 </a> <span class="c2html-keyword">return</span> <span class="c2html-macroconst">INVALID_MOVE_SYNTAX</span>; 22.342 +<a class="c2html-lineno" name="l286" href="#l286">286 </a> } 22.343 +<a class="c2html-lineno" name="l287" href="#l287">287 </a> 22.344 +<a class="c2html-lineno" name="l288" href="#l288">288 </a> <span class="c2html-comment">/* validate individual rules */</span> 22.345 +<a class="c2html-lineno" name="l289" href="#l289">289 </a> _Bool chkrules; 22.346 +<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>) { 22.347 +<a class="c2html-lineno" name="l291" href="#l291">291 </a> <span class="c2html-keyword">case</span> <span class="c2html-macroconst">PAWN</span>: 22.348 +<a class="c2html-lineno" name="l292" href="#l292">292 </a> chkrules = pawn_chkrules(gamestate, move) && 22.349 +<a class="c2html-lineno" name="l293" href="#l293">293 </a> !pawn_isblocked(gamestate, move); 22.350 +<a class="c2html-lineno" name="l294" href="#l294">294 </a> <span class="c2html-keyword">break</span>; 22.351 +<a class="c2html-lineno" name="l295" href="#l295">295 </a> <span class="c2html-keyword">case</span> <span class="c2html-macroconst">ROOK</span>: 22.352 +<a class="c2html-lineno" name="l296" href="#l296">296 </a> chkrules = rook_chkrules(move) && 22.353 +<a class="c2html-lineno" name="l297" href="#l297">297 </a> !rook_isblocked(gamestate, move); 22.354 +<a class="c2html-lineno" name="l298" href="#l298">298 </a> <span class="c2html-keyword">break</span>; 22.355 +<a class="c2html-lineno" name="l299" href="#l299">299 </a> <span class="c2html-keyword">case</span> <span class="c2html-macroconst">KNIGHT</span>: 22.356 +<a class="c2html-lineno" name="l300" href="#l300">300 </a> chkrules = knight_chkrules(move); <span class="c2html-comment">/* knight is never blocked */</span> 22.357 +<a class="c2html-lineno" name="l301" href="#l301">301 </a> <span class="c2html-keyword">break</span>; 22.358 +<a class="c2html-lineno" name="l302" href="#l302">302 </a> <span class="c2html-keyword">case</span> <span class="c2html-macroconst">BISHOP</span>: 22.359 +<a class="c2html-lineno" name="l303" href="#l303">303 </a> chkrules = bishop_chkrules(move) && 22.360 +<a class="c2html-lineno" name="l304" href="#l304">304 </a> !bishop_isblocked(gamestate, move); 22.361 +<a class="c2html-lineno" name="l305" href="#l305">305 </a> <span class="c2html-keyword">break</span>; 22.362 +<a class="c2html-lineno" name="l306" href="#l306">306 </a> <span class="c2html-keyword">case</span> <span class="c2html-macroconst">QUEEN</span>: 22.363 +<a class="c2html-lineno" name="l307" href="#l307">307 </a> chkrules = queen_chkrules(move) && 22.364 +<a class="c2html-lineno" name="l308" href="#l308">308 </a> !queen_isblocked(gamestate, move); 22.365 +<a class="c2html-lineno" name="l309" href="#l309">309 </a> <span class="c2html-keyword">break</span>; 22.366 +<a class="c2html-lineno" name="l310" href="#l310">310 </a> <span class="c2html-keyword">case</span> <span class="c2html-macroconst">KING</span>: 22.367 +<a class="c2html-lineno" name="l311" href="#l311">311 </a> chkrules = king_chkrules(gamestate, move) && 22.368 +<a class="c2html-lineno" name="l312" href="#l312">312 </a> !king_isblocked(gamestate, move); 22.369 +<a class="c2html-lineno" name="l313" href="#l313">313 </a> <span class="c2html-keyword">break</span>; 22.370 +<a class="c2html-lineno" name="l314" href="#l314">314 </a> <span class="c2html-keyword">default</span>: 22.371 +<a class="c2html-lineno" name="l315" href="#l315">315 </a> <span class="c2html-keyword">return</span> <span class="c2html-macroconst">INVALID_MOVE_SYNTAX</span>; 22.372 +<a class="c2html-lineno" name="l316" href="#l316">316 </a> } 22.373 +<a class="c2html-lineno" name="l317" href="#l317">317 </a> 22.374 +<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>; 22.375 +<a class="c2html-lineno" name="l319" href="#l319">319 </a>} 22.376 +<a class="c2html-lineno" name="l320" href="#l320">320 </a> 22.377 +<a class="c2html-lineno" name="l321" href="#l321">321 </a><span class="c2html-keyword">int</span> validate_move(GameState *gamestate, Move *move) { 22.378 +<a class="c2html-lineno" name="l322" href="#l322">322 </a> 22.379 +<a class="c2html-lineno" name="l323" href="#l323">323 </a> <span class="c2html-keyword">int</span> result = validate_move_rules(gamestate, move); 22.380 +<a class="c2html-lineno" name="l324" href="#l324">324 </a> 22.381 +<a class="c2html-lineno" name="l325" href="#l325">325 </a> <span class="c2html-comment">/* cancel processing to save resources */</span> 22.382 +<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>) { 22.383 +<a class="c2html-lineno" name="l327" href="#l327">327 </a> <span class="c2html-keyword">return</span> result; 22.384 +<a class="c2html-lineno" name="l328" href="#l328">328 </a> } 22.385 +<a class="c2html-lineno" name="l329" href="#l329">329 </a> 22.386 +<a class="c2html-lineno" name="l330" href="#l330">330 </a> <span class="c2html-comment">/* find kings for check validation */</span> 22.387 +<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>); 22.388 +<a class="c2html-lineno" name="l332" href="#l332">332 </a> 22.389 +<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>; 22.390 +<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++) { 22.391 +<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++) { 22.392 +<a class="c2html-lineno" name="l336" href="#l336">336 </a> <span class="c2html-keyword">if</span> (gamestate->board[row][file] == 22.393 +<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>)) { 22.394 +<a class="c2html-lineno" name="l338" href="#l338">338 </a> mykingfile = file; 22.395 +<a class="c2html-lineno" name="l339" href="#l339">339 </a> mykingrow = row; 22.396 +<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] == 22.397 +<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>)) { 22.398 +<a class="c2html-lineno" name="l342" href="#l342">342 </a> opkingfile = file; 22.399 +<a class="c2html-lineno" name="l343" href="#l343">343 </a> opkingrow = row; 22.400 +<a class="c2html-lineno" name="l344" href="#l344">344 </a> } 22.401 +<a class="c2html-lineno" name="l345" href="#l345">345 </a> } 22.402 +<a class="c2html-lineno" name="l346" href="#l346">346 </a> } 22.403 +<a class="c2html-lineno" name="l347" href="#l347">347 </a> 22.404 +<a class="c2html-lineno" name="l348" href="#l348">348 </a> <span class="c2html-comment">/* simulate move for check validation */</span> 22.405 +<a class="c2html-lineno" name="l349" href="#l349">349 </a> GameState simulation = gamestate_copy_sim(gamestate); 22.406 +<a class="c2html-lineno" name="l350" href="#l350">350 </a> Move simmove = *move; 22.407 +<a class="c2html-lineno" name="l351" href="#l351">351 </a> apply_move_impl(&simulation, &simmove, <span class="c2html-macroconst">1</span>); 22.408 +<a class="c2html-lineno" name="l352" href="#l352">352 </a> 22.409 +<a class="c2html-lineno" name="l353" href="#l353">353 </a> <span class="c2html-comment">/* don't move into or stay in check position */</span> 22.410 +<a class="c2html-lineno" name="l354" href="#l354">354 </a> <span class="c2html-keyword">if</span> (is_covered(&simulation, mykingrow, mykingfile, 22.411 +<a class="c2html-lineno" name="l355" href="#l355">355 </a> opponent_color(piececolor))) { 22.412 +<a class="c2html-lineno" name="l356" href="#l356">356 </a> 22.413 +<a class="c2html-lineno" name="l357" href="#l357">357 </a> gamestate_cleanup(&simulation); 22.414 +<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>) { 22.415 +<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>; 22.416 +<a class="c2html-lineno" name="l360" href="#l360">360 </a> } <span class="c2html-keyword">else</span> { 22.417 +<a class="c2html-lineno" name="l361" href="#l361">361 </a> <span class="c2html-comment">/* last move is always not null in this case */</span> 22.418 +<a class="c2html-lineno" name="l362" href="#l362">362 </a> <span class="c2html-keyword">return</span> gamestate->lastmove->move.check ? 22.419 +<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>; 22.420 +<a class="c2html-lineno" name="l364" href="#l364">364 </a> } 22.421 +<a class="c2html-lineno" name="l365" href="#l365">365 </a> } 22.422 +<a class="c2html-lineno" name="l366" href="#l366">366 </a> 22.423 +<a class="c2html-lineno" name="l367" href="#l367">367 </a> <span class="c2html-comment">/* correct check and checkmate flags (move is still valid) */</span> 22.424 +<a class="c2html-lineno" name="l368" href="#l368">368 </a> Move threats[<span class="c2html-macroconst">16</span>]; 22.425 +<a class="c2html-lineno" name="l369" href="#l369">369 </a> <span class="c2html-type">uint8_t</span> threatcount; 22.426 +<a class="c2html-lineno" name="l370" href="#l370">370 </a> move->check = get_threats(&simulation, opkingrow, opkingfile, 22.427 +<a class="c2html-lineno" name="l371" href="#l371">371 </a> piececolor, threats, &threatcount); 22.428 +<a class="c2html-lineno" name="l372" href="#l372">372 </a> 22.429 +<a class="c2html-lineno" name="l373" href="#l373">373 </a> <span class="c2html-keyword">if</span> (move->check) { 22.430 +<a class="c2html-lineno" name="l374" href="#l374">374 </a> <span class="c2html-comment">/* determine possible escape fields */</span> 22.431 +<a class="c2html-lineno" name="l375" href="#l375">375 </a> _Bool canescape = <span class="c2html-macroconst">0</span>; 22.432 +<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++) { 22.433 +<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++) { 22.434 +<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>) && 22.435 +<a class="c2html-lineno" name="l379" href="#l379">379 </a> isidx(opkingrow + dr) && isidx(opkingfile + df)) { 22.436 +<a class="c2html-lineno" name="l380" href="#l380">380 </a> 22.437 +<a class="c2html-lineno" name="l381" href="#l381">381 </a> <span class="c2html-comment">/* escape field neither blocked nor covered */</span> 22.438 +<a class="c2html-lineno" name="l382" href="#l382">382 </a> <span class="c2html-keyword">if</span> ((simulation.board[opkingrow + dr][opkingfile + df] 22.439 +<a class="c2html-lineno" name="l383" href="#l383">383 </a> & <span class="c2html-macroconst">COLOR_MASK</span>) != opponent_color(piececolor)) { 22.440 +<a class="c2html-lineno" name="l384" href="#l384">384 </a> canescape |= !is_covered(&simulation, 22.441 +<a class="c2html-lineno" name="l385" href="#l385">385 </a> opkingrow + dr, opkingfile + df, piececolor); 22.442 +<a class="c2html-lineno" name="l386" href="#l386">386 </a> } 22.443 +<a class="c2html-lineno" name="l387" href="#l387">387 </a> } 22.444 +<a class="c2html-lineno" name="l388" href="#l388">388 </a> } 22.445 +<a class="c2html-lineno" name="l389" href="#l389">389 </a> } 22.446 +<a class="c2html-lineno" name="l390" href="#l390">390 </a> <span class="c2html-comment">/* can't escape, can he capture? */</span> 22.447 +<a class="c2html-lineno" name="l391" href="#l391">391 </a> <span class="c2html-keyword">if</span> (!canescape && threatcount == <span class="c2html-macroconst">1</span>) { 22.448 +<a class="c2html-lineno" name="l392" href="#l392">392 </a> canescape = is_attacked(&simulation, threats[<span class="c2html-macroconst">0</span>].fromrow, 22.449 +<a class="c2html-lineno" name="l393" href="#l393">393 </a> threats[<span class="c2html-macroconst">0</span>].fromfile, opponent_color(piececolor)); 22.450 +<a class="c2html-lineno" name="l394" href="#l394">394 </a> } 22.451 +<a class="c2html-lineno" name="l395" href="#l395">395 </a> 22.452 +<a class="c2html-lineno" name="l396" href="#l396">396 </a> <span class="c2html-comment">/* can't capture, can he block? */</span> 22.453 +<a class="c2html-lineno" name="l397" href="#l397">397 </a> <span class="c2html-keyword">if</span> (!canescape && threatcount == <span class="c2html-macroconst">1</span>) { 22.454 +<a class="c2html-lineno" name="l398" href="#l398">398 </a> Move *threat = &(threats[<span class="c2html-macroconst">0</span>]); 22.455 +<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>; 22.456 +<a class="c2html-lineno" name="l400" href="#l400">400 </a> 22.457 +<a class="c2html-lineno" name="l401" href="#l401">401 </a> <span class="c2html-comment">/* knight, pawns and the king cannot be blocked */</span> 22.458 +<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> 22.459 +<a class="c2html-lineno" name="l403" href="#l403">403 </a> || threatpiece == <span class="c2html-macroconst">QUEEN</span>) { 22.460 +<a class="c2html-lineno" name="l404" href="#l404">404 </a> <span class="c2html-keyword">if</span> (threat->fromrow == threat->torow) { 22.461 +<a class="c2html-lineno" name="l405" href="#l405">405 </a> <span class="c2html-comment">/* rook aspect (on row) */</span> 22.462 +<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>; 22.463 +<a class="c2html-lineno" name="l407" href="#l407">407 </a> <span class="c2html-type">uint8_t</span> file = threat->fromfile; 22.464 +<a class="c2html-lineno" name="l408" href="#l408">408 </a> <span class="c2html-keyword">while</span> (!canescape && file != threat->tofile - d) { 22.465 +<a class="c2html-lineno" name="l409" href="#l409">409 </a> file += d; 22.466 +<a class="c2html-lineno" name="l410" href="#l410">410 </a> canescape |= is_protected(&simulation, 22.467 +<a class="c2html-lineno" name="l411" href="#l411">411 </a> threat->torow, file, opponent_color(piececolor)); 22.468 +<a class="c2html-lineno" name="l412" href="#l412">412 </a> } 22.469 +<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) { 22.470 +<a class="c2html-lineno" name="l414" href="#l414">414 </a> <span class="c2html-comment">/* rook aspect (on file) */</span> 22.471 +<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>; 22.472 +<a class="c2html-lineno" name="l416" href="#l416">416 </a> <span class="c2html-type">uint8_t</span> row = threat->fromrow; 22.473 +<a class="c2html-lineno" name="l417" href="#l417">417 </a> <span class="c2html-keyword">while</span> (!canescape && row != threat->torow - d) { 22.474 +<a class="c2html-lineno" name="l418" href="#l418">418 </a> row += d; 22.475 +<a class="c2html-lineno" name="l419" href="#l419">419 </a> canescape |= is_protected(&simulation, 22.476 +<a class="c2html-lineno" name="l420" href="#l420">420 </a> row, threat->tofile, opponent_color(piececolor)); 22.477 +<a class="c2html-lineno" name="l421" href="#l421">421 </a> } 22.478 +<a class="c2html-lineno" name="l422" href="#l422">422 </a> } <span class="c2html-keyword">else</span> { 22.479 +<a class="c2html-lineno" name="l423" href="#l423">423 </a> <span class="c2html-comment">/* bishop aspect */</span> 22.480 +<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>; 22.481 +<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>; 22.482 +<a class="c2html-lineno" name="l426" href="#l426">426 </a> 22.483 +<a class="c2html-lineno" name="l427" href="#l427">427 </a> <span class="c2html-type">uint8_t</span> row = threat->fromrow; 22.484 +<a class="c2html-lineno" name="l428" href="#l428">428 </a> <span class="c2html-type">uint8_t</span> file = threat->fromfile; 22.485 +<a class="c2html-lineno" name="l429" href="#l429">429 </a> <span class="c2html-keyword">while</span> (!canescape && file != threat->tofile - df 22.486 +<a class="c2html-lineno" name="l430" href="#l430">430 </a> && row != threat->torow - dr) { 22.487 +<a class="c2html-lineno" name="l431" href="#l431">431 </a> row += dr; 22.488 +<a class="c2html-lineno" name="l432" href="#l432">432 </a> file += df; 22.489 +<a class="c2html-lineno" name="l433" href="#l433">433 </a> canescape |= is_protected(&simulation, row, file, 22.490 +<a class="c2html-lineno" name="l434" href="#l434">434 </a> opponent_color(piececolor)); 22.491 +<a class="c2html-lineno" name="l435" href="#l435">435 </a> } 22.492 +<a class="c2html-lineno" name="l436" href="#l436">436 </a> } 22.493 +<a class="c2html-lineno" name="l437" href="#l437">437 </a> } 22.494 +<a class="c2html-lineno" name="l438" href="#l438">438 </a> } 22.495 +<a class="c2html-lineno" name="l439" href="#l439">439 </a> 22.496 +<a class="c2html-lineno" name="l440" href="#l440">440 </a> <span class="c2html-keyword">if</span> (!canescape) { 22.497 +<a class="c2html-lineno" name="l441" href="#l441">441 </a> gamestate->checkmate = <span class="c2html-macroconst">1</span>; 22.498 +<a class="c2html-lineno" name="l442" href="#l442">442 </a> } 22.499 +<a class="c2html-lineno" name="l443" href="#l443">443 </a> } 22.500 +<a class="c2html-lineno" name="l444" href="#l444">444 </a> 22.501 +<a class="c2html-lineno" name="l445" href="#l445">445 </a> gamestate_cleanup(&simulation); 22.502 +<a class="c2html-lineno" name="l446" href="#l446">446 </a> 22.503 +<a class="c2html-lineno" name="l447" href="#l447">447 </a> <span class="c2html-keyword">return</span> <span class="c2html-macroconst">VALID_MOVE_SEMANTICS</span>; 22.504 +<a class="c2html-lineno" name="l448" href="#l448">448 </a>} 22.505 +<a class="c2html-lineno" name="l449" href="#l449">449 </a> 22.506 +<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, 22.507 +<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) { 22.508 +<a class="c2html-lineno" name="l452" href="#l452">452 </a> Move candidates[<span class="c2html-macroconst">32</span>]; 22.509 +<a class="c2html-lineno" name="l453" href="#l453">453 </a> <span class="c2html-keyword">int</span> candidatecount = <span class="c2html-macroconst">0</span>; 22.510 +<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++) { 22.511 +<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++) { 22.512 +<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) { 22.513 +<a class="c2html-lineno" name="l457" href="#l457">457 </a> <span class="c2html-comment">// non-capturing move</span> 22.514 +<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)); 22.515 +<a class="c2html-lineno" name="l459" href="#l459">459 </a> candidates[candidatecount].piece = gamestate->board[r][f]; 22.516 +<a class="c2html-lineno" name="l460" href="#l460">460 </a> candidates[candidatecount].fromrow = r; 22.517 +<a class="c2html-lineno" name="l461" href="#l461">461 </a> candidates[candidatecount].fromfile = f; 22.518 +<a class="c2html-lineno" name="l462" href="#l462">462 </a> candidates[candidatecount].torow = row; 22.519 +<a class="c2html-lineno" name="l463" href="#l463">463 </a> candidates[candidatecount].tofile = file; 22.520 +<a class="c2html-lineno" name="l464" href="#l464">464 </a> candidatecount++; 22.521 +<a class="c2html-lineno" name="l465" href="#l465">465 </a> 22.522 +<a class="c2html-lineno" name="l466" href="#l466">466 </a> <span class="c2html-comment">// capturing move</span> 22.523 +<a class="c2html-lineno" name="l467" href="#l467">467 </a> memcpy(&(candidates[candidatecount]), 22.524 +<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)); 22.525 +<a class="c2html-lineno" name="l469" href="#l469">469 </a> candidates[candidatecount].capture = <span class="c2html-macroconst">1</span>; 22.526 +<a class="c2html-lineno" name="l470" href="#l470">470 </a> candidatecount++; 22.527 +<a class="c2html-lineno" name="l471" href="#l471">471 </a> } 22.528 +<a class="c2html-lineno" name="l472" href="#l472">472 </a> } 22.529 +<a class="c2html-lineno" name="l473" href="#l473">473 </a> } 22.530 +<a class="c2html-lineno" name="l474" href="#l474">474 </a> 22.531 +<a class="c2html-lineno" name="l475" href="#l475">475 </a> <span class="c2html-keyword">if</span> (threatcount) { 22.532 +<a class="c2html-lineno" name="l476" href="#l476">476 </a> *threatcount = <span class="c2html-macroconst">0</span>; 22.533 +<a class="c2html-lineno" name="l477" href="#l477">477 </a> } 22.534 +<a class="c2html-lineno" name="l478" href="#l478">478 </a> 22.535 +<a class="c2html-lineno" name="l479" href="#l479">479 </a> 22.536 +<a class="c2html-lineno" name="l480" href="#l480">480 </a> _Bool result = <span class="c2html-macroconst">0</span>; 22.537 +<a class="c2html-lineno" name="l481" href="#l481">481 </a> 22.538 +<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++) { 22.539 +<a class="c2html-lineno" name="l483" href="#l483">483 </a> <span class="c2html-keyword">if</span> (validate_move_rules(gamestate, &(candidates[i])) 22.540 +<a class="c2html-lineno" name="l484" href="#l484">484 </a> == <span class="c2html-macroconst">VALID_MOVE_SEMANTICS</span>) { 22.541 +<a class="c2html-lineno" name="l485" href="#l485">485 </a> result = <span class="c2html-macroconst">1</span>; 22.542 +<a class="c2html-lineno" name="l486" href="#l486">486 </a> <span class="c2html-keyword">if</span> (threats && threatcount) { 22.543 +<a class="c2html-lineno" name="l487" href="#l487">487 </a> threats[(*threatcount)++] = candidates[i]; 22.544 +<a class="c2html-lineno" name="l488" href="#l488">488 </a> } 22.545 +<a class="c2html-lineno" name="l489" href="#l489">489 </a> } 22.546 +<a class="c2html-lineno" name="l490" href="#l490">490 </a> } 22.547 +<a class="c2html-lineno" name="l491" href="#l491">491 </a> 22.548 +<a class="c2html-lineno" name="l492" href="#l492">492 </a> <span class="c2html-keyword">return</span> result; 22.549 +<a class="c2html-lineno" name="l493" href="#l493">493 </a>} 22.550 +<a class="c2html-lineno" name="l494" href="#l494">494 </a> 22.551 +<a class="c2html-lineno" name="l495" href="#l495">495 </a>_Bool is_pinned(GameState *gamestate, Move *move) { 22.552 +<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>; 22.553 +<a class="c2html-lineno" name="l497" href="#l497">497 </a> 22.554 +<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>; 22.555 +<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++) { 22.556 +<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++) { 22.557 +<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>)) { 22.558 +<a class="c2html-lineno" name="l502" href="#l502">502 </a> kingfile = file; 22.559 +<a class="c2html-lineno" name="l503" href="#l503">503 </a> kingrow = row; 22.560 +<a class="c2html-lineno" name="l504" href="#l504">504 </a> } 22.561 +<a class="c2html-lineno" name="l505" href="#l505">505 </a> } 22.562 +<a class="c2html-lineno" name="l506" href="#l506">506 </a> } 22.563 +<a class="c2html-lineno" name="l507" href="#l507">507 </a> 22.564 +<a class="c2html-lineno" name="l508" href="#l508">508 </a> GameState simulation = gamestate_copy_sim(gamestate); 22.565 +<a class="c2html-lineno" name="l509" href="#l509">509 </a> Move simmove = *move; 22.566 +<a class="c2html-lineno" name="l510" href="#l510">510 </a> apply_move(&simulation, &simmove); 22.567 +<a class="c2html-lineno" name="l511" href="#l511">511 </a> _Bool covered = is_covered(&simulation, 22.568 +<a class="c2html-lineno" name="l512" href="#l512">512 </a> kingrow, kingfile, opponent_color(color)); 22.569 +<a class="c2html-lineno" name="l513" href="#l513">513 </a> gamestate_cleanup(&simulation); 22.570 +<a class="c2html-lineno" name="l514" href="#l514">514 </a> 22.571 +<a class="c2html-lineno" name="l515" href="#l515">515 </a> <span class="c2html-keyword">return</span> covered; 22.572 +<a class="c2html-lineno" name="l516" href="#l516">516 </a>} 22.573 +<a class="c2html-lineno" name="l517" href="#l517">517 </a> 22.574 +<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, 22.575 +<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) { 22.576 +<a class="c2html-lineno" name="l520" href="#l520">520 </a> 22.577 +<a class="c2html-lineno" name="l521" href="#l521">521 </a> <span class="c2html-keyword">if</span> (threatcount) { 22.578 +<a class="c2html-lineno" name="l522" href="#l522">522 </a> *threatcount = <span class="c2html-macroconst">0</span>; 22.579 +<a class="c2html-lineno" name="l523" href="#l523">523 </a> } 22.580 +<a class="c2html-lineno" name="l524" href="#l524">524 </a> 22.581 +<a class="c2html-lineno" name="l525" href="#l525">525 </a> Move candidates[<span class="c2html-macroconst">16</span>]; 22.582 +<a class="c2html-lineno" name="l526" href="#l526">526 </a> <span class="c2html-type">uint8_t</span> candidatecount; 22.583 +<a class="c2html-lineno" name="l527" href="#l527">527 </a> <span class="c2html-keyword">if</span> (get_threats(gamestate, row, file, color, candidates, &candidatecount)) { 22.584 +<a class="c2html-lineno" name="l528" href="#l528">528 </a> 22.585 +<a class="c2html-lineno" name="l529" href="#l529">529 </a> _Bool result = <span class="c2html-macroconst">0</span>; 22.586 +<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>; 22.587 +<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++) { 22.588 +<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++) { 22.589 +<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>)) { 22.590 +<a class="c2html-lineno" name="l534" href="#l534">534 </a> kingfile = file; 22.591 +<a class="c2html-lineno" name="l535" href="#l535">535 </a> kingrow = row; 22.592 +<a class="c2html-lineno" name="l536" href="#l536">536 </a> } 22.593 +<a class="c2html-lineno" name="l537" href="#l537">537 </a> } 22.594 +<a class="c2html-lineno" name="l538" href="#l538">538 </a> } 22.595 +<a class="c2html-lineno" name="l539" href="#l539">539 </a> 22.596 +<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++) { 22.597 +<a class="c2html-lineno" name="l541" href="#l541">541 </a> GameState simulation = gamestate_copy_sim(gamestate); 22.598 +<a class="c2html-lineno" name="l542" href="#l542">542 </a> Move simmove = candidates[i]; 22.599 +<a class="c2html-lineno" name="l543" href="#l543">543 </a> apply_move(&simulation, &simmove); 22.600 +<a class="c2html-lineno" name="l544" href="#l544">544 </a> <span class="c2html-keyword">if</span> (!is_covered(&simulation, kingrow, kingfile, 22.601 +<a class="c2html-lineno" name="l545" href="#l545">545 </a> opponent_color(color))) { 22.602 +<a class="c2html-lineno" name="l546" href="#l546">546 </a> result = <span class="c2html-macroconst">1</span>; 22.603 +<a class="c2html-lineno" name="l547" href="#l547">547 </a> <span class="c2html-keyword">if</span> (threats && threatcount) { 22.604 +<a class="c2html-lineno" name="l548" href="#l548">548 </a> threats[(*threatcount)++] = candidates[i]; 22.605 +<a class="c2html-lineno" name="l549" href="#l549">549 </a> } 22.606 +<a class="c2html-lineno" name="l550" href="#l550">550 </a> } 22.607 +<a class="c2html-lineno" name="l551" href="#l551">551 </a> } 22.608 +<a class="c2html-lineno" name="l552" href="#l552">552 </a> 22.609 +<a class="c2html-lineno" name="l553" href="#l553">553 </a> <span class="c2html-keyword">return</span> result; 22.610 +<a class="c2html-lineno" name="l554" href="#l554">554 </a> } <span class="c2html-keyword">else</span> { 22.611 +<a class="c2html-lineno" name="l555" href="#l555">555 </a> <span class="c2html-keyword">return</span> <span class="c2html-macroconst">0</span>; 22.612 +<a class="c2html-lineno" name="l556" href="#l556">556 </a> } 22.613 +<a class="c2html-lineno" name="l557" href="#l557">557 </a>} 22.614 +<a class="c2html-lineno" name="l558" href="#l558">558 </a> 22.615 +<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) { 22.616 +<a class="c2html-lineno" name="l560" href="#l560">560 </a> 22.617 +<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>; 22.618 +<a class="c2html-lineno" name="l562" href="#l562">562 </a> _Bool incheck = gamestate->lastmove?gamestate->lastmove->move.check:<span class="c2html-macroconst">0</span>; 22.619 +<a class="c2html-lineno" name="l563" href="#l563">563 </a> 22.620 +<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>; 22.621 +<a class="c2html-lineno" name="l565" href="#l565">565 </a> <span class="c2html-type">uint8_t</span> threatcount; 22.622 +<a class="c2html-lineno" name="l566" href="#l566">566 </a> 22.623 +<a class="c2html-lineno" name="l567" href="#l567">567 </a> <span class="c2html-keyword">if</span> (get_threats(gamestate, move->torow, move->tofile, color, 22.624 +<a class="c2html-lineno" name="l568" href="#l568">568 </a> threats, &threatcount)) { 22.625 +<a class="c2html-lineno" name="l569" href="#l569">569 </a> 22.626 +<a class="c2html-lineno" name="l570" href="#l570">570 </a> <span class="c2html-keyword">int</span> reason = <span class="c2html-macroconst">INVALID_POSITION</span>; 22.627 +<a class="c2html-lineno" name="l571" href="#l571">571 </a> 22.628 +<a class="c2html-lineno" name="l572" href="#l572">572 </a> <span class="c2html-comment">// find threats for the specified position</span> 22.629 +<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++) { 22.630 +<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>)) 22.631 +<a class="c2html-lineno" name="l575" href="#l575">575 </a> == move->piece && 22.632 +<a class="c2html-lineno" name="l576" href="#l576">576 </a> (move->fromrow == <span class="c2html-macroconst">POS_UNSPECIFIED</span> || 22.633 +<a class="c2html-lineno" name="l577" href="#l577">577 </a> move->fromrow == threats[i].fromrow) && 22.634 +<a class="c2html-lineno" name="l578" href="#l578">578 </a> (move->fromfile == <span class="c2html-macroconst">POS_UNSPECIFIED</span> || 22.635 +<a class="c2html-lineno" name="l579" href="#l579">579 </a> move->fromfile == threats[i].fromfile)) { 22.636 +<a class="c2html-lineno" name="l580" href="#l580">580 </a> 22.637 +<a class="c2html-lineno" name="l581" href="#l581">581 </a> <span class="c2html-keyword">if</span> (threat) { 22.638 +<a class="c2html-lineno" name="l582" href="#l582">582 </a> <span class="c2html-keyword">return</span> <span class="c2html-macroconst">AMBIGUOUS_MOVE</span>; 22.639 +<a class="c2html-lineno" name="l583" href="#l583">583 </a> } <span class="c2html-keyword">else</span> { 22.640 +<a class="c2html-lineno" name="l584" href="#l584">584 </a> <span class="c2html-comment">// found threat is no real threat</span> 22.641 +<a class="c2html-lineno" name="l585" href="#l585">585 </a> <span class="c2html-keyword">if</span> (is_pinned(gamestate, &(threats[i]))) { 22.642 +<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>; 22.643 +<a class="c2html-lineno" name="l587" href="#l587">587 </a> } <span class="c2html-keyword">else</span> { 22.644 +<a class="c2html-lineno" name="l588" href="#l588">588 </a> threat = &(threats[i]); 22.645 +<a class="c2html-lineno" name="l589" href="#l589">589 </a> } 22.646 +<a class="c2html-lineno" name="l590" href="#l590">590 </a> } 22.647 +<a class="c2html-lineno" name="l591" href="#l591">591 </a> } 22.648 +<a class="c2html-lineno" name="l592" href="#l592">592 </a> } 22.649 +<a class="c2html-lineno" name="l593" href="#l593">593 </a> 22.650 +<a class="c2html-lineno" name="l594" href="#l594">594 </a> <span class="c2html-comment">// can't threaten specified position</span> 22.651 +<a class="c2html-lineno" name="l595" href="#l595">595 </a> <span class="c2html-keyword">if</span> (!threat) { 22.652 +<a class="c2html-lineno" name="l596" href="#l596">596 </a> <span class="c2html-keyword">return</span> reason; 22.653 +<a class="c2html-lineno" name="l597" href="#l597">597 </a> } 22.654 +<a class="c2html-lineno" name="l598" href="#l598">598 </a> 22.655 +<a class="c2html-lineno" name="l599" href="#l599">599 </a> memcpy(move, threat, <span class="c2html-keyword">sizeof</span>(Move)); 22.656 +<a class="c2html-lineno" name="l600" href="#l600">600 </a> <span class="c2html-keyword">return</span> <span class="c2html-macroconst">VALID_MOVE_SYNTAX</span>; 22.657 +<a class="c2html-lineno" name="l601" href="#l601">601 </a> } <span class="c2html-keyword">else</span> { 22.658 +<a class="c2html-lineno" name="l602" href="#l602">602 </a> <span class="c2html-keyword">return</span> <span class="c2html-macroconst">INVALID_POSITION</span>; 22.659 +<a class="c2html-lineno" name="l603" href="#l603">603 </a> } 22.660 +<a class="c2html-lineno" name="l604" href="#l604">604 </a>} 22.661 +<a class="c2html-lineno" name="l605" href="#l605">605 </a> 22.662 +<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) { 22.663 +<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)); 22.664 +<a class="c2html-lineno" name="l608" href="#l608">608 </a> move->fromfile = <span class="c2html-macroconst">POS_UNSPECIFIED</span>; 22.665 +<a class="c2html-lineno" name="l609" href="#l609">609 </a> move->fromrow = <span class="c2html-macroconst">POS_UNSPECIFIED</span>; 22.666 +<a class="c2html-lineno" name="l610" href="#l610">610 </a> 22.667 +<a class="c2html-lineno" name="l611" href="#l611">611 </a> <span class="c2html-type">size_t</span> len = strlen(mstr); 22.668 +<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>) { 22.669 +<a class="c2html-lineno" name="l613" href="#l613">613 </a> <span class="c2html-keyword">return</span> <span class="c2html-macroconst">INVALID_MOVE_SYNTAX</span>; 22.670 +<a class="c2html-lineno" name="l614" href="#l614">614 </a> } 22.671 +<a class="c2html-lineno" name="l615" href="#l615">615 </a> 22.672 +<a class="c2html-lineno" name="l616" href="#l616">616 </a> <span class="c2html-comment">/* evaluate check/checkmate flags */</span> 22.673 +<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>) { 22.674 +<a class="c2html-lineno" name="l618" href="#l618">618 </a> len--; mstr[len] = <span class="c2html-string">'\0'</span>; 22.675 +<a class="c2html-lineno" name="l619" href="#l619">619 </a> move->check = <span class="c2html-macroconst">1</span>; 22.676 +<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>) { 22.677 +<a class="c2html-lineno" name="l621" href="#l621">621 </a> len--; mstr[len] = <span class="c2html-string">'\0'</span>; 22.678 +<a class="c2html-lineno" name="l622" href="#l622">622 </a> <span class="c2html-comment">/* ignore - validation should set game state */</span> 22.679 +<a class="c2html-lineno" name="l623" href="#l623">623 </a> } 22.680 +<a class="c2html-lineno" name="l624" href="#l624">624 </a> 22.681 +<a class="c2html-lineno" name="l625" href="#l625">625 </a> <span class="c2html-comment">/* evaluate promotion */</span> 22.682 +<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>) { 22.683 +<a class="c2html-lineno" name="l627" href="#l627">627 </a> move->promotion = getpiece(mstr[len-<span class="c2html-macroconst">1</span>]); 22.684 +<a class="c2html-lineno" name="l628" href="#l628">628 </a> <span class="c2html-keyword">if</span> (!move->promotion) { 22.685 +<a class="c2html-lineno" name="l629" href="#l629">629 </a> <span class="c2html-keyword">return</span> <span class="c2html-macroconst">INVALID_MOVE_SYNTAX</span>; 22.686 +<a class="c2html-lineno" name="l630" href="#l630">630 </a> } <span class="c2html-keyword">else</span> { 22.687 +<a class="c2html-lineno" name="l631" href="#l631">631 </a> move->promotion |= color; 22.688 +<a class="c2html-lineno" name="l632" href="#l632">632 </a> len -= <span class="c2html-macroconst">2</span>; 22.689 +<a class="c2html-lineno" name="l633" href="#l633">633 </a> mstr[len] = <span class="c2html-macroconst">0</span>; 22.690 +<a class="c2html-lineno" name="l634" href="#l634">634 </a> } 22.691 +<a class="c2html-lineno" name="l635" href="#l635">635 </a> } 22.692 +<a class="c2html-lineno" name="l636" href="#l636">636 </a> 22.693 +<a class="c2html-lineno" name="l637" href="#l637">637 </a> <span class="c2html-keyword">if</span> (len == <span class="c2html-macroconst">2</span>) { 22.694 +<a class="c2html-lineno" name="l638" href="#l638">638 </a> <span class="c2html-comment">/* pawn move (e.g. "e4") */</span> 22.695 +<a class="c2html-lineno" name="l639" href="#l639">639 </a> move->piece = <span class="c2html-macroconst">PAWN</span>; 22.696 +<a class="c2html-lineno" name="l640" href="#l640">640 </a> move->tofile = fileidx(mstr[<span class="c2html-macroconst">0</span>]); 22.697 +<a class="c2html-lineno" name="l641" href="#l641">641 </a> move->torow = rowidx(mstr[<span class="c2html-macroconst">1</span>]); 22.698 +<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>) { 22.699 +<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>) { 22.700 +<a class="c2html-lineno" name="l644" href="#l644">644 </a> <span class="c2html-comment">/* king side castling */</span> 22.701 +<a class="c2html-lineno" name="l645" href="#l645">645 </a> move->piece = <span class="c2html-macroconst">KING</span>; 22.702 +<a class="c2html-lineno" name="l646" href="#l646">646 </a> move->fromfile = fileidx(<span class="c2html-string">'e'</span>); 22.703 +<a class="c2html-lineno" name="l647" href="#l647">647 </a> move->tofile = fileidx(<span class="c2html-string">'g'</span>); 22.704 +<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>; 22.705 +<a class="c2html-lineno" name="l649" href="#l649">649 </a> } <span class="c2html-keyword">else</span> { 22.706 +<a class="c2html-lineno" name="l650" href="#l650">650 </a> <span class="c2html-comment">/* move (e.g. "Nf3") */</span> 22.707 +<a class="c2html-lineno" name="l651" href="#l651">651 </a> move->piece = getpiece(mstr[<span class="c2html-macroconst">0</span>]); 22.708 +<a class="c2html-lineno" name="l652" href="#l652">652 </a> move->tofile = fileidx(mstr[<span class="c2html-macroconst">1</span>]); 22.709 +<a class="c2html-lineno" name="l653" href="#l653">653 </a> move->torow = rowidx(mstr[<span class="c2html-macroconst">2</span>]); 22.710 +<a class="c2html-lineno" name="l654" href="#l654">654 </a> } 22.711 +<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>) { 22.712 +<a class="c2html-lineno" name="l656" href="#l656">656 </a> move->piece = getpiece(mstr[<span class="c2html-macroconst">0</span>]); 22.713 +<a class="c2html-lineno" name="l657" href="#l657">657 </a> <span class="c2html-keyword">if</span> (!move->piece) { 22.714 +<a class="c2html-lineno" name="l658" href="#l658">658 </a> move->piece = <span class="c2html-macroconst">PAWN</span>; 22.715 +<a class="c2html-lineno" name="l659" href="#l659">659 </a> move->fromfile = fileidx(mstr[<span class="c2html-macroconst">0</span>]); 22.716 +<a class="c2html-lineno" name="l660" href="#l660">660 </a> } 22.717 +<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>) { 22.718 +<a class="c2html-lineno" name="l662" href="#l662">662 </a> <span class="c2html-comment">/* capture (e.g. "Nxf3", "dxe5") */</span> 22.719 +<a class="c2html-lineno" name="l663" href="#l663">663 </a> move->capture = <span class="c2html-macroconst">1</span>; 22.720 +<a class="c2html-lineno" name="l664" href="#l664">664 </a> } <span class="c2html-keyword">else</span> { 22.721 +<a class="c2html-lineno" name="l665" href="#l665">665 </a> <span class="c2html-comment">/* move (e.g. "Ndf3", "N2c3", "e2e4") */</span> 22.722 +<a class="c2html-lineno" name="l666" href="#l666">666 </a> <span class="c2html-keyword">if</span> (isfile(mstr[<span class="c2html-macroconst">1</span>])) { 22.723 +<a class="c2html-lineno" name="l667" href="#l667">667 </a> move->fromfile = fileidx(mstr[<span class="c2html-macroconst">1</span>]); 22.724 +<a class="c2html-lineno" name="l668" href="#l668">668 </a> <span class="c2html-keyword">if</span> (move->piece == <span class="c2html-macroconst">PAWN</span>) { 22.725 +<a class="c2html-lineno" name="l669" href="#l669">669 </a> move->piece = <span class="c2html-macroconst">0</span>; 22.726 +<a class="c2html-lineno" name="l670" href="#l670">670 </a> } 22.727 +<a class="c2html-lineno" name="l671" href="#l671">671 </a> } <span class="c2html-keyword">else</span> { 22.728 +<a class="c2html-lineno" name="l672" href="#l672">672 </a> move->fromrow = rowidx(mstr[<span class="c2html-macroconst">1</span>]); 22.729 +<a class="c2html-lineno" name="l673" href="#l673">673 </a> } 22.730 +<a class="c2html-lineno" name="l674" href="#l674">674 </a> } 22.731 +<a class="c2html-lineno" name="l675" href="#l675">675 </a> move->tofile = fileidx(mstr[<span class="c2html-macroconst">2</span>]); 22.732 +<a class="c2html-lineno" name="l676" href="#l676">676 </a> move->torow = rowidx(mstr[<span class="c2html-macroconst">3</span>]); 22.733 +<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>) { 22.734 +<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>) { 22.735 +<a class="c2html-lineno" name="l679" href="#l679">679 </a> <span class="c2html-comment">/* queen side castling "O-O-O" */</span> 22.736 +<a class="c2html-lineno" name="l680" href="#l680">680 </a> move->piece = <span class="c2html-macroconst">KING</span>; 22.737 +<a class="c2html-lineno" name="l681" href="#l681">681 </a> move->fromfile = fileidx(<span class="c2html-string">'e'</span>); 22.738 +<a class="c2html-lineno" name="l682" href="#l682">682 </a> move->tofile = fileidx(<span class="c2html-string">'c'</span>); 22.739 +<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>; 22.740 +<a class="c2html-lineno" name="l684" href="#l684">684 </a> } <span class="c2html-keyword">else</span> { 22.741 +<a class="c2html-lineno" name="l685" href="#l685">685 </a> move->piece = getpiece(mstr[<span class="c2html-macroconst">0</span>]); 22.742 +<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>) { 22.743 +<a class="c2html-lineno" name="l687" href="#l687">687 </a> move->capture = <span class="c2html-macroconst">1</span>; 22.744 +<a class="c2html-lineno" name="l688" href="#l688">688 </a> <span class="c2html-keyword">if</span> (move->piece) { 22.745 +<a class="c2html-lineno" name="l689" href="#l689">689 </a> <span class="c2html-comment">/* capture (e.g. "Ndxf3") */</span> 22.746 +<a class="c2html-lineno" name="l690" href="#l690">690 </a> move->fromfile = fileidx(mstr[<span class="c2html-macroconst">1</span>]); 22.747 +<a class="c2html-lineno" name="l691" href="#l691">691 </a> } <span class="c2html-keyword">else</span> { 22.748 +<a class="c2html-lineno" name="l692" href="#l692">692 </a> <span class="c2html-comment">/* long notation capture (e.g. "e5xf6") */</span> 22.749 +<a class="c2html-lineno" name="l693" href="#l693">693 </a> move->piece = <span class="c2html-macroconst">PAWN</span>; 22.750 +<a class="c2html-lineno" name="l694" href="#l694">694 </a> move->fromfile = fileidx(mstr[<span class="c2html-macroconst">0</span>]); 22.751 +<a class="c2html-lineno" name="l695" href="#l695">695 </a> move->fromrow = rowidx(mstr[<span class="c2html-macroconst">1</span>]); 22.752 +<a class="c2html-lineno" name="l696" href="#l696">696 </a> } 22.753 +<a class="c2html-lineno" name="l697" href="#l697">697 </a> } <span class="c2html-keyword">else</span> { 22.754 +<a class="c2html-lineno" name="l698" href="#l698">698 </a> <span class="c2html-comment">/* long notation move (e.g. "Nc5a4") */</span> 22.755 +<a class="c2html-lineno" name="l699" href="#l699">699 </a> move->fromfile = fileidx(mstr[<span class="c2html-macroconst">1</span>]); 22.756 +<a class="c2html-lineno" name="l700" href="#l700">700 </a> move->fromrow = rowidx(mstr[<span class="c2html-macroconst">2</span>]); 22.757 +<a class="c2html-lineno" name="l701" href="#l701">701 </a> } 22.758 +<a class="c2html-lineno" name="l702" href="#l702">702 </a> move->tofile = fileidx(mstr[<span class="c2html-macroconst">3</span>]); 22.759 +<a class="c2html-lineno" name="l703" href="#l703">703 </a> move->torow = rowidx(mstr[<span class="c2html-macroconst">4</span>]); 22.760 +<a class="c2html-lineno" name="l704" href="#l704">704 </a> } 22.761 +<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>) { 22.762 +<a class="c2html-lineno" name="l706" href="#l706">706 </a> <span class="c2html-comment">/* long notation capture (e.g. "Nc5xf3") */</span> 22.763 +<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>) { 22.764 +<a class="c2html-lineno" name="l708" href="#l708">708 </a> move->capture = <span class="c2html-macroconst">1</span>; 22.765 +<a class="c2html-lineno" name="l709" href="#l709">709 </a> move->piece = getpiece(mstr[<span class="c2html-macroconst">0</span>]); 22.766 +<a class="c2html-lineno" name="l710" href="#l710">710 </a> move->fromfile = fileidx(mstr[<span class="c2html-macroconst">1</span>]); 22.767 +<a class="c2html-lineno" name="l711" href="#l711">711 </a> move->fromrow = rowidx(mstr[<span class="c2html-macroconst">2</span>]); 22.768 +<a class="c2html-lineno" name="l712" href="#l712">712 </a> move->tofile = fileidx(mstr[<span class="c2html-macroconst">4</span>]); 22.769 +<a class="c2html-lineno" name="l713" href="#l713">713 </a> move->torow = rowidx(mstr[<span class="c2html-macroconst">5</span>]); 22.770 +<a class="c2html-lineno" name="l714" href="#l714">714 </a> } 22.771 +<a class="c2html-lineno" name="l715" href="#l715">715 </a> } 22.772 +<a class="c2html-lineno" name="l716" href="#l716">716 </a> 22.773 +<a class="c2html-lineno" name="l717" href="#l717">717 </a> 22.774 +<a class="c2html-lineno" name="l718" href="#l718">718 </a> <span class="c2html-keyword">if</span> (move->piece) { 22.775 +<a class="c2html-lineno" name="l719" href="#l719">719 </a> <span class="c2html-keyword">if</span> (move->piece == <span class="c2html-macroconst">PAWN</span> 22.776 +<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>) 22.777 +<a class="c2html-lineno" name="l721" href="#l721">721 </a> && !move->promotion) { 22.778 +<a class="c2html-lineno" name="l722" href="#l722">722 </a> <span class="c2html-keyword">return</span> <span class="c2html-macroconst">NEED_PROMOTION</span>; 22.779 +<a class="c2html-lineno" name="l723" href="#l723">723 </a> } 22.780 +<a class="c2html-lineno" name="l724" href="#l724">724 </a> 22.781 +<a class="c2html-lineno" name="l725" href="#l725">725 </a> move->piece |= color; 22.782 +<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> 22.783 +<a class="c2html-lineno" name="l727" href="#l727">727 </a> || move->fromrow == <span class="c2html-macroconst">POS_UNSPECIFIED</span>) { 22.784 +<a class="c2html-lineno" name="l728" href="#l728">728 </a> <span class="c2html-keyword">return</span> getlocation(gamestate, move); 22.785 +<a class="c2html-lineno" name="l729" href="#l729">729 </a> } <span class="c2html-keyword">else</span> { 22.786 +<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>; 22.787 +<a class="c2html-lineno" name="l731" href="#l731">731 </a> } 22.788 +<a class="c2html-lineno" name="l732" href="#l732">732 </a> } <span class="c2html-keyword">else</span> { 22.789 +<a class="c2html-lineno" name="l733" href="#l733">733 </a> <span class="c2html-keyword">return</span> <span class="c2html-macroconst">INVALID_MOVE_SYNTAX</span>; 22.790 +<a class="c2html-lineno" name="l734" href="#l734">734 </a> } 22.791 +<a class="c2html-lineno" name="l735" href="#l735">735 </a>} 22.792 +<a class="c2html-lineno" name="l736" href="#l736">736 </a> 22.793 +<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, 22.794 +<a class="c2html-lineno" name="l738" href="#l738">738 </a> <span class="c2html-type">uint8_t</span> color) { 22.795 +<a class="c2html-lineno" name="l739" href="#l739">739 </a> 22.796 +<a class="c2html-lineno" name="l740" href="#l740">740 </a> Move threats[<span class="c2html-macroconst">16</span>]; 22.797 +<a class="c2html-lineno" name="l741" href="#l741">741 </a> <span class="c2html-type">uint8_t</span> threatcount; 22.798 +<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)) { 22.799 +<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++) { 22.800 +<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>)) { 22.801 +<a class="c2html-lineno" name="l745" href="#l745">745 </a> <span class="c2html-keyword">return</span> <span class="c2html-macroconst">1</span>; 22.802 +<a class="c2html-lineno" name="l746" href="#l746">746 </a> } 22.803 +<a class="c2html-lineno" name="l747" href="#l747">747 </a> } 22.804 +<a class="c2html-lineno" name="l748" href="#l748">748 </a> <span class="c2html-keyword">return</span> <span class="c2html-macroconst">0</span>; 22.805 +<a class="c2html-lineno" name="l749" href="#l749">749 </a> } <span class="c2html-keyword">else</span> { 22.806 +<a class="c2html-lineno" name="l750" href="#l750">750 </a> <span class="c2html-keyword">return</span> <span class="c2html-macroconst">0</span>; 22.807 +<a class="c2html-lineno" name="l751" href="#l751">751 </a> } 22.808 +<a class="c2html-lineno" name="l752" href="#l752">752 </a>} 22.809 +<a class="c2html-lineno" name="l753" href="#l753">753 </a> 22.810 +<a class="c2html-lineno" name="l754" href="#l754">754 </a><span class="c2html-type">uint16_t</span> remaining_movetime(GameInfo *gameinfo, GameState *gamestate, 22.811 +<a class="c2html-lineno" name="l755" href="#l755">755 </a> <span class="c2html-type">uint8_t</span> color) { 22.812 +<a class="c2html-lineno" name="l756" href="#l756">756 </a> <span class="c2html-keyword">if</span> (!gameinfo->timecontrol) { 22.813 +<a class="c2html-lineno" name="l757" href="#l757">757 </a> <span class="c2html-keyword">return</span> <span class="c2html-macroconst">0</span>; 22.814 +<a class="c2html-lineno" name="l758" href="#l758">758 </a> } 22.815 +<a class="c2html-lineno" name="l759" href="#l759">759 </a> 22.816 +<a class="c2html-lineno" name="l760" href="#l760">760 </a> <span class="c2html-keyword">if</span> (gamestate->movelist) { 22.817 +<a class="c2html-lineno" name="l761" href="#l761">761 </a> <span class="c2html-type">uint16_t</span> time = gameinfo->time; 22.818 +<a class="c2html-lineno" name="l762" href="#l762">762 </a> <span class="c2html-type">suseconds_t</span> micros = <span class="c2html-macroconst">0</span>; 22.819 +<a class="c2html-lineno" name="l763" href="#l763">763 </a> 22.820 +<a class="c2html-lineno" name="l764" href="#l764">764 </a> MoveList *movelist = color == <span class="c2html-macroconst">WHITE</span> ? 22.821 +<a class="c2html-lineno" name="l765" href="#l765">765 </a> gamestate->movelist : gamestate->movelist->next; 22.822 +<a class="c2html-lineno" name="l766" href="#l766">766 </a> 22.823 +<a class="c2html-lineno" name="l767" href="#l767">767 </a> <span class="c2html-keyword">while</span> (movelist) { 22.824 +<a class="c2html-lineno" name="l768" href="#l768">768 </a> time += gameinfo->addtime; 22.825 +<a class="c2html-lineno" name="l769" href="#l769">769 </a> 22.826 +<a class="c2html-lineno" name="l770" href="#l770">770 </a> <span class="c2html-keyword">struct</span> movetimeval *movetime = &(movelist->move.movetime); 22.827 +<a class="c2html-lineno" name="l771" href="#l771">771 </a> <span class="c2html-keyword">if</span> (movetime->tv_sec >= time) { 22.828 +<a class="c2html-lineno" name="l772" href="#l772">772 </a> <span class="c2html-keyword">return</span> <span class="c2html-macroconst">0</span>; 22.829 +<a class="c2html-lineno" name="l773" href="#l773">773 </a> } 22.830 +<a class="c2html-lineno" name="l774" href="#l774">774 </a> 22.831 +<a class="c2html-lineno" name="l775" href="#l775">775 </a> time -= movetime->tv_sec; 22.832 +<a class="c2html-lineno" name="l776" href="#l776">776 </a> micros += movetime->tv_usec; 22.833 +<a class="c2html-lineno" name="l777" href="#l777">777 </a> 22.834 +<a class="c2html-lineno" name="l778" href="#l778">778 </a> movelist = movelist->next ? movelist->next->next : <span class="c2html-macroconst">NULL</span>; 22.835 +<a class="c2html-lineno" name="l779" href="#l779">779 </a> } 22.836 +<a class="c2html-lineno" name="l780" href="#l780">780 </a> 22.837 +<a class="c2html-lineno" name="l781" href="#l781">781 </a> <span class="c2html-type">time_t</span> sec; 22.838 +<a class="c2html-lineno" name="l782" href="#l782">782 </a> movelist = gamestate->lastmove; 22.839 +<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) { 22.840 +<a class="c2html-lineno" name="l784" href="#l784">784 </a> <span class="c2html-keyword">struct</span> movetimeval *lastmovetstamp = &(movelist->move.timestamp); 22.841 +<a class="c2html-lineno" name="l785" href="#l785">785 </a> <span class="c2html-keyword">struct</span> timeval currenttstamp; 22.842 +<a class="c2html-lineno" name="l786" href="#l786">786 </a> gettimeofday(¤ttstamp, <span class="c2html-macroconst">NULL</span>); 22.843 +<a class="c2html-lineno" name="l787" href="#l787">787 </a> micros += currenttstamp.tv_usec - lastmovetstamp->tv_usec; 22.844 +<a class="c2html-lineno" name="l788" href="#l788">788 </a> sec = currenttstamp.tv_sec - lastmovetstamp->tv_sec; 22.845 +<a class="c2html-lineno" name="l789" href="#l789">789 </a> <span class="c2html-keyword">if</span> (sec >= time) { 22.846 +<a class="c2html-lineno" name="l790" href="#l790">790 </a> <span class="c2html-keyword">return</span> <span class="c2html-macroconst">0</span>; 22.847 +<a class="c2html-lineno" name="l791" href="#l791">791 </a> } 22.848 +<a class="c2html-lineno" name="l792" href="#l792">792 </a> 22.849 +<a class="c2html-lineno" name="l793" href="#l793">793 </a> time -= sec; 22.850 +<a class="c2html-lineno" name="l794" href="#l794">794 </a> } 22.851 +<a class="c2html-lineno" name="l795" href="#l795">795 </a> 22.852 +<a class="c2html-lineno" name="l796" href="#l796">796 </a> sec = micros / 1e6L; 22.853 +<a class="c2html-lineno" name="l797" href="#l797">797 </a> 22.854 +<a class="c2html-lineno" name="l798" href="#l798">798 </a> <span class="c2html-keyword">if</span> (sec >= time) { 22.855 +<a class="c2html-lineno" name="l799" href="#l799">799 </a> <span class="c2html-keyword">return</span> <span class="c2html-macroconst">0</span>; 22.856 +<a class="c2html-lineno" name="l800" href="#l800">800 </a> } 22.857 +<a class="c2html-lineno" name="l801" href="#l801">801 </a> 22.858 +<a class="c2html-lineno" name="l802" href="#l802">802 </a> time -= sec; 22.859 +<a class="c2html-lineno" name="l803" href="#l803">803 </a> 22.860 +<a class="c2html-lineno" name="l804" href="#l804">804 </a> <span class="c2html-keyword">return</span> time; 22.861 +<a class="c2html-lineno" name="l805" href="#l805">805 </a> } <span class="c2html-keyword">else</span> { 22.862 +<a class="c2html-lineno" name="l806" href="#l806">806 </a> <span class="c2html-keyword">return</span> gameinfo->time; 22.863 +<a class="c2html-lineno" name="l807" href="#l807">807 </a> } 22.864 +<a class="c2html-lineno" name="l808" href="#l808">808 </a>} 22.865 +</div> 22.866 + </body> 22.867 +</html> 22.868 +
23.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 23.2 +++ b/test/gs/ctest.html Mon Apr 24 20:54:38 2023 +0200 23.3 @@ -0,0 +1,448 @@ 23.4 +<!DOCTYPE html> 23.5 +<html> 23.6 + <head> 23.7 + <title>c2html</title> 23.8 + <style type="text/css"> 23.9 + div.c2html-code { 23.10 + white-space: pre; 23.11 + font-family: monospace; 23.12 + } 23.13 + a.c2html-lineno { 23.14 + /* as long as user-select isn't widely spread, we throw the bomb */ 23.15 + -webkit-user-select: none; 23.16 + -moz-user-select: none; 23.17 + -ms-user-select: none; 23.18 + user-select: none; 23.19 + display: inline-block; 23.20 + font-style: italic; 23.21 + text-decoration: none; 23.22 + color: grey; 23.23 + } 23.24 + span.c2html-keyword { 23.25 + color: blue; 23.26 + } 23.27 + span.c2html-macroconst { 23.28 + color: cornflowerblue; 23.29 + } 23.30 + span.c2html-type { 23.31 + color: cornflowerblue; 23.32 + } 23.33 + span.c2html-directive { 23.34 + color: green; 23.35 + } 23.36 + span.c2html-string { 23.37 + color: darkorange; 23.38 + } 23.39 + span.c2html-comment { 23.40 + color: grey; 23.41 + } 23.42 + span.c2html-stdinclude { 23.43 + color: darkorange; 23.44 + } 23.45 + span.c2html-userinclude { 23.46 + color: darkorange; 23.47 + } 23.48 + a.c2html-userinclude { 23.49 + color: darkorange; 23.50 + text-decoration: underline; 23.51 + } 23.52 + </style> 23.53 + </head> 23.54 + <body> 23.55 + 23.56 +<div class="c2html-code"> 23.57 +<a class="c2html-lineno" name="l1" href="#l1"> 1 </a><span class="c2html-comment">/*</span> 23.58 +<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> 23.59 +<a class="c2html-lineno" name="l3" href="#l3"> 3 </a><span class="c2html-comment"> *</span> 23.60 +<a class="c2html-lineno" name="l4" href="#l4"> 4 </a><span class="c2html-comment"> * Copyright 2015 Olaf Wintermann. All rights reserved.</span> 23.61 +<a class="c2html-lineno" name="l5" href="#l5"> 5 </a><span class="c2html-comment"> *</span> 23.62 +<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> 23.63 +<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> 23.64 +<a class="c2html-lineno" name="l8" href="#l8"> 8 </a><span class="c2html-comment"> *</span> 23.65 +<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> 23.66 +<a class="c2html-lineno" name="l10" href="#l10"> 10 </a><span class="c2html-comment"> * notice, this list of conditions and the following disclaimer.</span> 23.67 +<a class="c2html-lineno" name="l11" href="#l11"> 11 </a><span class="c2html-comment"> *</span> 23.68 +<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> 23.69 +<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> 23.70 +<a class="c2html-lineno" name="l14" href="#l14"> 14 </a><span class="c2html-comment"> * documentation and/or other materials provided with the distribution.</span> 23.71 +<a class="c2html-lineno" name="l15" href="#l15"> 15 </a><span class="c2html-comment"> *</span> 23.72 +<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> 23.73 +<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> 23.74 +<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> 23.75 +<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> 23.76 +<a class="c2html-lineno" name="l20" href="#l20"> 20 </a><span class="c2html-comment"> * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR</span> 23.77 +<a class="c2html-lineno" name="l21" href="#l21"> 21 </a><span class="c2html-comment"> * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF</span> 23.78 +<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> 23.79 +<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> 23.80 +<a class="c2html-lineno" name="l24" href="#l24"> 24 </a><span class="c2html-comment"> * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)</span> 23.81 +<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> 23.82 +<a class="c2html-lineno" name="l26" href="#l26"> 26 </a><span class="c2html-comment"> * POSSIBILITY OF SUCH DAMAGE.</span> 23.83 +<a class="c2html-lineno" name="l27" href="#l27"> 27 </a><span class="c2html-comment"> */</span> 23.84 +<a class="c2html-lineno" name="l28" href="#l28"> 28 </a> 23.85 +<a class="c2html-lineno" name="l29" href="#l29"> 29 </a><span class="c2html-directive">#include</span> <span class="c2html-stdinclude"><time.h></span> 23.86 +<a class="c2html-lineno" name="l30" href="#l30"> 30 </a><span class="c2html-directive">#include</span> <span class="c2html-stdinclude"><stdio.h></span> 23.87 +<a class="c2html-lineno" name="l31" href="#l31"> 31 </a><span class="c2html-directive">#include</span> <span class="c2html-stdinclude"><stdlib.h></span> 23.88 +<a class="c2html-lineno" name="l32" href="#l32"> 32 </a><span class="c2html-directive">#include</span> <span class="c2html-stdinclude"><string.h></span> 23.89 +<a class="c2html-lineno" name="l33" href="#l33"> 33 </a><span class="c2html-directive">#include</span> <span class="c2html-stdinclude"><ucx/string.h></span> 23.90 +<a class="c2html-lineno" name="l34" href="#l34"> 34 </a><span class="c2html-directive">#include</span> <span class="c2html-stdinclude"><ucx/buffer.h></span> 23.91 +<a class="c2html-lineno" name="l35" href="#l35"> 35 </a><span class="c2html-directive">#include</span> <span class="c2html-stdinclude"><ucx/utils.h></span> 23.92 +<a class="c2html-lineno" name="l36" href="#l36"> 36 </a><span class="c2html-directive">#include</span> <span class="c2html-stdinclude"><libxml/tree.h></span> 23.93 +<a class="c2html-lineno" name="l37" href="#l37"> 37 </a><span class="c2html-directive">#include</span> <span class="c2html-stdinclude"><curl/curl.h></span> 23.94 +<a class="c2html-lineno" name="l38" href="#l38"> 38 </a> 23.95 +<a class="c2html-lineno" name="l39" href="#l39"> 39 </a><span class="c2html-directive">#include</span> <span class="c2html-stdinclude"><openssl/sha.h></span> 23.96 +<a class="c2html-lineno" name="l40" href="#l40"> 40 </a><span class="c2html-directive">#include</span> <span class="c2html-stdinclude"><openssl/hmac.h></span> 23.97 +<a class="c2html-lineno" name="l41" href="#l41"> 41 </a><span class="c2html-directive">#include</span> <span class="c2html-stdinclude"><openssl/evp.h></span> 23.98 +<a class="c2html-lineno" name="l42" href="#l42"> 42 </a><span class="c2html-directive">#include</span> <span class="c2html-stdinclude"><openssl/bio.h></span> 23.99 +<a class="c2html-lineno" name="l43" href="#l43"> 43 </a><span class="c2html-directive">#include</span> <span class="c2html-stdinclude"><openssl/buffer.h></span> 23.100 +<a class="c2html-lineno" name="l44" href="#l44"> 44 </a><span class="c2html-directive">#include</span> <span class="c2html-stdinclude"><openssl/rand.h></span> 23.101 +<a class="c2html-lineno" name="l45" href="#l45"> 45 </a> 23.102 +<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> 23.103 +<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> 23.104 +<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> 23.105 +<a class="c2html-lineno" name="l49" href="#l49"> 49 </a> 23.106 +<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> 23.107 +<a class="c2html-lineno" name="l51" href="#l51"> 51 </a> 23.108 +<a class="c2html-lineno" name="l52" href="#l52"> 52 </a><span class="c2html-comment">/* -------------------- This is a testing file. -------------------------- */</span> 23.109 +<a class="c2html-lineno" name="l53" href="#l53"> 53 </a><span class="c2html-comment">/*</span> 23.110 +<a class="c2html-lineno" name="l54" href="#l54"> 54 </a><span class="c2html-comment">time_t util_parse_creationdate(char *str) {</span> 23.111 +<a class="c2html-lineno" name="l55" href="#l55"> 55 </a><span class="c2html-comment"> // example: 2012-11-29T21:35:35Z</span> 23.112 +<a class="c2html-lineno" name="l56" href="#l56"> 56 </a><span class="c2html-comment"> if(!str) {</span> 23.113 +<a class="c2html-lineno" name="l57" href="#l57"> 57 </a><span class="c2html-comment"> return 0;</span> 23.114 +<a class="c2html-lineno" name="l58" href="#l58"> 58 </a><span class="c2html-comment"> }</span> 23.115 +<a class="c2html-lineno" name="l59" href="#l59"> 59 </a><span class="c2html-comment"> // TODO</span> 23.116 +<a class="c2html-lineno" name="l60" href="#l60"> 60 </a><span class="c2html-comment"> return 0;</span> 23.117 +<a class="c2html-lineno" name="l61" href="#l61"> 61 </a><span class="c2html-comment">}</span> 23.118 +<a class="c2html-lineno" name="l62" href="#l62"> 62 </a><span class="c2html-comment">*/</span> 23.119 +<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) { 23.120 +<a class="c2html-lineno" name="l64" href="#l64"> 64 </a> <span class="c2html-comment">// example: Thu, 29 Nov 2012 21:35:35 GMT</span> 23.121 +<a class="c2html-lineno" name="l65" href="#l65"> 65 </a> <span class="c2html-keyword">if</span>(!str) { 23.122 +<a class="c2html-lineno" name="l66" href="#l66"> 66 </a> <span class="c2html-keyword">return</span> <span class="c2html-macroconst">0</span>; 23.123 +<a class="c2html-lineno" name="l67" href="#l67"> 67 </a> } <span class="c2html-keyword">else</span> { 23.124 +<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>); 23.125 +<a class="c2html-lineno" name="l69" href="#l69"> 69 </a> } 23.126 +<a class="c2html-lineno" name="l70" href="#l70"> 70 </a>} 23.127 +<a class="c2html-lineno" name="l71" href="#l71"> 71 </a> 23.128 +<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) { 23.129 +<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>) { 23.130 +<a class="c2html-lineno" name="l74" href="#l74"> 74 </a> <span class="c2html-keyword">return</span> <span class="c2html-macroconst">1</span>; 23.131 +<a class="c2html-lineno" name="l75" href="#l75"> 75 </a> } 23.132 +<a class="c2html-lineno" name="l76" href="#l76"> 76 </a> <span class="c2html-keyword">return</span> <span class="c2html-macroconst">0</span>; 23.133 +<a class="c2html-lineno" name="l77" href="#l77"> 77 </a>} 23.134 +<a class="c2html-lineno" name="l78" href="#l78"> 78 </a> 23.135 +<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) { 23.136 +<a class="c2html-lineno" name="l80" href="#l80"> 80 </a> <span class="c2html-keyword">char</span> *end; 23.137 +<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>); 23.138 +<a class="c2html-lineno" name="l82" href="#l82"> 82 </a> <span class="c2html-keyword">if</span>(strlen(end) == <span class="c2html-macroconst">0</span>) { 23.139 +<a class="c2html-lineno" name="l83" href="#l83"> 83 </a> *value = val; 23.140 +<a class="c2html-lineno" name="l84" href="#l84"> 84 </a> <span class="c2html-keyword">return</span> <span class="c2html-macroconst">1</span>; 23.141 +<a class="c2html-lineno" name="l85" href="#l85"> 85 </a> } <span class="c2html-keyword">else</span> { 23.142 +<a class="c2html-lineno" name="l86" href="#l86"> 86 </a> <span class="c2html-keyword">return</span> <span class="c2html-macroconst">0</span>; 23.143 +<a class="c2html-lineno" name="l87" href="#l87"> 87 </a> } 23.144 +<a class="c2html-lineno" name="l88" href="#l88"> 88 </a>} 23.145 +<a class="c2html-lineno" name="l89" href="#l89"> 89 </a> 23.146 +<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) { 23.147 +<a class="c2html-lineno" name="l91" href="#l91"> 91 </a> <span class="c2html-keyword">char</span> *path = <span class="c2html-macroconst">NULL</span>; 23.148 +<a class="c2html-lineno" name="l92" href="#l92"> 92 </a> <span class="c2html-type">size_t</span> len = strlen(url); 23.149 +<a class="c2html-lineno" name="l93" href="#l93"> 93 </a> <span class="c2html-keyword">int</span> slashcount = <span class="c2html-macroconst">0</span>; 23.150 +<a class="c2html-lineno" name="l94" href="#l94"> 94 </a> <span class="c2html-keyword">int</span> slmax; 23.151 +<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>)) { 23.152 +<a class="c2html-lineno" name="l96" href="#l96"> 96 </a> slmax = <span class="c2html-macroconst">3</span>; 23.153 +<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>)) { 23.154 +<a class="c2html-lineno" name="l98" href="#l98"> 98 </a> slmax = <span class="c2html-macroconst">3</span>; 23.155 +<a class="c2html-lineno" name="l99" href="#l99"> 99 </a> } <span class="c2html-keyword">else</span> { 23.156 +<a class="c2html-lineno" name="l100" href="#l100">100 </a> slmax = <span class="c2html-macroconst">1</span>; 23.157 +<a class="c2html-lineno" name="l101" href="#l101">101 </a> } 23.158 +<a class="c2html-lineno" name="l102" href="#l102">102 </a> <span class="c2html-keyword">char</span> c; 23.159 +<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++) { 23.160 +<a class="c2html-lineno" name="l104" href="#l104">104 </a> c = url[i]; 23.161 +<a class="c2html-lineno" name="l105" href="#l105">105 </a> <span class="c2html-keyword">if</span>(c == <span class="c2html-string">'/'</span>) { 23.162 +<a class="c2html-lineno" name="l106" href="#l106">106 </a> slashcount++; 23.163 +<a class="c2html-lineno" name="l107" href="#l107">107 </a> <span class="c2html-keyword">if</span>(slashcount == slmax) { 23.164 +<a class="c2html-lineno" name="l108" href="#l108">108 </a> path = url + i; 23.165 +<a class="c2html-lineno" name="l109" href="#l109">109 </a> <span class="c2html-keyword">break</span>; 23.166 +<a class="c2html-lineno" name="l110" href="#l110">110 </a> } 23.167 +<a class="c2html-lineno" name="l111" href="#l111">111 </a> } 23.168 +<a class="c2html-lineno" name="l112" href="#l112">112 </a> } 23.169 +<a class="c2html-lineno" name="l113" href="#l113">113 </a> <span class="c2html-keyword">return</span> path; 23.170 +<a class="c2html-lineno" name="l114" href="#l114">114 </a>} 23.171 +<a class="c2html-lineno" name="l115" href="#l115">115 </a> 23.172 +<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) { 23.173 +<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>); 23.174 +<a class="c2html-lineno" name="l118" href="#l118">118 </a> <span class="c2html-keyword">char</span> *ret = strdup(unesc); 23.175 +<a class="c2html-lineno" name="l119" href="#l119">119 </a> curl_free(unesc); 23.176 +<a class="c2html-lineno" name="l120" href="#l120">120 </a> <span class="c2html-keyword">return</span> ret; 23.177 +<a class="c2html-lineno" name="l121" href="#l121">121 </a>} 23.178 +<a class="c2html-lineno" name="l122" href="#l122">122 </a> 23.179 +<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) { 23.180 +<a class="c2html-lineno" name="l124" href="#l124">124 </a> <span class="c2html-keyword">int</span> si = <span class="c2html-macroconst">0</span>; 23.181 +<a class="c2html-lineno" name="l125" href="#l125">125 </a> <span class="c2html-keyword">int</span> osi = <span class="c2html-macroconst">0</span>; 23.182 +<a class="c2html-lineno" name="l126" href="#l126">126 </a> <span class="c2html-keyword">int</span> i = <span class="c2html-macroconst">0</span>; 23.183 +<a class="c2html-lineno" name="l127" href="#l127">127 </a> <span class="c2html-keyword">int</span> p = <span class="c2html-macroconst">0</span>; 23.184 +<a class="c2html-lineno" name="l128" href="#l128">128 </a> <span class="c2html-keyword">char</span> c; 23.185 +<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>) { 23.186 +<a class="c2html-lineno" name="l130" href="#l130">130 </a> <span class="c2html-keyword">if</span>(c == <span class="c2html-string">'/'</span>) { 23.187 +<a class="c2html-lineno" name="l131" href="#l131">131 </a> osi = si; 23.188 +<a class="c2html-lineno" name="l132" href="#l132">132 </a> si = i; 23.189 +<a class="c2html-lineno" name="l133" href="#l133">133 </a> p = <span class="c2html-macroconst">1</span>; 23.190 +<a class="c2html-lineno" name="l134" href="#l134">134 </a> } 23.191 +<a class="c2html-lineno" name="l135" href="#l135">135 </a> i++; 23.192 +<a class="c2html-lineno" name="l136" href="#l136">136 </a> } 23.193 +<a class="c2html-lineno" name="l137" href="#l137">137 </a> 23.194 +<a class="c2html-lineno" name="l138" href="#l138">138 </a> <span class="c2html-keyword">char</span> *name = url + si + p; 23.195 +<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>) { 23.196 +<a class="c2html-lineno" name="l140" href="#l140">140 </a> name = url + osi + p; 23.197 +<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>) { 23.198 +<a class="c2html-lineno" name="l142" href="#l142">142 </a> <span class="c2html-keyword">return</span> url; 23.199 +<a class="c2html-lineno" name="l143" href="#l143">143 </a> } 23.200 +<a class="c2html-lineno" name="l144" href="#l144">144 </a> } 23.201 +<a class="c2html-lineno" name="l145" href="#l145">145 </a> 23.202 +<a class="c2html-lineno" name="l146" href="#l146">146 </a> <span class="c2html-keyword">return</span> name; 23.203 +<a class="c2html-lineno" name="l147" href="#l147">147 </a>} 23.204 +<a class="c2html-lineno" name="l148" href="#l148">148 </a> 23.205 +<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) { 23.206 +<a class="c2html-lineno" name="l150" href="#l150">150 </a><span class="c2html-directive">#ifdef</span> <span class="c2html-macroconst">_WIN32</span> 23.207 +<a class="c2html-lineno" name="l151" href="#l151">151 </a> <span class="c2html-keyword">return</span> mkdir(path); 23.208 +<a class="c2html-lineno" name="l152" href="#l152">152 </a><span class="c2html-directive">#else</span> 23.209 +<a class="c2html-lineno" name="l153" href="#l153">153 </a> <span class="c2html-keyword">return</span> mkdir(path, mode); 23.210 +<a class="c2html-lineno" name="l154" href="#l154">154 </a><span class="c2html-directive">#endif</span> 23.211 +<a class="c2html-lineno" name="l155" href="#l155">155 </a>} 23.212 +<a class="c2html-lineno" name="l156" href="#l156">156 </a> 23.213 +<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) { 23.214 +<a class="c2html-lineno" name="l158" href="#l158">158 </a> <span class="c2html-type">sstr_t</span> base = sstr(url_base); 23.215 +<a class="c2html-lineno" name="l159" href="#l159">159 </a> <span class="c2html-type">sstr_t</span> path; 23.216 +<a class="c2html-lineno" name="l160" href="#l160">160 </a> <span class="c2html-keyword">if</span>(p) { 23.217 +<a class="c2html-lineno" name="l161" href="#l161">161 </a> path = sstr(p); 23.218 +<a class="c2html-lineno" name="l162" href="#l162">162 </a> } <span class="c2html-keyword">else</span> { 23.219 +<a class="c2html-lineno" name="l163" href="#l163">163 </a> path = sstrn(<span class="c2html-string">""</span>, <span class="c2html-macroconst">0</span>); 23.220 +<a class="c2html-lineno" name="l164" href="#l164">164 </a> } 23.221 +<a class="c2html-lineno" name="l165" href="#l165">165 </a> 23.222 +<a class="c2html-lineno" name="l166" href="#l166">166 </a> <span class="c2html-keyword">int</span> add_separator = <span class="c2html-macroconst">0</span>; 23.223 +<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>) { 23.224 +<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>) { 23.225 +<a class="c2html-lineno" name="l169" href="#l169">169 </a> base.length--; 23.226 +<a class="c2html-lineno" name="l170" href="#l170">170 </a> } 23.227 +<a class="c2html-lineno" name="l171" href="#l171">171 </a> } <span class="c2html-keyword">else</span> { 23.228 +<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>) { 23.229 +<a class="c2html-lineno" name="l173" href="#l173">173 </a> add_separator = <span class="c2html-macroconst">1</span>; 23.230 +<a class="c2html-lineno" name="l174" href="#l174">174 </a> } 23.231 +<a class="c2html-lineno" name="l175" href="#l175">175 </a> } 23.232 +<a class="c2html-lineno" name="l176" href="#l176">176 </a> 23.233 +<a class="c2html-lineno" name="l177" href="#l177">177 </a> <span class="c2html-type">sstr_t</span> url; 23.234 +<a class="c2html-lineno" name="l178" href="#l178">178 </a> <span class="c2html-keyword">if</span>(add_separator) { 23.235 +<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); 23.236 +<a class="c2html-lineno" name="l180" href="#l180">180 </a> } <span class="c2html-keyword">else</span> { 23.237 +<a class="c2html-lineno" name="l181" href="#l181">181 </a> url = sstrcat(<span class="c2html-macroconst">2</span>, base, path); 23.238 +<a class="c2html-lineno" name="l182" href="#l182">182 </a> } 23.239 +<a class="c2html-lineno" name="l183" href="#l183">183 </a> 23.240 +<a class="c2html-lineno" name="l184" href="#l184">184 </a> <span class="c2html-keyword">return</span> url.ptr; 23.241 +<a class="c2html-lineno" name="l185" href="#l185">185 </a>} 23.242 +<a class="c2html-lineno" name="l186" href="#l186">186 </a> 23.243 +<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) { 23.244 +<a class="c2html-lineno" name="l188" href="#l188">188 </a> <span class="c2html-type">sstr_t</span> base = sstr(sn->base_url); 23.245 +<a class="c2html-lineno" name="l189" href="#l189">189 </a> <span class="c2html-type">sstr_t</span> href_str = sstr(href); 23.246 +<a class="c2html-lineno" name="l190" href="#l190">190 </a> 23.247 +<a class="c2html-lineno" name="l191" href="#l191">191 </a> <span class="c2html-keyword">char</span> *base_path = util_url_path(sn->base_url); 23.248 +<a class="c2html-lineno" name="l192" href="#l192">192 </a> base.length -= strlen(base_path); 23.249 +<a class="c2html-lineno" name="l193" href="#l193">193 </a> 23.250 +<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); 23.251 +<a class="c2html-lineno" name="l195" href="#l195">195 </a> 23.252 +<a class="c2html-lineno" name="l196" href="#l196">196 </a> curl_easy_setopt(sn->handle, <span class="c2html-macroconst">CURLOPT_URL</span>, url.ptr); 23.253 +<a class="c2html-lineno" name="l197" href="#l197">197 </a> free(url.ptr); 23.254 +<a class="c2html-lineno" name="l198" href="#l198">198 </a>} 23.255 +<a class="c2html-lineno" name="l199" href="#l199">199 </a> 23.256 +<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) { 23.257 +<a class="c2html-lineno" name="l201" href="#l201">201 </a> <span class="c2html-keyword">char</span> *space = malloc(<span class="c2html-macroconst">256</span>); 23.258 +<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>); 23.259 +<a class="c2html-lineno" name="l203" href="#l203">203 </a> 23.260 +<a class="c2html-lineno" name="l204" href="#l204">204 </a> <span class="c2html-comment">// add base url</span> 23.261 +<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); 23.262 +<a class="c2html-lineno" name="l206" href="#l206">206 </a> <span class="c2html-comment">// remove trailing slash</span> 23.263 +<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>); 23.264 +<a class="c2html-lineno" name="l208" href="#l208">208 </a> 23.265 +<a class="c2html-lineno" name="l209" href="#l209">209 </a> <span class="c2html-type">sstr_t</span> p = sstr(path); 23.266 +<a class="c2html-lineno" name="l210" href="#l210">210 </a> <span class="c2html-type">ssize_t</span> ntk = <span class="c2html-macroconst">0</span>; 23.267 +<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); 23.268 +<a class="c2html-lineno" name="l212" href="#l212">212 </a> 23.269 +<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++) { 23.270 +<a class="c2html-lineno" name="l214" href="#l214">214 </a> <span class="c2html-type">sstr_t</span> node = tks[i]; 23.271 +<a class="c2html-lineno" name="l215" href="#l215">215 </a> <span class="c2html-keyword">if</span>(node.length > <span class="c2html-macroconst">0</span>) { 23.272 +<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); 23.273 +<a class="c2html-lineno" name="l217" href="#l217">217 </a> ucx_buffer_putc(url, <span class="c2html-string">'/'</span>); 23.274 +<a class="c2html-lineno" name="l218" href="#l218">218 </a> ucx_buffer_write(esc, <span class="c2html-macroconst">1</span>, strlen(esc), url); 23.275 +<a class="c2html-lineno" name="l219" href="#l219">219 </a> curl_free(esc); 23.276 +<a class="c2html-lineno" name="l220" href="#l220">220 </a> } 23.277 +<a class="c2html-lineno" name="l221" href="#l221">221 </a> free(node.ptr); 23.278 +<a class="c2html-lineno" name="l222" href="#l222">222 </a> } 23.279 +<a class="c2html-lineno" name="l223" href="#l223">223 </a> free(tks); 23.280 +<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>) { 23.281 +<a class="c2html-lineno" name="l225" href="#l225">225 </a> ucx_buffer_putc(url, <span class="c2html-string">'/'</span>); 23.282 +<a class="c2html-lineno" name="l226" href="#l226">226 </a> } 23.283 +<a class="c2html-lineno" name="l227" href="#l227">227 </a> ucx_buffer_putc(url, <span class="c2html-macroconst">0</span>); 23.284 +<a class="c2html-lineno" name="l228" href="#l228">228 </a> 23.285 +<a class="c2html-lineno" name="l229" href="#l229">229 </a> space = url->space; 23.286 +<a class="c2html-lineno" name="l230" href="#l230">230 </a> ucx_buffer_free(url); 23.287 +<a class="c2html-lineno" name="l231" href="#l231">231 </a> 23.288 +<a class="c2html-lineno" name="l232" href="#l232">232 </a> <span class="c2html-keyword">return</span> space; 23.289 +<a class="c2html-lineno" name="l233" href="#l233">233 </a>} 23.290 +<a class="c2html-lineno" name="l234" href="#l234">234 </a> 23.291 +<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) { 23.292 +<a class="c2html-lineno" name="l236" href="#l236">236 </a> <span class="c2html-keyword">char</span> *name = util_resource_name(path); 23.293 +<a class="c2html-lineno" name="l237" href="#l237">237 </a> <span class="c2html-type">size_t</span> namelen = strlen(name); 23.294 +<a class="c2html-lineno" name="l238" href="#l238">238 </a> <span class="c2html-type">size_t</span> pathlen = strlen(path); 23.295 +<a class="c2html-lineno" name="l239" href="#l239">239 </a> <span class="c2html-type">size_t</span> parentlen = pathlen - namelen; 23.296 +<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>); 23.297 +<a class="c2html-lineno" name="l241" href="#l241">241 </a> memcpy(parent, path, parentlen); 23.298 +<a class="c2html-lineno" name="l242" href="#l242">242 </a> parent[parentlen] = <span class="c2html-string">'\0'</span>; 23.299 +<a class="c2html-lineno" name="l243" href="#l243">243 </a> <span class="c2html-keyword">return</span> parent; 23.300 +<a class="c2html-lineno" name="l244" href="#l244">244 </a>} 23.301 +<a class="c2html-lineno" name="l245" href="#l245">245 </a> 23.302 +<a class="c2html-lineno" name="l246" href="#l246">246 </a> 23.303 +<a class="c2html-lineno" name="l247" href="#l247">247 </a><span class="c2html-keyword">char</span>* util_xml_get_text(xmlNode *elm) { 23.304 +<a class="c2html-lineno" name="l248" href="#l248">248 </a> xmlNode *node = elm->children; 23.305 +<a class="c2html-lineno" name="l249" href="#l249">249 </a> <span class="c2html-keyword">while</span>(node) { 23.306 +<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>) { 23.307 +<a class="c2html-lineno" name="l251" href="#l251">251 </a> <span class="c2html-keyword">return</span> (<span class="c2html-keyword">char</span>*)node->content; 23.308 +<a class="c2html-lineno" name="l252" href="#l252">252 </a> } 23.309 +<a class="c2html-lineno" name="l253" href="#l253">253 </a> node = node->next; 23.310 +<a class="c2html-lineno" name="l254" href="#l254">254 </a> } 23.311 +<a class="c2html-lineno" name="l255" href="#l255">255 </a> <span class="c2html-keyword">return</span> <span class="c2html-macroconst">NULL</span>; 23.312 +<a class="c2html-lineno" name="l256" href="#l256">256 </a>} 23.313 +<a class="c2html-lineno" name="l257" href="#l257">257 </a> 23.314 +<a class="c2html-lineno" name="l258" href="#l258">258 </a> 23.315 +<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) { 23.316 +<a class="c2html-lineno" name="l260" href="#l260">260 </a> <span class="c2html-keyword">int</span> len = <span class="c2html-macroconst">0</span>; 23.317 +<a class="c2html-lineno" name="l261" href="#l261">261 </a> <span class="c2html-keyword">return</span> util_base64decode_len(in, &len); 23.318 +<a class="c2html-lineno" name="l262" href="#l262">262 </a>} 23.319 +<a class="c2html-lineno" name="l263" href="#l263">263 </a> 23.320 +<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) { 23.321 +<a class="c2html-lineno" name="l265" href="#l265">265 </a> <span class="c2html-type">size_t</span> len = strlen(in); 23.322 +<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); 23.323 +<a class="c2html-lineno" name="l267" href="#l267">267 </a> 23.324 +<a class="c2html-lineno" name="l268" href="#l268">268 </a> <span class="c2html-macroconst">BIO</span>* b = BIO_new_mem_buf(in, len); 23.325 +<a class="c2html-lineno" name="l269" href="#l269">269 </a> <span class="c2html-macroconst">BIO</span> *d = BIO_new(BIO_f_base64()); 23.326 +<a class="c2html-lineno" name="l270" href="#l270">270 </a> BIO_set_flags(d, <span class="c2html-macroconst">BIO_FLAGS_BASE64_NO_NL</span>); 23.327 +<a class="c2html-lineno" name="l271" href="#l271">271 </a> b = BIO_push(d, b); 23.328 +<a class="c2html-lineno" name="l272" href="#l272">272 </a> 23.329 +<a class="c2html-lineno" name="l273" href="#l273">273 </a> *outlen = BIO_read(b, out, len); 23.330 +<a class="c2html-lineno" name="l274" href="#l274">274 </a> BIO_free_all(b); 23.331 +<a class="c2html-lineno" name="l275" href="#l275">275 </a> 23.332 +<a class="c2html-lineno" name="l276" href="#l276">276 </a> <span class="c2html-keyword">return</span> out; 23.333 +<a class="c2html-lineno" name="l277" href="#l277">277 </a>} 23.334 +<a class="c2html-lineno" name="l278" href="#l278">278 </a> 23.335 +<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) { 23.336 +<a class="c2html-lineno" name="l280" href="#l280">280 </a> <span class="c2html-macroconst">BIO</span> *b; 23.337 +<a class="c2html-lineno" name="l281" href="#l281">281 </a> <span class="c2html-macroconst">BIO</span> *e; 23.338 +<a class="c2html-lineno" name="l282" href="#l282">282 </a> <span class="c2html-macroconst">BUF_MEM</span> *mem; 23.339 +<a class="c2html-lineno" name="l283" href="#l283">283 </a> 23.340 +<a class="c2html-lineno" name="l284" href="#l284">284 </a> e = BIO_new(BIO_f_base64()); 23.341 +<a class="c2html-lineno" name="l285" href="#l285">285 </a> b = BIO_new(BIO_s_mem()); 23.342 +<a class="c2html-lineno" name="l286" href="#l286">286 </a> 23.343 +<a class="c2html-lineno" name="l287" href="#l287">287 </a> e = BIO_push(e, b); 23.344 +<a class="c2html-lineno" name="l288" href="#l288">288 </a> BIO_write(e, in, len); 23.345 +<a class="c2html-lineno" name="l289" href="#l289">289 </a> BIO_flush(e); 23.346 +<a class="c2html-lineno" name="l290" href="#l290">290 </a> 23.347 +<a class="c2html-lineno" name="l291" href="#l291">291 </a> BIO_get_mem_ptr(e, &mem); 23.348 +<a class="c2html-lineno" name="l292" href="#l292">292 </a> <span class="c2html-keyword">char</span> *out = malloc(mem->length); 23.349 +<a class="c2html-lineno" name="l293" href="#l293">293 </a> memcpy(out, mem->data, mem->length -<span class="c2html-macroconst">1</span>); 23.350 +<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>; 23.351 +<a class="c2html-lineno" name="l295" href="#l295">295 </a> 23.352 +<a class="c2html-lineno" name="l296" href="#l296">296 </a> BIO_free_all(e); 23.353 +<a class="c2html-lineno" name="l297" href="#l297">297 </a> 23.354 +<a class="c2html-lineno" name="l298" href="#l298">298 </a> <span class="c2html-keyword">return</span> out; 23.355 +<a class="c2html-lineno" name="l299" href="#l299">299 </a>} 23.356 +<a class="c2html-lineno" name="l300" href="#l300">300 </a> 23.357 +<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) { 23.358 +<a class="c2html-lineno" name="l302" href="#l302">302 </a> DavKey *k = dav_context_get_key(sn->context, key); 23.359 +<a class="c2html-lineno" name="l303" href="#l303">303 </a> <span class="c2html-keyword">if</span>(!k) { 23.360 +<a class="c2html-lineno" name="l304" href="#l304">304 </a> <span class="c2html-comment">// TODO: session error</span> 23.361 +<a class="c2html-lineno" name="l305" href="#l305">305 </a> <span class="c2html-keyword">return</span> <span class="c2html-macroconst">NULL</span>; 23.362 +<a class="c2html-lineno" name="l306" href="#l306">306 </a> } 23.363 +<a class="c2html-lineno" name="l307" href="#l307">307 </a> 23.364 +<a class="c2html-lineno" name="l308" href="#l308">308 </a> <span class="c2html-keyword">char</span> *enc_str = aes_encrypt(str, k); 23.365 +<a class="c2html-lineno" name="l309" href="#l309">309 </a> <span class="c2html-keyword">char</span> *ret_str = dav_session_strdup(sn, enc_str); 23.366 +<a class="c2html-lineno" name="l310" href="#l310">310 </a> free(enc_str); 23.367 +<a class="c2html-lineno" name="l311" href="#l311">311 </a> <span class="c2html-keyword">return</span> ret_str; 23.368 +<a class="c2html-lineno" name="l312" href="#l312">312 </a>} 23.369 +<a class="c2html-lineno" name="l313" href="#l313">313 </a> 23.370 +<a class="c2html-lineno" name="l314" href="#l314">314 </a><span class="c2html-comment">/* commented out for testing reasons */</span> 23.371 +<a class="c2html-lineno" name="l315" href="#l315">315 </a><span class="c2html-comment">/*</span> 23.372 +<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> 23.373 +<a class="c2html-lineno" name="l317" href="#l317">317 </a><span class="c2html-comment"> DavKey *k = dav_context_get_key(sn->context, key);</span> 23.374 +<a class="c2html-lineno" name="l318" href="#l318">318 </a><span class="c2html-comment"> if(!k) {</span> 23.375 +<a class="c2html-lineno" name="l319" href="#l319">319 </a><span class="c2html-comment"> // TODO: session error</span> 23.376 +<a class="c2html-lineno" name="l320" href="#l320">320 </a><span class="c2html-comment"> return NULL;</span> 23.377 +<a class="c2html-lineno" name="l321" href="#l321">321 </a><span class="c2html-comment"> }</span> 23.378 +<a class="c2html-lineno" name="l322" href="#l322">322 </a><span class="c2html-comment"> </span> 23.379 +<a class="c2html-lineno" name="l323" href="#l323">323 </a><span class="c2html-comment"> char *dec_str = aes_decrypt(str, k);</span> 23.380 +<a class="c2html-lineno" name="l324" href="#l324">324 </a><span class="c2html-comment"> char *ret_str = dav_session_strdup(sn, dec_str);</span> 23.381 +<a class="c2html-lineno" name="l325" href="#l325">325 </a><span class="c2html-comment"> free(dec_str);</span> 23.382 +<a class="c2html-lineno" name="l326" href="#l326">326 </a><span class="c2html-comment"> return ret_str;</span> 23.383 +<a class="c2html-lineno" name="l327" href="#l327">327 </a><span class="c2html-comment">}</span> 23.384 +<a class="c2html-lineno" name="l328" href="#l328">328 </a><span class="c2html-comment">*/</span> 23.385 +<a class="c2html-lineno" name="l329" href="#l329">329 </a><span class="c2html-keyword">char</span>* util_random_str() { 23.386 +<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>); 23.387 +<a class="c2html-lineno" name="l331" href="#l331">331 </a> str[<span class="c2html-macroconst">24</span>] = <span class="c2html-string">'\0'</span>; 23.388 +<a class="c2html-lineno" name="l332" href="#l332">332 </a> 23.389 +<a class="c2html-lineno" name="l333" href="#l333">333 </a> <span class="c2html-type">sstr_t</span> t = <span class="c2html-macroconst">S</span>( 23.390 +<a class="c2html-lineno" name="l334" href="#l334">334 </a> <span class="c2html-string">"01234567890"</span> 23.391 +<a class="c2html-lineno" name="l335" href="#l335">335 </a> <span class="c2html-string">"abcdefghijklmnopqrstuvwxyz"</span> 23.392 +<a class="c2html-lineno" name="l336" href="#l336">336 </a> <span class="c2html-string">"ABCDEFGHIJKLMNOPQRSTUVWXYZ"</span>); 23.393 +<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; 23.394 +<a class="c2html-lineno" name="l338" href="#l338">338 </a> 23.395 +<a class="c2html-lineno" name="l339" href="#l339">339 </a> RAND_pseudo_bytes(str, <span class="c2html-macroconst">24</span>); 23.396 +<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++) { 23.397 +<a class="c2html-lineno" name="l341" href="#l341">341 </a> <span class="c2html-keyword">int</span> c = str[i] % t.length; 23.398 +<a class="c2html-lineno" name="l342" href="#l342">342 </a> str[i] = table[c]; 23.399 +<a class="c2html-lineno" name="l343" href="#l343">343 </a> } 23.400 +<a class="c2html-lineno" name="l344" href="#l344">344 </a> 23.401 +<a class="c2html-lineno" name="l345" href="#l345">345 </a> <span class="c2html-keyword">return</span> (<span class="c2html-keyword">char</span>*)str; 23.402 +<a class="c2html-lineno" name="l346" href="#l346">346 </a>} 23.403 +<a class="c2html-lineno" name="l347" href="#l347">347 </a> 23.404 +<a class="c2html-lineno" name="l348" href="#l348">348 </a><span class="c2html-comment">/*</span> 23.405 +<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> 23.406 +<a class="c2html-lineno" name="l350" href="#l350">350 </a><span class="c2html-comment"> * tokens are separated by space</span> 23.407 +<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> 23.408 +<a class="c2html-lineno" name="l352" href="#l352">352 </a><span class="c2html-comment"> */</span> 23.409 +<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) { 23.410 +<a class="c2html-lineno" name="l354" href="#l354">354 </a> <span class="c2html-keyword">int</span> i; 23.411 +<a class="c2html-lineno" name="l355" href="#l355">355 </a> <span class="c2html-keyword">int</span> token_start = -<span class="c2html-macroconst">1</span>; 23.412 +<a class="c2html-lineno" name="l356" href="#l356">356 </a> <span class="c2html-keyword">int</span> token_end = -<span class="c2html-macroconst">1</span>; 23.413 +<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++) { 23.414 +<a class="c2html-lineno" name="l358" href="#l358">358 </a> <span class="c2html-keyword">int</span> c; 23.415 +<a class="c2html-lineno" name="l359" href="#l359">359 </a> <span class="c2html-keyword">if</span>(i == str.length) { 23.416 +<a class="c2html-lineno" name="l360" href="#l360">360 </a> c = <span class="c2html-string">' '</span>; 23.417 +<a class="c2html-lineno" name="l361" href="#l361">361 </a> } <span class="c2html-keyword">else</span> { 23.418 +<a class="c2html-lineno" name="l362" href="#l362">362 </a> c = str.ptr[i]; 23.419 +<a class="c2html-lineno" name="l363" href="#l363">363 </a> } 23.420 +<a class="c2html-lineno" name="l364" href="#l364">364 </a> <span class="c2html-keyword">if</span>(c < <span class="c2html-macroconst">33</span>) { 23.421 +<a class="c2html-lineno" name="l365" href="#l365">365 </a> <span class="c2html-keyword">if</span>(token_start != -<span class="c2html-macroconst">1</span>) { 23.422 +<a class="c2html-lineno" name="l366" href="#l366">366 </a> token_end = i; 23.423 +<a class="c2html-lineno" name="l367" href="#l367">367 </a> <span class="c2html-type">size_t</span> len = token_end - token_start; 23.424 +<a class="c2html-lineno" name="l368" href="#l368">368 </a> <span class="c2html-type">sstr_t</span> tk = sstrsubsl(str, token_start, len); 23.425 +<a class="c2html-lineno" name="l369" href="#l369">369 </a> <span class="c2html-comment">//printf("token: {%.*s}\n", token.length, token.ptr);</span> 23.426 +<a class="c2html-lineno" name="l370" href="#l370">370 </a> <span class="c2html-keyword">if</span>(!sstrcmp(tk, token)) { 23.427 +<a class="c2html-lineno" name="l371" href="#l371">371 </a> *sub = sstrtrim(sstrsubsl(str, <span class="c2html-macroconst">0</span>, token_start)); 23.428 +<a class="c2html-lineno" name="l372" href="#l372">372 </a> <span class="c2html-keyword">break</span>; 23.429 +<a class="c2html-lineno" name="l373" href="#l373">373 </a> } 23.430 +<a class="c2html-lineno" name="l374" href="#l374">374 </a> token_start = -<span class="c2html-macroconst">1</span>; 23.431 +<a class="c2html-lineno" name="l375" href="#l375">375 </a> token_end = -<span class="c2html-macroconst">1</span>; 23.432 +<a class="c2html-lineno" name="l376" href="#l376">376 </a> } 23.433 +<a class="c2html-lineno" name="l377" href="#l377">377 </a> } <span class="c2html-keyword">else</span> { 23.434 +<a class="c2html-lineno" name="l378" href="#l378">378 </a> <span class="c2html-keyword">if</span>(token_start == -<span class="c2html-macroconst">1</span>) { 23.435 +<a class="c2html-lineno" name="l379" href="#l379">379 </a> token_start = i; 23.436 +<a class="c2html-lineno" name="l380" href="#l380">380 </a> } 23.437 +<a class="c2html-lineno" name="l381" href="#l381">381 </a> } 23.438 +<a class="c2html-lineno" name="l382" href="#l382">382 </a> } 23.439 +<a class="c2html-lineno" name="l383" href="#l383">383 </a> 23.440 +<a class="c2html-lineno" name="l384" href="#l384">384 </a> <span class="c2html-keyword">if</span>(i < str.length) { 23.441 +<a class="c2html-lineno" name="l385" href="#l385">385 </a> <span class="c2html-keyword">return</span> sstrtrim(sstrsubs(str, i)); 23.442 +<a class="c2html-lineno" name="l386" href="#l386">386 </a> } <span class="c2html-keyword">else</span> { 23.443 +<a class="c2html-lineno" name="l387" href="#l387">387 </a> str.ptr = <span class="c2html-macroconst">NULL</span>; 23.444 +<a class="c2html-lineno" name="l388" href="#l388">388 </a> str.length = <span class="c2html-macroconst">0</span>; 23.445 +<a class="c2html-lineno" name="l389" href="#l389">389 </a> <span class="c2html-keyword">return</span> str; 23.446 +<a class="c2html-lineno" name="l390" href="#l390">390 </a> } 23.447 +<a class="c2html-lineno" name="l391" href="#l391">391 </a>} 23.448 +</div> 23.449 + </body> 23.450 +</html> 23.451 +
24.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 24.2 +++ b/test/gs/javatest.html Mon Apr 24 20:54:38 2023 +0200 24.3 @@ -0,0 +1,225 @@ 24.4 +<!DOCTYPE html> 24.5 +<html> 24.6 + <head> 24.7 + <title>c2html</title> 24.8 + <style type="text/css"> 24.9 + div.c2html-code { 24.10 + white-space: pre; 24.11 + font-family: monospace; 24.12 + } 24.13 + a.c2html-lineno { 24.14 + /* as long as user-select isn't widely spread, we throw the bomb */ 24.15 + -webkit-user-select: none; 24.16 + -moz-user-select: none; 24.17 + -ms-user-select: none; 24.18 + user-select: none; 24.19 + display: inline-block; 24.20 + font-style: italic; 24.21 + text-decoration: none; 24.22 + color: grey; 24.23 + } 24.24 + span.c2html-keyword { 24.25 + color: blue; 24.26 + } 24.27 + span.c2html-macroconst { 24.28 + color: cornflowerblue; 24.29 + } 24.30 + span.c2html-type { 24.31 + color: teal; 24.32 + } 24.33 + span.c2html-directive { 24.34 + color: silver; 24.35 + } 24.36 + span.c2html-string { 24.37 + color: darkorange; 24.38 + } 24.39 + span.c2html-comment { 24.40 + color: grey; 24.41 + } 24.42 + span.c2html-stdinclude, span.c2html-userinclude, a.c2html-userinclude { 24.43 + } 24.44 + </style> 24.45 + </head> 24.46 + <body> 24.47 + 24.48 +<div class="c2html-code"> 24.49 +<a class="c2html-lineno" name="l1" href="#l1"> 1 </a><span class="c2html-comment">/*</span> 24.50 +<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> 24.51 +<a class="c2html-lineno" name="l3" href="#l3"> 3 </a><span class="c2html-comment"> *</span> 24.52 +<a class="c2html-lineno" name="l4" href="#l4"> 4 </a><span class="c2html-comment"> * Copyright 2014 Mike Becker. All rights reserved.</span> 24.53 +<a class="c2html-lineno" name="l5" href="#l5"> 5 </a><span class="c2html-comment"> *</span> 24.54 +<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> 24.55 +<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> 24.56 +<a class="c2html-lineno" name="l8" href="#l8"> 8 </a><span class="c2html-comment"> *</span> 24.57 +<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> 24.58 +<a class="c2html-lineno" name="l10" href="#l10"> 10 </a><span class="c2html-comment"> * notice, this list of conditions and the following disclaimer.</span> 24.59 +<a class="c2html-lineno" name="l11" href="#l11"> 11 </a><span class="c2html-comment"> *</span> 24.60 +<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> 24.61 +<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> 24.62 +<a class="c2html-lineno" name="l14" href="#l14"> 14 </a><span class="c2html-comment"> * documentation and/or other materials provided with the distribution.</span> 24.63 +<a class="c2html-lineno" name="l15" href="#l15"> 15 </a><span class="c2html-comment"> *</span> 24.64 +<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> 24.65 +<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> 24.66 +<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> 24.67 +<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> 24.68 +<a class="c2html-lineno" name="l20" href="#l20"> 20 </a><span class="c2html-comment"> * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR</span> 24.69 +<a class="c2html-lineno" name="l21" href="#l21"> 21 </a><span class="c2html-comment"> * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF</span> 24.70 +<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> 24.71 +<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> 24.72 +<a class="c2html-lineno" name="l24" href="#l24"> 24 </a><span class="c2html-comment"> * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)</span> 24.73 +<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> 24.74 +<a class="c2html-lineno" name="l26" href="#l26"> 26 </a><span class="c2html-comment"> * POSSIBILITY OF SUCH DAMAGE.</span> 24.75 +<a class="c2html-lineno" name="l27" href="#l27"> 27 </a><span class="c2html-comment"> *</span> 24.76 +<a class="c2html-lineno" name="l28" href="#l28"> 28 </a><span class="c2html-comment"> */</span> 24.77 +<a class="c2html-lineno" name="l29" href="#l29"> 29 </a> 24.78 +<a class="c2html-lineno" name="l30" href="#l30"> 30 </a><span class="c2html-keyword">package</span> de.uapcore.sigred.doc.base; 24.79 +<a class="c2html-lineno" name="l31" href="#l31"> 31 </a> 24.80 +<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>; 24.81 +<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>; 24.82 +<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>; 24.83 +<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>; 24.84 +<a class="c2html-lineno" name="l36" href="#l36"> 36 </a><span class="c2html-keyword">import</span> java.io.<span class="c2html-type">IOException</span>; 24.85 +<a class="c2html-lineno" name="l37" href="#l37"> 37 </a><span class="c2html-keyword">import</span> java.io.<span class="c2html-type">InputStream</span>; 24.86 +<a class="c2html-lineno" name="l38" href="#l38"> 38 </a><span class="c2html-keyword">import</span> java.io.<span class="c2html-type">OutputStream</span>; 24.87 +<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>; 24.88 +<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>; 24.89 +<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>; 24.90 +<a class="c2html-lineno" name="l42" href="#l42"> 42 </a><span class="c2html-keyword">import</span> org.dom4j.<span class="c2html-type">Document</span>; 24.91 +<a class="c2html-lineno" name="l43" href="#l43"> 43 </a><span class="c2html-keyword">import</span> org.dom4j.<span class="c2html-type">DocumentException</span>; 24.92 +<a class="c2html-lineno" name="l44" href="#l44"> 44 </a><span class="c2html-keyword">import</span> org.dom4j.<span class="c2html-type">DocumentHelper</span>; 24.93 +<a class="c2html-lineno" name="l45" href="#l45"> 45 </a><span class="c2html-keyword">import</span> org.dom4j.<span class="c2html-type">Element</span>; 24.94 +<a class="c2html-lineno" name="l46" href="#l46"> 46 </a><span class="c2html-keyword">import</span> org.dom4j.<span class="c2html-type">Namespace</span>; 24.95 +<a class="c2html-lineno" name="l47" href="#l47"> 47 </a><span class="c2html-keyword">import</span> org.dom4j.<span class="c2html-type">QName</span>; 24.96 +<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>; 24.97 +<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>; 24.98 +<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>; 24.99 +<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>; 24.100 +<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>; 24.101 +<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>; 24.102 +<a class="c2html-lineno" name="l54" href="#l54"> 54 </a> 24.103 +<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>> 24.104 +<a class="c2html-lineno" name="l56" href="#l56"> 56 </a> <span class="c2html-keyword">extends</span> <span class="c2html-type">FileBackedDocument</span> { 24.105 +<a class="c2html-lineno" name="l57" href="#l57"> 57 </a> 24.106 +<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>, 24.107 +<a class="c2html-lineno" name="l59" href="#l59"> 59 </a> <span class="c2html-string">"http://develop.uap-core.de/sigred/"</span>); 24.108 +<a class="c2html-lineno" name="l60" href="#l60"> 60 </a> 24.109 +<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> 24.110 +<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>); 24.111 +<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> 24.112 +<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>); 24.113 +<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> 24.114 +<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>); 24.115 +<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> 24.116 +<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>); 24.117 +<a class="c2html-lineno" name="l69" href="#l69"> 69 </a> 24.118 +<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; 24.119 +<a class="c2html-lineno" name="l71" href="#l71"> 71 </a> 24.120 +<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; 24.121 +<a class="c2html-lineno" name="l73" href="#l73"> 73 </a> 24.122 +<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) { 24.123 +<a class="c2html-lineno" name="l75" href="#l75"> 75 </a> <span class="c2html-type">T</span> g; 24.124 +<a class="c2html-lineno" name="l76" href="#l76"> 76 </a> <span class="c2html-keyword">try</span> { 24.125 +<a class="c2html-lineno" name="l77" href="#l77"> 77 </a> g = graphType.newInstance(); 24.126 +<a class="c2html-lineno" name="l78" href="#l78"> 78 </a> } <span class="c2html-keyword">catch</span> (<span class="c2html-type">ReflectiveOperationException</span> e) { 24.127 +<a class="c2html-lineno" name="l79" href="#l79"> 79 </a> <span class="c2html-keyword">assert</span> false; 24.128 +<a class="c2html-lineno" name="l80" href="#l80"> 80 </a> g = null; <span class="c2html-comment">// for the compiler</span> 24.129 +<a class="c2html-lineno" name="l81" href="#l81"> 81 </a> } 24.130 +<a class="c2html-lineno" name="l82" href="#l82"> 82 </a> graph = g; 24.131 +<a class="c2html-lineno" name="l83" href="#l83"> 83 </a> metadata = <span class="c2html-keyword">new</span> <span class="c2html-type">GraphDocumentMetadata</span>(); 24.132 +<a class="c2html-lineno" name="l84" href="#l84"> 84 </a> } 24.133 +<a class="c2html-lineno" name="l85" href="#l85"> 85 </a> 24.134 +<a class="c2html-lineno" name="l86" href="#l86"> 86 </a> <span class="c2html-keyword">public</span> <span class="c2html-type">T</span> getGraph() { 24.135 +<a class="c2html-lineno" name="l87" href="#l87"> 87 </a> <span class="c2html-keyword">return</span> graph; 24.136 +<a class="c2html-lineno" name="l88" href="#l88"> 88 </a> } 24.137 +<a class="c2html-lineno" name="l89" href="#l89"> 89 </a> 24.138 +<a class="c2html-lineno" name="l90" href="#l90"> 90 </a> <span class="c2html-keyword">public</span> <span class="c2html-type">GraphDocumentMetadata</span> getMetadata() { 24.139 +<a class="c2html-lineno" name="l91" href="#l91"> 91 </a> <span class="c2html-keyword">return</span> metadata; 24.140 +<a class="c2html-lineno" name="l92" href="#l92"> 92 </a> } 24.141 +<a class="c2html-lineno" name="l93" href="#l93"> 93 </a> 24.142 +<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>; 24.143 +<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>; 24.144 +<a class="c2html-lineno" name="l96" href="#l96"> 96 </a> 24.145 +<a class="c2html-lineno" name="l97" href="#l97"> 97 </a> <span class="c2html-directive">@Override</span> 24.146 +<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> { 24.147 +<a class="c2html-lineno" name="l99" href="#l99"> 99 </a> <span class="c2html-type">Document</span> doc = <span class="c2html-type">DocumentHelper</span>.createDocument(); 24.148 +<a class="c2html-lineno" name="l100" href="#l100">100 </a> 24.149 +<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>); 24.150 +<a class="c2html-lineno" name="l102" href="#l102">102 </a> 24.151 +<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>); 24.152 +<a class="c2html-lineno" name="l104" href="#l104">104 </a> 24.153 +<a class="c2html-lineno" name="l105" href="#l105">105 </a> metadata.write(metadataNode); 24.154 +<a class="c2html-lineno" name="l106" href="#l106">106 </a> 24.155 +<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>) { 24.156 +<a class="c2html-lineno" name="l108" href="#l108">108 </a> writeGraph(rootNode.addElement(<span class="c2html-type">TAG_GRAPH</span>)); 24.157 +<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>) { 24.158 +<a class="c2html-lineno" name="l110" href="#l110">110 </a> writeGraph(rootNode.addElement(<span class="c2html-type">TAG_DIGRAPH</span>)); 24.159 +<a class="c2html-lineno" name="l111" href="#l111">111 </a> } <span class="c2html-keyword">else</span> { 24.160 +<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>); 24.161 +<a class="c2html-lineno" name="l113" href="#l113">113 </a> } 24.162 +<a class="c2html-lineno" name="l114" href="#l114">114 </a> 24.163 +<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()); 24.164 +<a class="c2html-lineno" name="l116" href="#l116">116 </a> writer.write(doc); 24.165 +<a class="c2html-lineno" name="l117" href="#l117">117 </a> writer.flush(); 24.166 +<a class="c2html-lineno" name="l118" href="#l118">118 </a> } 24.167 +<a class="c2html-lineno" name="l119" href="#l119">119 </a> 24.168 +<a class="c2html-lineno" name="l120" href="#l120">120 </a> <span class="c2html-directive">@Override</span> 24.169 +<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> { 24.170 +<a class="c2html-lineno" name="l122" href="#l122">122 </a> <span class="c2html-keyword">try</span> { 24.171 +<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); 24.172 +<a class="c2html-lineno" name="l124" href="#l124">124 </a> reader.setStripWhitespaceText(true); 24.173 +<a class="c2html-lineno" name="l125" href="#l125">125 </a> 24.174 +<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>+ 24.175 +<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); 24.176 +<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> + 24.177 +<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>, 24.178 +<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( 24.179 +<a class="c2html-lineno" name="l131" href="#l131">131 </a> <span class="c2html-string">"graph-document.xsd"</span>).toExternalForm())); 24.180 +<a class="c2html-lineno" name="l132" href="#l132">132 </a> 24.181 +<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); 24.182 +<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><>(); 24.183 +<a class="c2html-lineno" name="l135" href="#l135">135 </a> <span class="c2html-comment">// TODO: we should do more detailed error handling here</span> 24.184 +<a class="c2html-lineno" name="l136" href="#l136">136 </a> reader.setErrorHandler(<span class="c2html-keyword">new</span> <span class="c2html-type">ErrorHandler</span>() { 24.185 +<a class="c2html-lineno" name="l137" href="#l137">137 </a> <span class="c2html-directive">@Override</span> 24.186 +<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> { 24.187 +<a class="c2html-lineno" name="l139" href="#l139">139 </a> } 24.188 +<a class="c2html-lineno" name="l140" href="#l140">140 </a> 24.189 +<a class="c2html-lineno" name="l141" href="#l141">141 </a> <span class="c2html-directive">@Override</span> 24.190 +<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> { 24.191 +<a class="c2html-lineno" name="l143" href="#l143">143 </a> xmlerror.set(exception); 24.192 +<a class="c2html-lineno" name="l144" href="#l144">144 </a> passed.set(false); 24.193 +<a class="c2html-lineno" name="l145" href="#l145">145 </a> } 24.194 +<a class="c2html-lineno" name="l146" href="#l146">146 </a> 24.195 +<a class="c2html-lineno" name="l147" href="#l147">147 </a> <span class="c2html-directive">@Override</span> 24.196 +<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> { 24.197 +<a class="c2html-lineno" name="l149" href="#l149">149 </a> xmlerror.set(exception); 24.198 +<a class="c2html-lineno" name="l150" href="#l150">150 </a> passed.set(false); 24.199 +<a class="c2html-lineno" name="l151" href="#l151">151 </a> } 24.200 +<a class="c2html-lineno" name="l152" href="#l152">152 </a> 24.201 +<a class="c2html-lineno" name="l153" href="#l153">153 </a> }); 24.202 +<a class="c2html-lineno" name="l154" href="#l154">154 </a> <span class="c2html-type">Document</span> doc = reader.read(in); 24.203 +<a class="c2html-lineno" name="l155" href="#l155">155 </a> <span class="c2html-keyword">if</span> (!passed.get()) { 24.204 +<a class="c2html-lineno" name="l156" href="#l156">156 </a> <span class="c2html-comment">// TODO: provide details (maybe via separate error object?)</span> 24.205 +<a class="c2html-lineno" name="l157" href="#l157">157 </a> <span class="c2html-keyword">throw</span> xmlerror.get(); 24.206 +<a class="c2html-lineno" name="l158" href="#l158">158 </a> } 24.207 +<a class="c2html-lineno" name="l159" href="#l159">159 </a> 24.208 +<a class="c2html-lineno" name="l160" href="#l160">160 </a> doc.normalize(); 24.209 +<a class="c2html-lineno" name="l161" href="#l161">161 </a> 24.210 +<a class="c2html-lineno" name="l162" href="#l162">162 </a> <span class="c2html-type">Element</span> root = doc.getRootElement(); 24.211 +<a class="c2html-lineno" name="l163" href="#l163">163 </a> metadata.read(root.element(<span class="c2html-type">TAG_METADATA</span>)); 24.212 +<a class="c2html-lineno" name="l164" href="#l164">164 </a> 24.213 +<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>) { 24.214 +<a class="c2html-lineno" name="l166" href="#l166">166 </a> readGraph(root.element(<span class="c2html-type">TAG_GRAPH</span>)); 24.215 +<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>) { 24.216 +<a class="c2html-lineno" name="l168" href="#l168">168 </a> readGraph(root.element(<span class="c2html-type">TAG_DIGRAPH</span>)); 24.217 +<a class="c2html-lineno" name="l169" href="#l169">169 </a> } <span class="c2html-keyword">else</span> { 24.218 +<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>); 24.219 +<a class="c2html-lineno" name="l171" href="#l171">171 </a> } 24.220 +<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) { 24.221 +<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); 24.222 +<a class="c2html-lineno" name="l174" href="#l174">174 </a> } 24.223 +<a class="c2html-lineno" name="l175" href="#l175">175 </a> } 24.224 +<a class="c2html-lineno" name="l176" href="#l176">176 </a>} 24.225 +</div> 24.226 + </body> 24.227 +</html> 24.228 +
25.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 25.2 +++ b/test/gs/plain.html Mon Apr 24 20:54:38 2023 +0200 25.3 @@ -0,0 +1,63 @@ 25.4 +<!DOCTYPE html> 25.5 +<html> 25.6 + <head> 25.7 + <title>c2html</title> 25.8 + <style type="text/css"> 25.9 + div.c2html-code { 25.10 + white-space: pre; 25.11 + font-family: monospace; 25.12 + } 25.13 + a.c2html-lineno { 25.14 + /* as long as user-select isn't widely spread, we throw the bomb */ 25.15 + -webkit-user-select: none; 25.16 + -moz-user-select: none; 25.17 + -ms-user-select: none; 25.18 + user-select: none; 25.19 + display: inline-block; 25.20 + font-style: italic; 25.21 + text-decoration: none; 25.22 + color: grey; 25.23 + } 25.24 + span.c2html-keyword { 25.25 + color: blue; 25.26 + } 25.27 + span.c2html-macroconst { 25.28 + color: cornflowerblue; 25.29 + } 25.30 + span.c2html-type { 25.31 + color: cornflowerblue; 25.32 + } 25.33 + span.c2html-directive { 25.34 + color: green; 25.35 + } 25.36 + span.c2html-string { 25.37 + color: darkorange; 25.38 + } 25.39 + span.c2html-comment { 25.40 + color: grey; 25.41 + } 25.42 + span.c2html-stdinclude { 25.43 + color: darkorange; 25.44 + } 25.45 + span.c2html-userinclude { 25.46 + color: darkorange; 25.47 + } 25.48 + a.c2html-userinclude { 25.49 + color: darkorange; 25.50 + text-decoration: underline; 25.51 + } 25.52 + </style> 25.53 + </head> 25.54 + <body> 25.55 + 25.56 +<div class="c2html-code"> 25.57 +<a class="c2html-lineno" name="l1" href="#l1">1 </a></body> 25.58 +<a class="c2html-lineno" name="l2" href="#l2">2 </a></html> 25.59 +<a class="c2html-lineno" name="l3" href="#l3">3 </a><!c 25.60 +<a class="c2html-lineno" name="l4" href="#l4">4 </a>pblock_free(q); 25.61 +<a class="c2html-lineno" name="l5" href="#l5">5 </a>!> 25.62 +<a class="c2html-lineno" name="l6" href="#l6">6 </a> 25.63 +</div> 25.64 + </body> 25.65 +</html> 25.66 +
26.1 --- a/test/header.html Thu Nov 10 18:44:48 2016 +0100 26.2 +++ b/test/header.html Mon Apr 24 20:54:38 2023 +0200 26.3 @@ -3,6 +3,10 @@ 26.4 <head> 26.5 <title>c2html</title> 26.6 <style type="text/css"> 26.7 + div.c2html-code { 26.8 + white-space: pre; 26.9 + font-family: monospace; 26.10 + } 26.11 a.c2html-lineno { 26.12 /* as long as user-select isn't widely spread, we throw the bomb */ 26.13 -webkit-user-select: none;
27.1 --- a/test/jheader.html Thu Nov 10 18:44:48 2016 +0100 27.2 +++ b/test/jheader.html Mon Apr 24 20:54:38 2023 +0200 27.3 @@ -3,6 +3,10 @@ 27.4 <head> 27.5 <title>c2html</title> 27.6 <style type="text/css"> 27.7 + div.c2html-code { 27.8 + white-space: pre; 27.9 + font-family: monospace; 27.10 + } 27.11 a.c2html-lineno { 27.12 /* as long as user-select isn't widely spread, we throw the bomb */ 27.13 -webkit-user-select: none;
28.1 --- a/test/v2-regression/bigtest.html Thu Nov 10 18:44:48 2016 +0100 28.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 28.3 @@ -1,855 +0,0 @@ 28.4 -<!DOCTYPE html> 28.5 -<html> 28.6 - <head> 28.7 - <title>c2html</title> 28.8 - <style type="text/css"> 28.9 - span.c2html-lineno a { 28.10 - font-style: italic; 28.11 - text-decoration: none; 28.12 - color: grey; 28.13 - } 28.14 - span.c2html-keyword { 28.15 - color: blue; 28.16 - } 28.17 - span.c2html-macroconst { 28.18 - color: cornflowerblue; 28.19 - } 28.20 - span.c2html-type { 28.21 - color: cornflowerblue; 28.22 - } 28.23 - span.c2html-directive { 28.24 - color: green; 28.25 - } 28.26 - span.c2html-string { 28.27 - color: darkorange; 28.28 - } 28.29 - span.c2html-comment { 28.30 - color: grey; 28.31 - } 28.32 - span.c2html-stdinclude { 28.33 - color: darkorange; 28.34 - } 28.35 - span.c2html-userinclude { 28.36 - color: darkorange; 28.37 - } 28.38 - a.c2html-userinclude { 28.39 - color: darkorange; 28.40 - text-decoration: underline; 28.41 - } 28.42 - </style> 28.43 - </head> 28.44 - <body> 28.45 - 28.46 -<pre> 28.47 -<span class="c2html-lineno"><a name="l1" href="#l1"> 1 </a></span> <span class="c2html-comment">/*</span> 28.48 -<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> 28.49 -<span class="c2html-lineno"><a name="l3" href="#l3"> 3 </a></span> <span class="c2html-comment"> *</span> 28.50 -<span class="c2html-lineno"><a name="l4" href="#l4"> 4 </a></span> <span class="c2html-comment"> * Copyright 2014 Mike Becker. All rights reserved.</span> 28.51 -<span class="c2html-lineno"><a name="l5" href="#l5"> 5 </a></span> <span class="c2html-comment"> *</span> 28.52 -<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> 28.53 -<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> 28.54 -<span class="c2html-lineno"><a name="l8" href="#l8"> 8 </a></span> <span class="c2html-comment"> *</span> 28.55 -<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> 28.56 -<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> 28.57 -<span class="c2html-lineno"><a name="l11" href="#l11"> 11 </a></span> <span class="c2html-comment"> *</span> 28.58 -<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> 28.59 -<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> 28.60 -<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> 28.61 -<span class="c2html-lineno"><a name="l15" href="#l15"> 15 </a></span> <span class="c2html-comment"> *</span> 28.62 -<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> 28.63 -<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> 28.64 -<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> 28.65 -<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> 28.66 -<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> 28.67 -<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> 28.68 -<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> 28.69 -<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> 28.70 -<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> 28.71 -<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> 28.72 -<span class="c2html-lineno"><a name="l26" href="#l26"> 26 </a></span> <span class="c2html-comment"> * POSSIBILITY OF SUCH DAMAGE.</span> 28.73 -<span class="c2html-lineno"><a name="l27" href="#l27"> 27 </a></span> <span class="c2html-comment"> *</span> 28.74 -<span class="c2html-lineno"><a name="l28" href="#l28"> 28 </a></span> <span class="c2html-comment"> */</span> 28.75 -<span class="c2html-lineno"><a name="l29" href="#l29"> 29 </a></span> 28.76 -<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> 28.77 -<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> 28.78 -<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> 28.79 -<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> 28.80 -<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> 28.81 -<span class="c2html-lineno"><a name="l35" href="#l35"> 35 </a></span> 28.82 -<span class="c2html-lineno"><a name="l36" href="#l36"> 36 </a></span> <span class="c2html-keyword">static</span> GameState gamestate_copy_sim(GameState *gamestate) { 28.83 -<span class="c2html-lineno"><a name="l37" href="#l37"> 37 </a></span> GameState simulation = *gamestate; 28.84 -<span class="c2html-lineno"><a name="l38" href="#l38"> 38 </a></span> <span class="c2html-keyword">if</span> (simulation.lastmove) { 28.85 -<span class="c2html-lineno"><a name="l39" href="#l39"> 39 </a></span> MoveList *lastmovecopy = malloc(<span class="c2html-keyword">sizeof</span>(MoveList)); 28.86 -<span class="c2html-lineno"><a name="l40" href="#l40"> 40 </a></span> *lastmovecopy = *(simulation.lastmove); 28.87 -<span class="c2html-lineno"><a name="l41" href="#l41"> 41 </a></span> simulation.movelist = simulation.lastmove = lastmovecopy; 28.88 -<span class="c2html-lineno"><a name="l42" href="#l42"> 42 </a></span> } 28.89 -<span class="c2html-lineno"><a name="l43" href="#l43"> 43 </a></span> 28.90 -<span class="c2html-lineno"><a name="l44" href="#l44"> 44 </a></span> <span class="c2html-keyword">return</span> simulation; 28.91 -<span class="c2html-lineno"><a name="l45" href="#l45"> 45 </a></span> } 28.92 -<span class="c2html-lineno"><a name="l46" href="#l46"> 46 </a></span> 28.93 -<span class="c2html-lineno"><a name="l47" href="#l47"> 47 </a></span> <span class="c2html-keyword">void</span> gamestate_init(GameState *gamestate) { 28.94 -<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)); 28.95 -<span class="c2html-lineno"><a name="l49" href="#l49"> 49 </a></span> 28.96 -<span class="c2html-lineno"><a name="l50" href="#l50"> 50 </a></span> Board initboard = { 28.97 -<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>}, 28.98 -<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>}, 28.99 -<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>}, 28.100 -<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>}, 28.101 -<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>}, 28.102 -<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>}, 28.103 -<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>}, 28.104 -<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>} 28.105 -<span class="c2html-lineno"><a name="l59" href="#l59"> 59 </a></span> }; 28.106 -<span class="c2html-lineno"><a name="l60" href="#l60"> 60 </a></span> memcpy(gamestate->board, initboard, <span class="c2html-keyword">sizeof</span>(Board)); 28.107 -<span class="c2html-lineno"><a name="l61" href="#l61"> 61 </a></span> } 28.108 -<span class="c2html-lineno"><a name="l62" href="#l62"> 62 </a></span> 28.109 -<span class="c2html-lineno"><a name="l63" href="#l63"> 63 </a></span> <span class="c2html-keyword">void</span> gamestate_cleanup(GameState *gamestate) { 28.110 -<span class="c2html-lineno"><a name="l64" href="#l64"> 64 </a></span> MoveList *elem; 28.111 -<span class="c2html-lineno"><a name="l65" href="#l65"> 65 </a></span> elem = gamestate->movelist; 28.112 -<span class="c2html-lineno"><a name="l66" href="#l66"> 66 </a></span> <span class="c2html-keyword">while</span> (elem) { 28.113 -<span class="c2html-lineno"><a name="l67" href="#l67"> 67 </a></span> MoveList *cur = elem; 28.114 -<span class="c2html-lineno"><a name="l68" href="#l68"> 68 </a></span> elem = elem->next; 28.115 -<span class="c2html-lineno"><a name="l69" href="#l69"> 69 </a></span> free(cur); 28.116 -<span class="c2html-lineno"><a name="l70" href="#l70"> 70 </a></span> }; 28.117 -<span class="c2html-lineno"><a name="l71" href="#l71"> 71 </a></span> } 28.118 -<span class="c2html-lineno"><a name="l72" href="#l72"> 72 </a></span> 28.119 -<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> 28.120 -<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) { 28.121 -<span class="c2html-lineno"><a name="l75" href="#l75"> 75 </a></span> <span class="c2html-keyword">char</span> *string = move->string; 28.122 -<span class="c2html-lineno"><a name="l76" href="#l76"> 76 </a></span> 28.123 -<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> 28.124 -<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>); 28.125 -<span class="c2html-lineno"><a name="l79" href="#l79"> 79 </a></span> 28.126 -<span class="c2html-lineno"><a name="l80" href="#l80"> 80 </a></span> <span class="c2html-comment">/* special formats for castling */</span> 28.127 -<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> && 28.128 -<span class="c2html-lineno"><a name="l82" href="#l82"> 82 </a></span> abs(move->tofile-move->fromfile) == <span class="c2html-macroconst">2</span>) { 28.129 -<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>)) { 28.130 -<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>); 28.131 -<span class="c2html-lineno"><a name="l85" href="#l85"> 85 </a></span> } <span class="c2html-keyword">else</span> { 28.132 -<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>); 28.133 -<span class="c2html-lineno"><a name="l87" href="#l87"> 87 </a></span> } 28.134 -<span class="c2html-lineno"><a name="l88" href="#l88"> 88 </a></span> } 28.135 -<span class="c2html-lineno"><a name="l89" href="#l89"> 89 </a></span> 28.136 -<span class="c2html-lineno"><a name="l90" href="#l90"> 90 </a></span> <span class="c2html-comment">/* start by notating the piece character */</span> 28.137 -<span class="c2html-lineno"><a name="l91" href="#l91"> 91 </a></span> string[<span class="c2html-macroconst">0</span>] = getpiecechr(move->piece); 28.138 -<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>; 28.139 -<span class="c2html-lineno"><a name="l93" href="#l93"> 93 </a></span> 28.140 -<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> 28.141 -<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>; 28.142 -<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>) { 28.143 -<span class="c2html-lineno"><a name="l97" href="#l97"> 97 </a></span> <span class="c2html-keyword">if</span> (move->capture) { 28.144 -<span class="c2html-lineno"><a name="l98" href="#l98"> 98 </a></span> string[idx++] = filechr(move->fromfile); 28.145 -<span class="c2html-lineno"><a name="l99" href="#l99"> 99 </a></span> } 28.146 -<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>) { 28.147 -<span class="c2html-lineno"><a name="l101" href="#l101">101 </a></span> Move threats[<span class="c2html-macroconst">16</span>]; 28.148 -<span class="c2html-lineno"><a name="l102" href="#l102">102 </a></span> <span class="c2html-type">uint8_t</span> threatcount; 28.149 -<span class="c2html-lineno"><a name="l103" href="#l103">103 </a></span> get_real_threats(gamestate, move->torow, move->tofile, 28.150 -<span class="c2html-lineno"><a name="l104" href="#l104">104 </a></span> move->piece&<span class="c2html-macroconst">COLOR_MASK</span>, threats, &threatcount); 28.151 -<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>) { 28.152 -<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>; 28.153 -<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++) { 28.154 -<span class="c2html-lineno"><a name="l108" href="#l108">108 </a></span> <span class="c2html-keyword">if</span> (threats[i].fromrow == move->fromrow) { 28.155 -<span class="c2html-lineno"><a name="l109" href="#l109">109 </a></span> ambrows++; 28.156 -<span class="c2html-lineno"><a name="l110" href="#l110">110 </a></span> } 28.157 -<span class="c2html-lineno"><a name="l111" href="#l111">111 </a></span> <span class="c2html-keyword">if</span> (threats[i].fromfile == move->fromfile) { 28.158 -<span class="c2html-lineno"><a name="l112" href="#l112">112 </a></span> ambfiles++; 28.159 -<span class="c2html-lineno"><a name="l113" href="#l113">113 </a></span> } 28.160 -<span class="c2html-lineno"><a name="l114" href="#l114">114 </a></span> } 28.161 -<span class="c2html-lineno"><a name="l115" href="#l115">115 </a></span> <span class="c2html-comment">/* ambiguous row, name file */</span> 28.162 -<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>) { 28.163 -<span class="c2html-lineno"><a name="l117" href="#l117">117 </a></span> string[idx++] = filechr(move->fromfile); 28.164 -<span class="c2html-lineno"><a name="l118" href="#l118">118 </a></span> } 28.165 -<span class="c2html-lineno"><a name="l119" href="#l119">119 </a></span> <span class="c2html-comment">/* ambiguous file, name row */</span> 28.166 -<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>) { 28.167 -<span class="c2html-lineno"><a name="l121" href="#l121">121 </a></span> string[idx++] = filechr(move->fromrow); 28.168 -<span class="c2html-lineno"><a name="l122" href="#l122">122 </a></span> } 28.169 -<span class="c2html-lineno"><a name="l123" href="#l123">123 </a></span> } 28.170 -<span class="c2html-lineno"><a name="l124" href="#l124">124 </a></span> } 28.171 -<span class="c2html-lineno"><a name="l125" href="#l125">125 </a></span> 28.172 -<span class="c2html-lineno"><a name="l126" href="#l126">126 </a></span> <span class="c2html-comment">/* capturing? */</span> 28.173 -<span class="c2html-lineno"><a name="l127" href="#l127">127 </a></span> <span class="c2html-keyword">if</span> (move->capture) { 28.174 -<span class="c2html-lineno"><a name="l128" href="#l128">128 </a></span> string[idx++] = <span class="c2html-string">'x'</span>; 28.175 -<span class="c2html-lineno"><a name="l129" href="#l129">129 </a></span> } 28.176 -<span class="c2html-lineno"><a name="l130" href="#l130">130 </a></span> 28.177 -<span class="c2html-lineno"><a name="l131" href="#l131">131 </a></span> <span class="c2html-comment">/* destination */</span> 28.178 -<span class="c2html-lineno"><a name="l132" href="#l132">132 </a></span> string[idx++] = filechr(move->tofile); 28.179 -<span class="c2html-lineno"><a name="l133" href="#l133">133 </a></span> string[idx++] = rowchr(move->torow); 28.180 -<span class="c2html-lineno"><a name="l134" href="#l134">134 </a></span> 28.181 -<span class="c2html-lineno"><a name="l135" href="#l135">135 </a></span> <span class="c2html-comment">/* promotion? */</span> 28.182 -<span class="c2html-lineno"><a name="l136" href="#l136">136 </a></span> <span class="c2html-keyword">if</span> (move->promotion) { 28.183 -<span class="c2html-lineno"><a name="l137" href="#l137">137 </a></span> string[idx++] = <span class="c2html-string">'='</span>; 28.184 -<span class="c2html-lineno"><a name="l138" href="#l138">138 </a></span> string[idx++] = getpiecechr(move->promotion); 28.185 -<span class="c2html-lineno"><a name="l139" href="#l139">139 </a></span> } 28.186 -<span class="c2html-lineno"><a name="l140" href="#l140">140 </a></span> 28.187 -<span class="c2html-lineno"><a name="l141" href="#l141">141 </a></span> <span class="c2html-comment">/* check? */</span> 28.188 -<span class="c2html-lineno"><a name="l142" href="#l142">142 </a></span> <span class="c2html-keyword">if</span> (move->check) { 28.189 -<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> 28.190 -<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>; 28.191 -<span class="c2html-lineno"><a name="l145" href="#l145">145 </a></span> } 28.192 -<span class="c2html-lineno"><a name="l146" href="#l146">146 </a></span> } 28.193 -<span class="c2html-lineno"><a name="l147" href="#l147">147 </a></span> 28.194 -<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) { 28.195 -<span class="c2html-lineno"><a name="l149" href="#l149">149 </a></span> MoveList *elem = malloc(<span class="c2html-keyword">sizeof</span>(MoveList)); 28.196 -<span class="c2html-lineno"><a name="l150" href="#l150">150 </a></span> elem->next = <span class="c2html-macroconst">NULL</span>; 28.197 -<span class="c2html-lineno"><a name="l151" href="#l151">151 </a></span> elem->move = *move; 28.198 -<span class="c2html-lineno"><a name="l152" href="#l152">152 </a></span> 28.199 -<span class="c2html-lineno"><a name="l153" href="#l153">153 </a></span> <span class="c2html-keyword">struct</span> timeval curtimestamp; 28.200 -<span class="c2html-lineno"><a name="l154" href="#l154">154 </a></span> gettimeofday(&curtimestamp, <span class="c2html-macroconst">NULL</span>); 28.201 -<span class="c2html-lineno"><a name="l155" href="#l155">155 </a></span> elem->move.timestamp.tv_sec = curtimestamp.tv_sec; 28.202 -<span class="c2html-lineno"><a name="l156" href="#l156">156 </a></span> elem->move.timestamp.tv_usec = curtimestamp.tv_usec; 28.203 -<span class="c2html-lineno"><a name="l157" href="#l157">157 </a></span> 28.204 -<span class="c2html-lineno"><a name="l158" href="#l158">158 </a></span> <span class="c2html-keyword">if</span> (gamestate->lastmove) { 28.205 -<span class="c2html-lineno"><a name="l159" href="#l159">159 </a></span> <span class="c2html-keyword">struct</span> movetimeval *lasttstamp = &(gamestate->lastmove->move.timestamp); 28.206 -<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; 28.207 -<span class="c2html-lineno"><a name="l161" href="#l161">161 </a></span> <span class="c2html-type">suseconds_t</span> micros; 28.208 -<span class="c2html-lineno"><a name="l162" href="#l162">162 </a></span> <span class="c2html-keyword">if</span> (curtimestamp.tv_usec < lasttstamp->tv_usec) { 28.209 -<span class="c2html-lineno"><a name="l163" href="#l163">163 </a></span> micros = 1e6L-(lasttstamp->tv_usec - curtimestamp.tv_usec); 28.210 -<span class="c2html-lineno"><a name="l164" href="#l164">164 </a></span> sec--; 28.211 -<span class="c2html-lineno"><a name="l165" href="#l165">165 </a></span> } <span class="c2html-keyword">else</span> { 28.212 -<span class="c2html-lineno"><a name="l166" href="#l166">166 </a></span> micros = curtimestamp.tv_usec - lasttstamp->tv_usec; 28.213 -<span class="c2html-lineno"><a name="l167" href="#l167">167 </a></span> } 28.214 -<span class="c2html-lineno"><a name="l168" href="#l168">168 </a></span> 28.215 -<span class="c2html-lineno"><a name="l169" href="#l169">169 </a></span> elem->move.movetime.tv_sec = sec; 28.216 -<span class="c2html-lineno"><a name="l170" href="#l170">170 </a></span> elem->move.movetime.tv_usec = micros; 28.217 -<span class="c2html-lineno"><a name="l171" href="#l171">171 </a></span> 28.218 -<span class="c2html-lineno"><a name="l172" href="#l172">172 </a></span> gamestate->lastmove->next = elem; 28.219 -<span class="c2html-lineno"><a name="l173" href="#l173">173 </a></span> gamestate->lastmove = elem; 28.220 -<span class="c2html-lineno"><a name="l174" href="#l174">174 </a></span> } <span class="c2html-keyword">else</span> { 28.221 -<span class="c2html-lineno"><a name="l175" href="#l175">175 </a></span> elem->move.movetime.tv_usec = <span class="c2html-macroconst">0</span>; 28.222 -<span class="c2html-lineno"><a name="l176" href="#l176">176 </a></span> elem->move.movetime.tv_sec = <span class="c2html-macroconst">0</span>; 28.223 -<span class="c2html-lineno"><a name="l177" href="#l177">177 </a></span> gamestate->movelist = gamestate->lastmove = elem; 28.224 -<span class="c2html-lineno"><a name="l178" href="#l178">178 </a></span> } 28.225 -<span class="c2html-lineno"><a name="l179" href="#l179">179 </a></span> } 28.226 -<span class="c2html-lineno"><a name="l180" href="#l180">180 </a></span> 28.227 -<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) { 28.228 -<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>) { 28.229 -<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>; 28.230 -<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>; 28.231 -<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>; 28.232 -<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>; 28.233 -<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>; 28.234 -<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>; 28.235 -<span class="c2html-lineno"><a name="l189" href="#l189">189 </a></span> } 28.236 -<span class="c2html-lineno"><a name="l190" href="#l190">190 </a></span> } 28.237 -<span class="c2html-lineno"><a name="l191" href="#l191">191 </a></span> 28.238 -<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) { 28.239 -<span class="c2html-lineno"><a name="l193" href="#l193">193 </a></span> <span class="c2html-keyword">switch</span> (c) { 28.240 -<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>; 28.241 -<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>; 28.242 -<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>; 28.243 -<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>; 28.244 -<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>; 28.245 -<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>; 28.246 -<span class="c2html-lineno"><a name="l200" href="#l200">200 </a></span> } 28.247 -<span class="c2html-lineno"><a name="l201" href="#l201">201 </a></span> } 28.248 -<span class="c2html-lineno"><a name="l202" href="#l202">202 </a></span> 28.249 -<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) { 28.250 -<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>; 28.251 -<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>; 28.252 -<span class="c2html-lineno"><a name="l206" href="#l206">206 </a></span> 28.253 -<span class="c2html-lineno"><a name="l207" href="#l207">207 </a></span> <span class="c2html-comment">/* en passant capture */</span> 28.254 -<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> && 28.255 -<span class="c2html-lineno"><a name="l209" href="#l209">209 </a></span> mdst(gamestate->board, move) == <span class="c2html-macroconst">0</span>) { 28.256 -<span class="c2html-lineno"><a name="l210" href="#l210">210 </a></span> gamestate->board[move->fromrow][move->tofile] = <span class="c2html-macroconst">0</span>; 28.257 -<span class="c2html-lineno"><a name="l211" href="#l211">211 </a></span> } 28.258 -<span class="c2html-lineno"><a name="l212" href="#l212">212 </a></span> 28.259 -<span class="c2html-lineno"><a name="l213" href="#l213">213 </a></span> <span class="c2html-comment">/* remove old en passant threats */</span> 28.260 -<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++) { 28.261 -<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>; 28.262 -<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>; 28.263 -<span class="c2html-lineno"><a name="l217" href="#l217">217 </a></span> } 28.264 -<span class="c2html-lineno"><a name="l218" href="#l218">218 </a></span> 28.265 -<span class="c2html-lineno"><a name="l219" href="#l219">219 </a></span> <span class="c2html-comment">/* add new en passant threat */</span> 28.266 -<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> && ( 28.267 -<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>) || 28.268 -<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>))) { 28.269 -<span class="c2html-lineno"><a name="l223" href="#l223">223 </a></span> move->piece |= <span class="c2html-macroconst">ENPASSANT_THREAT</span>; 28.270 -<span class="c2html-lineno"><a name="l224" href="#l224">224 </a></span> } 28.271 -<span class="c2html-lineno"><a name="l225" href="#l225">225 </a></span> 28.272 -<span class="c2html-lineno"><a name="l226" href="#l226">226 </a></span> <span class="c2html-comment">/* move (and maybe capture or promote) */</span> 28.273 -<span class="c2html-lineno"><a name="l227" href="#l227">227 </a></span> msrc(gamestate->board, move) = <span class="c2html-macroconst">0</span>; 28.274 -<span class="c2html-lineno"><a name="l228" href="#l228">228 </a></span> <span class="c2html-keyword">if</span> (move->promotion) { 28.275 -<span class="c2html-lineno"><a name="l229" href="#l229">229 </a></span> mdst(gamestate->board, move) = move->promotion; 28.276 -<span class="c2html-lineno"><a name="l230" href="#l230">230 </a></span> } <span class="c2html-keyword">else</span> { 28.277 -<span class="c2html-lineno"><a name="l231" href="#l231">231 </a></span> mdst(gamestate->board, move) = move->piece; 28.278 -<span class="c2html-lineno"><a name="l232" href="#l232">232 </a></span> } 28.279 -<span class="c2html-lineno"><a name="l233" href="#l233">233 </a></span> 28.280 -<span class="c2html-lineno"><a name="l234" href="#l234">234 </a></span> <span class="c2html-comment">/* castling */</span> 28.281 -<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>)) { 28.282 -<span class="c2html-lineno"><a name="l236" href="#l236">236 </a></span> 28.283 -<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>)) { 28.284 -<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>; 28.285 -<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>; 28.286 -<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>)) { 28.287 -<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>; 28.288 -<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>; 28.289 -<span class="c2html-lineno"><a name="l243" href="#l243">243 </a></span> } 28.290 -<span class="c2html-lineno"><a name="l244" href="#l244">244 </a></span> } 28.291 -<span class="c2html-lineno"><a name="l245" href="#l245">245 </a></span> 28.292 -<span class="c2html-lineno"><a name="l246" href="#l246">246 </a></span> <span class="c2html-keyword">if</span> (!simulate) { 28.293 -<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>]) { 28.294 -<span class="c2html-lineno"><a name="l248" href="#l248">248 </a></span> format_move(gamestate, move); 28.295 -<span class="c2html-lineno"><a name="l249" href="#l249">249 </a></span> } 28.296 -<span class="c2html-lineno"><a name="l250" href="#l250">250 </a></span> } 28.297 -<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> 28.298 -<span class="c2html-lineno"><a name="l252" href="#l252">252 </a></span> addmove(gamestate, move); 28.299 -<span class="c2html-lineno"><a name="l253" href="#l253">253 </a></span> } 28.300 -<span class="c2html-lineno"><a name="l254" href="#l254">254 </a></span> 28.301 -<span class="c2html-lineno"><a name="l255" href="#l255">255 </a></span> <span class="c2html-keyword">void</span> apply_move(GameState *gamestate, Move *move) { 28.302 -<span class="c2html-lineno"><a name="l256" href="#l256">256 </a></span> apply_move_impl(gamestate, move, <span class="c2html-macroconst">0</span>); 28.303 -<span class="c2html-lineno"><a name="l257" href="#l257">257 </a></span> } 28.304 -<span class="c2html-lineno"><a name="l258" href="#l258">258 </a></span> 28.305 -<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) { 28.306 -<span class="c2html-lineno"><a name="l260" href="#l260">260 </a></span> <span class="c2html-comment">/* validate indices (don't trust opponent) */</span> 28.307 -<span class="c2html-lineno"><a name="l261" href="#l261">261 </a></span> <span class="c2html-keyword">if</span> (!chkidx(move)) { 28.308 -<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>; 28.309 -<span class="c2html-lineno"><a name="l263" href="#l263">263 </a></span> } 28.310 -<span class="c2html-lineno"><a name="l264" href="#l264">264 </a></span> 28.311 -<span class="c2html-lineno"><a name="l265" href="#l265">265 </a></span> <span class="c2html-comment">/* must move */</span> 28.312 -<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) { 28.313 -<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>; 28.314 -<span class="c2html-lineno"><a name="l268" href="#l268">268 </a></span> } 28.315 -<span class="c2html-lineno"><a name="l269" href="#l269">269 </a></span> 28.316 -<span class="c2html-lineno"><a name="l270" href="#l270">270 </a></span> <span class="c2html-comment">/* does piece exist */</span> 28.317 -<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>)) 28.318 -<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>))) { 28.319 -<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>; 28.320 -<span class="c2html-lineno"><a name="l274" href="#l274">274 </a></span> } 28.321 -<span class="c2html-lineno"><a name="l275" href="#l275">275 </a></span> 28.322 -<span class="c2html-lineno"><a name="l276" href="#l276">276 </a></span> <span class="c2html-comment">/* can't capture own pieces */</span> 28.323 -<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>) 28.324 -<span class="c2html-lineno"><a name="l278" href="#l278">278 </a></span> == (move->piece & <span class="c2html-macroconst">COLOR_MASK</span>)) { 28.325 -<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>; 28.326 -<span class="c2html-lineno"><a name="l280" href="#l280">280 </a></span> } 28.327 -<span class="c2html-lineno"><a name="l281" href="#l281">281 </a></span> 28.328 -<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> 28.329 -<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) || 28.330 -<span class="c2html-lineno"><a name="l284" href="#l284">284 </a></span> (mdst(gamestate->board, move) != <span class="c2html-macroconst">0</span> && !move->capture)) { 28.331 -<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>; 28.332 -<span class="c2html-lineno"><a name="l286" href="#l286">286 </a></span> } 28.333 -<span class="c2html-lineno"><a name="l287" href="#l287">287 </a></span> 28.334 -<span class="c2html-lineno"><a name="l288" href="#l288">288 </a></span> <span class="c2html-comment">/* validate individual rules */</span> 28.335 -<span class="c2html-lineno"><a name="l289" href="#l289">289 </a></span> _Bool chkrules; 28.336 -<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>) { 28.337 -<span class="c2html-lineno"><a name="l291" href="#l291">291 </a></span> <span class="c2html-keyword">case</span> <span class="c2html-macroconst">PAWN</span>: 28.338 -<span class="c2html-lineno"><a name="l292" href="#l292">292 </a></span> chkrules = pawn_chkrules(gamestate, move) && 28.339 -<span class="c2html-lineno"><a name="l293" href="#l293">293 </a></span> !pawn_isblocked(gamestate, move); 28.340 -<span class="c2html-lineno"><a name="l294" href="#l294">294 </a></span> <span class="c2html-keyword">break</span>; 28.341 -<span class="c2html-lineno"><a name="l295" href="#l295">295 </a></span> <span class="c2html-keyword">case</span> <span class="c2html-macroconst">ROOK</span>: 28.342 -<span class="c2html-lineno"><a name="l296" href="#l296">296 </a></span> chkrules = rook_chkrules(move) && 28.343 -<span class="c2html-lineno"><a name="l297" href="#l297">297 </a></span> !rook_isblocked(gamestate, move); 28.344 -<span class="c2html-lineno"><a name="l298" href="#l298">298 </a></span> <span class="c2html-keyword">break</span>; 28.345 -<span class="c2html-lineno"><a name="l299" href="#l299">299 </a></span> <span class="c2html-keyword">case</span> <span class="c2html-macroconst">KNIGHT</span>: 28.346 -<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> 28.347 -<span class="c2html-lineno"><a name="l301" href="#l301">301 </a></span> <span class="c2html-keyword">break</span>; 28.348 -<span class="c2html-lineno"><a name="l302" href="#l302">302 </a></span> <span class="c2html-keyword">case</span> <span class="c2html-macroconst">BISHOP</span>: 28.349 -<span class="c2html-lineno"><a name="l303" href="#l303">303 </a></span> chkrules = bishop_chkrules(move) && 28.350 -<span class="c2html-lineno"><a name="l304" href="#l304">304 </a></span> !bishop_isblocked(gamestate, move); 28.351 -<span class="c2html-lineno"><a name="l305" href="#l305">305 </a></span> <span class="c2html-keyword">break</span>; 28.352 -<span class="c2html-lineno"><a name="l306" href="#l306">306 </a></span> <span class="c2html-keyword">case</span> <span class="c2html-macroconst">QUEEN</span>: 28.353 -<span class="c2html-lineno"><a name="l307" href="#l307">307 </a></span> chkrules = queen_chkrules(move) && 28.354 -<span class="c2html-lineno"><a name="l308" href="#l308">308 </a></span> !queen_isblocked(gamestate, move); 28.355 -<span class="c2html-lineno"><a name="l309" href="#l309">309 </a></span> <span class="c2html-keyword">break</span>; 28.356 -<span class="c2html-lineno"><a name="l310" href="#l310">310 </a></span> <span class="c2html-keyword">case</span> <span class="c2html-macroconst">KING</span>: 28.357 -<span class="c2html-lineno"><a name="l311" href="#l311">311 </a></span> chkrules = king_chkrules(gamestate, move) && 28.358 -<span class="c2html-lineno"><a name="l312" href="#l312">312 </a></span> !king_isblocked(gamestate, move); 28.359 -<span class="c2html-lineno"><a name="l313" href="#l313">313 </a></span> <span class="c2html-keyword">break</span>; 28.360 -<span class="c2html-lineno"><a name="l314" href="#l314">314 </a></span> <span class="c2html-keyword">default</span>: 28.361 -<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>; 28.362 -<span class="c2html-lineno"><a name="l316" href="#l316">316 </a></span> } 28.363 -<span class="c2html-lineno"><a name="l317" href="#l317">317 </a></span> 28.364 -<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>; 28.365 -<span class="c2html-lineno"><a name="l319" href="#l319">319 </a></span> } 28.366 -<span class="c2html-lineno"><a name="l320" href="#l320">320 </a></span> 28.367 -<span class="c2html-lineno"><a name="l321" href="#l321">321 </a></span> <span class="c2html-keyword">int</span> validate_move(GameState *gamestate, Move *move) { 28.368 -<span class="c2html-lineno"><a name="l322" href="#l322">322 </a></span> 28.369 -<span class="c2html-lineno"><a name="l323" href="#l323">323 </a></span> <span class="c2html-keyword">int</span> result = validate_move_rules(gamestate, move); 28.370 -<span class="c2html-lineno"><a name="l324" href="#l324">324 </a></span> 28.371 -<span class="c2html-lineno"><a name="l325" href="#l325">325 </a></span> <span class="c2html-comment">/* cancel processing to save resources */</span> 28.372 -<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>) { 28.373 -<span class="c2html-lineno"><a name="l327" href="#l327">327 </a></span> <span class="c2html-keyword">return</span> result; 28.374 -<span class="c2html-lineno"><a name="l328" href="#l328">328 </a></span> } 28.375 -<span class="c2html-lineno"><a name="l329" href="#l329">329 </a></span> 28.376 -<span class="c2html-lineno"><a name="l330" href="#l330">330 </a></span> <span class="c2html-comment">/* find kings for check validation */</span> 28.377 -<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>); 28.378 -<span class="c2html-lineno"><a name="l332" href="#l332">332 </a></span> 28.379 -<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>; 28.380 -<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++) { 28.381 -<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++) { 28.382 -<span class="c2html-lineno"><a name="l336" href="#l336">336 </a></span> <span class="c2html-keyword">if</span> (gamestate->board[row][file] == 28.383 -<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>)) { 28.384 -<span class="c2html-lineno"><a name="l338" href="#l338">338 </a></span> mykingfile = file; 28.385 -<span class="c2html-lineno"><a name="l339" href="#l339">339 </a></span> mykingrow = row; 28.386 -<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] == 28.387 -<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>)) { 28.388 -<span class="c2html-lineno"><a name="l342" href="#l342">342 </a></span> opkingfile = file; 28.389 -<span class="c2html-lineno"><a name="l343" href="#l343">343 </a></span> opkingrow = row; 28.390 -<span class="c2html-lineno"><a name="l344" href="#l344">344 </a></span> } 28.391 -<span class="c2html-lineno"><a name="l345" href="#l345">345 </a></span> } 28.392 -<span class="c2html-lineno"><a name="l346" href="#l346">346 </a></span> } 28.393 -<span class="c2html-lineno"><a name="l347" href="#l347">347 </a></span> 28.394 -<span class="c2html-lineno"><a name="l348" href="#l348">348 </a></span> <span class="c2html-comment">/* simulate move for check validation */</span> 28.395 -<span class="c2html-lineno"><a name="l349" href="#l349">349 </a></span> GameState simulation = gamestate_copy_sim(gamestate); 28.396 -<span class="c2html-lineno"><a name="l350" href="#l350">350 </a></span> Move simmove = *move; 28.397 -<span class="c2html-lineno"><a name="l351" href="#l351">351 </a></span> apply_move_impl(&simulation, &simmove, <span class="c2html-macroconst">1</span>); 28.398 -<span class="c2html-lineno"><a name="l352" href="#l352">352 </a></span> 28.399 -<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> 28.400 -<span class="c2html-lineno"><a name="l354" href="#l354">354 </a></span> <span class="c2html-keyword">if</span> (is_covered(&simulation, mykingrow, mykingfile, 28.401 -<span class="c2html-lineno"><a name="l355" href="#l355">355 </a></span> opponent_color(piececolor))) { 28.402 -<span class="c2html-lineno"><a name="l356" href="#l356">356 </a></span> 28.403 -<span class="c2html-lineno"><a name="l357" href="#l357">357 </a></span> gamestate_cleanup(&simulation); 28.404 -<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>) { 28.405 -<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>; 28.406 -<span class="c2html-lineno"><a name="l360" href="#l360">360 </a></span> } <span class="c2html-keyword">else</span> { 28.407 -<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> 28.408 -<span class="c2html-lineno"><a name="l362" href="#l362">362 </a></span> <span class="c2html-keyword">return</span> gamestate->lastmove->move.check ? 28.409 -<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>; 28.410 -<span class="c2html-lineno"><a name="l364" href="#l364">364 </a></span> } 28.411 -<span class="c2html-lineno"><a name="l365" href="#l365">365 </a></span> } 28.412 -<span class="c2html-lineno"><a name="l366" href="#l366">366 </a></span> 28.413 -<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> 28.414 -<span class="c2html-lineno"><a name="l368" href="#l368">368 </a></span> Move threats[<span class="c2html-macroconst">16</span>]; 28.415 -<span class="c2html-lineno"><a name="l369" href="#l369">369 </a></span> <span class="c2html-type">uint8_t</span> threatcount; 28.416 -<span class="c2html-lineno"><a name="l370" href="#l370">370 </a></span> move->check = get_threats(&simulation, opkingrow, opkingfile, 28.417 -<span class="c2html-lineno"><a name="l371" href="#l371">371 </a></span> piececolor, threats, &threatcount); 28.418 -<span class="c2html-lineno"><a name="l372" href="#l372">372 </a></span> 28.419 -<span class="c2html-lineno"><a name="l373" href="#l373">373 </a></span> <span class="c2html-keyword">if</span> (move->check) { 28.420 -<span class="c2html-lineno"><a name="l374" href="#l374">374 </a></span> <span class="c2html-comment">/* determine possible escape fields */</span> 28.421 -<span class="c2html-lineno"><a name="l375" href="#l375">375 </a></span> _Bool canescape = <span class="c2html-macroconst">0</span>; 28.422 -<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++) { 28.423 -<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++) { 28.424 -<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>) && 28.425 -<span class="c2html-lineno"><a name="l379" href="#l379">379 </a></span> isidx(opkingrow + dr) && isidx(opkingfile + df)) { 28.426 -<span class="c2html-lineno"><a name="l380" href="#l380">380 </a></span> 28.427 -<span class="c2html-lineno"><a name="l381" href="#l381">381 </a></span> <span class="c2html-comment">/* escape field neither blocked nor covered */</span> 28.428 -<span class="c2html-lineno"><a name="l382" href="#l382">382 </a></span> <span class="c2html-keyword">if</span> ((simulation.board[opkingrow + dr][opkingfile + df] 28.429 -<span class="c2html-lineno"><a name="l383" href="#l383">383 </a></span> & <span class="c2html-macroconst">COLOR_MASK</span>) != opponent_color(piececolor)) { 28.430 -<span class="c2html-lineno"><a name="l384" href="#l384">384 </a></span> canescape |= !is_covered(&simulation, 28.431 -<span class="c2html-lineno"><a name="l385" href="#l385">385 </a></span> opkingrow + dr, opkingfile + df, piececolor); 28.432 -<span class="c2html-lineno"><a name="l386" href="#l386">386 </a></span> } 28.433 -<span class="c2html-lineno"><a name="l387" href="#l387">387 </a></span> } 28.434 -<span class="c2html-lineno"><a name="l388" href="#l388">388 </a></span> } 28.435 -<span class="c2html-lineno"><a name="l389" href="#l389">389 </a></span> } 28.436 -<span class="c2html-lineno"><a name="l390" href="#l390">390 </a></span> <span class="c2html-comment">/* can't escape, can he capture? */</span> 28.437 -<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>) { 28.438 -<span class="c2html-lineno"><a name="l392" href="#l392">392 </a></span> canescape = is_attacked(&simulation, threats[<span class="c2html-macroconst">0</span>].fromrow, 28.439 -<span class="c2html-lineno"><a name="l393" href="#l393">393 </a></span> threats[<span class="c2html-macroconst">0</span>].fromfile, opponent_color(piececolor)); 28.440 -<span class="c2html-lineno"><a name="l394" href="#l394">394 </a></span> } 28.441 -<span class="c2html-lineno"><a name="l395" href="#l395">395 </a></span> 28.442 -<span class="c2html-lineno"><a name="l396" href="#l396">396 </a></span> <span class="c2html-comment">/* can't capture, can he block? */</span> 28.443 -<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>) { 28.444 -<span class="c2html-lineno"><a name="l398" href="#l398">398 </a></span> Move *threat = &(threats[<span class="c2html-macroconst">0</span>]); 28.445 -<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>; 28.446 -<span class="c2html-lineno"><a name="l400" href="#l400">400 </a></span> 28.447 -<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> 28.448 -<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> 28.449 -<span class="c2html-lineno"><a name="l403" href="#l403">403 </a></span> || threatpiece == <span class="c2html-macroconst">QUEEN</span>) { 28.450 -<span class="c2html-lineno"><a name="l404" href="#l404">404 </a></span> <span class="c2html-keyword">if</span> (threat->fromrow == threat->torow) { 28.451 -<span class="c2html-lineno"><a name="l405" href="#l405">405 </a></span> <span class="c2html-comment">/* rook aspect (on row) */</span> 28.452 -<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>; 28.453 -<span class="c2html-lineno"><a name="l407" href="#l407">407 </a></span> <span class="c2html-type">uint8_t</span> file = threat->fromfile; 28.454 -<span class="c2html-lineno"><a name="l408" href="#l408">408 </a></span> <span class="c2html-keyword">while</span> (!canescape && file != threat->tofile - d) { 28.455 -<span class="c2html-lineno"><a name="l409" href="#l409">409 </a></span> file += d; 28.456 -<span class="c2html-lineno"><a name="l410" href="#l410">410 </a></span> canescape |= is_protected(&simulation, 28.457 -<span class="c2html-lineno"><a name="l411" href="#l411">411 </a></span> threat->torow, file, opponent_color(piececolor)); 28.458 -<span class="c2html-lineno"><a name="l412" href="#l412">412 </a></span> } 28.459 -<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) { 28.460 -<span class="c2html-lineno"><a name="l414" href="#l414">414 </a></span> <span class="c2html-comment">/* rook aspect (on file) */</span> 28.461 -<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>; 28.462 -<span class="c2html-lineno"><a name="l416" href="#l416">416 </a></span> <span class="c2html-type">uint8_t</span> row = threat->fromrow; 28.463 -<span class="c2html-lineno"><a name="l417" href="#l417">417 </a></span> <span class="c2html-keyword">while</span> (!canescape && row != threat->torow - d) { 28.464 -<span class="c2html-lineno"><a name="l418" href="#l418">418 </a></span> row += d; 28.465 -<span class="c2html-lineno"><a name="l419" href="#l419">419 </a></span> canescape |= is_protected(&simulation, 28.466 -<span class="c2html-lineno"><a name="l420" href="#l420">420 </a></span> row, threat->tofile, opponent_color(piececolor)); 28.467 -<span class="c2html-lineno"><a name="l421" href="#l421">421 </a></span> } 28.468 -<span class="c2html-lineno"><a name="l422" href="#l422">422 </a></span> } <span class="c2html-keyword">else</span> { 28.469 -<span class="c2html-lineno"><a name="l423" href="#l423">423 </a></span> <span class="c2html-comment">/* bishop aspect */</span> 28.470 -<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>; 28.471 -<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>; 28.472 -<span class="c2html-lineno"><a name="l426" href="#l426">426 </a></span> 28.473 -<span class="c2html-lineno"><a name="l427" href="#l427">427 </a></span> <span class="c2html-type">uint8_t</span> row = threat->fromrow; 28.474 -<span class="c2html-lineno"><a name="l428" href="#l428">428 </a></span> <span class="c2html-type">uint8_t</span> file = threat->fromfile; 28.475 -<span class="c2html-lineno"><a name="l429" href="#l429">429 </a></span> <span class="c2html-keyword">while</span> (!canescape && file != threat->tofile - df 28.476 -<span class="c2html-lineno"><a name="l430" href="#l430">430 </a></span> && row != threat->torow - dr) { 28.477 -<span class="c2html-lineno"><a name="l431" href="#l431">431 </a></span> row += dr; 28.478 -<span class="c2html-lineno"><a name="l432" href="#l432">432 </a></span> file += df; 28.479 -<span class="c2html-lineno"><a name="l433" href="#l433">433 </a></span> canescape |= is_protected(&simulation, row, file, 28.480 -<span class="c2html-lineno"><a name="l434" href="#l434">434 </a></span> opponent_color(piececolor)); 28.481 -<span class="c2html-lineno"><a name="l435" href="#l435">435 </a></span> } 28.482 -<span class="c2html-lineno"><a name="l436" href="#l436">436 </a></span> } 28.483 -<span class="c2html-lineno"><a name="l437" href="#l437">437 </a></span> } 28.484 -<span class="c2html-lineno"><a name="l438" href="#l438">438 </a></span> } 28.485 -<span class="c2html-lineno"><a name="l439" href="#l439">439 </a></span> 28.486 -<span class="c2html-lineno"><a name="l440" href="#l440">440 </a></span> <span class="c2html-keyword">if</span> (!canescape) { 28.487 -<span class="c2html-lineno"><a name="l441" href="#l441">441 </a></span> gamestate->checkmate = <span class="c2html-macroconst">1</span>; 28.488 -<span class="c2html-lineno"><a name="l442" href="#l442">442 </a></span> } 28.489 -<span class="c2html-lineno"><a name="l443" href="#l443">443 </a></span> } 28.490 -<span class="c2html-lineno"><a name="l444" href="#l444">444 </a></span> 28.491 -<span class="c2html-lineno"><a name="l445" href="#l445">445 </a></span> gamestate_cleanup(&simulation); 28.492 -<span class="c2html-lineno"><a name="l446" href="#l446">446 </a></span> 28.493 -<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>; 28.494 -<span class="c2html-lineno"><a name="l448" href="#l448">448 </a></span> } 28.495 -<span class="c2html-lineno"><a name="l449" href="#l449">449 </a></span> 28.496 -<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, 28.497 -<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) { 28.498 -<span class="c2html-lineno"><a name="l452" href="#l452">452 </a></span> Move candidates[<span class="c2html-macroconst">32</span>]; 28.499 -<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>; 28.500 -<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++) { 28.501 -<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++) { 28.502 -<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) { 28.503 -<span class="c2html-lineno"><a name="l457" href="#l457">457 </a></span> <span class="c2html-comment">// non-capturing move</span> 28.504 -<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)); 28.505 -<span class="c2html-lineno"><a name="l459" href="#l459">459 </a></span> candidates[candidatecount].piece = gamestate->board[r][f]; 28.506 -<span class="c2html-lineno"><a name="l460" href="#l460">460 </a></span> candidates[candidatecount].fromrow = r; 28.507 -<span class="c2html-lineno"><a name="l461" href="#l461">461 </a></span> candidates[candidatecount].fromfile = f; 28.508 -<span class="c2html-lineno"><a name="l462" href="#l462">462 </a></span> candidates[candidatecount].torow = row; 28.509 -<span class="c2html-lineno"><a name="l463" href="#l463">463 </a></span> candidates[candidatecount].tofile = file; 28.510 -<span class="c2html-lineno"><a name="l464" href="#l464">464 </a></span> candidatecount++; 28.511 -<span class="c2html-lineno"><a name="l465" href="#l465">465 </a></span> 28.512 -<span class="c2html-lineno"><a name="l466" href="#l466">466 </a></span> <span class="c2html-comment">// capturing move</span> 28.513 -<span class="c2html-lineno"><a name="l467" href="#l467">467 </a></span> memcpy(&(candidates[candidatecount]), 28.514 -<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)); 28.515 -<span class="c2html-lineno"><a name="l469" href="#l469">469 </a></span> candidates[candidatecount].capture = <span class="c2html-macroconst">1</span>; 28.516 -<span class="c2html-lineno"><a name="l470" href="#l470">470 </a></span> candidatecount++; 28.517 -<span class="c2html-lineno"><a name="l471" href="#l471">471 </a></span> } 28.518 -<span class="c2html-lineno"><a name="l472" href="#l472">472 </a></span> } 28.519 -<span class="c2html-lineno"><a name="l473" href="#l473">473 </a></span> } 28.520 -<span class="c2html-lineno"><a name="l474" href="#l474">474 </a></span> 28.521 -<span class="c2html-lineno"><a name="l475" href="#l475">475 </a></span> <span class="c2html-keyword">if</span> (threatcount) { 28.522 -<span class="c2html-lineno"><a name="l476" href="#l476">476 </a></span> *threatcount = <span class="c2html-macroconst">0</span>; 28.523 -<span class="c2html-lineno"><a name="l477" href="#l477">477 </a></span> } 28.524 -<span class="c2html-lineno"><a name="l478" href="#l478">478 </a></span> 28.525 -<span class="c2html-lineno"><a name="l479" href="#l479">479 </a></span> 28.526 -<span class="c2html-lineno"><a name="l480" href="#l480">480 </a></span> _Bool result = <span class="c2html-macroconst">0</span>; 28.527 -<span class="c2html-lineno"><a name="l481" href="#l481">481 </a></span> 28.528 -<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++) { 28.529 -<span class="c2html-lineno"><a name="l483" href="#l483">483 </a></span> <span class="c2html-keyword">if</span> (validate_move_rules(gamestate, &(candidates[i])) 28.530 -<span class="c2html-lineno"><a name="l484" href="#l484">484 </a></span> == <span class="c2html-macroconst">VALID_MOVE_SEMANTICS</span>) { 28.531 -<span class="c2html-lineno"><a name="l485" href="#l485">485 </a></span> result = <span class="c2html-macroconst">1</span>; 28.532 -<span class="c2html-lineno"><a name="l486" href="#l486">486 </a></span> <span class="c2html-keyword">if</span> (threats && threatcount) { 28.533 -<span class="c2html-lineno"><a name="l487" href="#l487">487 </a></span> threats[(*threatcount)++] = candidates[i]; 28.534 -<span class="c2html-lineno"><a name="l488" href="#l488">488 </a></span> } 28.535 -<span class="c2html-lineno"><a name="l489" href="#l489">489 </a></span> } 28.536 -<span class="c2html-lineno"><a name="l490" href="#l490">490 </a></span> } 28.537 -<span class="c2html-lineno"><a name="l491" href="#l491">491 </a></span> 28.538 -<span class="c2html-lineno"><a name="l492" href="#l492">492 </a></span> <span class="c2html-keyword">return</span> result; 28.539 -<span class="c2html-lineno"><a name="l493" href="#l493">493 </a></span> } 28.540 -<span class="c2html-lineno"><a name="l494" href="#l494">494 </a></span> 28.541 -<span class="c2html-lineno"><a name="l495" href="#l495">495 </a></span> _Bool is_pinned(GameState *gamestate, Move *move) { 28.542 -<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>; 28.543 -<span class="c2html-lineno"><a name="l497" href="#l497">497 </a></span> 28.544 -<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>; 28.545 -<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++) { 28.546 -<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++) { 28.547 -<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>)) { 28.548 -<span class="c2html-lineno"><a name="l502" href="#l502">502 </a></span> kingfile = file; 28.549 -<span class="c2html-lineno"><a name="l503" href="#l503">503 </a></span> kingrow = row; 28.550 -<span class="c2html-lineno"><a name="l504" href="#l504">504 </a></span> } 28.551 -<span class="c2html-lineno"><a name="l505" href="#l505">505 </a></span> } 28.552 -<span class="c2html-lineno"><a name="l506" href="#l506">506 </a></span> } 28.553 -<span class="c2html-lineno"><a name="l507" href="#l507">507 </a></span> 28.554 -<span class="c2html-lineno"><a name="l508" href="#l508">508 </a></span> GameState simulation = gamestate_copy_sim(gamestate); 28.555 -<span class="c2html-lineno"><a name="l509" href="#l509">509 </a></span> Move simmove = *move; 28.556 -<span class="c2html-lineno"><a name="l510" href="#l510">510 </a></span> apply_move(&simulation, &simmove); 28.557 -<span class="c2html-lineno"><a name="l511" href="#l511">511 </a></span> _Bool covered = is_covered(&simulation, 28.558 -<span class="c2html-lineno"><a name="l512" href="#l512">512 </a></span> kingrow, kingfile, opponent_color(color)); 28.559 -<span class="c2html-lineno"><a name="l513" href="#l513">513 </a></span> gamestate_cleanup(&simulation); 28.560 -<span class="c2html-lineno"><a name="l514" href="#l514">514 </a></span> 28.561 -<span class="c2html-lineno"><a name="l515" href="#l515">515 </a></span> <span class="c2html-keyword">return</span> covered; 28.562 -<span class="c2html-lineno"><a name="l516" href="#l516">516 </a></span> } 28.563 -<span class="c2html-lineno"><a name="l517" href="#l517">517 </a></span> 28.564 -<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, 28.565 -<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) { 28.566 -<span class="c2html-lineno"><a name="l520" href="#l520">520 </a></span> 28.567 -<span class="c2html-lineno"><a name="l521" href="#l521">521 </a></span> <span class="c2html-keyword">if</span> (threatcount) { 28.568 -<span class="c2html-lineno"><a name="l522" href="#l522">522 </a></span> *threatcount = <span class="c2html-macroconst">0</span>; 28.569 -<span class="c2html-lineno"><a name="l523" href="#l523">523 </a></span> } 28.570 -<span class="c2html-lineno"><a name="l524" href="#l524">524 </a></span> 28.571 -<span class="c2html-lineno"><a name="l525" href="#l525">525 </a></span> Move candidates[<span class="c2html-macroconst">16</span>]; 28.572 -<span class="c2html-lineno"><a name="l526" href="#l526">526 </a></span> <span class="c2html-type">uint8_t</span> candidatecount; 28.573 -<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)) { 28.574 -<span class="c2html-lineno"><a name="l528" href="#l528">528 </a></span> 28.575 -<span class="c2html-lineno"><a name="l529" href="#l529">529 </a></span> _Bool result = <span class="c2html-macroconst">0</span>; 28.576 -<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>; 28.577 -<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++) { 28.578 -<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++) { 28.579 -<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>)) { 28.580 -<span class="c2html-lineno"><a name="l534" href="#l534">534 </a></span> kingfile = file; 28.581 -<span class="c2html-lineno"><a name="l535" href="#l535">535 </a></span> kingrow = row; 28.582 -<span class="c2html-lineno"><a name="l536" href="#l536">536 </a></span> } 28.583 -<span class="c2html-lineno"><a name="l537" href="#l537">537 </a></span> } 28.584 -<span class="c2html-lineno"><a name="l538" href="#l538">538 </a></span> } 28.585 -<span class="c2html-lineno"><a name="l539" href="#l539">539 </a></span> 28.586 -<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++) { 28.587 -<span class="c2html-lineno"><a name="l541" href="#l541">541 </a></span> GameState simulation = gamestate_copy_sim(gamestate); 28.588 -<span class="c2html-lineno"><a name="l542" href="#l542">542 </a></span> Move simmove = candidates[i]; 28.589 -<span class="c2html-lineno"><a name="l543" href="#l543">543 </a></span> apply_move(&simulation, &simmove); 28.590 -<span class="c2html-lineno"><a name="l544" href="#l544">544 </a></span> <span class="c2html-keyword">if</span> (!is_covered(&simulation, kingrow, kingfile, 28.591 -<span class="c2html-lineno"><a name="l545" href="#l545">545 </a></span> opponent_color(color))) { 28.592 -<span class="c2html-lineno"><a name="l546" href="#l546">546 </a></span> result = <span class="c2html-macroconst">1</span>; 28.593 -<span class="c2html-lineno"><a name="l547" href="#l547">547 </a></span> <span class="c2html-keyword">if</span> (threats && threatcount) { 28.594 -<span class="c2html-lineno"><a name="l548" href="#l548">548 </a></span> threats[(*threatcount)++] = candidates[i]; 28.595 -<span class="c2html-lineno"><a name="l549" href="#l549">549 </a></span> } 28.596 -<span class="c2html-lineno"><a name="l550" href="#l550">550 </a></span> } 28.597 -<span class="c2html-lineno"><a name="l551" href="#l551">551 </a></span> } 28.598 -<span class="c2html-lineno"><a name="l552" href="#l552">552 </a></span> 28.599 -<span class="c2html-lineno"><a name="l553" href="#l553">553 </a></span> <span class="c2html-keyword">return</span> result; 28.600 -<span class="c2html-lineno"><a name="l554" href="#l554">554 </a></span> } <span class="c2html-keyword">else</span> { 28.601 -<span class="c2html-lineno"><a name="l555" href="#l555">555 </a></span> <span class="c2html-keyword">return</span> <span class="c2html-macroconst">0</span>; 28.602 -<span class="c2html-lineno"><a name="l556" href="#l556">556 </a></span> } 28.603 -<span class="c2html-lineno"><a name="l557" href="#l557">557 </a></span> } 28.604 -<span class="c2html-lineno"><a name="l558" href="#l558">558 </a></span> 28.605 -<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) { 28.606 -<span class="c2html-lineno"><a name="l560" href="#l560">560 </a></span> 28.607 -<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>; 28.608 -<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>; 28.609 -<span class="c2html-lineno"><a name="l563" href="#l563">563 </a></span> 28.610 -<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>; 28.611 -<span class="c2html-lineno"><a name="l565" href="#l565">565 </a></span> <span class="c2html-type">uint8_t</span> threatcount; 28.612 -<span class="c2html-lineno"><a name="l566" href="#l566">566 </a></span> 28.613 -<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, 28.614 -<span class="c2html-lineno"><a name="l568" href="#l568">568 </a></span> threats, &threatcount)) { 28.615 -<span class="c2html-lineno"><a name="l569" href="#l569">569 </a></span> 28.616 -<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>; 28.617 -<span class="c2html-lineno"><a name="l571" href="#l571">571 </a></span> 28.618 -<span class="c2html-lineno"><a name="l572" href="#l572">572 </a></span> <span class="c2html-comment">// find threats for the specified position</span> 28.619 -<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++) { 28.620 -<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>)) 28.621 -<span class="c2html-lineno"><a name="l575" href="#l575">575 </a></span> == move->piece && 28.622 -<span class="c2html-lineno"><a name="l576" href="#l576">576 </a></span> (move->fromrow == <span class="c2html-macroconst">POS_UNSPECIFIED</span> || 28.623 -<span class="c2html-lineno"><a name="l577" href="#l577">577 </a></span> move->fromrow == threats[i].fromrow) && 28.624 -<span class="c2html-lineno"><a name="l578" href="#l578">578 </a></span> (move->fromfile == <span class="c2html-macroconst">POS_UNSPECIFIED</span> || 28.625 -<span class="c2html-lineno"><a name="l579" href="#l579">579 </a></span> move->fromfile == threats[i].fromfile)) { 28.626 -<span class="c2html-lineno"><a name="l580" href="#l580">580 </a></span> 28.627 -<span class="c2html-lineno"><a name="l581" href="#l581">581 </a></span> <span class="c2html-keyword">if</span> (threat) { 28.628 -<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>; 28.629 -<span class="c2html-lineno"><a name="l583" href="#l583">583 </a></span> } <span class="c2html-keyword">else</span> { 28.630 -<span class="c2html-lineno"><a name="l584" href="#l584">584 </a></span> <span class="c2html-comment">// found threat is no real threat</span> 28.631 -<span class="c2html-lineno"><a name="l585" href="#l585">585 </a></span> <span class="c2html-keyword">if</span> (is_pinned(gamestate, &(threats[i]))) { 28.632 -<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>; 28.633 -<span class="c2html-lineno"><a name="l587" href="#l587">587 </a></span> } <span class="c2html-keyword">else</span> { 28.634 -<span class="c2html-lineno"><a name="l588" href="#l588">588 </a></span> threat = &(threats[i]); 28.635 -<span class="c2html-lineno"><a name="l589" href="#l589">589 </a></span> } 28.636 -<span class="c2html-lineno"><a name="l590" href="#l590">590 </a></span> } 28.637 -<span class="c2html-lineno"><a name="l591" href="#l591">591 </a></span> } 28.638 -<span class="c2html-lineno"><a name="l592" href="#l592">592 </a></span> } 28.639 -<span class="c2html-lineno"><a name="l593" href="#l593">593 </a></span> 28.640 -<span class="c2html-lineno"><a name="l594" href="#l594">594 </a></span> <span class="c2html-comment">// can't threaten specified position</span> 28.641 -<span class="c2html-lineno"><a name="l595" href="#l595">595 </a></span> <span class="c2html-keyword">if</span> (!threat) { 28.642 -<span class="c2html-lineno"><a name="l596" href="#l596">596 </a></span> <span class="c2html-keyword">return</span> reason; 28.643 -<span class="c2html-lineno"><a name="l597" href="#l597">597 </a></span> } 28.644 -<span class="c2html-lineno"><a name="l598" href="#l598">598 </a></span> 28.645 -<span class="c2html-lineno"><a name="l599" href="#l599">599 </a></span> memcpy(move, threat, <span class="c2html-keyword">sizeof</span>(Move)); 28.646 -<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>; 28.647 -<span class="c2html-lineno"><a name="l601" href="#l601">601 </a></span> } <span class="c2html-keyword">else</span> { 28.648 -<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>; 28.649 -<span class="c2html-lineno"><a name="l603" href="#l603">603 </a></span> } 28.650 -<span class="c2html-lineno"><a name="l604" href="#l604">604 </a></span> } 28.651 -<span class="c2html-lineno"><a name="l605" href="#l605">605 </a></span> 28.652 -<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) { 28.653 -<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)); 28.654 -<span class="c2html-lineno"><a name="l608" href="#l608">608 </a></span> move->fromfile = <span class="c2html-macroconst">POS_UNSPECIFIED</span>; 28.655 -<span class="c2html-lineno"><a name="l609" href="#l609">609 </a></span> move->fromrow = <span class="c2html-macroconst">POS_UNSPECIFIED</span>; 28.656 -<span class="c2html-lineno"><a name="l610" href="#l610">610 </a></span> 28.657 -<span class="c2html-lineno"><a name="l611" href="#l611">611 </a></span> <span class="c2html-type">size_t</span> len = strlen(mstr); 28.658 -<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>) { 28.659 -<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>; 28.660 -<span class="c2html-lineno"><a name="l614" href="#l614">614 </a></span> } 28.661 -<span class="c2html-lineno"><a name="l615" href="#l615">615 </a></span> 28.662 -<span class="c2html-lineno"><a name="l616" href="#l616">616 </a></span> <span class="c2html-comment">/* evaluate check/checkmate flags */</span> 28.663 -<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>) { 28.664 -<span class="c2html-lineno"><a name="l618" href="#l618">618 </a></span> len--; mstr[len] = <span class="c2html-string">'\0'</span>; 28.665 -<span class="c2html-lineno"><a name="l619" href="#l619">619 </a></span> move->check = <span class="c2html-macroconst">1</span>; 28.666 -<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>) { 28.667 -<span class="c2html-lineno"><a name="l621" href="#l621">621 </a></span> len--; mstr[len] = <span class="c2html-string">'\0'</span>; 28.668 -<span class="c2html-lineno"><a name="l622" href="#l622">622 </a></span> <span class="c2html-comment">/* ignore - validation should set game state */</span> 28.669 -<span class="c2html-lineno"><a name="l623" href="#l623">623 </a></span> } 28.670 -<span class="c2html-lineno"><a name="l624" href="#l624">624 </a></span> 28.671 -<span class="c2html-lineno"><a name="l625" href="#l625">625 </a></span> <span class="c2html-comment">/* evaluate promotion */</span> 28.672 -<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>) { 28.673 -<span class="c2html-lineno"><a name="l627" href="#l627">627 </a></span> move->promotion = getpiece(mstr[len-<span class="c2html-macroconst">1</span>]); 28.674 -<span class="c2html-lineno"><a name="l628" href="#l628">628 </a></span> <span class="c2html-keyword">if</span> (!move->promotion) { 28.675 -<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>; 28.676 -<span class="c2html-lineno"><a name="l630" href="#l630">630 </a></span> } <span class="c2html-keyword">else</span> { 28.677 -<span class="c2html-lineno"><a name="l631" href="#l631">631 </a></span> move->promotion |= color; 28.678 -<span class="c2html-lineno"><a name="l632" href="#l632">632 </a></span> len -= <span class="c2html-macroconst">2</span>; 28.679 -<span class="c2html-lineno"><a name="l633" href="#l633">633 </a></span> mstr[len] = <span class="c2html-macroconst">0</span>; 28.680 -<span class="c2html-lineno"><a name="l634" href="#l634">634 </a></span> } 28.681 -<span class="c2html-lineno"><a name="l635" href="#l635">635 </a></span> } 28.682 -<span class="c2html-lineno"><a name="l636" href="#l636">636 </a></span> 28.683 -<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>) { 28.684 -<span class="c2html-lineno"><a name="l638" href="#l638">638 </a></span> <span class="c2html-comment">/* pawn move (e.g. "e4") */</span> 28.685 -<span class="c2html-lineno"><a name="l639" href="#l639">639 </a></span> move->piece = <span class="c2html-macroconst">PAWN</span>; 28.686 -<span class="c2html-lineno"><a name="l640" href="#l640">640 </a></span> move->tofile = fileidx(mstr[<span class="c2html-macroconst">0</span>]); 28.687 -<span class="c2html-lineno"><a name="l641" href="#l641">641 </a></span> move->torow = rowidx(mstr[<span class="c2html-macroconst">1</span>]); 28.688 -<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>) { 28.689 -<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>) { 28.690 -<span class="c2html-lineno"><a name="l644" href="#l644">644 </a></span> <span class="c2html-comment">/* king side castling */</span> 28.691 -<span class="c2html-lineno"><a name="l645" href="#l645">645 </a></span> move->piece = <span class="c2html-macroconst">KING</span>; 28.692 -<span class="c2html-lineno"><a name="l646" href="#l646">646 </a></span> move->fromfile = fileidx(<span class="c2html-string">'e'</span>); 28.693 -<span class="c2html-lineno"><a name="l647" href="#l647">647 </a></span> move->tofile = fileidx(<span class="c2html-string">'g'</span>); 28.694 -<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>; 28.695 -<span class="c2html-lineno"><a name="l649" href="#l649">649 </a></span> } <span class="c2html-keyword">else</span> { 28.696 -<span class="c2html-lineno"><a name="l650" href="#l650">650 </a></span> <span class="c2html-comment">/* move (e.g. "Nf3") */</span> 28.697 -<span class="c2html-lineno"><a name="l651" href="#l651">651 </a></span> move->piece = getpiece(mstr[<span class="c2html-macroconst">0</span>]); 28.698 -<span class="c2html-lineno"><a name="l652" href="#l652">652 </a></span> move->tofile = fileidx(mstr[<span class="c2html-macroconst">1</span>]); 28.699 -<span class="c2html-lineno"><a name="l653" href="#l653">653 </a></span> move->torow = rowidx(mstr[<span class="c2html-macroconst">2</span>]); 28.700 -<span class="c2html-lineno"><a name="l654" href="#l654">654 </a></span> } 28.701 -<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>) { 28.702 -<span class="c2html-lineno"><a name="l656" href="#l656">656 </a></span> move->piece = getpiece(mstr[<span class="c2html-macroconst">0</span>]); 28.703 -<span class="c2html-lineno"><a name="l657" href="#l657">657 </a></span> <span class="c2html-keyword">if</span> (!move->piece) { 28.704 -<span class="c2html-lineno"><a name="l658" href="#l658">658 </a></span> move->piece = <span class="c2html-macroconst">PAWN</span>; 28.705 -<span class="c2html-lineno"><a name="l659" href="#l659">659 </a></span> move->fromfile = fileidx(mstr[<span class="c2html-macroconst">0</span>]); 28.706 -<span class="c2html-lineno"><a name="l660" href="#l660">660 </a></span> } 28.707 -<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>) { 28.708 -<span class="c2html-lineno"><a name="l662" href="#l662">662 </a></span> <span class="c2html-comment">/* capture (e.g. "Nxf3", "dxe5") */</span> 28.709 -<span class="c2html-lineno"><a name="l663" href="#l663">663 </a></span> move->capture = <span class="c2html-macroconst">1</span>; 28.710 -<span class="c2html-lineno"><a name="l664" href="#l664">664 </a></span> } <span class="c2html-keyword">else</span> { 28.711 -<span class="c2html-lineno"><a name="l665" href="#l665">665 </a></span> <span class="c2html-comment">/* move (e.g. "Ndf3", "N2c3", "e2e4") */</span> 28.712 -<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>])) { 28.713 -<span class="c2html-lineno"><a name="l667" href="#l667">667 </a></span> move->fromfile = fileidx(mstr[<span class="c2html-macroconst">1</span>]); 28.714 -<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>) { 28.715 -<span class="c2html-lineno"><a name="l669" href="#l669">669 </a></span> move->piece = <span class="c2html-macroconst">0</span>; 28.716 -<span class="c2html-lineno"><a name="l670" href="#l670">670 </a></span> } 28.717 -<span class="c2html-lineno"><a name="l671" href="#l671">671 </a></span> } <span class="c2html-keyword">else</span> { 28.718 -<span class="c2html-lineno"><a name="l672" href="#l672">672 </a></span> move->fromrow = rowidx(mstr[<span class="c2html-macroconst">1</span>]); 28.719 -<span class="c2html-lineno"><a name="l673" href="#l673">673 </a></span> } 28.720 -<span class="c2html-lineno"><a name="l674" href="#l674">674 </a></span> } 28.721 -<span class="c2html-lineno"><a name="l675" href="#l675">675 </a></span> move->tofile = fileidx(mstr[<span class="c2html-macroconst">2</span>]); 28.722 -<span class="c2html-lineno"><a name="l676" href="#l676">676 </a></span> move->torow = rowidx(mstr[<span class="c2html-macroconst">3</span>]); 28.723 -<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>) { 28.724 -<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>) { 28.725 -<span class="c2html-lineno"><a name="l679" href="#l679">679 </a></span> <span class="c2html-comment">/* queen side castling "O-O-O" */</span> 28.726 -<span class="c2html-lineno"><a name="l680" href="#l680">680 </a></span> move->piece = <span class="c2html-macroconst">KING</span>; 28.727 -<span class="c2html-lineno"><a name="l681" href="#l681">681 </a></span> move->fromfile = fileidx(<span class="c2html-string">'e'</span>); 28.728 -<span class="c2html-lineno"><a name="l682" href="#l682">682 </a></span> move->tofile = fileidx(<span class="c2html-string">'c'</span>); 28.729 -<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>; 28.730 -<span class="c2html-lineno"><a name="l684" href="#l684">684 </a></span> } <span class="c2html-keyword">else</span> { 28.731 -<span class="c2html-lineno"><a name="l685" href="#l685">685 </a></span> move->piece = getpiece(mstr[<span class="c2html-macroconst">0</span>]); 28.732 -<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>) { 28.733 -<span class="c2html-lineno"><a name="l687" href="#l687">687 </a></span> move->capture = <span class="c2html-macroconst">1</span>; 28.734 -<span class="c2html-lineno"><a name="l688" href="#l688">688 </a></span> <span class="c2html-keyword">if</span> (move->piece) { 28.735 -<span class="c2html-lineno"><a name="l689" href="#l689">689 </a></span> <span class="c2html-comment">/* capture (e.g. "Ndxf3") */</span> 28.736 -<span class="c2html-lineno"><a name="l690" href="#l690">690 </a></span> move->fromfile = fileidx(mstr[<span class="c2html-macroconst">1</span>]); 28.737 -<span class="c2html-lineno"><a name="l691" href="#l691">691 </a></span> } <span class="c2html-keyword">else</span> { 28.738 -<span class="c2html-lineno"><a name="l692" href="#l692">692 </a></span> <span class="c2html-comment">/* long notation capture (e.g. "e5xf6") */</span> 28.739 -<span class="c2html-lineno"><a name="l693" href="#l693">693 </a></span> move->piece = <span class="c2html-macroconst">PAWN</span>; 28.740 -<span class="c2html-lineno"><a name="l694" href="#l694">694 </a></span> move->fromfile = fileidx(mstr[<span class="c2html-macroconst">0</span>]); 28.741 -<span class="c2html-lineno"><a name="l695" href="#l695">695 </a></span> move->fromrow = rowidx(mstr[<span class="c2html-macroconst">1</span>]); 28.742 -<span class="c2html-lineno"><a name="l696" href="#l696">696 </a></span> } 28.743 -<span class="c2html-lineno"><a name="l697" href="#l697">697 </a></span> } <span class="c2html-keyword">else</span> { 28.744 -<span class="c2html-lineno"><a name="l698" href="#l698">698 </a></span> <span class="c2html-comment">/* long notation move (e.g. "Nc5a4") */</span> 28.745 -<span class="c2html-lineno"><a name="l699" href="#l699">699 </a></span> move->fromfile = fileidx(mstr[<span class="c2html-macroconst">1</span>]); 28.746 -<span class="c2html-lineno"><a name="l700" href="#l700">700 </a></span> move->fromrow = rowidx(mstr[<span class="c2html-macroconst">2</span>]); 28.747 -<span class="c2html-lineno"><a name="l701" href="#l701">701 </a></span> } 28.748 -<span class="c2html-lineno"><a name="l702" href="#l702">702 </a></span> move->tofile = fileidx(mstr[<span class="c2html-macroconst">3</span>]); 28.749 -<span class="c2html-lineno"><a name="l703" href="#l703">703 </a></span> move->torow = rowidx(mstr[<span class="c2html-macroconst">4</span>]); 28.750 -<span class="c2html-lineno"><a name="l704" href="#l704">704 </a></span> } 28.751 -<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>) { 28.752 -<span class="c2html-lineno"><a name="l706" href="#l706">706 </a></span> <span class="c2html-comment">/* long notation capture (e.g. "Nc5xf3") */</span> 28.753 -<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>) { 28.754 -<span class="c2html-lineno"><a name="l708" href="#l708">708 </a></span> move->capture = <span class="c2html-macroconst">1</span>; 28.755 -<span class="c2html-lineno"><a name="l709" href="#l709">709 </a></span> move->piece = getpiece(mstr[<span class="c2html-macroconst">0</span>]); 28.756 -<span class="c2html-lineno"><a name="l710" href="#l710">710 </a></span> move->fromfile = fileidx(mstr[<span class="c2html-macroconst">1</span>]); 28.757 -<span class="c2html-lineno"><a name="l711" href="#l711">711 </a></span> move->fromrow = rowidx(mstr[<span class="c2html-macroconst">2</span>]); 28.758 -<span class="c2html-lineno"><a name="l712" href="#l712">712 </a></span> move->tofile = fileidx(mstr[<span class="c2html-macroconst">4</span>]); 28.759 -<span class="c2html-lineno"><a name="l713" href="#l713">713 </a></span> move->torow = rowidx(mstr[<span class="c2html-macroconst">5</span>]); 28.760 -<span class="c2html-lineno"><a name="l714" href="#l714">714 </a></span> } 28.761 -<span class="c2html-lineno"><a name="l715" href="#l715">715 </a></span> } 28.762 -<span class="c2html-lineno"><a name="l716" href="#l716">716 </a></span> 28.763 -<span class="c2html-lineno"><a name="l717" href="#l717">717 </a></span> 28.764 -<span class="c2html-lineno"><a name="l718" href="#l718">718 </a></span> <span class="c2html-keyword">if</span> (move->piece) { 28.765 -<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> 28.766 -<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>) 28.767 -<span class="c2html-lineno"><a name="l721" href="#l721">721 </a></span> && !move->promotion) { 28.768 -<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>; 28.769 -<span class="c2html-lineno"><a name="l723" href="#l723">723 </a></span> } 28.770 -<span class="c2html-lineno"><a name="l724" href="#l724">724 </a></span> 28.771 -<span class="c2html-lineno"><a name="l725" href="#l725">725 </a></span> move->piece |= color; 28.772 -<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> 28.773 -<span class="c2html-lineno"><a name="l727" href="#l727">727 </a></span> || move->fromrow == <span class="c2html-macroconst">POS_UNSPECIFIED</span>) { 28.774 -<span class="c2html-lineno"><a name="l728" href="#l728">728 </a></span> <span class="c2html-keyword">return</span> getlocation(gamestate, move); 28.775 -<span class="c2html-lineno"><a name="l729" href="#l729">729 </a></span> } <span class="c2html-keyword">else</span> { 28.776 -<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>; 28.777 -<span class="c2html-lineno"><a name="l731" href="#l731">731 </a></span> } 28.778 -<span class="c2html-lineno"><a name="l732" href="#l732">732 </a></span> } <span class="c2html-keyword">else</span> { 28.779 -<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>; 28.780 -<span class="c2html-lineno"><a name="l734" href="#l734">734 </a></span> } 28.781 -<span class="c2html-lineno"><a name="l735" href="#l735">735 </a></span> } 28.782 -<span class="c2html-lineno"><a name="l736" href="#l736">736 </a></span> 28.783 -<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, 28.784 -<span class="c2html-lineno"><a name="l738" href="#l738">738 </a></span> <span class="c2html-type">uint8_t</span> color) { 28.785 -<span class="c2html-lineno"><a name="l739" href="#l739">739 </a></span> 28.786 -<span class="c2html-lineno"><a name="l740" href="#l740">740 </a></span> Move threats[<span class="c2html-macroconst">16</span>]; 28.787 -<span class="c2html-lineno"><a name="l741" href="#l741">741 </a></span> <span class="c2html-type">uint8_t</span> threatcount; 28.788 -<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)) { 28.789 -<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++) { 28.790 -<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>)) { 28.791 -<span class="c2html-lineno"><a name="l745" href="#l745">745 </a></span> <span class="c2html-keyword">return</span> <span class="c2html-macroconst">1</span>; 28.792 -<span class="c2html-lineno"><a name="l746" href="#l746">746 </a></span> } 28.793 -<span class="c2html-lineno"><a name="l747" href="#l747">747 </a></span> } 28.794 -<span class="c2html-lineno"><a name="l748" href="#l748">748 </a></span> <span class="c2html-keyword">return</span> <span class="c2html-macroconst">0</span>; 28.795 -<span class="c2html-lineno"><a name="l749" href="#l749">749 </a></span> } <span class="c2html-keyword">else</span> { 28.796 -<span class="c2html-lineno"><a name="l750" href="#l750">750 </a></span> <span class="c2html-keyword">return</span> <span class="c2html-macroconst">0</span>; 28.797 -<span class="c2html-lineno"><a name="l751" href="#l751">751 </a></span> } 28.798 -<span class="c2html-lineno"><a name="l752" href="#l752">752 </a></span> } 28.799 -<span class="c2html-lineno"><a name="l753" href="#l753">753 </a></span> 28.800 -<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, 28.801 -<span class="c2html-lineno"><a name="l755" href="#l755">755 </a></span> <span class="c2html-type">uint8_t</span> color) { 28.802 -<span class="c2html-lineno"><a name="l756" href="#l756">756 </a></span> <span class="c2html-keyword">if</span> (!gameinfo->timecontrol) { 28.803 -<span class="c2html-lineno"><a name="l757" href="#l757">757 </a></span> <span class="c2html-keyword">return</span> <span class="c2html-macroconst">0</span>; 28.804 -<span class="c2html-lineno"><a name="l758" href="#l758">758 </a></span> } 28.805 -<span class="c2html-lineno"><a name="l759" href="#l759">759 </a></span> 28.806 -<span class="c2html-lineno"><a name="l760" href="#l760">760 </a></span> <span class="c2html-keyword">if</span> (gamestate->movelist) { 28.807 -<span class="c2html-lineno"><a name="l761" href="#l761">761 </a></span> <span class="c2html-type">uint16_t</span> time = gameinfo->time; 28.808 -<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>; 28.809 -<span class="c2html-lineno"><a name="l763" href="#l763">763 </a></span> 28.810 -<span class="c2html-lineno"><a name="l764" href="#l764">764 </a></span> MoveList *movelist = color == <span class="c2html-macroconst">WHITE</span> ? 28.811 -<span class="c2html-lineno"><a name="l765" href="#l765">765 </a></span> gamestate->movelist : gamestate->movelist->next; 28.812 -<span class="c2html-lineno"><a name="l766" href="#l766">766 </a></span> 28.813 -<span class="c2html-lineno"><a name="l767" href="#l767">767 </a></span> <span class="c2html-keyword">while</span> (movelist) { 28.814 -<span class="c2html-lineno"><a name="l768" href="#l768">768 </a></span> time += gameinfo->addtime; 28.815 -<span class="c2html-lineno"><a name="l769" href="#l769">769 </a></span> 28.816 -<span class="c2html-lineno"><a name="l770" href="#l770">770 </a></span> <span class="c2html-keyword">struct</span> movetimeval *movetime = &(movelist->move.movetime); 28.817 -<span class="c2html-lineno"><a name="l771" href="#l771">771 </a></span> <span class="c2html-keyword">if</span> (movetime->tv_sec >= time) { 28.818 -<span class="c2html-lineno"><a name="l772" href="#l772">772 </a></span> <span class="c2html-keyword">return</span> <span class="c2html-macroconst">0</span>; 28.819 -<span class="c2html-lineno"><a name="l773" href="#l773">773 </a></span> } 28.820 -<span class="c2html-lineno"><a name="l774" href="#l774">774 </a></span> 28.821 -<span class="c2html-lineno"><a name="l775" href="#l775">775 </a></span> time -= movetime->tv_sec; 28.822 -<span class="c2html-lineno"><a name="l776" href="#l776">776 </a></span> micros += movetime->tv_usec; 28.823 -<span class="c2html-lineno"><a name="l777" href="#l777">777 </a></span> 28.824 -<span class="c2html-lineno"><a name="l778" href="#l778">778 </a></span> movelist = movelist->next ? movelist->next->next : <span class="c2html-macroconst">NULL</span>; 28.825 -<span class="c2html-lineno"><a name="l779" href="#l779">779 </a></span> } 28.826 -<span class="c2html-lineno"><a name="l780" href="#l780">780 </a></span> 28.827 -<span class="c2html-lineno"><a name="l781" href="#l781">781 </a></span> <span class="c2html-type">time_t</span> sec; 28.828 -<span class="c2html-lineno"><a name="l782" href="#l782">782 </a></span> movelist = gamestate->lastmove; 28.829 -<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) { 28.830 -<span class="c2html-lineno"><a name="l784" href="#l784">784 </a></span> <span class="c2html-keyword">struct</span> movetimeval *lastmovetstamp = &(movelist->move.timestamp); 28.831 -<span class="c2html-lineno"><a name="l785" href="#l785">785 </a></span> <span class="c2html-keyword">struct</span> timeval currenttstamp; 28.832 -<span class="c2html-lineno"><a name="l786" href="#l786">786 </a></span> gettimeofday(¤ttstamp, <span class="c2html-macroconst">NULL</span>); 28.833 -<span class="c2html-lineno"><a name="l787" href="#l787">787 </a></span> micros += currenttstamp.tv_usec - lastmovetstamp->tv_usec; 28.834 -<span class="c2html-lineno"><a name="l788" href="#l788">788 </a></span> sec = currenttstamp.tv_sec - lastmovetstamp->tv_sec; 28.835 -<span class="c2html-lineno"><a name="l789" href="#l789">789 </a></span> <span class="c2html-keyword">if</span> (sec >= time) { 28.836 -<span class="c2html-lineno"><a name="l790" href="#l790">790 </a></span> <span class="c2html-keyword">return</span> <span class="c2html-macroconst">0</span>; 28.837 -<span class="c2html-lineno"><a name="l791" href="#l791">791 </a></span> } 28.838 -<span class="c2html-lineno"><a name="l792" href="#l792">792 </a></span> 28.839 -<span class="c2html-lineno"><a name="l793" href="#l793">793 </a></span> time -= sec; 28.840 -<span class="c2html-lineno"><a name="l794" href="#l794">794 </a></span> } 28.841 -<span class="c2html-lineno"><a name="l795" href="#l795">795 </a></span> 28.842 -<span class="c2html-lineno"><a name="l796" href="#l796">796 </a></span> sec = micros / 1e6L; 28.843 -<span class="c2html-lineno"><a name="l797" href="#l797">797 </a></span> 28.844 -<span class="c2html-lineno"><a name="l798" href="#l798">798 </a></span> <span class="c2html-keyword">if</span> (sec >= time) { 28.845 -<span class="c2html-lineno"><a name="l799" href="#l799">799 </a></span> <span class="c2html-keyword">return</span> <span class="c2html-macroconst">0</span>; 28.846 -<span class="c2html-lineno"><a name="l800" href="#l800">800 </a></span> } 28.847 -<span class="c2html-lineno"><a name="l801" href="#l801">801 </a></span> 28.848 -<span class="c2html-lineno"><a name="l802" href="#l802">802 </a></span> time -= sec; 28.849 -<span class="c2html-lineno"><a name="l803" href="#l803">803 </a></span> 28.850 -<span class="c2html-lineno"><a name="l804" href="#l804">804 </a></span> <span class="c2html-keyword">return</span> time; 28.851 -<span class="c2html-lineno"><a name="l805" href="#l805">805 </a></span> } <span class="c2html-keyword">else</span> { 28.852 -<span class="c2html-lineno"><a name="l806" href="#l806">806 </a></span> <span class="c2html-keyword">return</span> gameinfo->time; 28.853 -<span class="c2html-lineno"><a name="l807" href="#l807">807 </a></span> } 28.854 -<span class="c2html-lineno"><a name="l808" href="#l808">808 </a></span> } 28.855 -</pre> 28.856 - </body> 28.857 -</html> 28.858 -
29.1 --- a/test/v2-regression/ctest.html Thu Nov 10 18:44:48 2016 +0100 29.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 29.3 @@ -1,438 +0,0 @@ 29.4 -<!DOCTYPE html> 29.5 -<html> 29.6 - <head> 29.7 - <title>c2html</title> 29.8 - <style type="text/css"> 29.9 - span.c2html-lineno a { 29.10 - font-style: italic; 29.11 - text-decoration: none; 29.12 - color: grey; 29.13 - } 29.14 - span.c2html-keyword { 29.15 - color: blue; 29.16 - } 29.17 - span.c2html-macroconst { 29.18 - color: cornflowerblue; 29.19 - } 29.20 - span.c2html-type { 29.21 - color: cornflowerblue; 29.22 - } 29.23 - span.c2html-directive { 29.24 - color: green; 29.25 - } 29.26 - span.c2html-string { 29.27 - color: darkorange; 29.28 - } 29.29 - span.c2html-comment { 29.30 - color: grey; 29.31 - } 29.32 - span.c2html-stdinclude { 29.33 - color: darkorange; 29.34 - } 29.35 - span.c2html-userinclude { 29.36 - color: darkorange; 29.37 - } 29.38 - a.c2html-userinclude { 29.39 - color: darkorange; 29.40 - text-decoration: underline; 29.41 - } 29.42 - </style> 29.43 - </head> 29.44 - <body> 29.45 - 29.46 -<pre> 29.47 -<span class="c2html-lineno"><a name="l1" href="#l1"> 1 </a></span> <span class="c2html-comment">/*</span> 29.48 -<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> 29.49 -<span class="c2html-lineno"><a name="l3" href="#l3"> 3 </a></span> <span class="c2html-comment"> *</span> 29.50 -<span class="c2html-lineno"><a name="l4" href="#l4"> 4 </a></span> <span class="c2html-comment"> * Copyright 2015 Olaf Wintermann. All rights reserved.</span> 29.51 -<span class="c2html-lineno"><a name="l5" href="#l5"> 5 </a></span> <span class="c2html-comment"> *</span> 29.52 -<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> 29.53 -<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> 29.54 -<span class="c2html-lineno"><a name="l8" href="#l8"> 8 </a></span> <span class="c2html-comment"> *</span> 29.55 -<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> 29.56 -<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> 29.57 -<span class="c2html-lineno"><a name="l11" href="#l11"> 11 </a></span> <span class="c2html-comment"> *</span> 29.58 -<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> 29.59 -<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> 29.60 -<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> 29.61 -<span class="c2html-lineno"><a name="l15" href="#l15"> 15 </a></span> <span class="c2html-comment"> *</span> 29.62 -<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> 29.63 -<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> 29.64 -<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> 29.65 -<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> 29.66 -<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> 29.67 -<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> 29.68 -<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> 29.69 -<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> 29.70 -<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> 29.71 -<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> 29.72 -<span class="c2html-lineno"><a name="l26" href="#l26"> 26 </a></span> <span class="c2html-comment"> * POSSIBILITY OF SUCH DAMAGE.</span> 29.73 -<span class="c2html-lineno"><a name="l27" href="#l27"> 27 </a></span> <span class="c2html-comment"> */</span> 29.74 -<span class="c2html-lineno"><a name="l28" href="#l28"> 28 </a></span> 29.75 -<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> 29.76 -<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> 29.77 -<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> 29.78 -<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> 29.79 -<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> 29.80 -<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> 29.81 -<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> 29.82 -<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> 29.83 -<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> 29.84 -<span class="c2html-lineno"><a name="l38" href="#l38"> 38 </a></span> 29.85 -<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> 29.86 -<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> 29.87 -<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> 29.88 -<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> 29.89 -<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> 29.90 -<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> 29.91 -<span class="c2html-lineno"><a name="l45" href="#l45"> 45 </a></span> 29.92 -<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> 29.93 -<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> 29.94 -<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> 29.95 -<span class="c2html-lineno"><a name="l49" href="#l49"> 49 </a></span> 29.96 -<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> 29.97 -<span class="c2html-lineno"><a name="l51" href="#l51"> 51 </a></span> 29.98 -<span class="c2html-lineno"><a name="l52" href="#l52"> 52 </a></span> <span class="c2html-comment">/* -------------------- This is a testing file. -------------------------- */</span> 29.99 -<span class="c2html-lineno"><a name="l53" href="#l53"> 53 </a></span> <span class="c2html-comment">/*</span> 29.100 -<span class="c2html-lineno"><a name="l54" href="#l54"> 54 </a></span> <span class="c2html-comment">time_t util_parse_creationdate(char *str) {</span> 29.101 -<span class="c2html-lineno"><a name="l55" href="#l55"> 55 </a></span> <span class="c2html-comment"> // example: 2012-11-29T21:35:35Z</span> 29.102 -<span class="c2html-lineno"><a name="l56" href="#l56"> 56 </a></span> <span class="c2html-comment"> if(!str) {</span> 29.103 -<span class="c2html-lineno"><a name="l57" href="#l57"> 57 </a></span> <span class="c2html-comment"> return 0;</span> 29.104 -<span class="c2html-lineno"><a name="l58" href="#l58"> 58 </a></span> <span class="c2html-comment"> }</span> 29.105 -<span class="c2html-lineno"><a name="l59" href="#l59"> 59 </a></span> <span class="c2html-comment"> // TODO</span> 29.106 -<span class="c2html-lineno"><a name="l60" href="#l60"> 60 </a></span> <span class="c2html-comment"> return 0;</span> 29.107 -<span class="c2html-lineno"><a name="l61" href="#l61"> 61 </a></span> <span class="c2html-comment">}</span> 29.108 -<span class="c2html-lineno"><a name="l62" href="#l62"> 62 </a></span> <span class="c2html-comment">*/</span> 29.109 -<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) { 29.110 -<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> 29.111 -<span class="c2html-lineno"><a name="l65" href="#l65"> 65 </a></span> <span class="c2html-keyword">if</span>(!str) { 29.112 -<span class="c2html-lineno"><a name="l66" href="#l66"> 66 </a></span> <span class="c2html-keyword">return</span> <span class="c2html-macroconst">0</span>; 29.113 -<span class="c2html-lineno"><a name="l67" href="#l67"> 67 </a></span> } <span class="c2html-keyword">else</span> { 29.114 -<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>); 29.115 -<span class="c2html-lineno"><a name="l69" href="#l69"> 69 </a></span> } 29.116 -<span class="c2html-lineno"><a name="l70" href="#l70"> 70 </a></span> } 29.117 -<span class="c2html-lineno"><a name="l71" href="#l71"> 71 </a></span> 29.118 -<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) { 29.119 -<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>) { 29.120 -<span class="c2html-lineno"><a name="l74" href="#l74"> 74 </a></span> <span class="c2html-keyword">return</span> <span class="c2html-macroconst">1</span>; 29.121 -<span class="c2html-lineno"><a name="l75" href="#l75"> 75 </a></span> } 29.122 -<span class="c2html-lineno"><a name="l76" href="#l76"> 76 </a></span> <span class="c2html-keyword">return</span> <span class="c2html-macroconst">0</span>; 29.123 -<span class="c2html-lineno"><a name="l77" href="#l77"> 77 </a></span> } 29.124 -<span class="c2html-lineno"><a name="l78" href="#l78"> 78 </a></span> 29.125 -<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) { 29.126 -<span class="c2html-lineno"><a name="l80" href="#l80"> 80 </a></span> <span class="c2html-keyword">char</span> *end; 29.127 -<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>); 29.128 -<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>) { 29.129 -<span class="c2html-lineno"><a name="l83" href="#l83"> 83 </a></span> *value = val; 29.130 -<span class="c2html-lineno"><a name="l84" href="#l84"> 84 </a></span> <span class="c2html-keyword">return</span> <span class="c2html-macroconst">1</span>; 29.131 -<span class="c2html-lineno"><a name="l85" href="#l85"> 85 </a></span> } <span class="c2html-keyword">else</span> { 29.132 -<span class="c2html-lineno"><a name="l86" href="#l86"> 86 </a></span> <span class="c2html-keyword">return</span> <span class="c2html-macroconst">0</span>; 29.133 -<span class="c2html-lineno"><a name="l87" href="#l87"> 87 </a></span> } 29.134 -<span class="c2html-lineno"><a name="l88" href="#l88"> 88 </a></span> } 29.135 -<span class="c2html-lineno"><a name="l89" href="#l89"> 89 </a></span> 29.136 -<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) { 29.137 -<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>; 29.138 -<span class="c2html-lineno"><a name="l92" href="#l92"> 92 </a></span> <span class="c2html-type">size_t</span> len = strlen(url); 29.139 -<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>; 29.140 -<span class="c2html-lineno"><a name="l94" href="#l94"> 94 </a></span> <span class="c2html-keyword">int</span> slmax; 29.141 -<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>)) { 29.142 -<span class="c2html-lineno"><a name="l96" href="#l96"> 96 </a></span> slmax = <span class="c2html-macroconst">3</span>; 29.143 -<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>)) { 29.144 -<span class="c2html-lineno"><a name="l98" href="#l98"> 98 </a></span> slmax = <span class="c2html-macroconst">3</span>; 29.145 -<span class="c2html-lineno"><a name="l99" href="#l99"> 99 </a></span> } <span class="c2html-keyword">else</span> { 29.146 -<span class="c2html-lineno"><a name="l100" href="#l100">100 </a></span> slmax = <span class="c2html-macroconst">1</span>; 29.147 -<span class="c2html-lineno"><a name="l101" href="#l101">101 </a></span> } 29.148 -<span class="c2html-lineno"><a name="l102" href="#l102">102 </a></span> <span class="c2html-keyword">char</span> c; 29.149 -<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++) { 29.150 -<span class="c2html-lineno"><a name="l104" href="#l104">104 </a></span> c = url[i]; 29.151 -<span class="c2html-lineno"><a name="l105" href="#l105">105 </a></span> <span class="c2html-keyword">if</span>(c == <span class="c2html-string">'/'</span>) { 29.152 -<span class="c2html-lineno"><a name="l106" href="#l106">106 </a></span> slashcount++; 29.153 -<span class="c2html-lineno"><a name="l107" href="#l107">107 </a></span> <span class="c2html-keyword">if</span>(slashcount == slmax) { 29.154 -<span class="c2html-lineno"><a name="l108" href="#l108">108 </a></span> path = url + i; 29.155 -<span class="c2html-lineno"><a name="l109" href="#l109">109 </a></span> <span class="c2html-keyword">break</span>; 29.156 -<span class="c2html-lineno"><a name="l110" href="#l110">110 </a></span> } 29.157 -<span class="c2html-lineno"><a name="l111" href="#l111">111 </a></span> } 29.158 -<span class="c2html-lineno"><a name="l112" href="#l112">112 </a></span> } 29.159 -<span class="c2html-lineno"><a name="l113" href="#l113">113 </a></span> <span class="c2html-keyword">return</span> path; 29.160 -<span class="c2html-lineno"><a name="l114" href="#l114">114 </a></span> } 29.161 -<span class="c2html-lineno"><a name="l115" href="#l115">115 </a></span> 29.162 -<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) { 29.163 -<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>); 29.164 -<span class="c2html-lineno"><a name="l118" href="#l118">118 </a></span> <span class="c2html-keyword">char</span> *ret = strdup(unesc); 29.165 -<span class="c2html-lineno"><a name="l119" href="#l119">119 </a></span> curl_free(unesc); 29.166 -<span class="c2html-lineno"><a name="l120" href="#l120">120 </a></span> <span class="c2html-keyword">return</span> ret; 29.167 -<span class="c2html-lineno"><a name="l121" href="#l121">121 </a></span> } 29.168 -<span class="c2html-lineno"><a name="l122" href="#l122">122 </a></span> 29.169 -<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) { 29.170 -<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>; 29.171 -<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>; 29.172 -<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>; 29.173 -<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>; 29.174 -<span class="c2html-lineno"><a name="l128" href="#l128">128 </a></span> <span class="c2html-keyword">char</span> c; 29.175 -<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>) { 29.176 -<span class="c2html-lineno"><a name="l130" href="#l130">130 </a></span> <span class="c2html-keyword">if</span>(c == <span class="c2html-string">'/'</span>) { 29.177 -<span class="c2html-lineno"><a name="l131" href="#l131">131 </a></span> osi = si; 29.178 -<span class="c2html-lineno"><a name="l132" href="#l132">132 </a></span> si = i; 29.179 -<span class="c2html-lineno"><a name="l133" href="#l133">133 </a></span> p = <span class="c2html-macroconst">1</span>; 29.180 -<span class="c2html-lineno"><a name="l134" href="#l134">134 </a></span> } 29.181 -<span class="c2html-lineno"><a name="l135" href="#l135">135 </a></span> i++; 29.182 -<span class="c2html-lineno"><a name="l136" href="#l136">136 </a></span> } 29.183 -<span class="c2html-lineno"><a name="l137" href="#l137">137 </a></span> 29.184 -<span class="c2html-lineno"><a name="l138" href="#l138">138 </a></span> <span class="c2html-keyword">char</span> *name = url + si + p; 29.185 -<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>) { 29.186 -<span class="c2html-lineno"><a name="l140" href="#l140">140 </a></span> name = url + osi + p; 29.187 -<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>) { 29.188 -<span class="c2html-lineno"><a name="l142" href="#l142">142 </a></span> <span class="c2html-keyword">return</span> url; 29.189 -<span class="c2html-lineno"><a name="l143" href="#l143">143 </a></span> } 29.190 -<span class="c2html-lineno"><a name="l144" href="#l144">144 </a></span> } 29.191 -<span class="c2html-lineno"><a name="l145" href="#l145">145 </a></span> 29.192 -<span class="c2html-lineno"><a name="l146" href="#l146">146 </a></span> <span class="c2html-keyword">return</span> name; 29.193 -<span class="c2html-lineno"><a name="l147" href="#l147">147 </a></span> } 29.194 -<span class="c2html-lineno"><a name="l148" href="#l148">148 </a></span> 29.195 -<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) { 29.196 -<span class="c2html-lineno"><a name="l150" href="#l150">150 </a></span> <span class="c2html-directive">#ifdef</span> <span class="c2html-macroconst">_WIN32</span> 29.197 -<span class="c2html-lineno"><a name="l151" href="#l151">151 </a></span> <span class="c2html-keyword">return</span> mkdir(path); 29.198 -<span class="c2html-lineno"><a name="l152" href="#l152">152 </a></span> <span class="c2html-directive">#else</span> 29.199 -<span class="c2html-lineno"><a name="l153" href="#l153">153 </a></span> <span class="c2html-keyword">return</span> mkdir(path, mode); 29.200 -<span class="c2html-lineno"><a name="l154" href="#l154">154 </a></span> <span class="c2html-directive">#endif</span> 29.201 -<span class="c2html-lineno"><a name="l155" href="#l155">155 </a></span> } 29.202 -<span class="c2html-lineno"><a name="l156" href="#l156">156 </a></span> 29.203 -<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) { 29.204 -<span class="c2html-lineno"><a name="l158" href="#l158">158 </a></span> <span class="c2html-type">sstr_t</span> base = sstr(url_base); 29.205 -<span class="c2html-lineno"><a name="l159" href="#l159">159 </a></span> <span class="c2html-type">sstr_t</span> path; 29.206 -<span class="c2html-lineno"><a name="l160" href="#l160">160 </a></span> <span class="c2html-keyword">if</span>(p) { 29.207 -<span class="c2html-lineno"><a name="l161" href="#l161">161 </a></span> path = sstr(p); 29.208 -<span class="c2html-lineno"><a name="l162" href="#l162">162 </a></span> } <span class="c2html-keyword">else</span> { 29.209 -<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>); 29.210 -<span class="c2html-lineno"><a name="l164" href="#l164">164 </a></span> } 29.211 -<span class="c2html-lineno"><a name="l165" href="#l165">165 </a></span> 29.212 -<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>; 29.213 -<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>) { 29.214 -<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>) { 29.215 -<span class="c2html-lineno"><a name="l169" href="#l169">169 </a></span> base.length--; 29.216 -<span class="c2html-lineno"><a name="l170" href="#l170">170 </a></span> } 29.217 -<span class="c2html-lineno"><a name="l171" href="#l171">171 </a></span> } <span class="c2html-keyword">else</span> { 29.218 -<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>) { 29.219 -<span class="c2html-lineno"><a name="l173" href="#l173">173 </a></span> add_separator = <span class="c2html-macroconst">1</span>; 29.220 -<span class="c2html-lineno"><a name="l174" href="#l174">174 </a></span> } 29.221 -<span class="c2html-lineno"><a name="l175" href="#l175">175 </a></span> } 29.222 -<span class="c2html-lineno"><a name="l176" href="#l176">176 </a></span> 29.223 -<span class="c2html-lineno"><a name="l177" href="#l177">177 </a></span> <span class="c2html-type">sstr_t</span> url; 29.224 -<span class="c2html-lineno"><a name="l178" href="#l178">178 </a></span> <span class="c2html-keyword">if</span>(add_separator) { 29.225 -<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); 29.226 -<span class="c2html-lineno"><a name="l180" href="#l180">180 </a></span> } <span class="c2html-keyword">else</span> { 29.227 -<span class="c2html-lineno"><a name="l181" href="#l181">181 </a></span> url = sstrcat(<span class="c2html-macroconst">2</span>, base, path); 29.228 -<span class="c2html-lineno"><a name="l182" href="#l182">182 </a></span> } 29.229 -<span class="c2html-lineno"><a name="l183" href="#l183">183 </a></span> 29.230 -<span class="c2html-lineno"><a name="l184" href="#l184">184 </a></span> <span class="c2html-keyword">return</span> url.ptr; 29.231 -<span class="c2html-lineno"><a name="l185" href="#l185">185 </a></span> } 29.232 -<span class="c2html-lineno"><a name="l186" href="#l186">186 </a></span> 29.233 -<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) { 29.234 -<span class="c2html-lineno"><a name="l188" href="#l188">188 </a></span> <span class="c2html-type">sstr_t</span> base = sstr(sn->base_url); 29.235 -<span class="c2html-lineno"><a name="l189" href="#l189">189 </a></span> <span class="c2html-type">sstr_t</span> href_str = sstr(href); 29.236 -<span class="c2html-lineno"><a name="l190" href="#l190">190 </a></span> 29.237 -<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); 29.238 -<span class="c2html-lineno"><a name="l192" href="#l192">192 </a></span> base.length -= strlen(base_path); 29.239 -<span class="c2html-lineno"><a name="l193" href="#l193">193 </a></span> 29.240 -<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); 29.241 -<span class="c2html-lineno"><a name="l195" href="#l195">195 </a></span> 29.242 -<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); 29.243 -<span class="c2html-lineno"><a name="l197" href="#l197">197 </a></span> free(url.ptr); 29.244 -<span class="c2html-lineno"><a name="l198" href="#l198">198 </a></span> } 29.245 -<span class="c2html-lineno"><a name="l199" href="#l199">199 </a></span> 29.246 -<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) { 29.247 -<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>); 29.248 -<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>); 29.249 -<span class="c2html-lineno"><a name="l203" href="#l203">203 </a></span> 29.250 -<span class="c2html-lineno"><a name="l204" href="#l204">204 </a></span> <span class="c2html-comment">// add base url</span> 29.251 -<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); 29.252 -<span class="c2html-lineno"><a name="l206" href="#l206">206 </a></span> <span class="c2html-comment">// remove trailing slash</span> 29.253 -<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>); 29.254 -<span class="c2html-lineno"><a name="l208" href="#l208">208 </a></span> 29.255 -<span class="c2html-lineno"><a name="l209" href="#l209">209 </a></span> <span class="c2html-type">sstr_t</span> p = sstr(path); 29.256 -<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>; 29.257 -<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); 29.258 -<span class="c2html-lineno"><a name="l212" href="#l212">212 </a></span> 29.259 -<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++) { 29.260 -<span class="c2html-lineno"><a name="l214" href="#l214">214 </a></span> <span class="c2html-type">sstr_t</span> node = tks[i]; 29.261 -<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>) { 29.262 -<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); 29.263 -<span class="c2html-lineno"><a name="l217" href="#l217">217 </a></span> ucx_buffer_putc(url, <span class="c2html-string">'/'</span>); 29.264 -<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); 29.265 -<span class="c2html-lineno"><a name="l219" href="#l219">219 </a></span> curl_free(esc); 29.266 -<span class="c2html-lineno"><a name="l220" href="#l220">220 </a></span> } 29.267 -<span class="c2html-lineno"><a name="l221" href="#l221">221 </a></span> free(node.ptr); 29.268 -<span class="c2html-lineno"><a name="l222" href="#l222">222 </a></span> } 29.269 -<span class="c2html-lineno"><a name="l223" href="#l223">223 </a></span> free(tks); 29.270 -<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>) { 29.271 -<span class="c2html-lineno"><a name="l225" href="#l225">225 </a></span> ucx_buffer_putc(url, <span class="c2html-string">'/'</span>); 29.272 -<span class="c2html-lineno"><a name="l226" href="#l226">226 </a></span> } 29.273 -<span class="c2html-lineno"><a name="l227" href="#l227">227 </a></span> ucx_buffer_putc(url, <span class="c2html-macroconst">0</span>); 29.274 -<span class="c2html-lineno"><a name="l228" href="#l228">228 </a></span> 29.275 -<span class="c2html-lineno"><a name="l229" href="#l229">229 </a></span> space = url->space; 29.276 -<span class="c2html-lineno"><a name="l230" href="#l230">230 </a></span> ucx_buffer_free(url); 29.277 -<span class="c2html-lineno"><a name="l231" href="#l231">231 </a></span> 29.278 -<span class="c2html-lineno"><a name="l232" href="#l232">232 </a></span> <span class="c2html-keyword">return</span> space; 29.279 -<span class="c2html-lineno"><a name="l233" href="#l233">233 </a></span> } 29.280 -<span class="c2html-lineno"><a name="l234" href="#l234">234 </a></span> 29.281 -<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) { 29.282 -<span class="c2html-lineno"><a name="l236" href="#l236">236 </a></span> <span class="c2html-keyword">char</span> *name = util_resource_name(path); 29.283 -<span class="c2html-lineno"><a name="l237" href="#l237">237 </a></span> <span class="c2html-type">size_t</span> namelen = strlen(name); 29.284 -<span class="c2html-lineno"><a name="l238" href="#l238">238 </a></span> <span class="c2html-type">size_t</span> pathlen = strlen(path); 29.285 -<span class="c2html-lineno"><a name="l239" href="#l239">239 </a></span> <span class="c2html-type">size_t</span> parentlen = pathlen - namelen; 29.286 -<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>); 29.287 -<span class="c2html-lineno"><a name="l241" href="#l241">241 </a></span> memcpy(parent, path, parentlen); 29.288 -<span class="c2html-lineno"><a name="l242" href="#l242">242 </a></span> parent[parentlen] = <span class="c2html-string">'\0'</span>; 29.289 -<span class="c2html-lineno"><a name="l243" href="#l243">243 </a></span> <span class="c2html-keyword">return</span> parent; 29.290 -<span class="c2html-lineno"><a name="l244" href="#l244">244 </a></span> } 29.291 -<span class="c2html-lineno"><a name="l245" href="#l245">245 </a></span> 29.292 -<span class="c2html-lineno"><a name="l246" href="#l246">246 </a></span> 29.293 -<span class="c2html-lineno"><a name="l247" href="#l247">247 </a></span> <span class="c2html-keyword">char</span>* util_xml_get_text(xmlNode *elm) { 29.294 -<span class="c2html-lineno"><a name="l248" href="#l248">248 </a></span> xmlNode *node = elm->children; 29.295 -<span class="c2html-lineno"><a name="l249" href="#l249">249 </a></span> <span class="c2html-keyword">while</span>(node) { 29.296 -<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>) { 29.297 -<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; 29.298 -<span class="c2html-lineno"><a name="l252" href="#l252">252 </a></span> } 29.299 -<span class="c2html-lineno"><a name="l253" href="#l253">253 </a></span> node = node->next; 29.300 -<span class="c2html-lineno"><a name="l254" href="#l254">254 </a></span> } 29.301 -<span class="c2html-lineno"><a name="l255" href="#l255">255 </a></span> <span class="c2html-keyword">return</span> <span class="c2html-macroconst">NULL</span>; 29.302 -<span class="c2html-lineno"><a name="l256" href="#l256">256 </a></span> } 29.303 -<span class="c2html-lineno"><a name="l257" href="#l257">257 </a></span> 29.304 -<span class="c2html-lineno"><a name="l258" href="#l258">258 </a></span> 29.305 -<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) { 29.306 -<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>; 29.307 -<span class="c2html-lineno"><a name="l261" href="#l261">261 </a></span> <span class="c2html-keyword">return</span> util_base64decode_len(in, &len); 29.308 -<span class="c2html-lineno"><a name="l262" href="#l262">262 </a></span> } 29.309 -<span class="c2html-lineno"><a name="l263" href="#l263">263 </a></span> 29.310 -<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) { 29.311 -<span class="c2html-lineno"><a name="l265" href="#l265">265 </a></span> <span class="c2html-type">size_t</span> len = strlen(in); 29.312 -<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); 29.313 -<span class="c2html-lineno"><a name="l267" href="#l267">267 </a></span> 29.314 -<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); 29.315 -<span class="c2html-lineno"><a name="l269" href="#l269">269 </a></span> <span class="c2html-macroconst">BIO</span> *d = BIO_new(BIO_f_base64()); 29.316 -<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>); 29.317 -<span class="c2html-lineno"><a name="l271" href="#l271">271 </a></span> b = BIO_push(d, b); 29.318 -<span class="c2html-lineno"><a name="l272" href="#l272">272 </a></span> 29.319 -<span class="c2html-lineno"><a name="l273" href="#l273">273 </a></span> *outlen = BIO_read(b, out, len); 29.320 -<span class="c2html-lineno"><a name="l274" href="#l274">274 </a></span> BIO_free_all(b); 29.321 -<span class="c2html-lineno"><a name="l275" href="#l275">275 </a></span> 29.322 -<span class="c2html-lineno"><a name="l276" href="#l276">276 </a></span> <span class="c2html-keyword">return</span> out; 29.323 -<span class="c2html-lineno"><a name="l277" href="#l277">277 </a></span> } 29.324 -<span class="c2html-lineno"><a name="l278" href="#l278">278 </a></span> 29.325 -<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) { 29.326 -<span class="c2html-lineno"><a name="l280" href="#l280">280 </a></span> <span class="c2html-macroconst">BIO</span> *b; 29.327 -<span class="c2html-lineno"><a name="l281" href="#l281">281 </a></span> <span class="c2html-macroconst">BIO</span> *e; 29.328 -<span class="c2html-lineno"><a name="l282" href="#l282">282 </a></span> <span class="c2html-macroconst">BUF_MEM</span> *mem; 29.329 -<span class="c2html-lineno"><a name="l283" href="#l283">283 </a></span> 29.330 -<span class="c2html-lineno"><a name="l284" href="#l284">284 </a></span> e = BIO_new(BIO_f_base64()); 29.331 -<span class="c2html-lineno"><a name="l285" href="#l285">285 </a></span> b = BIO_new(BIO_s_mem()); 29.332 -<span class="c2html-lineno"><a name="l286" href="#l286">286 </a></span> 29.333 -<span class="c2html-lineno"><a name="l287" href="#l287">287 </a></span> e = BIO_push(e, b); 29.334 -<span class="c2html-lineno"><a name="l288" href="#l288">288 </a></span> BIO_write(e, in, len); 29.335 -<span class="c2html-lineno"><a name="l289" href="#l289">289 </a></span> BIO_flush(e); 29.336 -<span class="c2html-lineno"><a name="l290" href="#l290">290 </a></span> 29.337 -<span class="c2html-lineno"><a name="l291" href="#l291">291 </a></span> BIO_get_mem_ptr(e, &mem); 29.338 -<span class="c2html-lineno"><a name="l292" href="#l292">292 </a></span> <span class="c2html-keyword">char</span> *out = malloc(mem->length); 29.339 -<span class="c2html-lineno"><a name="l293" href="#l293">293 </a></span> memcpy(out, mem->data, mem->length -<span class="c2html-macroconst">1</span>); 29.340 -<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>; 29.341 -<span class="c2html-lineno"><a name="l295" href="#l295">295 </a></span> 29.342 -<span class="c2html-lineno"><a name="l296" href="#l296">296 </a></span> BIO_free_all(e); 29.343 -<span class="c2html-lineno"><a name="l297" href="#l297">297 </a></span> 29.344 -<span class="c2html-lineno"><a name="l298" href="#l298">298 </a></span> <span class="c2html-keyword">return</span> out; 29.345 -<span class="c2html-lineno"><a name="l299" href="#l299">299 </a></span> } 29.346 -<span class="c2html-lineno"><a name="l300" href="#l300">300 </a></span> 29.347 -<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) { 29.348 -<span class="c2html-lineno"><a name="l302" href="#l302">302 </a></span> DavKey *k = dav_context_get_key(sn->context, key); 29.349 -<span class="c2html-lineno"><a name="l303" href="#l303">303 </a></span> <span class="c2html-keyword">if</span>(!k) { 29.350 -<span class="c2html-lineno"><a name="l304" href="#l304">304 </a></span> <span class="c2html-comment">// TODO: session error</span> 29.351 -<span class="c2html-lineno"><a name="l305" href="#l305">305 </a></span> <span class="c2html-keyword">return</span> <span class="c2html-macroconst">NULL</span>; 29.352 -<span class="c2html-lineno"><a name="l306" href="#l306">306 </a></span> } 29.353 -<span class="c2html-lineno"><a name="l307" href="#l307">307 </a></span> 29.354 -<span class="c2html-lineno"><a name="l308" href="#l308">308 </a></span> <span class="c2html-keyword">char</span> *enc_str = aes_encrypt(str, k); 29.355 -<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); 29.356 -<span class="c2html-lineno"><a name="l310" href="#l310">310 </a></span> free(enc_str); 29.357 -<span class="c2html-lineno"><a name="l311" href="#l311">311 </a></span> <span class="c2html-keyword">return</span> ret_str; 29.358 -<span class="c2html-lineno"><a name="l312" href="#l312">312 </a></span> } 29.359 -<span class="c2html-lineno"><a name="l313" href="#l313">313 </a></span> 29.360 -<span class="c2html-lineno"><a name="l314" href="#l314">314 </a></span> <span class="c2html-comment">/* commented out for testing reasons */</span> 29.361 -<span class="c2html-lineno"><a name="l315" href="#l315">315 </a></span> <span class="c2html-comment">/*</span> 29.362 -<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> 29.363 -<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> 29.364 -<span class="c2html-lineno"><a name="l318" href="#l318">318 </a></span> <span class="c2html-comment"> if(!k) {</span> 29.365 -<span class="c2html-lineno"><a name="l319" href="#l319">319 </a></span> <span class="c2html-comment"> // TODO: session error</span> 29.366 -<span class="c2html-lineno"><a name="l320" href="#l320">320 </a></span> <span class="c2html-comment"> return NULL;</span> 29.367 -<span class="c2html-lineno"><a name="l321" href="#l321">321 </a></span> <span class="c2html-comment"> }</span> 29.368 -<span class="c2html-lineno"><a name="l322" href="#l322">322 </a></span> <span class="c2html-comment"> </span> 29.369 -<span class="c2html-lineno"><a name="l323" href="#l323">323 </a></span> <span class="c2html-comment"> char *dec_str = aes_decrypt(str, k);</span> 29.370 -<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> 29.371 -<span class="c2html-lineno"><a name="l325" href="#l325">325 </a></span> <span class="c2html-comment"> free(dec_str);</span> 29.372 -<span class="c2html-lineno"><a name="l326" href="#l326">326 </a></span> <span class="c2html-comment"> return ret_str;</span> 29.373 -<span class="c2html-lineno"><a name="l327" href="#l327">327 </a></span> <span class="c2html-comment">}</span> 29.374 -<span class="c2html-lineno"><a name="l328" href="#l328">328 </a></span> <span class="c2html-comment">*/</span> 29.375 -<span class="c2html-lineno"><a name="l329" href="#l329">329 </a></span> <span class="c2html-keyword">char</span>* util_random_str() { 29.376 -<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>); 29.377 -<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>; 29.378 -<span class="c2html-lineno"><a name="l332" href="#l332">332 </a></span> 29.379 -<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>( 29.380 -<span class="c2html-lineno"><a name="l334" href="#l334">334 </a></span> <span class="c2html-string">"01234567890"</span> 29.381 -<span class="c2html-lineno"><a name="l335" href="#l335">335 </a></span> <span class="c2html-string">"abcdefghijklmnopqrstuvwxyz"</span> 29.382 -<span class="c2html-lineno"><a name="l336" href="#l336">336 </a></span> <span class="c2html-string">"ABCDEFGHIJKLMNOPQRSTUVWXYZ"</span>); 29.383 -<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; 29.384 -<span class="c2html-lineno"><a name="l338" href="#l338">338 </a></span> 29.385 -<span class="c2html-lineno"><a name="l339" href="#l339">339 </a></span> RAND_pseudo_bytes(str, <span class="c2html-macroconst">24</span>); 29.386 -<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++) { 29.387 -<span class="c2html-lineno"><a name="l341" href="#l341">341 </a></span> <span class="c2html-keyword">int</span> c = str[i] % t.length; 29.388 -<span class="c2html-lineno"><a name="l342" href="#l342">342 </a></span> str[i] = table[c]; 29.389 -<span class="c2html-lineno"><a name="l343" href="#l343">343 </a></span> } 29.390 -<span class="c2html-lineno"><a name="l344" href="#l344">344 </a></span> 29.391 -<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; 29.392 -<span class="c2html-lineno"><a name="l346" href="#l346">346 </a></span> } 29.393 -<span class="c2html-lineno"><a name="l347" href="#l347">347 </a></span> 29.394 -<span class="c2html-lineno"><a name="l348" href="#l348">348 </a></span> <span class="c2html-comment">/*</span> 29.395 -<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> 29.396 -<span class="c2html-lineno"><a name="l350" href="#l350">350 </a></span> <span class="c2html-comment"> * tokens are separated by space</span> 29.397 -<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> 29.398 -<span class="c2html-lineno"><a name="l352" href="#l352">352 </a></span> <span class="c2html-comment"> */</span> 29.399 -<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) { 29.400 -<span class="c2html-lineno"><a name="l354" href="#l354">354 </a></span> <span class="c2html-keyword">int</span> i; 29.401 -<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>; 29.402 -<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>; 29.403 -<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++) { 29.404 -<span class="c2html-lineno"><a name="l358" href="#l358">358 </a></span> <span class="c2html-keyword">int</span> c; 29.405 -<span class="c2html-lineno"><a name="l359" href="#l359">359 </a></span> <span class="c2html-keyword">if</span>(i == str.length) { 29.406 -<span class="c2html-lineno"><a name="l360" href="#l360">360 </a></span> c = <span class="c2html-string">' '</span>; 29.407 -<span class="c2html-lineno"><a name="l361" href="#l361">361 </a></span> } <span class="c2html-keyword">else</span> { 29.408 -<span class="c2html-lineno"><a name="l362" href="#l362">362 </a></span> c = str.ptr[i]; 29.409 -<span class="c2html-lineno"><a name="l363" href="#l363">363 </a></span> } 29.410 -<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>) { 29.411 -<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>) { 29.412 -<span class="c2html-lineno"><a name="l366" href="#l366">366 </a></span> token_end = i; 29.413 -<span class="c2html-lineno"><a name="l367" href="#l367">367 </a></span> <span class="c2html-type">size_t</span> len = token_end - token_start; 29.414 -<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); 29.415 -<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> 29.416 -<span class="c2html-lineno"><a name="l370" href="#l370">370 </a></span> <span class="c2html-keyword">if</span>(!sstrcmp(tk, token)) { 29.417 -<span class="c2html-lineno"><a name="l371" href="#l371">371 </a></span> *sub = sstrtrim(sstrsubsl(str, <span class="c2html-macroconst">0</span>, token_start)); 29.418 -<span class="c2html-lineno"><a name="l372" href="#l372">372 </a></span> <span class="c2html-keyword">break</span>; 29.419 -<span class="c2html-lineno"><a name="l373" href="#l373">373 </a></span> } 29.420 -<span class="c2html-lineno"><a name="l374" href="#l374">374 </a></span> token_start = -<span class="c2html-macroconst">1</span>; 29.421 -<span class="c2html-lineno"><a name="l375" href="#l375">375 </a></span> token_end = -<span class="c2html-macroconst">1</span>; 29.422 -<span class="c2html-lineno"><a name="l376" href="#l376">376 </a></span> } 29.423 -<span class="c2html-lineno"><a name="l377" href="#l377">377 </a></span> } <span class="c2html-keyword">else</span> { 29.424 -<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>) { 29.425 -<span class="c2html-lineno"><a name="l379" href="#l379">379 </a></span> token_start = i; 29.426 -<span class="c2html-lineno"><a name="l380" href="#l380">380 </a></span> } 29.427 -<span class="c2html-lineno"><a name="l381" href="#l381">381 </a></span> } 29.428 -<span class="c2html-lineno"><a name="l382" href="#l382">382 </a></span> } 29.429 -<span class="c2html-lineno"><a name="l383" href="#l383">383 </a></span> 29.430 -<span class="c2html-lineno"><a name="l384" href="#l384">384 </a></span> <span class="c2html-keyword">if</span>(i < str.length) { 29.431 -<span class="c2html-lineno"><a name="l385" href="#l385">385 </a></span> <span class="c2html-keyword">return</span> sstrtrim(sstrsubs(str, i)); 29.432 -<span class="c2html-lineno"><a name="l386" href="#l386">386 </a></span> } <span class="c2html-keyword">else</span> { 29.433 -<span class="c2html-lineno"><a name="l387" href="#l387">387 </a></span> str.ptr = <span class="c2html-macroconst">NULL</span>; 29.434 -<span class="c2html-lineno"><a name="l388" href="#l388">388 </a></span> str.length = <span class="c2html-macroconst">0</span>; 29.435 -<span class="c2html-lineno"><a name="l389" href="#l389">389 </a></span> <span class="c2html-keyword">return</span> str; 29.436 -<span class="c2html-lineno"><a name="l390" href="#l390">390 </a></span> } 29.437 -<span class="c2html-lineno"><a name="l391" href="#l391">391 </a></span> } 29.438 -</pre> 29.439 - </body> 29.440 -</html> 29.441 -
30.1 --- a/test/v2-regression/javatest.html Thu Nov 10 18:44:48 2016 +0100 30.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 30.3 @@ -1,215 +0,0 @@ 30.4 -<!DOCTYPE html> 30.5 -<html> 30.6 - <head> 30.7 - <title>c2html</title> 30.8 - <style type="text/css"> 30.9 - span.c2html-lineno a { 30.10 - font-style: italic; 30.11 - text-decoration: none; 30.12 - color: grey; 30.13 - } 30.14 - span.c2html-keyword { 30.15 - color: blue; 30.16 - } 30.17 - span.c2html-macroconst { 30.18 - color: cornflowerblue; 30.19 - } 30.20 - span.c2html-type { 30.21 - color: teal; 30.22 - } 30.23 - span.c2html-directive { 30.24 - color: silver; 30.25 - } 30.26 - span.c2html-string { 30.27 - color: darkorange; 30.28 - } 30.29 - span.c2html-comment { 30.30 - color: grey; 30.31 - } 30.32 - span.c2html-stdinclude, span.c2html-userinclude, a.c2html-userinclude { 30.33 - } 30.34 - </style> 30.35 - </head> 30.36 - <body> 30.37 - 30.38 -<pre> 30.39 -<span class="c2html-lineno"><a name="l1" href="#l1"> 1 </a></span> <span class="c2html-comment">/*</span> 30.40 -<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> 30.41 -<span class="c2html-lineno"><a name="l3" href="#l3"> 3 </a></span> <span class="c2html-comment"> *</span> 30.42 -<span class="c2html-lineno"><a name="l4" href="#l4"> 4 </a></span> <span class="c2html-comment"> * Copyright 2014 Mike Becker. All rights reserved.</span> 30.43 -<span class="c2html-lineno"><a name="l5" href="#l5"> 5 </a></span> <span class="c2html-comment"> *</span> 30.44 -<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> 30.45 -<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> 30.46 -<span class="c2html-lineno"><a name="l8" href="#l8"> 8 </a></span> <span class="c2html-comment"> *</span> 30.47 -<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> 30.48 -<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> 30.49 -<span class="c2html-lineno"><a name="l11" href="#l11"> 11 </a></span> <span class="c2html-comment"> *</span> 30.50 -<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> 30.51 -<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> 30.52 -<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> 30.53 -<span class="c2html-lineno"><a name="l15" href="#l15"> 15 </a></span> <span class="c2html-comment"> *</span> 30.54 -<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> 30.55 -<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> 30.56 -<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> 30.57 -<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> 30.58 -<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> 30.59 -<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> 30.60 -<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> 30.61 -<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> 30.62 -<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> 30.63 -<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> 30.64 -<span class="c2html-lineno"><a name="l26" href="#l26"> 26 </a></span> <span class="c2html-comment"> * POSSIBILITY OF SUCH DAMAGE.</span> 30.65 -<span class="c2html-lineno"><a name="l27" href="#l27"> 27 </a></span> <span class="c2html-comment"> *</span> 30.66 -<span class="c2html-lineno"><a name="l28" href="#l28"> 28 </a></span> <span class="c2html-comment"> */</span> 30.67 -<span class="c2html-lineno"><a name="l29" href="#l29"> 29 </a></span> 30.68 -<span class="c2html-lineno"><a name="l30" href="#l30"> 30 </a></span> <span class="c2html-keyword">package</span> de.uapcore.sigred.doc.base; 30.69 -<span class="c2html-lineno"><a name="l31" href="#l31"> 31 </a></span> 30.70 -<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>; 30.71 -<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>; 30.72 -<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>; 30.73 -<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>; 30.74 -<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>; 30.75 -<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>; 30.76 -<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>; 30.77 -<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>; 30.78 -<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>; 30.79 -<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>; 30.80 -<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>; 30.81 -<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>; 30.82 -<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>; 30.83 -<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>; 30.84 -<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>; 30.85 -<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>; 30.86 -<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>; 30.87 -<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>; 30.88 -<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>; 30.89 -<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>; 30.90 -<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>; 30.91 -<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>; 30.92 -<span class="c2html-lineno"><a name="l54" href="#l54"> 54 </a></span> 30.93 -<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>> 30.94 -<span class="c2html-lineno"><a name="l56" href="#l56"> 56 </a></span> <span class="c2html-keyword">extends</span> <span class="c2html-type">FileBackedDocument</span> { 30.95 -<span class="c2html-lineno"><a name="l57" href="#l57"> 57 </a></span> 30.96 -<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>, 30.97 -<span class="c2html-lineno"><a name="l59" href="#l59"> 59 </a></span> <span class="c2html-string">"http://develop.uap-core.de/sigred/"</span>); 30.98 -<span class="c2html-lineno"><a name="l60" href="#l60"> 60 </a></span> 30.99 -<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> 30.100 -<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>); 30.101 -<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> 30.102 -<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>); 30.103 -<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> 30.104 -<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>); 30.105 -<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> 30.106 -<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>); 30.107 -<span class="c2html-lineno"><a name="l69" href="#l69"> 69 </a></span> 30.108 -<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; 30.109 -<span class="c2html-lineno"><a name="l71" href="#l71"> 71 </a></span> 30.110 -<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; 30.111 -<span class="c2html-lineno"><a name="l73" href="#l73"> 73 </a></span> 30.112 -<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) { 30.113 -<span class="c2html-lineno"><a name="l75" href="#l75"> 75 </a></span> <span class="c2html-type">T</span> g; 30.114 -<span class="c2html-lineno"><a name="l76" href="#l76"> 76 </a></span> <span class="c2html-keyword">try</span> { 30.115 -<span class="c2html-lineno"><a name="l77" href="#l77"> 77 </a></span> g = graphType.newInstance(); 30.116 -<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) { 30.117 -<span class="c2html-lineno"><a name="l79" href="#l79"> 79 </a></span> <span class="c2html-keyword">assert</span> false; 30.118 -<span class="c2html-lineno"><a name="l80" href="#l80"> 80 </a></span> g = null; <span class="c2html-comment">// for the compiler</span> 30.119 -<span class="c2html-lineno"><a name="l81" href="#l81"> 81 </a></span> } 30.120 -<span class="c2html-lineno"><a name="l82" href="#l82"> 82 </a></span> graph = g; 30.121 -<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>(); 30.122 -<span class="c2html-lineno"><a name="l84" href="#l84"> 84 </a></span> } 30.123 -<span class="c2html-lineno"><a name="l85" href="#l85"> 85 </a></span> 30.124 -<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() { 30.125 -<span class="c2html-lineno"><a name="l87" href="#l87"> 87 </a></span> <span class="c2html-keyword">return</span> graph; 30.126 -<span class="c2html-lineno"><a name="l88" href="#l88"> 88 </a></span> } 30.127 -<span class="c2html-lineno"><a name="l89" href="#l89"> 89 </a></span> 30.128 -<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() { 30.129 -<span class="c2html-lineno"><a name="l91" href="#l91"> 91 </a></span> <span class="c2html-keyword">return</span> metadata; 30.130 -<span class="c2html-lineno"><a name="l92" href="#l92"> 92 </a></span> } 30.131 -<span class="c2html-lineno"><a name="l93" href="#l93"> 93 </a></span> 30.132 -<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>; 30.133 -<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>; 30.134 -<span class="c2html-lineno"><a name="l96" href="#l96"> 96 </a></span> 30.135 -<span class="c2html-lineno"><a name="l97" href="#l97"> 97 </a></span> <span class="c2html-directive">@Override</span> 30.136 -<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> { 30.137 -<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(); 30.138 -<span class="c2html-lineno"><a name="l100" href="#l100">100 </a></span> 30.139 -<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>); 30.140 -<span class="c2html-lineno"><a name="l102" href="#l102">102 </a></span> 30.141 -<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>); 30.142 -<span class="c2html-lineno"><a name="l104" href="#l104">104 </a></span> 30.143 -<span class="c2html-lineno"><a name="l105" href="#l105">105 </a></span> metadata.write(metadataNode); 30.144 -<span class="c2html-lineno"><a name="l106" href="#l106">106 </a></span> 30.145 -<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>) { 30.146 -<span class="c2html-lineno"><a name="l108" href="#l108">108 </a></span> writeGraph(rootNode.addElement(<span class="c2html-type">TAG_GRAPH</span>)); 30.147 -<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>) { 30.148 -<span class="c2html-lineno"><a name="l110" href="#l110">110 </a></span> writeGraph(rootNode.addElement(<span class="c2html-type">TAG_DIGRAPH</span>)); 30.149 -<span class="c2html-lineno"><a name="l111" href="#l111">111 </a></span> } <span class="c2html-keyword">else</span> { 30.150 -<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>); 30.151 -<span class="c2html-lineno"><a name="l113" href="#l113">113 </a></span> } 30.152 -<span class="c2html-lineno"><a name="l114" href="#l114">114 </a></span> 30.153 -<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()); 30.154 -<span class="c2html-lineno"><a name="l116" href="#l116">116 </a></span> writer.write(doc); 30.155 -<span class="c2html-lineno"><a name="l117" href="#l117">117 </a></span> writer.flush(); 30.156 -<span class="c2html-lineno"><a name="l118" href="#l118">118 </a></span> } 30.157 -<span class="c2html-lineno"><a name="l119" href="#l119">119 </a></span> 30.158 -<span class="c2html-lineno"><a name="l120" href="#l120">120 </a></span> <span class="c2html-directive">@Override</span> 30.159 -<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> { 30.160 -<span class="c2html-lineno"><a name="l122" href="#l122">122 </a></span> <span class="c2html-keyword">try</span> { 30.161 -<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); 30.162 -<span class="c2html-lineno"><a name="l124" href="#l124">124 </a></span> reader.setStripWhitespaceText(true); 30.163 -<span class="c2html-lineno"><a name="l125" href="#l125">125 </a></span> 30.164 -<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>+ 30.165 -<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); 30.166 -<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> + 30.167 -<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>, 30.168 -<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( 30.169 -<span class="c2html-lineno"><a name="l131" href="#l131">131 </a></span> <span class="c2html-string">"graph-document.xsd"</span>).toExternalForm())); 30.170 -<span class="c2html-lineno"><a name="l132" href="#l132">132 </a></span> 30.171 -<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); 30.172 -<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><>(); 30.173 -<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> 30.174 -<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>() { 30.175 -<span class="c2html-lineno"><a name="l137" href="#l137">137 </a></span> <span class="c2html-directive">@Override</span> 30.176 -<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> { 30.177 -<span class="c2html-lineno"><a name="l139" href="#l139">139 </a></span> } 30.178 -<span class="c2html-lineno"><a name="l140" href="#l140">140 </a></span> 30.179 -<span class="c2html-lineno"><a name="l141" href="#l141">141 </a></span> <span class="c2html-directive">@Override</span> 30.180 -<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> { 30.181 -<span class="c2html-lineno"><a name="l143" href="#l143">143 </a></span> xmlerror.set(exception); 30.182 -<span class="c2html-lineno"><a name="l144" href="#l144">144 </a></span> passed.set(false); 30.183 -<span class="c2html-lineno"><a name="l145" href="#l145">145 </a></span> } 30.184 -<span class="c2html-lineno"><a name="l146" href="#l146">146 </a></span> 30.185 -<span class="c2html-lineno"><a name="l147" href="#l147">147 </a></span> <span class="c2html-directive">@Override</span> 30.186 -<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> { 30.187 -<span class="c2html-lineno"><a name="l149" href="#l149">149 </a></span> xmlerror.set(exception); 30.188 -<span class="c2html-lineno"><a name="l150" href="#l150">150 </a></span> passed.set(false); 30.189 -<span class="c2html-lineno"><a name="l151" href="#l151">151 </a></span> } 30.190 -<span class="c2html-lineno"><a name="l152" href="#l152">152 </a></span> 30.191 -<span class="c2html-lineno"><a name="l153" href="#l153">153 </a></span> }); 30.192 -<span class="c2html-lineno"><a name="l154" href="#l154">154 </a></span> <span class="c2html-type">Document</span> doc = reader.read(in); 30.193 -<span class="c2html-lineno"><a name="l155" href="#l155">155 </a></span> <span class="c2html-keyword">if</span> (!passed.get()) { 30.194 -<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> 30.195 -<span class="c2html-lineno"><a name="l157" href="#l157">157 </a></span> <span class="c2html-keyword">throw</span> xmlerror.get(); 30.196 -<span class="c2html-lineno"><a name="l158" href="#l158">158 </a></span> } 30.197 -<span class="c2html-lineno"><a name="l159" href="#l159">159 </a></span> 30.198 -<span class="c2html-lineno"><a name="l160" href="#l160">160 </a></span> doc.normalize(); 30.199 -<span class="c2html-lineno"><a name="l161" href="#l161">161 </a></span> 30.200 -<span class="c2html-lineno"><a name="l162" href="#l162">162 </a></span> <span class="c2html-type">Element</span> root = doc.getRootElement(); 30.201 -<span class="c2html-lineno"><a name="l163" href="#l163">163 </a></span> metadata.read(root.element(<span class="c2html-type">TAG_METADATA</span>)); 30.202 -<span class="c2html-lineno"><a name="l164" href="#l164">164 </a></span> 30.203 -<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>) { 30.204 -<span class="c2html-lineno"><a name="l166" href="#l166">166 </a></span> readGraph(root.element(<span class="c2html-type">TAG_GRAPH</span>)); 30.205 -<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>) { 30.206 -<span class="c2html-lineno"><a name="l168" href="#l168">168 </a></span> readGraph(root.element(<span class="c2html-type">TAG_DIGRAPH</span>)); 30.207 -<span class="c2html-lineno"><a name="l169" href="#l169">169 </a></span> } <span class="c2html-keyword">else</span> { 30.208 -<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>); 30.209 -<span class="c2html-lineno"><a name="l171" href="#l171">171 </a></span> } 30.210 -<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) { 30.211 -<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); 30.212 -<span class="c2html-lineno"><a name="l174" href="#l174">174 </a></span> } 30.213 -<span class="c2html-lineno"><a name="l175" href="#l175">175 </a></span> } 30.214 -<span class="c2html-lineno"><a name="l176" href="#l176">176 </a></span> } 30.215 -</pre> 30.216 - </body> 30.217 -</html> 30.218 -
31.1 --- a/test/v2-regression/plain.html Thu Nov 10 18:44:48 2016 +0100 31.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 31.3 @@ -1,53 +0,0 @@ 31.4 -<!DOCTYPE html> 31.5 -<html> 31.6 - <head> 31.7 - <title>c2html</title> 31.8 - <style type="text/css"> 31.9 - span.c2html-lineno a { 31.10 - font-style: italic; 31.11 - text-decoration: none; 31.12 - color: grey; 31.13 - } 31.14 - span.c2html-keyword { 31.15 - color: blue; 31.16 - } 31.17 - span.c2html-macroconst { 31.18 - color: cornflowerblue; 31.19 - } 31.20 - span.c2html-type { 31.21 - color: cornflowerblue; 31.22 - } 31.23 - span.c2html-directive { 31.24 - color: green; 31.25 - } 31.26 - span.c2html-string { 31.27 - color: darkorange; 31.28 - } 31.29 - span.c2html-comment { 31.30 - color: grey; 31.31 - } 31.32 - span.c2html-stdinclude { 31.33 - color: darkorange; 31.34 - } 31.35 - span.c2html-userinclude { 31.36 - color: darkorange; 31.37 - } 31.38 - a.c2html-userinclude { 31.39 - color: darkorange; 31.40 - text-decoration: underline; 31.41 - } 31.42 - </style> 31.43 - </head> 31.44 - <body> 31.45 - 31.46 -<pre> 31.47 -<span class="c2html-lineno"><a name="l1" href="#l1">1 </a></span> </body> 31.48 -<span class="c2html-lineno"><a name="l2" href="#l2">2 </a></span> </html> 31.49 -<span class="c2html-lineno"><a name="l3" href="#l3">3 </a></span> <!c 31.50 -<span class="c2html-lineno"><a name="l4" href="#l4">4 </a></span> pblock_free(q); 31.51 -<span class="c2html-lineno"><a name="l5" href="#l5">5 </a></span> !> 31.52 -<span class="c2html-lineno"><a name="l6" href="#l6">6 </a></span> 31.53 -</pre> 31.54 - </body> 31.55 -</html> 31.56 -