Fix compilation on old gcc (#606)

* Fixing char* use in FLUID
* Fixing const cast
This commit is contained in:
Matthias Melcher 2022-12-22 00:18:01 +01:00 committed by GitHub
parent 1d212b7a03
commit d98c663893
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 36 additions and 12 deletions

View File

@ -91,7 +91,7 @@ public:
private:
void init();
void alloc_buf(int size);
void alloc_buf(int size, bool preserve_text=false);
void release();
public:
@ -99,10 +99,12 @@ public:
void value(const char *str, int slen);
const char *value() const { return value_; }
char *buffer() { return value_; }
int size() const { return size_; }
int slen() const;
int capacity() const;
void capacity(int num_bytes);
void debug(const char *info = 0) const; // output string info
void hexdump(const char *info = 0) const; // output string info + hexdump

View File

@ -612,7 +612,7 @@ int write_code(const char *s, const char *t) {
write_c("// Initialize I18N stuff now for menus...\n");
write_c("#%sinclude <locale.h>\n", indent());
write_c("static char *_locale = setlocale(LC_MESSAGES, \"\");\n");
write_c("static nl_catd _catalog = catopen(\"%s\", 0);\n", P.i18n_program);
write_c("static nl_catd _catalog = catopen(\"%s\", 0);\n", P.i18n_program.value());
}
}
if (conditional) {

View File

@ -254,7 +254,6 @@ Project P;
Project::Project() :
i18n_type(0),
i18n_program(""),
include_H_from_C(1),
use_FL_COMMAND(0),
utf8_in_src(0),
@ -277,7 +276,7 @@ void Project::reset() {
i18n_static_function = "";
i18n_file = "";
i18n_set = "";
i18n_program[0] = 0;
i18n_program = "";
include_H_from_C = 1;
use_FL_COMMAND = 0;
utf8_in_src = 0;
@ -998,8 +997,9 @@ int write_code_files() {
}
char cname[FL_PATH_MAX+1];
char hname[FL_PATH_MAX+1];
strlcpy(P.i18n_program, fl_filename_name(filename), FL_PATH_MAX);
fl_filename_setext(P.i18n_program, FL_PATH_MAX, "");
P.i18n_program = fl_filename_name(filename);
P.i18n_program.capacity(FL_PATH_MAX);
fl_filename_setext(P.i18n_program.buffer(), FL_PATH_MAX, "");
if (P.code_file_name[0] == '.' && strchr(P.code_file_name, '/') == NULL) {
strlcpy(cname, fl_filename_name(filename), FL_PATH_MAX);
fl_filename_setext(cname, FL_PATH_MAX, P.code_file_name);
@ -1878,8 +1878,9 @@ void update_sourceview_cb(Fl_Button*, void*)
sv_strings->buffer()->loadfile(fn);
sv_strings->scroll(top, 0);
} else if (sv_source->visible_r() || sv_header->visible_r()) {
strlcpy(P.i18n_program, fl_filename_name(sv_source_filename), FL_PATH_MAX);
fl_filename_setext(P.i18n_program, FL_PATH_MAX, "");
P.i18n_program = fl_filename_name(sv_source_filename);
P.i18n_program.capacity(FL_PATH_MAX);
fl_filename_setext(P.i18n_program.buffer(), FL_PATH_MAX, "");
Fd_String code_file_name_bak = P.code_file_name;
P.code_file_name = sv_source_filename;
Fd_String header_file_name_bak = P.header_file_name;

View File

@ -105,7 +105,7 @@ public:
Fd_String i18n_static_function;
Fd_String i18n_file;
Fd_String i18n_set;
char i18n_program[FL_PATH_MAX+1];
Fd_String i18n_program;
int include_H_from_C;
int use_FL_COMMAND;
int utf8_in_src;

View File

@ -304,7 +304,7 @@ void Fl_JPEG_Image::load_jpg_(const char *filename, const char *sharename, const
if (data_length==-1)
jpeg_unprotected_mem_src(&dinfo, data);
else
jpeg_mem_src(&dinfo, data, (size_t)data_length);
jpeg_mem_src(&dinfo, const_cast<unsigned char*>data, (size_t)data_length);
}
jpeg_read_header(&dinfo, TRUE);

View File

@ -69,7 +69,12 @@ Fl_String::~Fl_String() {
delete[] value_;
}
void Fl_String::alloc_buf(int size) {
/** Grow the buffer size to at least size+1 bytes.
By default, this call destroys the contents of the current buffer.
\param size in bytes
\param preserve_text copy existing text into the new buffer
*/
void Fl_String::alloc_buf(int size, bool preserve_text) {
if (size < 0)
return;
if (size > 0 && size <= capacity_)
@ -79,7 +84,14 @@ void Fl_String::alloc_buf(int size) {
char *new_value = new char[new_size];
capacity_ = new_size - 1;
size_ = 0;
if (preserve_text) {
size_ = (int)strlen(value_);
// the new buffer always has a higher capacity than the old one
// make sure we copy the trailing NUL.
memcpy(new_value, value_, size_+1);
} else {
size_ = 0;
}
delete[] value_;
value_ = new_value;
}
@ -111,6 +123,15 @@ int Fl_String::capacity() const {
return capacity_; // > 0 ? capacity_ - 1 : capacity_;
}
/** Set the minumum capacity to num_bytes plus one for a terminating NUL.
The cintents of the string buffer will be copied if needed.
\param num_bytes minimum size of buffer
*/
void Fl_String::capacity(int num_bytes) {
alloc_buf(num_bytes, true);
}
void Fl_String::release() {
delete[] value_;
value_ = 0;