commit 397a7f2c3b921120efaf2f64dd2f5ccec48d5a44 Author: Daniel Swanson Date: Fri Jun 4 12:23:03 2021 -0500 drop unused helpers, add copywrite headers, use _unlocked everywhere diff --git a/.travis.yml b/.travis.yml index 68b3f02..81a415b 100644 --- a/.travis.yml +++ b/.travis.yml @@ -9,7 +9,7 @@ compiler: addons: homebrew: packages: - - icu4c + - icu4c before_install: - if [ $TRAVIS_OS_NAME = linux ]; then sudo apt-get install -y swig; else brew install swig; fi diff --git a/CMakeLists.txt b/CMakeLists.txt index 09755dd..faf4e38 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -110,7 +110,7 @@ endif() # Unlocked I/O functions include(CheckSymbolExists) set(CMAKE_REQUIRED_DEFINITIONS -D_POSIX_C_SOURCE=200112 -D_GNU_SOURCE) -foreach(func fread_unlocked fwrite_unlocked fgetc_unlocked fputc_unlocked fputs_unlocked fgetwc_unlocked fputwc_unlocked fputws_unlocked) +foreach(func fread_unlocked fwrite_unlocked fgetc_unlocked fputc_unlocked fputs_unlocked) string(TOUPPER ${func} _uc) CHECK_SYMBOL_EXISTS(${func} "stdio.h" HAVE_DECL_${_uc}) if(HAVE_DECL_${_uc}) diff --git a/lttoolbox/att_compiler.cc b/lttoolbox/att_compiler.cc index 3ce0fb4..ce58ec9 100644 --- a/lttoolbox/att_compiler.cc +++ b/lttoolbox/att_compiler.cc @@ -439,7 +439,7 @@ void AttCompiler::write(FILE *output) { // FILE* output = fopen(file_name, "wb"); - fwrite(HEADER_LTTOOLBOX, 1, 4, output); + fwrite_unlocked(HEADER_LTTOOLBOX, 1, 4, output); uint64_t features = 0; write_le(output, features); diff --git a/lttoolbox/compiler.cc b/lttoolbox/compiler.cc index 851c68b..f7fa14b 100644 --- a/lttoolbox/compiler.cc +++ b/lttoolbox/compiler.cc @@ -943,7 +943,7 @@ Compiler::procRegexp() void Compiler::write(FILE *output) { - fwrite(HEADER_LTTOOLBOX, 1, 4, output); + fwrite_unlocked(HEADER_LTTOOLBOX, 1, 4, output); uint64_t features = 0; write_le(output, features); diff --git a/lttoolbox/compression.h b/lttoolbox/compression.h index c3b20c4..8b5a2b1 100644 --- a/lttoolbox/compression.h +++ b/lttoolbox/compression.h @@ -42,7 +42,7 @@ enum TD_FEATURES : uint64_t { inline auto write_u64(FILE *out, uint64_t value) { - auto rv = fwrite(reinterpret_cast(&value), 1, sizeof(value), out); + auto rv = fwrite_unlocked(reinterpret_cast(&value), 1, sizeof(value), out); if (rv != sizeof(value)) { throw std::runtime_error("Failed to write uint64_t"); } @@ -77,7 +77,7 @@ inline auto write_le(Stream& out, uint64_t value) { inline auto read_u64(FILE *in) { uint64_t value = 0; - if (fread(reinterpret_cast(&value), 1, sizeof(value), in) != sizeof(value)) { + if (fread_unlocked(reinterpret_cast(&value), 1, sizeof(value), in) != sizeof(value)) { throw std::runtime_error("Failed to read uint64_t"); } return value; diff --git a/lttoolbox/fst_processor.cc b/lttoolbox/fst_processor.cc index 0bc6cc6..99aa12f 100644 --- a/lttoolbox/fst_processor.cc +++ b/lttoolbox/fst_processor.cc @@ -1021,7 +1021,7 @@ FSTProcessor::load(FILE *input) fpos_t pos; if (fgetpos(input, &pos) == 0) { char header[4]{}; - fread(header, 1, 4, input); + fread_unlocked(header, 1, 4, input); if (strncmp(header, HEADER_LTTOOLBOX, 4) == 0) { auto features = read_le(input); if (features >= LTF_UNKNOWN) { diff --git a/lttoolbox/input_file.cc b/lttoolbox/input_file.cc index e7dea2f..5ea70e3 100644 --- a/lttoolbox/input_file.cc +++ b/lttoolbox/input_file.cc @@ -1,3 +1,20 @@ +/* + * Copyright (C) 2021 Apertium + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation; either version 2 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, see . + */ + #include #include #include @@ -48,7 +65,7 @@ InputFile::internal_read() return; } int i = 1; - cbuffer[0] = fgetc(infile); + cbuffer[0] = fgetc_unlocked(infile); if (cbuffer[0] == EOF) { ubuffer[buffer_size++] = U_EOF; return; @@ -58,17 +75,17 @@ InputFile::internal_read() } if ((cbuffer[0] & 0xF0) == 0xF0) { i += 3; - if (fread(cbuffer+1, 1, 3, infile) != 3) { + if (fread_unlocked(cbuffer+1, 1, 3, infile) != 3) { throw std::runtime_error("Could not read 3 expected bytes from stream"); } } else if ((cbuffer[0] & 0xE0) == 0xE0) { i += 2; - if (fread(cbuffer+1, 1, 2, infile) != 2) { + if (fread_unlocked(cbuffer+1, 1, 2, infile) != 2) { throw std::runtime_error("Could not read 2 expected bytes from stream"); } } else if ((cbuffer[0] & 0xC0) == 0xC0) { i += 1; - if (fread(cbuffer+1, 1, 1, infile) != 1) { + if (fread_unlocked(cbuffer+1, 1, 1, infile) != 1) { throw std::runtime_error("Could not read 1 expected byte from stream"); } } diff --git a/lttoolbox/input_file.h b/lttoolbox/input_file.h index 56608ca..2afa743 100644 --- a/lttoolbox/input_file.h +++ b/lttoolbox/input_file.h @@ -1,3 +1,20 @@ +/* + * Copyright (C) 2021 Apertium + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation; either version 2 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, see . + */ + #ifndef _LT_INPUT_FILE_H_ #define _LT_INPUT_FILE_H_ diff --git a/lttoolbox/lt_print.cc b/lttoolbox/lt_print.cc index c3c9ec4..8139e02 100644 --- a/lttoolbox/lt_print.cc +++ b/lttoolbox/lt_print.cc @@ -135,7 +135,7 @@ int main(int argc, char *argv[]) fpos_t pos; if (fgetpos(input, &pos) == 0) { char header[4]{}; - fread(header, 1, 4, input); + fread_unlocked(header, 1, 4, input); if (strncmp(header, HEADER_LTTOOLBOX, 4) == 0) { auto features = read_le(input); if (features >= LTF_UNKNOWN) { @@ -179,7 +179,7 @@ int main(int argc, char *argv[]) it->second.show(alphabet, output, 0, hfst); if(it != penum) { - u_fputs("--"_u, output); + u_fprintf(output, "--\n"); } } diff --git a/lttoolbox/lt_trim.cc b/lttoolbox/lt_trim.cc index eabff7c..f685752 100644 --- a/lttoolbox/lt_trim.cc +++ b/lttoolbox/lt_trim.cc @@ -46,7 +46,7 @@ read_fst(FILE *bin_file) fpos_t pos; if (fgetpos(bin_file, &pos) == 0) { char header[4]{}; - fread(header, 1, 4, bin_file); + fread_unlocked(header, 1, 4, bin_file); if (strncmp(header, HEADER_LTTOOLBOX, 4) == 0) { auto features = read_le(bin_file); if (features >= LTF_UNKNOWN) { diff --git a/lttoolbox/my_stdio.h b/lttoolbox/my_stdio.h index 6cf2083..a446278 100644 --- a/lttoolbox/my_stdio.h +++ b/lttoolbox/my_stdio.h @@ -46,20 +46,4 @@ #define fread_unlocked fread #endif -#if !HAVE_DECL_FGETWC_UNLOCKED -#define fgetwc_unlocked fgetwc -#endif - -#if !HAVE_DECL_FPUTWC_UNLOCKED -#define fputwc_unlocked fputwc -#endif - -#if !HAVE_DECL_FPUTWS_UNLOCKED -#define fputws_unlocked fputws -#endif - -#if !HAVE_DECL_UNGETWC_UNLOCKED -#define ungetwc_unlocked ungetwc -#endif - #endif diff --git a/lttoolbox/tmx_compiler.cc b/lttoolbox/tmx_compiler.cc index b013e98..5ca3b8d 100644 --- a/lttoolbox/tmx_compiler.cc +++ b/lttoolbox/tmx_compiler.cc @@ -433,7 +433,7 @@ TMXCompiler::procNode() void TMXCompiler::write(FILE *output) { - fwrite(HEADER_LTTOOLBOX, 1, 4, output); + fwrite_unlocked(HEADER_LTTOOLBOX, 1, 4, output); uint64_t features = 0; write_le(output, features); diff --git a/lttoolbox/trans_exe.cc b/lttoolbox/trans_exe.cc index 75ae212..4dcc5aa 100644 --- a/lttoolbox/trans_exe.cc +++ b/lttoolbox/trans_exe.cc @@ -71,7 +71,7 @@ TransExe::read(FILE *input, Alphabet const &alphabet) fpos_t pos; if (fgetpos(input, &pos) == 0) { char header[4]{}; - fread(header, 1, 4, input); + fread_unlocked(header, 1, 4, input); if (strncmp(header, HEADER_TRANSDUCER, 4) == 0) { auto features = read_le(input); if (features >= TDF_UNKNOWN) { diff --git a/lttoolbox/transducer.cc b/lttoolbox/transducer.cc index e27c972..99ea468 100644 --- a/lttoolbox/transducer.cc +++ b/lttoolbox/transducer.cc @@ -535,7 +535,7 @@ bool Transducer::weighted() { void Transducer::write(FILE *output, int const decalage) { - fwrite(HEADER_TRANSDUCER, 1, 4, output); + fwrite_unlocked(HEADER_TRANSDUCER, 1, 4, output); bool write_weights = weighted(); @@ -596,7 +596,7 @@ Transducer::read(FILE *input, int const decalage) fpos_t pos; if (fgetpos(input, &pos) == 0) { char header[4]{}; - fread(header, 1, 4, input); + fread_unlocked(header, 1, 4, input); if (strncmp(header, HEADER_TRANSDUCER, 4) == 0) { auto features = read_le(input); if (features >= TDF_UNKNOWN) { diff --git a/lttoolbox/ustring.cc b/lttoolbox/ustring.cc index dd6130a..b2081a3 100644 --- a/lttoolbox/ustring.cc +++ b/lttoolbox/ustring.cc @@ -1,18 +1,28 @@ +/* + * Copyright (C) 2021 Apertium + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation; either version 2 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, see . + */ + #include "ustring.h" #include -#include #include #include using namespace icu; -void -u_fputs(const UString& str, UFILE* output) -{ - u_fputs(str.c_str(), output); -} - void write(const UString& str, UFILE* output) { @@ -51,12 +61,3 @@ to_ustring(const char* s) utf8::utf8to16(s, s+sz, std::back_inserter(ret)); return ret; } - -const char* -to_char(const UString& str) -{ - std::string stemp; - UnicodeString utemp = str.c_str(); - utemp.toUTF8String(stemp); - return stemp.c_str(); -} diff --git a/lttoolbox/ustring.h b/lttoolbox/ustring.h index 907e5e9..b808ed2 100644 --- a/lttoolbox/ustring.h +++ b/lttoolbox/ustring.h @@ -1,3 +1,20 @@ +/* + * Copyright (C) 2021 Apertium + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation; either version 2 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, see . + */ + #ifndef _LT_USTRING_H_ #define _LT_USTRING_H_ @@ -7,8 +24,6 @@ typedef std::basic_string UString; -void u_fputs(const UString& str, UFILE* output); - void write(const UString& str, UFILE* output); // like std::stoi, throws invalid_argument if unable to parse @@ -20,9 +35,6 @@ double stod(const UString& str); // for command-line arguments UString to_ustring(const char* str); -// for interfacing with e.g. XML library -const char* to_char(const UString& str); - static std::ostream& operator<<(std::ostream& ostr, const UString& str) { std::string res;