|
|
1.1 ! root 1: /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */ ! 2: ! 3: /* ! 4: $Header: spos.c,v 1.1 85/08/22 15:50:07 timo Exp $ ! 5: */ ! 6: ! 7: /* ! 8: * B editor -- Save and restore focus position. ! 9: */ ! 10: ! 11: #include "b.h" ! 12: #include "bobj.h" ! 13: #include "feat.h" ! 14: #include "file.h" ! 15: ! 16: #ifdef SAVEPOS ! 17: #define MAXPATHLEN 1024 /* See getwd(3) */ ! 18: #define MAXSAVE 50 /* Maximum number of entries kept in SAVEPOSFILE */ ! 19: ! 20: #define strval(v) Str(v) ! 21: ! 22: /* ! 23: * Keep a simple database of file name vs. line number, so that ! 24: * when an edit session is stopped and later continued, the focus ! 25: * is restored exactly. ! 26: * The database is kept in most-recently-used-first order. ! 27: * When it is rewritten, only its first MAXSAVE lines are saved, ! 28: * thus limiting the amount of disk space wasted by files ! 29: * that were once edited but then removed, renamed or forgotten. ! 30: */ ! 31: ! 32: ! 33: Visible int ! 34: getpos(file) ! 35: register string file; ! 36: { ! 37: register FILE *fp = fopen(posfile, "r"); ! 38: char buf[BUFSIZ]; ! 39: auto int l1; ! 40: int nread; ! 41: register int len = strlen(file); ! 42: ! 43: if (!fp) ! 44: return 0; ! 45: while (fgets(buf, sizeof buf, fp) != NULL) { ! 46: if (strncmp(buf, file, len) == 0 ! 47: && (buf[len] == '\t' || buf[len] == ' ')) { ! 48: nread= sscanf(buf+len+1, "%d", &l1); ! 49: if (nread >= 1) { ! 50: fclose(fp); ! 51: return l1; ! 52: } ! 53: } ! 54: } ! 55: fclose(fp); ! 56: return 0; ! 57: } ! 58: ! 59: ! 60: /* ! 61: * Save focus position for file 'file'. ! 62: * Return Yes if save succeeded. ! 63: */ ! 64: ! 65: Visible bool ! 66: savepos(file, line) ! 67: register string file; ! 68: int line; ! 69: { ! 70: register int nsave = 0; ! 71: register int i; ! 72: register FILE *fp = fopen(posfile, "r"); ! 73: char buf[BUFSIZ]; ! 74: register int len = strlen(file); ! 75: value saved[MAXSAVE]; ! 76: ! 77: if (fp) { ! 78: while (fgets(buf, sizeof buf, fp) != NULL && nsave < MAXSAVE) { ! 79: if (strncmp(file, buf, len) == 0 ! 80: && (buf[len] == ' ' || buf[len] == '\t')) ! 81: continue; ! 82: saved[nsave] = mk_text(buf); ! 83: ++nsave; ! 84: } ! 85: fclose(fp); ! 86: } ! 87: fp = fopen(posfile, "w"); ! 88: if (fp == NULL) ! 89: return No; ! 90: fprintf(fp, "%s\t%d\n", file, line); ! 91: for (i = 0; i < nsave; ++i) { ! 92: fputs(strval(saved[i]), fp); ! 93: release(saved[i]); ! 94: } ! 95: if (fclose(fp) == EOF) return No; ! 96: return Yes; ! 97: } ! 98: ! 99: #endif SAVEPOS
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.