[PATCH 4/4] Move the variable initialisations closer to use and add blanks

Dirk Hohndel dirk at hohndel.org
Mon Dec 16 09:54:23 UTC 2013


On Mon, 2013-12-16 at 09:06 -0800, Thiago Macieira wrote:
> Makes the code easier to read, IMHO.

Yeah, this is one of those things that are REALLY a matter of taste and
a matter of what you are used to.

I always have preferred this style, having the variables declared as
close to first use as reasonable.

Linus has been much more in the traditional C developer style (having
all variables at the top of the function.

So far I have mostly pushed for us to follow Linus' initial direction,
but with the C++ I had pretty much given up on that.

I will take your patch because I do think it makes things more readable.

Linus - if you have good reasons why we shouldn't do this, please speak
up. 

/D

> Signed-off-by: Thiago Macieira <thiago at macieira.org>
> ---
>  qt-ui/subsurfacewebservices.cpp | 39 +++++++++++++++++----------------------
>  1 file changed, 17 insertions(+), 22 deletions(-)
> 
> diff --git a/qt-ui/subsurfacewebservices.cpp b/qt-ui/subsurfacewebservices.cpp
> index f9b6647..2ff311c 100644
> --- a/qt-ui/subsurfacewebservices.cpp
> +++ b/qt-ui/subsurfacewebservices.cpp
> @@ -106,18 +106,7 @@ QString DivelogsDeWebServices::prepare_dives_for_divelogs(const bool selected, Q
>  		return QString();
>  	}
>  
> -	int i;
> -	struct dive *dive;
> -	FILE *f;
> -	char filename[PATH_MAX];
> -	int streamsize;
> -	char *membuf;
> -	xsltStylesheetPtr xslt = NULL;
> -	xmlDoc *transformed;
> -	struct zip_source *s[dive_table.nr];
> -	struct zip *zip;
> -
> -	xslt = get_stylesheet("divelogs-export.xslt");
> +	xsltStylesheetPtr xslt = get_stylesheet("divelogs-export.xslt");
>  	if (!xslt) {
>  		qDebug() << errPrefix << "missing stylesheet";
>  		return NULL;
> @@ -127,7 +116,7 @@ QString DivelogsDeWebServices::prepare_dives_for_divelogs(const bool selected, Q
>  	QString tempfile = QDir::tempPath() + "/import-" + QString::number(qrand() % 99999999) + ".dld";
>  
>  	int error_code;
> -	zip = zip_open(QFile::encodeName(tempfile), ZIP_CREATE, &error_code);
> +	struct zip *zip = zip_open(QFile::encodeName(tempfile), ZIP_CREATE, &error_code);
>  	if (!zip) {
>  		char buffer[1024];
>  		zip_error_to_str(buffer, sizeof buffer, error_code, errno);
> @@ -137,22 +126,24 @@ QString DivelogsDeWebServices::prepare_dives_for_divelogs(const bool selected, Q
>  	}
>  
>  	/* walk the dive list in chronological order */
> -	for (i = 0; i < dive_table.nr; i++) {
> -		dive = get_dive(i);
> +	for (int i = 0; i < dive_table.nr; i++) {
> +		struct dive *dive = get_dive(i);
>  		if (!dive)
>  			continue;
>  		if (selected && !dive->selected)
>  			continue;
> -		f = tmpfile();
> +
> +		FILE *f = tmpfile();
>  		if (!f) {
>  			*errorMsg = tr("cannot create temporary file: %1").arg(qt_error_string());
>  			goto error_close_zip;
>  		}
>  		save_dive(f, dive);
>  		fseek(f, 0, SEEK_END);
> -		streamsize = ftell(f);
> +		int streamsize = ftell(f);
>  		rewind(f);
> -		membuf = (char *)malloc(streamsize + 1);
> +
> +		char *membuf = (char *)malloc(streamsize + 1);
>  		if (!membuf || !fread(membuf, streamsize, 1, f)) {
>  			*errorMsg = tr("internal error: %1").arg(qt_error_string());
>  			fclose(f);
> @@ -161,6 +152,7 @@ QString DivelogsDeWebServices::prepare_dives_for_divelogs(const bool selected, Q
>  		}
>  		membuf[streamsize] = 0;
>  		fclose(f);
> +
>  		/*
>  		 * Parse the memory buffer into XML document and
>  		 * transform it to divelogs.de format, finally dumping
> @@ -174,17 +166,20 @@ QString DivelogsDeWebServices::prepare_dives_for_divelogs(const bool selected, Q
>  			goto error_close_zip;
>  		}
>  		free((void *)membuf);
> -		transformed = xsltApplyStylesheet(xslt, doc, NULL);
> +
> +		xmlDoc *transformed = xsltApplyStylesheet(xslt, doc, NULL);
>  		xmlDocDumpMemory(transformed, (xmlChar **) &membuf, &streamsize);
>  		xmlFreeDoc(doc);
>  		xmlFreeDoc(transformed);
> +
>  		/*
>  		 * Save the XML document into a zip file.
>  		 */
> +		char filename[PATH_MAX];
>  		snprintf(filename, PATH_MAX, "%d.xml", i + 1);
> -		s[i] = zip_source_buffer(zip, membuf, streamsize, 1);
> -		if (s[i]) {
> -			int64_t ret = zip_add(zip, filename, s[i]);
> +		struct zip_source *s = zip_source_buffer(zip, membuf, streamsize, 1);
> +		if (s) {
> +			int64_t ret = zip_add(zip, filename, s);
>  			if (ret == -1)
>  				qDebug() << errPrefix << "failed to include dive:" << i;
>  		}




More information about the subsurface mailing list