📄 loadsave.cpp
字号:
char line[512];
int crystal, cycle, baud;
while(fgets(line, sizeof(line), f)) {
if(strcmp(line, "IO LIST\n")==0) {
if(!LoadIoListFromFile(f)) {
fclose(f);
return FALSE;
}
} else if(sscanf(line, "CRYSTAL=%d", &crystal)) {
Prog.mcuClock = crystal;
} else if(sscanf(line, "CYCLE=%d", &cycle)) {
Prog.cycleTime = cycle;
} else if(sscanf(line, "BAUD=%d", &baud)) {
Prog.baudRate = baud;
} else if(memcmp(line, "COMPILED=", 9)==0) {
line[strlen(line)-1] = '\0';
strcpy(CurrentCompileFile, line+9);
} else if(memcmp(line, "MICRO=", 6)==0) {
line[strlen(line)-1] = '\0';
int i;
for(i = 0; i < NUM_SUPPORTED_MCUS; i++) {
if(strcmp(SupportedMcus[i].mcuName, line+6)==0) {
Prog.mcu = &SupportedMcus[i];
break;
}
}
if(i == NUM_SUPPORTED_MCUS) {
Error(_("Microcontroller '%s' not supported.\r\n\r\n"
"Defaulting to no selected MCU."), line+6);
}
} else if(strcmp(line, "PROGRAM\n")==0) {
break;
}
}
if(strcmp(line, "PROGRAM\n") != 0) goto failed;
int rung;
for(rung = 0;;) {
if(!fgets(line, sizeof(line), f)) break;
if(strcmp(line, "RUNG\n")!=0) goto failed;
Prog.rungs[rung] = LoadSeriesFromFile(f);
if(!Prog.rungs[rung]) goto failed;
rung++;
}
Prog.numRungs = rung;
fclose(f);
return TRUE;
failed:
fclose(f);
NewProgram();
Error(_("File format error; perhaps this program is for a newer version "
"of LDmicro?"));
return FALSE;
}
//-----------------------------------------------------------------------------
// Helper routine for outputting hierarchical representation of the ladder
// logic: indent on file f, by depth*4 spaces.
//-----------------------------------------------------------------------------
static void Indent(FILE *f, int depth)
{
int i;
for(i = 0; i < depth; i++) {
fprintf(f, " ");
}
}
//-----------------------------------------------------------------------------
// Save an element to a file. If it is a leaf, then output a single line
// describing it and return. If it is a subcircuit, call ourselves
// recursively (with depth+1, so that the indentation is right) to handle
// the members of the subcircuit. Special case for depth=0: we do not
// output the SERIES/END delimiters. This is because the root is delimited
// by RUNG/END markers output elsewhere.
//-----------------------------------------------------------------------------
static void SaveElemToFile(FILE *f, int which, void *any, int depth)
{
ElemLeaf *l = (ElemLeaf *)any;
char *s;
Indent(f, depth);
switch(which) {
case ELEM_PLACEHOLDER:
fprintf(f, "PLACEHOLDER\n");
break;
case ELEM_COMMENT: {
fprintf(f, "COMMENT ");
char *s = l->d.comment.str;
for(; *s; s++) {
if(*s == '\\') {
fprintf(f, "\\\\");
} else if(*s == '\n') {
fprintf(f, "\\n");
} else if(*s == '\r') {
fprintf(f, "\\r");
} else {
fprintf(f, "%c", *s);
}
}
fprintf(f, "\n");
break;
}
case ELEM_OPEN:
fprintf(f, "OPEN\n");
break;
case ELEM_SHORT:
fprintf(f, "SHORT\n");
break;
case ELEM_MASTER_RELAY:
fprintf(f, "MASTER_RELAY\n");
break;
case ELEM_SHIFT_REGISTER:
fprintf(f, "SHIFT_REGISTER %s %d\n", l->d.shiftRegister.name,
l->d.shiftRegister.stages);
break;
case ELEM_CONTACTS:
fprintf(f, "CONTACTS %s %d\n", l->d.contacts.name,
l->d.contacts.negated);
break;
case ELEM_COIL:
fprintf(f, "COIL %s %d %d %d\n", l->d.coil.name, l->d.coil.negated,
l->d.coil.setOnly, l->d.coil.resetOnly);
break;
case ELEM_TON:
s = "TON"; goto timer;
case ELEM_TOF:
s = "TOF"; goto timer;
case ELEM_RTO:
s = "RTO"; goto timer;
timer:
fprintf(f, "%s %s %d\n", s, l->d.timer.name, l->d.timer.delay);
break;
case ELEM_CTU:
s = "CTU"; goto counter;
case ELEM_CTD:
s = "CTD"; goto counter;
case ELEM_CTC:
s = "CTC"; goto counter;
counter:
fprintf(f, "%s %s %d\n", s, l->d.counter.name, l->d.counter.max);
break;
case ELEM_RES:
fprintf(f, "RES %s\n", l->d.reset.name);
break;
case ELEM_MOVE:
fprintf(f, "MOVE %s %s\n", l->d.move.dest, l->d.move.src);
break;
case ELEM_ADD: s = "ADD"; goto math;
case ELEM_SUB: s = "SUB"; goto math;
case ELEM_MUL: s = "MUL"; goto math;
case ELEM_DIV: s = "DIV"; goto math;
math:
fprintf(f, "%s %s %s %s\n", s, l->d.math.dest, l->d.math.op1,
l->d.math.op2);
break;
case ELEM_EQU: s = "EQU"; goto cmp;
case ELEM_NEQ: s = "NEQ"; goto cmp;
case ELEM_GRT: s = "GRT"; goto cmp;
case ELEM_GEQ: s = "GEQ"; goto cmp;
case ELEM_LES: s = "LES"; goto cmp;
case ELEM_LEQ: s = "LEQ"; goto cmp;
cmp:
fprintf(f, "%s %s %s\n", s, l->d.cmp.op1, l->d.cmp.op2);
break;
case ELEM_ONE_SHOT_RISING:
fprintf(f, "OSR\n");
break;
case ELEM_ONE_SHOT_FALLING:
fprintf(f, "OSF\n");
break;
case ELEM_READ_ADC:
fprintf(f, "READ_ADC %s\n", l->d.readAdc.name);
break;
case ELEM_SET_PWM:
fprintf(f, "SET_PWM %s %d\n", l->d.setPwm.name,
l->d.setPwm.targetFreq);
break;
case ELEM_UART_RECV:
fprintf(f, "UART_RECV %s\n", l->d.uart.name);
break;
case ELEM_UART_SEND:
fprintf(f, "UART_SEND %s\n", l->d.uart.name);
break;
case ELEM_PERSIST:
fprintf(f, "PERSIST %s\n", l->d.persist.var);
break;
case ELEM_FORMATTED_STRING: {
int i;
fprintf(f, "FORMATTED_STRING ");
if(*(l->d.fmtdStr.var)) {
fprintf(f, "%s", l->d.fmtdStr.var);
} else {
fprintf(f, "(none)");
}
fprintf(f, " %d", strlen(l->d.fmtdStr.string));
for(i = 0; i < (int)strlen(l->d.fmtdStr.string); i++) {
fprintf(f, " %d", l->d.fmtdStr.string[i]);
}
fprintf(f, "\n");
break;
}
case ELEM_LOOK_UP_TABLE: {
int i;
fprintf(f, "LOOK_UP_TABLE %s %s %d %d", l->d.lookUpTable.dest,
l->d.lookUpTable.index, l->d.lookUpTable.count,
l->d.lookUpTable.editAsString);
for(i = 0; i < l->d.lookUpTable.count; i++) {
fprintf(f, " %d", l->d.lookUpTable.vals[i]);
}
fprintf(f, "\n");
break;
}
case ELEM_PIECEWISE_LINEAR: {
int i;
fprintf(f, "PIECEWISE_LINEAR %s %s %d", l->d.piecewiseLinear.dest,
l->d.piecewiseLinear.index, l->d.piecewiseLinear.count);
for(i = 0; i < l->d.piecewiseLinear.count*2; i++) {
fprintf(f, " %d", l->d.piecewiseLinear.vals[i]);
}
fprintf(f, "\n");
break;
}
case ELEM_SERIES_SUBCKT: {
ElemSubcktSeries *s = (ElemSubcktSeries *)any;
int i;
if(depth == 0) {
fprintf(f, "RUNG\n");
} else {
fprintf(f, "SERIES\n");
}
for(i = 0; i < s->count; i++) {
SaveElemToFile(f, s->contents[i].which, s->contents[i].d.any,
depth+1);
}
Indent(f, depth);
fprintf(f, "END\n");
break;
}
case ELEM_PARALLEL_SUBCKT: {
ElemSubcktParallel *s = (ElemSubcktParallel *)any;
int i;
fprintf(f, "PARALLEL\n");
for(i = 0; i < s->count; i++) {
SaveElemToFile(f, s->contents[i].which, s->contents[i].d.any,
depth+1);
}
Indent(f, depth);
fprintf(f, "END\n");
break;
}
default:
oops();
break;
}
}
//-----------------------------------------------------------------------------
// Save the program in memory to the given file. Returns TRUE for success,
// FALSE otherwise.
//-----------------------------------------------------------------------------
BOOL SaveProjectToFile(char *filename)
{
FILE *f = fopen(filename, "w");
if(!f) return FALSE;
fprintf(f, "LDmicro0.1\n");
if(Prog.mcu) {
fprintf(f, "MICRO=%s\n", Prog.mcu->mcuName);
}
fprintf(f, "CYCLE=%d\n", Prog.cycleTime);
fprintf(f, "CRYSTAL=%d\n", Prog.mcuClock);
fprintf(f, "BAUD=%d\n", Prog.baudRate);
if(strlen(CurrentCompileFile) > 0) {
fprintf(f, "COMPILED=%s\n", CurrentCompileFile);
}
fprintf(f, "\n");
// list extracted from schematic, but the pin assignments are not
fprintf(f, "IO LIST\n", Prog.mcuClock);
SaveIoListToFile(f);
fprintf(f, "END\n", Prog.mcuClock);
fprintf(f, "\n", Prog.mcuClock);
fprintf(f, "PROGRAM\n", Prog.mcuClock);
int i;
for(i = 0; i < Prog.numRungs; i++) {
SaveElemToFile(f, ELEM_SERIES_SUBCKT, Prog.rungs[i], 0);
}
fclose(f);
return TRUE;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -