/*************************************************************************** * Copyright (C) 2012 by semico * * mail@semico.ru * * * * 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. * * * * This program is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * * GNU General Public License for more details. * * * * You should have received a copy of the GNU General Public License * * along with this program; if not, write to the * * Free Software Foundation, Inc., * * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * ***************************************************************************/ /* Перекодирование текстового файла таблицы данных тирования в файл TIT программы titr.exe */ #ifdef HAVE_CONFIG_H #include #endif #include #include #include // Коды выхода BSD from @(#)sysexits.h 8.1 (Berkeley) 6/2/93 // Copyright (c) 1987, 1993 // The Regents of the University of California. All rights reserved. #define EX_USAGE 64 /* command line usage error */ #define EX_DATAERR 65 /* data format error */ #define EX_NOINPUT 66 /* cannot open input */ #define EX_NOUSER 67 /* addressee unknown */ #define EX_NOHOST 68 /* host name unknown */ #define EX_UNAVAILABLE 69 /* service unavailable */ #define EX_SOFTWARE 70 /* internal software error */ #define EX_OSERR 71 /* system error (e.g., can't fork) */ #define EX_OSFILE 72 /* critical OS file missing */ #define EX_CANTCREAT 73 /* can't create (user) output file */ #define EX_IOERR 74 /* input/output error */ #define EX_TEMPFAIL 75 /* temp failure; user is invited to retry */ #define EX_PROTOCOL 76 /* remote error in protocol */ #define EX_NOPERM 77 /* permission denied */ #define EX_CONFIG 78 /* configuration error */ // end sysexits.h char mkversion[]="Version 0.11"; /* 0.1 - первая версия 25.05.12 0.11 - 28.05.12 - устранена ошибка при установке размерности. */ int dos=0; /* 0-GNU/Linux 1-DOS 2-Windows (Win32) */ char mes0[8]; /* конец строки ОС */ char fname[6][256]; /* 0-TXT без расширения 1-TXT исходный 2-TIT */ int fname_vsego; /* всего файлов в строке аргументов */ FILE *fp0, *fp1,*fp2; //потоки 0-CFG 1-TXT 2-TIT char ctemp[512]; /* вспомогательная строка */ char opis[256]; /* описание таблицы */ double vg[256],eg[256]; /* таблица данных */ float f1,f2; int vsegog; /* количество точек */ int param; /* размерность 0-3 */ /*--------------------------------------------------------- init_msg() -----------------*/ int init_msg(void) { /* инициализация сообщений */ int re=0; if ((dos==1)||(dos==2)) strcpy(mes0,"\r\n"); /* DOS & Win - ВК + ПС */ else strcpy(mes0,"\n"); /* Linux - только ПС */ end: return(re); } /*---------------------------------------------------------- txtread() ----------------*/ int txtread(void) { /* перенос текстовой части из TXT в TIT, разбор файла */ int i,j,k,k1,k2,m; char c; int re=0; vsegog=0; /* запись заголовка */ strcpy(ctemp,opis); strcat(ctemp,"\r\n"); j=strlen(ctemp); /* строка заголовка */ for (i=0; i255) vsegog=255; } goto a0; end: return(re); } /*--------------------------------------------------------------------------*/ int i2tit (int x) { int b1,b2; char c1,c2; int m; int re=0; /* преобразует int в шестнадцатиричную строку и записывает её в fp2 */ b1=x/256; b2=x%256; c1=(char)(b1/16); if (c1<10) c1+=48; else c1+=55; c2=(char)(b1%16); if (c2<10) c2+=48; else c2+=55; m=fputc(c1,fp2); /* копирование символа */ if (m==EOF) {re=EX_IOERR; goto end;} m=fputc(c2,fp2); /* копирование символа */ if (m==EOF) {re=EX_IOERR; goto end;} c1=(char)(b2/16); if (c1<10) c1+=48; else c1+=55; c2=(char)(b2%16); if (c2<10) c2+=48; else c2+=55; m=fputc(c1,fp2); /* копирование символа */ if (m==EOF) {re=EX_IOERR; goto end;} m=fputc(c2,fp2); /* копирование символа */ if (m==EOF) {re=EX_IOERR; goto end;} end: return (re); } /*--------------------------------------------------------------------------*/ int c2tit(char c) { int b,m; int re=0; char c1,c2; // преобразует char в шестнадцатиричную строку в fp2 b=(int)c; if (b<0) b+=256; c1=(char)(b/16); if (c1<10) c1+=48; else c1+=55; c2=(char)(b%16); if (c2<10) c2+=48; else c2+=55; m=fputc(c1,fp2); /* копирование символа */ if (m==EOF) {re=EX_IOERR; goto end;} m=fputc(c2,fp2); /* копирование символа */ if (m==EOF) {re=EX_IOERR; goto end;} end: return (re); } /*--------------------------------------------------------------------------*/ int d2tit(double d) { // преобразует double в шестнадцатиричную строку в fp2 int i,j,k; int re=0; char temp[32]; char temp1[128]; /* gcvt(d,8,temp); */ sprintf(temp,"%g",d); j=strlen(temp); if (j>30) j=30; i=i2tit(j); if (i!=0) {re=i; goto end;} // сначала записывается длина int, потом сама строка for (i=0; i4) j=4; strncpy(fname[j+1],argv[i],255); if (j<4) fname_vsego++; } else { c=argv[i][1]; if ((c=='h')||(c=='H')||(c=='?')) { /* help */ printf("txt2tit - convertor TXT to TIT files for Titr.exe %s",mes0); printf("txt2tit TXTfile [key] %s",mes0); printf("\t-h,-H,-?\tthis help; %s",mes0); printf("\t-v,-V\tversion; %s",mes0); printf("\t-w,-W\twarranty and copyright; %s",mes0); printf("%s",mes0); printf("\t-dX,-DX\tdimension: -d0=E(mV)(default), -d1=pH, -d2=pX, -d3=cond. %s",mes0); printf("%s",mes0); printf("TXTfile content: %s",mes0); printf("v0 x0 %s",mes0); printf("v1 x1 %s",mes0); printf("v2 x2 %s",mes0); printf("... %s",mes0); printf("vN xN %s",mes0); goto end; } if ((c=='v')||(c=='V')) { /* version */ printf(mkversion); if (dos==0) printf(" (GNU/Linux)"); if (dos==1) printf(" (DOS)"); if (dos==2) printf(" (Win32)"); goto end; } if ((c=='w')||(c=='W')) { /* copyleft & warranty */ printf("Copyright (C) 2012 by NPP SEMIKO (Russia, Novosibirsk) %s%s",mes0,mes0); printf("This program is free software; you can redistribute it and/or modify %s",mes0); printf("it under the terms of the GNU General Public License as published by %s",mes0); printf("the Free Software Foundation; either version 2 of the License, or %s",mes0); printf("(at your option) any later version. %s%s",mes0,mes0); printf("This program is distributed in the hope that it will be useful, %s",mes0); printf("but WITHOUT ANY WARRANTY; without even the implied warranty of %s",mes0); printf("MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the %s",mes0); printf("GNU General Public License for more details. %s%s",mes0,mes0); printf("You should have received a copy of the GNU General Public License %s",mes0); printf("along with this program; if not, write to the Free Software %s",mes0); printf("Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA"); goto end; } if ((c=='d')||(c=='D')) { /* dimension */ switch(argv[i][2]) { case '0': param=0; break; case '1': param=1; break; case '2': param=2; break; case '3': param=3; break; default: param=0; } } } nextargv:; } nach: if (fname_vsego<=0) {printf("TXT file absent, read \"txt2tit -h\""); re=EX_USAGE; goto end;} /* нет файла TXT */ fp1=fopen(fname[1],"rt"); /* открыть TXT как текст для чтения */ if (fp1==NULL) {printf("TXT file no input"); re=EX_NOINPUT; goto end;} /* файл TXT не открыт */ /* найти имя файла без расширения */ i=0; k=strlen(fname[1]); if (dos==0) goto m1; /* в Linux - расширение не трогать */ for (i=k; i>0; i--) { if (fname[1][i]=='.') {i=k; goto m1;} /* в других ОС убрать текущее расширение у имени файла */ } m1: strncpy(fname[0],fname[1],248); if ((i>0)&&(i