Permalink
Browse files

Remove needless #defines of buffer lengths.

Code had many instances of:

   #undef BUF_LEN
   #define BUF_LEN 64
   char buf[BUF_LEN];
   snprintf (buf, BUF_LEN, ....);

which can be replaced with:

   char buf[64];
   snprintf (buf, sizeof (buf), ....);

which is far cleaner and more difficult to get wrong.



git-svn-id: http://svn.metadecks.org/sweep/trunk@691 e2d53364-18fc-0310-981e-b60a84739af0
  • Loading branch information...
erikd
erikd committed Dec 29, 2009
1 parent 1f4b68b commit 28b0b791349681560e988e12784cabe33d97b712
@@ -652,8 +652,7 @@ ladspa_meta_apply (sw_sample * sample,
static void
ladspa_meta_add_procs (gchar * dir, gchar * name)
{
-#define PATH_LEN 256
- gchar path[PATH_LEN];
+ gchar path[256];
void * module;
LADSPA_Descriptor_Function desc_func;
const LADSPA_Descriptor * d;
@@ -662,7 +661,7 @@ ladspa_meta_add_procs (gchar * dir, gchar * name)
int valid_mask;
sw_procedure * proc;
- snprintf (path, PATH_LEN, "%s/%s", dir, name);
+ snprintf (path, sizeof (path), "%s/%s", dir, name);
module = dlopen (path, RTLD_NOW);
if (!module) return;
View
@@ -56,8 +56,7 @@ about_dialog_create()
*/
GtkWidget *vbox;
GtkWidget *label;
-#define BUF_LEN 64
- gchar buf[BUF_LEN];
+ gchar buf[64];
GtkWidget * button;
GtkWidget * about_image;
GtkWidget * about_ebox;
@@ -97,7 +96,7 @@ about_dialog_create()
* style->font = gdk_font_load("-Adobe-Helvetica-Medium-R-Normal--*-140-*-*-*-*-*-*");
* gtk_widget_push_style(style);
*/
- snprintf (buf, BUF_LEN, "%s %s", _("This is Sweep version"), VERSION);
+ snprintf (buf, sizeof (buf), "%s %s", _("This is Sweep version"), VERSION);
label = gtk_label_new(buf);
gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, TRUE, 0);
gtk_widget_show(label);
View
@@ -138,14 +138,12 @@ static sw_operation dup_channels_op = {
void
dup_channels (sw_sample * sample, int new_channels)
{
-#undef BUF_LEN
-#define BUF_LEN 128
- char buf[BUF_LEN];
+ char buf[128];
if (sample->sounddata->format->channels == 1) {
- g_snprintf (buf, BUF_LEN, _("Duplicate to %d channels"), new_channels);
+ g_snprintf (buf, sizeof (buf), _("Duplicate to %d channels"), new_channels);
} else {
- g_snprintf (buf, BUF_LEN, _("Duplicate from %d to %d channels"),
+ g_snprintf (buf, sizeof (buf), _("Duplicate from %d to %d channels"),
sample->sounddata->format->channels, new_channels);
}
@@ -628,11 +626,9 @@ static sw_operation change_channels_op = {
void
change_channels (sw_sample * sample, int new_channels)
{
-#undef BUF_LEN
-#define BUF_LEN 128
- char buf[BUF_LEN];
+ char buf[128];
- g_snprintf (buf, BUF_LEN, _("Convert from %d to %d channels"),
+ g_snprintf (buf, sizeof (buf), _("Convert from %d to %d channels"),
sample->sounddata->format->channels, new_channels);
schedule_operation (sample, buf, &change_channels_op,
View
@@ -340,8 +340,7 @@ db_ruler_draw_ticks (GtkRuler *ruler)
gint scale; /* Number of units per major unit */
gfloat subd_incr;
gfloat start, end, cur;
-#define UNIT_STR_LEN 32
- gchar unit_str[UNIT_STR_LEN];
+ gchar unit_str[32];
gint digit_height;
gint digit_offset;
gint text_height;
@@ -467,9 +466,9 @@ if (ruler_scale[scale] * abs_increment > 2 * digit_height)
db_cur = 20 * log10 (a_cur);
if (db_cur > -10.0) {
- snprintf (unit_str, UNIT_STR_LEN, "%1.1f", db_cur);
+ snprintf (unit_str, sizeof (unit_str), "%1.1f", db_cur);
} else {
- snprintf (unit_str, UNIT_STR_LEN, "%2.0f", db_cur);
+ snprintf (unit_str, sizeof (unit_str), "%2.0f", db_cur);
}
pango_layout_set_text (layout, unit_str, -1);
View
@@ -142,10 +142,7 @@ static gboolean
sweep_dir_exists (char * pathname)
{
struct stat statbuf;
-
-#undef BUF_LEN
-#define BUF_LEN 512
- char buf[BUF_LEN];
+ char buf[512];
gchar * dirname;
@@ -154,7 +151,7 @@ sweep_dir_exists (char * pathname)
if (dirname && strcmp (dirname, "") && stat (dirname, &statbuf) == -1) {
switch (errno) {
case ENOENT:
- snprintf (buf, BUF_LEN, _("%s does not exist."), dirname);
+ snprintf (buf, sizeof (buf), _("%s does not exist."), dirname);
info_dialog_new (_("Directory does not exist"), NULL, buf);
g_free (dirname);
return FALSE;
@@ -360,13 +357,11 @@ sample_revert_cb (GtkWidget * widget, gpointer data)
{
sw_view * view = (sw_view *)data;
sw_sample * sample;
-#undef BUF_LEN
-#define BUF_LEN 512
- char buf[BUF_LEN];
+ char buf[512];
sample = view->sample;
- snprintf (buf, BUF_LEN,
+ snprintf (buf, sizeof (buf),
_("Are you sure you want to revert %s to\n%s?\n\n"
"All changes and undo information will be lost."),
g_basename (sample->pathname), sample->pathname);
@@ -691,10 +686,7 @@ sample_save_as_cb(GtkWidget * widget, gpointer data)
save_as_data * sd;
-#undef BUF_LEN
-#define BUF_LEN 512
- char buf[BUF_LEN];
-
+ char buf[512];
char * last_save;
win_width = gdk_screen_width () / 2;
@@ -807,7 +799,7 @@ sample_save_as_cb(GtkWidget * widget, gpointer data)
if (access(filename, W_OK) == -1) {
sweep_perror (errno, _("You are not allowed to write to\n%s"), filename);
} else {
- snprintf (buf, BUF_LEN, _("%s exists. Overwrite?"), filename);
+ snprintf (buf, sizeof (buf), _("%s exists. Overwrite?"), filename);
question_dialog_new (sample, _("File exists"), buf,
_("Overwrite"), _("Don't overwrite"),
@@ -867,17 +859,15 @@ sample_save_cb (GtkWidget * widget, gpointer data)
{
sw_view * view = (sw_view *)data;
sw_sample * sample;
-#undef BUF_LEN
-#define BUF_LEN 512
- char buf[BUF_LEN];
+ char buf[512];
sample = view->sample;
if (sample->last_mtime == 0 ||
sample->file_method == SWEEP_FILE_METHOD_MP3) {
sample_save_as_cb (widget, data);
} else if (sample_mtime_changed (sample)) {
- snprintf (buf, BUF_LEN,
+ snprintf (buf, sizeof (buf),
_("%s\n has changed on disk.\n\n"
"Are you sure you want to save?"),
sample->pathname);
View
@@ -86,22 +86,21 @@ file_is_mpeg_audio (const char * pathname)
*
* reimplement this check as a contextual verfification of
* a sequence of frames, starting from somewhere in the middle
- * of a file. in the mean time, raise BUF_LEN from 2048 to 8192
+ * of a file. in the mean time, raise size of buf from 2048 to 8192
* to mitigate the chance of a false negative. (with an increased
* change of a false positive as a result.)
*/
-#define BUF_LEN 8192
- unsigned char buf[BUF_LEN];
+ unsigned char buf[8192];
int n, i;
fd = open (pathname, O_RDONLY);
if (fd == -1) goto out_false;
- n = read (fd, buf, BUF_LEN);
+ n = read (fd, buf, sizeof (buf));
if (n < 4) goto out_false;
/* Check for MPEG frame marker */
- for (i = 0; i < BUF_LEN-1; i++) {
+ for (i = 0; i < sizeof (buf)-1; i++) {
if ((buf[i] & 0xff) == 0xff && (buf[i+1] & 0xe0) == 0xe0) {
goto out_true;
}
@@ -377,9 +376,7 @@ static sw_operation mad_load_op = {
static sw_sample *
sample_load_mad_info (sw_sample * sample, char * pathname)
{
-#undef BUF_LEN
-#define BUF_LEN 128
- char buf[BUF_LEN];
+ char buf[128];
gboolean isnew = (sample == NULL);
@@ -426,7 +423,7 @@ sample_load_mad_info (sw_sample * sample, char * pathname)
trim_registered_ops (sample, 0);
}
- g_snprintf (buf, BUF_LEN, _("Loading %s"), g_basename (sample->pathname));
+ g_snprintf (buf, sizeof (buf), _("Loading %s"), g_basename (sample->pathname));
schedule_operation (sample, buf, &mad_load_op, sample);
View
@@ -52,8 +52,6 @@
#include "sw_chooser.h"
#include "view.h"
-#define BUF_LEN 128
-
extern GtkStyle * style_wb;
#if defined (SNDFILE_1)
@@ -77,11 +75,9 @@ typedef struct {
static void
sweep_sndfile_perror (SNDFILE * sndfile, gchar * pathname)
{
-#undef BUF_LEN
-#define BUF_LEN 128
- char buf[BUF_LEN];
+ char buf[128];
- sf_error_str (sndfile, buf, BUF_LEN);
+ sf_error_str (sndfile, buf, sizeof (buf));
sweep_perror (errno, "libsndfile: %s\n\n%s", buf,
(pathname == NULL) ? "" : pathname);
@@ -622,9 +618,7 @@ _sndfile_sample_load (sw_sample * sample, gchar * pathname, SF_INFO * sfinfo,
gboolean try_raw)
{
SNDFILE * sndfile;
-#undef BUF_LEN
-#define BUF_LEN 128
- char buf[BUF_LEN];
+ char buf[128];
gchar * message;
gboolean isnew = (sample == NULL);
@@ -652,9 +646,9 @@ _sndfile_sample_load (sw_sample * sample, gchar * pathname, SF_INFO * sfinfo,
return NULL;
}
- sf_error_str (NULL, buf, BUF_LEN);
- if (!strncmp (buf, RAW_ERR_STR_1, BUF_LEN) ||
- !strncmp (buf, RAW_ERR_STR_2, BUF_LEN)) {
+ sf_error_str (NULL, buf, sizeof (buf));
+ if (!strncmp (buf, RAW_ERR_STR_1, sizeof (buf)) ||
+ !strncmp (buf, RAW_ERR_STR_2, sizeof (buf))) {
so = g_malloc0 (sizeof(*so));
so->saving = FALSE;
@@ -721,7 +715,7 @@ _sndfile_sample_load (sw_sample * sample, gchar * pathname, SF_INFO * sfinfo,
trim_registered_ops (sample, 0);
}
- g_snprintf (buf, BUF_LEN, _("Loading %s"), g_basename (sample->pathname));
+ g_snprintf (buf, sizeof (buf), _("Loading %s"), g_basename (sample->pathname));
sf = g_malloc0 (sizeof(sf_data));
@@ -946,9 +940,9 @@ static sw_operation sndfile_save_op = {
int
sndfile_sample_save (sw_sample * sample, gchar * pathname)
{
- char buf[BUF_LEN];
+ char buf[128];
- g_snprintf (buf, BUF_LEN, _("Saving %s"), g_basename (pathname));
+ g_snprintf (buf, sizeof (buf), _("Saving %s"), g_basename (pathname));
schedule_operation (sample, buf, &sndfile_save_op, g_strdup (pathname));
View
@@ -82,8 +82,6 @@
#include <speex_callbacks.h>
#endif
-#define BUFFER_LEN 1024
-
#include <glib.h>
#include <gdk/gdkkeysyms.h>
#include <gtk/gtk.h>
@@ -120,8 +118,6 @@
#define HAVE_SPEEX_BETA4
#endif
-#define BUF_LEN 128
-
#define MODE_KEY "Speex_Mode"
#define FEATURES_KEY "Speex_Features"
#define QUALITY_KEY "Speex_Quality"
@@ -526,9 +522,7 @@ static sw_operation speex_load_op = {
static sw_sample *
sample_load_speex_info (sw_sample * sample, char * pathname)
{
-#undef BUF_LEN
-#define BUF_LEN 128
- char buf[BUF_LEN];
+ char buf[128];
gboolean isnew = (sample == NULL);
@@ -574,7 +568,7 @@ sample_load_speex_info (sw_sample * sample, char * pathname)
trim_registered_ops (sample, 0);
}
- g_snprintf (buf, BUF_LEN, _("Loading %s"), g_basename (sample->pathname));
+ g_snprintf (buf, sizeof (buf), _("Loading %s"), g_basename (sample->pathname));
schedule_operation (sample, buf, &speex_load_op, sample);
@@ -957,11 +951,8 @@ speex_sample_save_thread (sw_op_instance * inst)
/* Report success or failure; Calculate and display statistics */
-#undef BUF_LEN
-#define BUF_LEN 16
-
if (remaining <= 0) {
- char time_buf[BUF_LEN], bytes_buf[BUF_LEN];
+ char time_buf[16], bytes_buf[16];
sample_store_and_free_pathname (sample, pathname);
@@ -972,10 +963,10 @@ speex_sample_save_thread (sw_op_instance * inst)
sample->edit_ignore_mtime = FALSE;
sample->modified = FALSE;
- snprint_time (time_buf, BUF_LEN,
+ snprint_time (time_buf, sizeof (time_buf),
frames_to_time (format, nr_frames - remaining));
- snprint_bytes (bytes_buf, BUF_LEN, bytes_written);
+ snprint_bytes (bytes_buf, sizeof (bytes_buf), bytes_written);
average_bitrate =
8.0/1000.0*((double)bytes_written/((double)nr_frames/(double)format->rate));
@@ -988,12 +979,12 @@ speex_sample_save_thread (sw_op_instance * inst)
bytes_buf, time_buf,
average_bitrate);
} else {
- char time_buf[BUF_LEN], bytes_buf[BUF_LEN];
+ char time_buf[16], bytes_buf[16];
- snprint_time (time_buf, BUF_LEN,
+ snprint_time (time_buf, sizeof (time_buf),
frames_to_time (format, nr_frames - remaining));
- snprint_bytes (bytes_buf, BUF_LEN, bytes_written);
+ snprint_bytes (bytes_buf, sizeof (bytes_buf), bytes_written);
average_bitrate =
8.0/1000.0*((double)bytes_written/((double)(nr_frames - remaining)/(double)format->rate));
@@ -1034,11 +1025,9 @@ static sw_operation speex_save_op = {
int
speex_sample_save (sw_sample * sample, char * pathname)
{
-#undef BUF_LEN
-#define BUF_LEN 64
- char buf[BUF_LEN];
+ char buf[64];
- g_snprintf (buf, BUF_LEN, _("Saving %s"), g_basename (pathname));
+ g_snprintf (buf, sizeof (buf), _("Saving %s"), g_basename (pathname));
schedule_operation (sample, buf, &speex_save_op, pathname);
Oops, something went wrong.

0 comments on commit 28b0b79

Please sign in to comment.