📄 files.c
字号:
/* Classic Ladder Project *//* Copyright (C) 2001 Marc Le Douarain *//* mavati@club-internet.fr *//* http://www.multimania.com/mavati/classicladder *//* February 2001 *//* Last update : 27 December 2001 *//* ---------------------------------------------------------------------------- *//* Load/Save Rungs , Timers , Monostables and Arithmetic expressions parameters *//* ---------------------------------------------------------------------------- *//* This library is free software; you can redistribute it and/or *//* modify it under the terms of the GNU Lesser General Public *//* License as published by the Free Software Foundation; either *//* version 2.1 of the License, or (at your option) any later version. *//* This library 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 *//* Lesser General Public License for more details. *//* You should have received a copy of the GNU Lesser General Public *//* License along with this library; if not, write to the Free Software *//* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */#include <stdio.h>#include <string.h>#include <stdlib.h>#include <unistd.h>#include "classicladder.h"#include "global.h"#include "edit.h"#include "files.h"#ifdef debug#define dbg_printf printf#elsestatic inline int dbg_printf(char *f, ...) {return 0;}#endifStrDatasForBase CorresDatasForBase[3] = { {BASE_MINS , TIME_BASE_MINS , "%.1fmn" , "Mins" } , {BASE_SECS , TIME_BASE_SECS , "%.1fs" , "Secs" } , {BASE_100MS , TIME_BASE_100MS , "%.0f00ms" , "100msecs" } };char ConvRawLineOfElements(char * RawLine,int y,StrRung * StorageRung){ char * StartOfValue; char * EndOfValue; int x = 0; char EndOfLine; StartOfValue = RawLine; EndOfValue = RawLine; do { /* Extract Element Type */ StartOfValue = EndOfValue; do { EndOfValue++; } while(*EndOfValue!='-'); *EndOfValue++ = '\0'; StorageRung->Element[x][y].Type = atoi(StartOfValue); /* Extract ConnectedWithTop */ StartOfValue = EndOfValue; do { EndOfValue++; } while(*EndOfValue!='-'); *EndOfValue++ = '\0'; StorageRung->Element[x][y].ConnectedWithTop = atoi(StartOfValue); /* Extract Var Type */ StartOfValue = EndOfValue; do { EndOfValue++; } while(*EndOfValue!='/'); *EndOfValue++ = '\0'; StorageRung->Element[x][y].VarType = atoi(StartOfValue); /* Extract Var Offset in the type table */ StartOfValue = EndOfValue; do { EndOfValue++; } while( (*EndOfValue!=',') && (*EndOfValue!=10) ); EndOfLine = TRUE; if (*EndOfValue==',') EndOfLine = FALSE; *EndOfValue++ = '\0'; StorageRung->Element[x][y].VarNum = atoi(StartOfValue); /* Next Element */ x++; } while(!EndOfLine); return (x);}char LoadRung(char * FileName,StrRung * BufRung){ FILE * File; char Okay = FALSE; char Line[300]; char * LineOk; int y = 0; File = fopen(FileName,"rt"); if (File) { do { LineOk = fgets(Line,300,File); if (LineOk) { char FndElements; switch(Line[0]) { case ';': break; case '#': if(strncmp(&Line[1],"VER=",4)==0) { if (atoi(&Line[5])>1) { printf("Rung version not supported...\n"); LineOk = FALSE; } } if(strncmp(&Line[1],"LABEL=",6)==0) { strcpy(BufRung->Label,&Line[7]); if (BufRung->Label[strlen(BufRung->Label)-1]=='\n') BufRung->Label[strlen(BufRung->Label)-1]='\0'; } if(strncmp(&Line[1],"COMMENT=",8)==0) { strcpy(BufRung->Comment,&Line[9]); if (BufRung->Comment[strlen(BufRung->Comment)-1]=='\n') BufRung->Comment[strlen(BufRung->Comment)-1]='\0'; } break; default: FndElements = ConvRawLineOfElements(Line,y,BufRung); y++; } } } while(LineOk); fclose(File); Okay = TRUE; } return (Okay);}char SaveRung(char * FileName,StrRung * BufRung,int * NumRungForJumps){ FILE * File; char Okay = FALSE; int x,y; File = fopen(FileName,"wt"); if (File) { fprintf(File,"; Rung :\n"); fprintf(File,"; all the blocks with the following format :\n"); fprintf(File,"; type (see classicladder.h) - ConnectedWithTop - VarType (see classicladder.h) / VarOffset\n"); fprintf(File,"#VER=1.0\n"); fprintf(File,"#LABEL=%s\n",BufRung->Label); fprintf(File,"#COMMENT=%s\n",BufRung->Comment); for (y=0;y<RUNG_HEIGHT;y++) { for(x=0;x<RUNG_WIDTH;x++) { /* for jumps we must use the new number of the rung when saved */ if ( (NumRungForJumps) && (BufRung->Element[x][y].Type==ELE_OUTPUT_JUMP) ) fprintf(File,"%d-%d-%d/%d",BufRung->Element[x][y].Type, BufRung->Element[x][y].ConnectedWithTop , BufRung->Element[x][y].VarType , NumRungForJumps[BufRung->Element[x][y].VarNum]); else fprintf(File,"%d-%d-%d/%d",BufRung->Element[x][y].Type, BufRung->Element[x][y].ConnectedWithTop , BufRung->Element[x][y].VarType , BufRung->Element[x][y].VarNum); if (x<RUNG_WIDTH-1) fprintf(File," , "); } fprintf(File,"\n"); } fclose(File); Okay = TRUE; } return (Okay);}void LoadAllRungs(char * BaseName,StrRung * Rungs,int * TheFirst,int * TheLast,int * TheCurrent){ int NumRung; StrRung * PrevRung = NULL; int PrevNumRung = 0; *TheFirst = -1; *TheCurrent = -1; for(NumRung=0; NumRung<NBR_RUNGS; NumRung++) { char RungFile[400]; sprintf(RungFile,"%s%d.csv",BaseName,NumRung); dbg_printf("Loading file : %s",RungFile); if (LoadRung(RungFile,Rungs)) { if (*TheFirst==-1) { *TheFirst = NumRung; *TheCurrent = NumRung; } if (PrevRung) { PrevRung->NextRung = NumRung; } Rungs->Used = TRUE; Rungs->PrevRung = PrevNumRung; *TheLast = NumRung; PrevNumRung = NumRung; PrevRung = Rungs; dbg_printf(" - ok.\n"); } else dbg_printf(" - not found.\n"); //DumpRung(Rungs); Rungs++; } /* no rungs loaded ? */ /* we must keep at least one empty ! */ if (*TheFirst<0) { *TheFirst = 0; *TheCurrent = 0; }}void SaveAllRungs(char * BaseName){ int NumRung; int NumSavedRung; char RungFile[400]; int NewNumRungForJumps[NBR_RUNGS]; int Done = FALSE; /* save the new number for the rungs (used for Jump)*/ NumSavedRung = 0; NumRung = InfosGene->FirstRung; do { NewNumRungForJumps[NumRung] = NumSavedRung++; if (NumRung == InfosGene->LastRung) Done = TRUE; else NumRung = RungArray[NumRung].NextRung; } while(!Done); /* delete all before */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -