Skip to content

Commit

Permalink
[hyper] Reduce uses of sprintf (#52)
Browse files Browse the repository at this point in the history
In many cases, a better and safer alternative is provided by C++20's `std::format` functionality.
  • Loading branch information
GabrielDosReis authored Jul 19, 2024
1 parent 1202b30 commit 298cfee
Show file tree
Hide file tree
Showing 16 changed files with 130 additions and 163 deletions.
11 changes: 6 additions & 5 deletions src/hyper/ReadBitmap.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
Copyright (C) 1991-2002, The Numerical Algorithms Group Ltd.
All rights reserved.
Copyright (C) 2007-2023, Gabriel Dos Reis.
Copyright (C) 2007-2024, Gabriel Dos Reis.
All rights reserved.
Redistribution and use in source and binary forms, with or without
Expand Down Expand Up @@ -34,6 +34,7 @@
*/

#include "openaxiom-c-macros.h"
#include <string.h>
#include "debug.h"
#include "halloc.h"
#include "sockio.h"
Expand All @@ -51,9 +52,9 @@ static int read_w_and_h(FILE * fd , unsigned int * width , unsigned int * height
* XReadBitmapFile does not seeem to work to well (whatecer that means).
*/

XImage *
HTReadBitmapFile(Display *display,int screen,char * filename,
int *width, int *height)
XImage*
HTReadBitmapFile(Display* display, int screen, const char* filename,
int* width, int* height)
{
XImage *image;
FILE *fd;
Expand Down Expand Up @@ -251,7 +252,7 @@ insert_image_struct(char *filename)

/* strcpy(image->filename, filename); */

sprintf(image->filename, "%s", filename);
strcpy(image->filename, filename);
hash_insert(&gImageHashTable,(char *) image, image->filename);
}
return image;
Expand Down
8 changes: 3 additions & 5 deletions src/hyper/event.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
Copyright (C) 1991-2002, The Numerical Algorithms Group Ltd.
All rights reserved.
Copyright (C) 2007-2023, Gabriel Dos Reis.
Copyright (C) 2007-2024, Gabriel Dos Reis.
All rights reserved.
Redistribution and use in source and binary forms, with or without
Expand Down Expand Up @@ -35,6 +35,7 @@

#define _EVENT_C

#include <format>
#include "openaxiom-c-macros.h"

#include <X11/X.h>
Expand Down Expand Up @@ -264,10 +265,7 @@ paste_button(PasteNode * paste)
static void
killAxiomPage(HyperDocPage * page)
{
char command[512];

sprintf(command, "(|htpDestroyPage| '%s)", page->name);
send_lisp_command(command);
send_lisp_command(std::format("(|htpDestroyPage| '{})", page->name).c_str());
}

static void
Expand Down
7 changes: 4 additions & 3 deletions src/hyper/extent2.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
Copyright (C) 1991-2002, The Numerical Algorithms Group Ltd.
All rights reserved.
Copyright (C) 2007-2023, Gabriel Dos Reis.
Copyright (C) 2007-2024, Gabriel Dos Reis.
All rights reserved.
Redistribution and use in source and binary forms, with or without
Expand Down Expand Up @@ -42,6 +42,7 @@
****************************************************************************/

#include "openaxiom-c-macros.h"
#include <string.h>
#include "debug.h"
#include "halloc.h"
#include "sockio.h"
Expand Down Expand Up @@ -862,7 +863,7 @@ insert_bitmap_file(TextNode * node)
image->height = image->image.xi->height;
image->filename = (char *) halloc(sizeof(char) * strlen(filename) +1,"Image Filename");
/* strcpy(image->filename, filename); */
sprintf(image->filename, "%s", filename);
strcpy(image->filename, filename);
hash_insert(&gImageHashTable, (char *)image, image->filename);
}
node->width = image->width;
Expand Down Expand Up @@ -909,7 +910,7 @@ insert_pixmap_file(TextNode * node)
image->filename = (char *) halloc(sizeof(char) * strlen(filename) +1,
"insert_pixmap--filename");
/* strcpy(image->filename, filename); */
sprintf(image->filename, "%s", filename);
strcpy(image->filename, filename);
image->image.xi = xi;
hash_insert(&gImageHashTable, (char *)image, image->filename);
}
Expand Down
14 changes: 7 additions & 7 deletions src/hyper/htadd.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
Copyright (C) 1991-2002, The Numerical Algorithms Group Ltd.
All rights reserved.
Copyright (C) 2007-2023, Gabriel Dos Reis.
Copyright (C) 2007-2024, Gabriel Dos Reis.
All rights reserved.
Redistribution and use in source and binary forms, with or without
Expand Down Expand Up @@ -35,7 +35,7 @@

