mirror of
git://projects.qi-hardware.com/cae-tools.git
synced 2025-04-21 12:27:27 +03:00
Introducing cameo, a toolbox of CAM operations.
This commit is contained in:
108
cameo/gnuplot.c
Normal file
108
cameo/gnuplot.c
Normal file
@@ -0,0 +1,108 @@
|
||||
/*
|
||||
* gnuplot.c - Gnuplot file input/output
|
||||
*
|
||||
* Written 2010 by Werner Almesberger
|
||||
* Copyright 2010 Werner Almesberger
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*/
|
||||
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#include "path.h"
|
||||
#include "gnuplot.h"
|
||||
|
||||
|
||||
struct path *gnuplot_read(const char *name, double r_tool_default)
|
||||
{
|
||||
FILE *file;
|
||||
int lineno = 0;
|
||||
char buf[1024];
|
||||
double x, y, z, tmp;
|
||||
double r_tool = r_tool_default;
|
||||
int n;
|
||||
struct path *paths = NULL, *path = NULL;
|
||||
struct path **lnk = &paths;
|
||||
|
||||
file = name ? fopen(name, "r") : stdin;
|
||||
if (!file) {
|
||||
perror(name);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
while (fgets(buf, sizeof(buf), file)) {
|
||||
lineno++;
|
||||
if (sscanf(buf, "#%%r_tool=%lf\n", &tmp) == 1)
|
||||
r_tool = tmp;
|
||||
if (*buf == '#')
|
||||
continue;
|
||||
n = sscanf(buf, "%lf %lf %lf\n", &x, &y, &z);
|
||||
switch (n) {
|
||||
case -1:
|
||||
path = NULL;
|
||||
r_tool = r_tool_default;
|
||||
continue;
|
||||
case 2:
|
||||
z = 0;
|
||||
/* fall through */
|
||||
case 3:
|
||||
break;
|
||||
default:
|
||||
fprintf(stderr, "invalid data at %s line %d\n",
|
||||
name ? name : "(stdin)", lineno);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
if (!path) {
|
||||
path = path_new(r_tool);
|
||||
*lnk = path;
|
||||
lnk = &path->next;
|
||||
}
|
||||
path_add(path, x, y, z);
|
||||
}
|
||||
fclose(file);
|
||||
return paths;
|
||||
}
|
||||
|
||||
|
||||
static int gnuplot_do_write(FILE *file, const struct path *paths)
|
||||
{
|
||||
const struct path *path;
|
||||
const struct point *p;
|
||||
|
||||
for (path = paths; path; path = path->next) {
|
||||
if (path != paths)
|
||||
if (fprintf(file, "\n") < 0)
|
||||
return 0;
|
||||
if (fprintf(file, "#%%r_tool=%f\n", path->r_tool) < 0)
|
||||
return 0;
|
||||
for (p = path->first; p; p = p->next)
|
||||
if (fprintf(file, "%f %f %f\n", p->x, p->y, p->z) < 0)
|
||||
return 0;
|
||||
}
|
||||
if (file != stdout && fclose(file) < 0)
|
||||
return 0;
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
void gnuplot_write(const char *name, const struct path *paths)
|
||||
{
|
||||
FILE *file;
|
||||
|
||||
file = name ? fopen(name, "w") : stdout;
|
||||
if (!file) {
|
||||
perror(name);
|
||||
exit(1);
|
||||
}
|
||||
if (!gnuplot_do_write(file, paths)) {
|
||||
perror(name ? name : "(stdout)");
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user