curvedraw

changeset 10:95fada20c638

foo
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 19 Dec 2015 17:40:27 +0200
parents c2008ba88d03
children 099fd7adb900
files src/app.cc src/curvefile.cc src/curvefile.h
diffstat 3 files changed, 34 insertions(+), 19 deletions(-) [+]
line diff
     1.1 --- a/src/app.cc	Sat Dec 19 08:00:53 2015 +0200
     1.2 +++ b/src/app.cc	Sat Dec 19 17:40:27 2015 +0200
     1.3 @@ -257,11 +257,29 @@
     1.4  		case 'e':
     1.5  		case 'E':
     1.6  			// TODO: GUI for filename at least
     1.7 -			if(!save_curves(stdout, &curves[0], (int)curves.size())) {
     1.8 +			if(!save_curves("test.curves", &curves[0], (int)curves.size())) {
     1.9  				fprintf(stderr, "failed to export curves\n");
    1.10  			}
    1.11  			printf("exported %d curves\n", (int)curves.size());
    1.12  			break;
    1.13 +
    1.14 +		case 'i':
    1.15 +		case 'I':
    1.16 +			{
    1.17 +				std::list<Curve*> clist = load_curves("test.curves");
    1.18 +				if(clist.empty()) {
    1.19 +					fprintf(stderr, "failed to import curves\n");
    1.20 +				}
    1.21 +
    1.22 +				int num = 0;
    1.23 +				std::list<Curve*>::iterator it = clist.begin();
    1.24 +				while(it != clist.end()) {
    1.25 +					curves.push_back(*it++);
    1.26 +					++num;
    1.27 +				}
    1.28 +				printf("imported %d curves\n", num);
    1.29 +			}
    1.30 +			break;
    1.31  		}
    1.32  	}
    1.33  
     2.1 --- a/src/curvefile.cc	Sat Dec 19 08:00:53 2015 +0200
     2.2 +++ b/src/curvefile.cc	Sat Dec 19 17:40:27 2015 +0200
     2.3 @@ -53,12 +53,13 @@
     2.4  	return true;
     2.5  }
     2.6  
     2.7 -Curve **load_curves(const char *fname, int *countret)
     2.8 +std::list<Curve*> load_curves(const char *fname)
     2.9  {
    2.10 +	std::list<Curve*> res;
    2.11  	FILE *fp = fopen(fname, "r");
    2.12 -	if(!fp) return false;
    2.13 +	if(!fp) return res;
    2.14  
    2.15 -	Curve **res = load_curves(fp, countret);
    2.16 +	res = load_curves(fp);
    2.17  	fclose(fp);
    2.18  	return res;
    2.19  }
    2.20 @@ -167,33 +168,28 @@
    2.21  	return 0;
    2.22  }
    2.23  
    2.24 -Curve **load_curves(FILE *fp, int *countret)
    2.25 +std::list<Curve*> load_curves(FILE *fp)
    2.26  {
    2.27 +	std::list<Curve*> curves;
    2.28  	if(!expect_str(fp, "GCURVES")) {
    2.29  		fprintf(stderr, "load_curves: failed to load, invalid file format\n");
    2.30 -		return 0;
    2.31 +		return curves;
    2.32  	}
    2.33  
    2.34 -	std::vector<Curve*> curves;
    2.35  	Curve *curve;
    2.36 -
    2.37  	while((curve = curve_block(fp))) {
    2.38  		curves.push_back(curve);
    2.39  	}
    2.40  
    2.41  	int ncurves = (int)curves.size();
    2.42  	if(!feof(fp)) {
    2.43 -		for(int i=0; i<ncurves; i++) {
    2.44 -			delete curves[i];
    2.45 +		std::list<Curve*>::iterator it = curves.begin();
    2.46 +		while(it != curves.end()) {
    2.47 +			delete *it++;
    2.48  		}
    2.49 -		return 0;
    2.50 +		return curves;
    2.51  	}
    2.52  
    2.53 -	Curve **carr = new Curve*[ncurves];
    2.54 -	for(int i=0; i<ncurves; i++) {
    2.55 -		carr[i] = curves[i];
    2.56 -	}
    2.57 -	*countret = ncurves;
    2.58 -	return carr;
    2.59 +	return curves;
    2.60  }
    2.61  
     3.1 --- a/src/curvefile.h	Sat Dec 19 08:00:53 2015 +0200
     3.2 +++ b/src/curvefile.h	Sat Dec 19 17:40:27 2015 +0200
     3.3 @@ -2,12 +2,13 @@
     3.4  #define CURVEFILE_H_
     3.5  
     3.6  #include <stdio.h>
     3.7 +#include <list>
     3.8  #include "curve.h"
     3.9  
    3.10  bool save_curves(const char *fname, const Curve * const *curves, int count);
    3.11  bool save_curves(FILE *fp, const Curve * const *curves, int count);
    3.12  
    3.13 -Curve **load_curves(const char *fname, int *countret);
    3.14 -Curve **load_curves(FILE *fp, int *countret);
    3.15 +std::list<Curve*> load_curves(const char *fname);
    3.16 +std::list<Curve*> load_curves(FILE *fp);
    3.17  
    3.18  #endif	// CURVEFILE_H_
    3.19 \ No newline at end of file