示例#1
0
	void TextEditor::byteToCursor(Point2D<uint>& cursor, uint byte) const
	{
		if (byte > 0)
		{
			AnyString part(pText, 0, Math::Min(byte, pText.size()));
			assert(part.utf8valid() and "invalid UTF8 string part");

			cursor.y = part.countChar('\n');
			if (cursor.y > 0)
			{
				auto pos = part.rfind('\n');
				if (pos < part.size())
				{
					++pos;
					AnyString lastline(part, pos);
					assert(lastline.utf8valid() and "invalid UTF8 sub string");
					cursor.x = columnCount(lastline);
				}
				else
					cursor.x = 0;
			}
			else
				cursor.x = columnCount(part);
		}
		else
			cursor.reset();
	}
示例#2
0
文件: process.c 项目: coyizumi/cs111
void
process(void)
{
	struct s_command *cp;
	SPACE tspace;
	size_t oldpsl = 0;
	char *p;

	p = NULL;

	for (linenum = 0; mf_fgets(&PS, REPLACE);) {
		pd = 0;
top:
		cp = prog;
redirect:
		while (cp != NULL) {
			if (!applies(cp)) {
				cp = cp->next;
				continue;
			}
			switch (cp->code) {
			case '{':
				cp = cp->u.c;
				goto redirect;
			case 'a':
				if (appendx >= appendnum)
					if ((appends = realloc(appends,
					    sizeof(struct s_appends) *
					    (appendnum *= 2))) == NULL)
						err(1, "realloc");
				appends[appendx].type = AP_STRING;
				appends[appendx].s = cp->t;
				appends[appendx].len = strlen(cp->t);
				appendx++;
				break;
			case 'b':
				cp = cp->u.c;
				goto redirect;
			case 'c':
				pd = 1;
				psl = 0;
				if (cp->a2 == NULL || lastaddr || lastline())
					(void)fprintf(outfile, "%s", cp->t);
				break;
			case 'd':
				pd = 1;
				goto new;
			case 'D':
				if (pd)
					goto new;
				if (psl == 0 ||
				    (p = memchr(ps, '\n', psl)) == NULL) {
					pd = 1;
					goto new;
				} else {
					psl -= (p + 1) - ps;
					memmove(ps, p + 1, psl);
					goto top;
				}
			case 'g':
				cspace(&PS, hs, hsl, REPLACE);
				break;
			case 'G':
				cspace(&PS, "\n", 1, APPEND);
				cspace(&PS, hs, hsl, APPEND);
				break;
			case 'h':
				cspace(&HS, ps, psl, REPLACE);
				break;
			case 'H':
				cspace(&HS, "\n", 1, APPEND);
				cspace(&HS, ps, psl, APPEND);
				break;
			case 'i':
				(void)fprintf(outfile, "%s", cp->t);
				break;
			case 'l':
				lputs(ps, psl);
				break;
			case 'n':
				if (!nflag && !pd)
					OUT();
				flush_appends();
				if (!mf_fgets(&PS, REPLACE))
					exit(0);
				pd = 0;
				break;
			case 'N':
				flush_appends();
				cspace(&PS, "\n", 1, APPEND);
				if (!mf_fgets(&PS, APPEND))
					exit(0);
				break;
			case 'p':
				if (pd)
					break;
				OUT();
				break;
			case 'P':
				if (pd)
					break;
				if ((p = memchr(ps, '\n', psl)) != NULL) {
					oldpsl = psl;
					psl = p - ps;
				}
				OUT();
				if (p != NULL)
					psl = oldpsl;
				break;
			case 'q':
				if (!nflag && !pd)
					OUT();
				flush_appends();
				exit(0);
			case 'r':
				if (appendx >= appendnum)
					if ((appends = realloc(appends,
					    sizeof(struct s_appends) *
					    (appendnum *= 2))) == NULL)
						err(1, "realloc");
				appends[appendx].type = AP_FILE;
				appends[appendx].s = cp->t;
				appends[appendx].len = strlen(cp->t);
				appendx++;
				break;
			case 's':
				sdone |= substitute(cp);
				break;
			case 't':
				if (sdone) {
					sdone = 0;
					cp = cp->u.c;
					goto redirect;
				}
				break;
			case 'w':
				if (pd)
					break;
				if (cp->u.fd == -1 && (cp->u.fd = open(cp->t,
				    O_WRONLY|O_APPEND|O_CREAT|O_TRUNC,
				    DEFFILEMODE)) == -1)
					err(1, "%s", cp->t);
				if (write(cp->u.fd, ps, psl) != (ssize_t)psl ||
				    write(cp->u.fd, "\n", 1) != 1)
					err(1, "%s", cp->t);
				break;
			case 'x':
				/*
				 * If the hold space is null, make it empty
				 * but not null.  Otherwise the pattern space
				 * will become null after the swap, which is
				 * an abnormal condition.
				 */
				if (hs == NULL)
					cspace(&HS, "", 0, REPLACE);
				tspace = PS;
				PS = HS;
				HS = tspace;
				break;
			case 'y':
				if (pd || psl == 0)
					break;
				do_tr(cp->u.y);
				break;
			case ':':
			case '}':
				break;
			case '=':
				(void)fprintf(outfile, "%lu\n", linenum);
			}
			cp = cp->next;
		} /* for all cp */
示例#3
0
文件: main.c 项目: Hooman3/freebsd
/*
 * Like fgets, but go through the list of files chaining them together.
 * Set len to the length of the line.
 */
int
mf_fgets(SPACE *sp, enum e_spflag spflag)
{
	struct stat sb;
	ssize_t len;
	char *dirbuf, *basebuf;
	static char *p = NULL;
	static size_t plen = 0;
	int c;
	static int firstfile;

	if (infile == NULL) {
		/* stdin? */
		if (files->fname == NULL) {
			if (inplace != NULL)
				errx(1, "-I or -i may not be used with stdin");
			infile = stdin;
			fname = "stdin";
			outfile = stdout;
			outfname = "stdout";
		}
		firstfile = 1;
	}

	for (;;) {
		if (infile != NULL && (c = getc(infile)) != EOF) {
			(void)ungetc(c, infile);
			break;
		}
		/* If we are here then either eof or no files are open yet */
		if (infile == stdin) {
			sp->len = 0;
			return (0);
		}
		if (infile != NULL) {
			fclose(infile);
			if (*oldfname != '\0') {
				/* if there was a backup file, remove it */
				unlink(oldfname);
				/*
				 * Backup the original.  Note that hard links
				 * are not supported on all filesystems.
				 */
				if ((link(fname, oldfname) != 0) &&
				   (rename(fname, oldfname) != 0)) {
					warn("rename()");
					if (*tmpfname)
						unlink(tmpfname);
					exit(1);
				}
				*oldfname = '\0';
			}
			if (*tmpfname != '\0') {
				if (outfile != NULL && outfile != stdout)
					if (fclose(outfile) != 0) {
						warn("fclose()");
						unlink(tmpfname);
						exit(1);
					}
				outfile = NULL;
				if (rename(tmpfname, fname) != 0) {
					/* this should not happen really! */
					warn("rename()");
					unlink(tmpfname);
					exit(1);
				}
				*tmpfname = '\0';
			}
			outfname = NULL;
		}
		if (firstfile == 0)
			files = files->next;
		else
			firstfile = 0;
		if (files == NULL) {
			sp->len = 0;
			return (0);
		}
		fname = files->fname;
		if (inplace != NULL) {
			if (lstat(fname, &sb) != 0)
				err(1, "%s", fname);
			if (!(sb.st_mode & S_IFREG))
				errx(1, "%s: %s %s", fname,
				    "in-place editing only",
				    "works for regular files");
			if (*inplace != '\0') {
				strlcpy(oldfname, fname,
				    sizeof(oldfname));
				len = strlcat(oldfname, inplace,
				    sizeof(oldfname));
				if (len > (ssize_t)sizeof(oldfname))
					errx(1, "%s: name too long", fname);
			}
			if ((dirbuf = strdup(fname)) == NULL ||
			    (basebuf = strdup(fname)) == NULL)
				err(1, "strdup");
			len = snprintf(tmpfname, sizeof(tmpfname),
			    "%s/.!%ld!%s", dirname(dirbuf), (long)getpid(),
			    basename(basebuf));
			free(dirbuf);
			free(basebuf);
			if (len >= (ssize_t)sizeof(tmpfname))
				errx(1, "%s: name too long", fname);
			unlink(tmpfname);
			if (outfile != NULL && outfile != stdout)
				fclose(outfile);
			if ((outfile = fopen(tmpfname, "w")) == NULL)
				err(1, "%s", fname);
			fchown(fileno(outfile), sb.st_uid, sb.st_gid);
			fchmod(fileno(outfile), sb.st_mode & ALLPERMS);
			outfname = tmpfname;
			if (!ispan) {
				linenum = 0;
				resetstate();
			}
		} else {
			outfile = stdout;
			outfname = "stdout";
		}
		if ((infile = fopen(fname, "r")) == NULL) {
			warn("%s", fname);
			rval = 1;
			continue;
		}
	}
	/*
	 * We are here only when infile is open and we still have something
	 * to read from it.
	 *
	 * Use getline() so that we can handle essentially infinite input
	 * data.  The p and plen are static so each invocation gives
	 * getline() the same buffer which is expanded as needed.
	 */
	len = getline(&p, &plen, infile);
	if (len == -1)
		err(1, "%s", fname);
	if (len != 0 && p[len - 1] == '\n') {
		sp->append_newline = 1;
		len--;
	} else if (!lastline()) {
		sp->append_newline = 1;
	} else {
		sp->append_newline = 0;
	}
	cspace(sp, p, len, spflag);

	linenum++;

	return (1);
}
示例#4
0
文件: level5.c 项目: YugeShunki/git
void LEVEL5(void){//レベル5の動作
  if(a==5){
    while(1){
      gclr(win);
      human();//棒人間
      input();//入力待ち
      
      newpen(win, 1);//色は白
      //防護壁表記
      fillrect(win,defence_x, defence_y, defence_w, defence_h1);
      fillrect(win,defence_x+40.0, defence_y+60.0, defence_w, defence_h2);
      fillrect(win,defence_x+80.0, defence_y, defence_w, defence_h3);
      defence_y -= 15.0;//速度
      
      atari();//当たり判定
      //一番下まで落ちたら    
      if(defence_y == 0){
	while(1){
	  gclr(win);//画面消去
	  human();//棒人間
	  input();//入力待ち
	  bougoheki();//防護壁	
	  atari();//当たり判定
	  newpen(win, 2);//太陽の色は赤で
	  fillcirc(win,all_x, all_y, 200, 200);//太陽
	  all_y-=5.0;//落下速度
	  
	  //太陽が防護壁に当たったら
	  if(all_y==280.0){
	    while(1){
	      gclr(win);//画面消去
	      human();//棒人間
	      input();//入力待ち
	      bougoheki();//防護壁
	      atari();//当たり判定
	      newpen(win, 2);//線は赤
	      
	      //太陽ぶつかった後の線
	      lastline();
	      msleep(time);
	      
	      if(explo_y1<0){//下に伸びた線が0以下になったら
		if(defence_x+40 < x && x < defence_x+80){//中にいれば
		  newpen(win, 1);
		  drawstr(win, 180, 250, 24, 0, "SAFE");
		  msleep(time*10);
		  gclr(win);
		  newpen(win, 1);
		  drawstr(win, 100, 300, 24, 0.0, "CONGRATULATIONS!");
		  drawstr(win, 170, 250, 24, 0.0, "CLEAR");
		  msleep(time*30);
		  exit(0);
		}
		else {//外にいれば
		  newpen(win, 1);
		  drawstr(win, 190, 250, 24, 0, "OUT");
		  msleep(time*10);
		  gclr(win);
		  newpen(win, 1);
		  drawstr(win, 150, 250, 24, 0.0, "GAMEOVER");
		  msleep(time*30);
		  exit(0);
		}
	      }
	    }
	  }
	  msleep(time);
	} 
      }
      msleep(time);   
    } 
  }
}