/* HyperDoc database file manager */


#include <format>
#include "openaxiom-c-macros.h"

#include <errno.h>
Expand Down Expand Up @@ -186,7 +186,7 @@ build_db_filename(short flag, char *db_dir, char *dbfilename)
int ret_status;
char *SPAD;
struct stat buff;
char path[256];
std::string path;


if (flag & System) {
Expand All @@ -197,15 +197,15 @@ build_db_filename(short flag, char *db_dir, char *dbfilename)
exit(-1);
}
sprintf(dbfilename, "%s/share/hypertex/pages/%s", SPAD, db_file_name);
sprintf(path, "%s/share/hypertex/pages", SPAD);
path = std::format("{}/share/hypertex/pages", SPAD);
}
else if (flag & Named) {
sprintf(dbfilename, "%s/%s", db_dir, db_file_name);
strcpy(path, db_dir);
path = db_dir;
}
else { /* use the current directory */
sprintf(dbfilename, "./%s", db_file_name);
sprintf(path, "./");
path = "./";
}
/* fprintf(stderr,"htadd:build_db_filename:dbfilename=%s\n",dbfilename);*/
/* Now see if I can write to the file */
Expand All @@ -214,7 +214,7 @@ build_db_filename(short flag, char *db_dir, char *dbfilename)
if (ret_status == -1) {
if (errno == ENOENT)
/* If the file does not exist, check the path. */
ret_status = stat(path, &buff);
ret_status = stat(path.c_str(), &buff);
if (ret_status == -1) {
perror("build_db_file");
exit(1);
Expand Down
42 changes: 18 additions & 24 deletions src/hyper/htinp.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
Copyright (C) 1991-2002, The Numerical Algorithms Group Ltd.
All rights reserved.
Copyright (C) 2007-2023, Gabriel Dos Reis.
Copyright (C) 2007-2024, Gabriel Dos Reis.
All rights reserved.
Redistribution and use in source and binary forms, with or without
Expand Down Expand Up @@ -33,6 +33,8 @@
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#include <format>

#include <sys/stat.h>
#include <sys/signal.h>

Expand Down Expand Up @@ -70,9 +72,6 @@ int num_active_files = 0;
char *inactive_file_list[MaxInputFiles];
int num_inactive_files = 0;
int include_bf = 0;
char buf_for_record_commands[256];



void
make_record()
Expand All @@ -82,9 +81,9 @@ make_record()
send_lisp_command("(setq |$testingSystem| T)");
send_lisp_command("(setq |$printLoadMsgs| NIL)");
send_lisp_command("(setq |$BreakMode| '|resume|)");
sprintf(buf_for_record_commands,"(|inputFile2RecordFile| '\"%s\")",input_file_list[i]);
fprintf(stderr,"%s\n",buf_for_record_commands);
send_lisp_command(buf_for_record_commands);
auto cmd = std::format("(|inputFile2RecordFile| '\"{}\")", input_file_list[i]);
fprintf(stderr,"%s\n",cmd.c_str());
send_lisp_command(cmd.c_str());
}
if (kill_spad){
auto status = connect_spad();
Expand All @@ -102,9 +101,9 @@ verify_record()
send_lisp_command("(setq |$testingSystem| T)");
send_lisp_command("(setq |$printLoadMsgs| NIL)");
send_lisp_command("(setq |$BreakMode| '|resume|)");
sprintf(buf_for_record_commands,"(|verifyRecordFile| '\"%s\")",input_file_list[i]);
fprintf(stderr,"%s\n",buf_for_record_commands);
send_lisp_command(buf_for_record_commands);
auto cmd = std::format("(|verifyRecordFile| '\"{}\")", input_file_list[i]);
fprintf(stderr,"%s\n", cmd.c_str());
send_lisp_command(cmd.c_str());
}
if (kill_spad) {
auto status = connect_spad();
Expand Down Expand Up @@ -416,9 +415,9 @@ get_graph_output(char* command, const char* pagename, TokenType com_type)
get_string_buf(spad_socket, buf, 1024);
}
unescape_string(command);
sprintf(buf, "(|processInteractive| '(|write| |%s| \"%s%d\" \"image\") NIL)", "%",
auto cmd = std::format("(|processInteractive| '(|write| |{}| \"{}{}\" \"image\") NIL)", "%",
pagename, example_number);
send_lisp_command(buf);
send_lisp_command(cmd.c_str());
send_lisp_command("(|setViewportProcess|)");
send_lisp_command("(|processInteractive| '(|close| (|%%| -3)) NIL)");
send_lisp_command("(|waitForViewport|)");
Expand All @@ -427,27 +426,22 @@ get_graph_output(char* command, const char* pagename, TokenType com_type)
static void
send_command(char *command, TokenType com_type)
{
char buf[1024];

if (com_type != TokenType::Spadsrc) {
escape_string(command);
sprintf(buf, "(|parseAndEvalToHypertex| '\"%s\")", command);
send_lisp_command(buf);
auto buf = std::format("(|parseAndEvalToHypertex| '\"{}\")", command);
send_lisp_command(buf.c_str());
}
else {
FILE *f;
char name[512], str[512]/*, *c*/;

sprintf(name, "/tmp/hyper%s.input", oa_getenv("SPADNUM"));
f = fopen(name, "w");
auto name = std::format("/tmp/hyper{}.input", oa_getenv("SPADNUM"));
FILE* f = fopen(name.c_str(), "w");
if (f == NULL) {
fprintf(stderr, "Can't open temporary input file %s\n", name);
fprintf(stderr, "Can't open temporary input file %s\n", name.c_str());
return;
}
fprintf(f, "%s", command);
fclose(f);
sprintf(str, "(|parseAndEvalToHypertex| '\")read %s\")", name);
send_lisp_command(str);
auto str = std::format("(|parseAndEvalToHypertex| '\")read {}\")", name);
send_lisp_command(str.c_str());
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/hyper/hyper.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
Copyright (C) 1991-2002, The Numerical Algorithms Group Ltd.
All rights reserved.
Copyright (C) 2007-2023, Gabriel Dos Reis.
Copyright (C) 2007-2024, Gabriel Dos Reis.
All rights reserved.
Redistribution and use in source and binary forms, with or without
Expand Down Expand Up @@ -94,7 +94,7 @@ extern void change_input_focus(HyperLink * link);
extern void next_input_focus();
extern void prev_input_focus();
extern int delete_item(char * name);
extern XImage * HTReadBitmapFile(Display * display , int screen , char * filename , int * width , int * height);
extern XImage* HTReadBitmapFile(Display* display , int screen , const char* filename , int* width , int* height);
extern ImageStruct * insert_image_struct(char * filename);
extern void compute_form_page(HyperDocPage * page);
extern int window_width(int cols);
Expand Down
8 changes: 3 additions & 5 deletions src/hyper/keyin.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
Copyright (C) 1991-2002, The Numerical Algorithms Group Ltd.
All rights reserved.
Copyright (C) 2007-2023, Gabriel Dos Reis.
Copyright (C) 2007-2024, Gabriel Dos Reis.
All rights reserved.
Redistribution and use in source and binary forms, with or without
Expand Down Expand Up @@ -41,6 +41,7 @@
*
****************************************************************************/

#include <format>
#include <X11/keysym.h>
#include "openaxiom-c-macros.h"
#include "debug.h"
Expand Down Expand Up @@ -132,8 +133,6 @@ handle_key(XEvent *event)
/*char *head = "echo htadd -l ";*/
/*char *blank1 = " ";*/
/*char *blank2 = " \n";*/
char buffer[180];

charcount = XLookupString((XKeyEvent *)event, key_buffer, key_buffer_size, &keysym ,&compstatus); /* 5 args */

key_buffer[charcount] = '\0';
Expand All @@ -154,8 +153,7 @@ handle_key(XEvent *event)
if (event->xkey.state & ShiftMask) {
name = gWindow->page->name;
filename = gWindow->page->filename;
sprintf(buffer, "htadd -l %s\n", filename);
system(buffer);
system(std::format("htadd -l {}\n", filename).c_str());
if (auto ptr = gFileHashTable.find(filename); ptr != gFileHashTable.end()) {
fclose(ptr->second);
gFileHashTable.erase(ptr);
Expand Down
28 changes: 13 additions & 15 deletions src/hyper/lex.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
Copyright (C) 1991-2002, The Numerical Algorithms Group Ltd.
All rights reserved.
Copyright (C) 2007-2023, Gabriel Dos Reis.
Copyright (C) 2007-2024, Gabriel Dos Reis.
All rights reserved.
Redistribution and use in source and binary forms, with or without
Expand Down Expand Up @@ -63,6 +63,7 @@
#include <string.h>
#include <stack>
#include <vector>
#include <format>

#include "debug.h"
#include "sockio.h"
Expand Down Expand Up @@ -876,7 +877,7 @@ token_name(TokenType type)
strcpy(ebuffer, "~");
break;
case TokenType::Cond:
sprintf(ebuffer, "\\%s", token.id);
strcpy(ebuffer, std::format("\\{}", token.id).c_str());
break;
case TokenType::Icorrection:
strcpy(ebuffer, "\\/");
Expand All @@ -888,7 +889,8 @@ token_name(TokenType type)
strcpy(ebuffer, "\\begin{patch}");
break;
default:
sprintf(ebuffer, " %d ", type);
strcpy(ebuffer, std::format(" {} ", static_cast<int>(type)).c_str());
break;
}
}
}
Expand Down Expand Up @@ -926,31 +928,27 @@ print_next_ten_tokens()
void
print_page_and_filename()
{
char obuff[128];

std::string obuff;
if (gPageBeingParsed->type == TokenType::Normal) {

/*
* Now try to inform the user as close to possible where the error
* occurred
*/
sprintf(obuff, "(HyperDoc) While parsing %s on line %d\n\tin the file %s\n",
gPageBeingParsed->name, line_number,
gPageBeingParsed->filename);
obuff = std::format("(HyperDoc) While parsing {} on line {}\n\tin the file {}\n",
gPageBeingParsed->name, line_number,
gPageBeingParsed->filename);
}
else if (gPageBeingParsed->type == TokenType::SpadGen) {
sprintf(obuff, "While parsing %s from the Spad socket\n",
gPageBeingParsed->name);
obuff = std::format("While parsing {} from the Spad socket\n", gPageBeingParsed->name);
}
else if (gPageBeingParsed->type == TokenType::Unixfd) {
sprintf(obuff, "While parsing %s from a Unixpipe\n",
gPageBeingParsed->name);
obuff = std::format("While parsing {} from a Unixpipe\n", gPageBeingParsed->name);
}
else {
/* Unknown page type */
sprintf(obuff, "While parsing %s\n", gPageBeingParsed->name);
obuff = std::format("While parsing {}\n", gPageBeingParsed->name);
}
fprintf(stderr, "%s", obuff);
fprintf(stderr, "%s", obuff.c_str());
}


Expand Down
Loading

0 comments on commit 298cfee

Please sign in to comment.