1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/error.c Wed Nov 01 21:00:33 2023 +0100 1.3 @@ -0,0 +1,71 @@ 1.4 +/* 1.5 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 1.6 + * Copyright 2023 Mike Becker. All rights reserved. 1.7 + * 1.8 + * Redistribution and use in source and binary forms, with or without 1.9 + * modification, are permitted provided that the following conditions are met: 1.10 + * 1.11 + * 1. Redistributions of source code must retain the above copyright 1.12 + * notice, this list of conditions and the following disclaimer. 1.13 + * 1.14 + * 2. Redistributions in binary form must reproduce the above copyright 1.15 + * notice, this list of conditions and the following disclaimer in the 1.16 + * documentation and/or other materials provided with the distribution. 1.17 + * 1.18 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 1.19 + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1.20 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 1.21 + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 1.22 + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 1.23 + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 1.24 + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 1.25 + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 1.26 + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 1.27 + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 1.28 + * POSSIBILITY OF SUCH DAMAGE. 1.29 + */ 1.30 + 1.31 +#include "ascension/context.h" 1.32 +#include "ascension/error.h" 1.33 +#include "ascension/utils.h" 1.34 + 1.35 +#include <cx/buffer.h> 1.36 + 1.37 +void asc_error_cchar(char const* text) { 1.38 + asc_error_cxstr(cx_str(text)); 1.39 +} 1.40 + 1.41 +void asc_error_cuchar(unsigned char const* text) { 1.42 + asc_error_cxstr(cx_str((char const*)text)); 1.43 +} 1.44 + 1.45 +void asc_error_cxstr(cxstring text) { 1.46 + if (text.length == 0) return; 1.47 + 1.48 + // write error to debug output 1.49 + asc_dprintf("ERROR: %*.s", (int)text.length, text.ptr); 1.50 + 1.51 + // write error to buffer 1.52 + CxBuffer* buf = &asc_context.error_buffer; 1.53 + cxBufferWrite(text.ptr, 1, text.length, buf); 1.54 + cxBufferPut(buf, '\n'); 1.55 + 1.56 + asc_set_flag(&asc_context.flags, ASC_FLAG_HAS_ERROR); 1.57 +} 1.58 + 1.59 +bool asc_has_error(void) { 1.60 + return asc_test_flag(asc_context.flags, ASC_FLAG_HAS_ERROR); 1.61 +} 1.62 + 1.63 +char const* asc_get_error(void) { 1.64 + // we zero-terminate the current buffer contents before providing them 1.65 + cxBufferPut(&asc_context.error_buffer, 0); 1.66 + --asc_context.error_buffer.pos; 1.67 + --asc_context.error_buffer.size; 1.68 + return asc_context.error_buffer.space; 1.69 +} 1.70 + 1.71 +void asc_clear_error(void) { 1.72 + cxBufferClear(&asc_context.error_buffer); 1.73 + asc_clear_flag(&asc_context.flags, ASC_FLAG_HAS_ERROR); 1.74 +}