Ejemplo n.º 1
0
void
Buffer::update(int ch)
{
    switch (ch) {
    case KEY_UP:
        prevline();
        break;
    case KEY_DOWN:
        nextline();
        break;
    case KEY_RIGHT:
        nextchar();
        break;
    case KEY_LEFT:
        prevchar();
        break;
    case KEY_HOME:
        begofline();
        break;
    case KEY_END:
        endofline();
        break;
    case 127:
        delchar();
        break;
    case '\n':
    case '\r':
        newline();
        break;
    default:
        addchar(ch);
    }
}
/* sample input: "LU",86.25,"11/4/1998","2:19PM",+4.0625 */
char *csvgetline(FILE *fin)
{	
	int i, c;
	char *newl, *news;

	if (line == NULL)			/* allocate on first call */
		csvinit(1, 1);
	for (i=0; (c=getc(fin))!=EOF && !endofline(fin,c); i++) {
		if (i >= maxline-1) {	/* grow line */
			maxline *= 2;		/* double current size */
			newl = (char *) realloc(line, maxline);
			if (newl == NULL) {
				reset();
				return NULL;
			}
			line = newl;
			news = (char *) realloc(sline, maxline);
			if (news == NULL) {
				reset();
				return NULL;
			}
			sline = news;


		}
		line[i] = c;
	}
	line[i] = '\0';
	if (split() == NOMEM) {
		reset();
		return NULL;			/* out of memory */
	}
	return (c == EOF && i == 0) ? NULL : line;
}
Ejemplo n.º 3
0
void
Buffer::prevline()
{
    if (m_cur.y != 0) {
        m_cur.y--;
    }
    if (m_cur.x > m_lines[m_cur.y].length()) {
        endofline();
    }
}
Ejemplo n.º 4
0
void
Buffer::nextline()
{
    if (m_cur.y != m_lines.size() - 1) {
        m_cur.y++;
    }
    if (m_cur.x > m_lines[m_cur.y].length()) {
        endofline();
    }
}
Ejemplo n.º 5
0
/* Get one line */
char *csvgetline(FILE *fin)
{
  int i, c;
  char *new_line, *new_sep;

  if (line == NULL) {      /* allocate on first call */
    max_line = max_field = 1;
    line     = (char *)  malloc(max_line);
    sep_line = (char *)  malloc(max_line);
    field    = (char **) malloc(max_field * sizeof(field[0]));

    if (line == NULL || sep_line == NULL || field == NULL) {
      reset();
      return NULL;         /* out of memory */
    }
  }

  for (i = 0; (c = getc(fin)) != EOF && !endofline(fin,c); i++) {
    if (i >= max_line - 1) {  
      max_line *= 2;          
      new_line = (char *) realloc(line, max_line);

      if (new_line == NULL) {
        reset();
        return NULL;
      }

      line = new_line;
      new_sep = (char *) realloc(sep_line, max_line);

      if (new_sep == NULL) {
        reset();
        return NULL;
      }

      sep_line = new_sep;
    }

    line[i] = c;
  }

  line[i] = '\0';
  if (split() == NOMEM) {
    reset();
    return NULL; /* out of memory */
  }

  /* If first line of csv, create the header array */
  if (h_count == 0) {
    headers = (char **) malloc(nfields);
  }

  return (c == EOF && i == 0) ? NULL : line;
}
Ejemplo n.º 6
0
void
Buffer::prevchar()
{
    if (m_cur.x != 0) {
        m_cur.x--;
    }
    else if (m_cur.y != 0) {
        m_cur.y--;
        endofline();
    }
}
Ejemplo n.º 7
0
char *csvgetline(FILE* fin)
{
	if (fin == NULL)
	{
		return NULL;
	}
	int i, c;
	char* newl, *news;

	if (line == NULL)
	{
		max_field = max_line = 1;
		line = (char*) malloc(max_line);
		sline = (char*) malloc(max_line);
		field = (char**) malloc(max_field * sizeof(field[0]));
		if (line == NULL || sline == NULL || field == NULL)
		{
			reset();
			return NULL;
		}
	}

	for (i = 0; (c = getc(fin)) != EOF && !endofline(fin, c); i++)
	{
		if (i >= max_line - 1)
		{
			max_line *= 2;
			newl = (char*)realloc(line, max_line);
			news = (char*)realloc(sline, max_line);

			if (newl == NULL 
				|| news == NULL)
			{
				reset();
				return NULL;
			}
			line = newl;
			sline = news;
		}
		line[i] = c;
	}

	line[i] = '\0';
	if (split() == NOMEM)
	{
		reset();
		return NULL;
	}	
	return (c == EOF && i == 0)? NULL:line;
}
Ejemplo n.º 8
0
void
beginningofline(void)
{
    if (zmult < 0) {
	zmult = -zmult;
	endofline();
	return;
    }
    while (zmult--) {
	if (cs == 0)
	    return;
	if (line[cs - 1] == '\n')
	    if (!--cs)
		return;
	while (cs && line[cs - 1] != '\n')
	    cs--;
    }
}
Ejemplo n.º 9
0
char* fileGetLine(FILE *fin)
{
  int i, c;
  int maxline = INIT;
  char *line = (char*)malloc (sizeof(char) * maxline);

  for (i = 0;(c = getc(fin)) != EOF && !(endofline(fin, c)); i++)
    {
      if (i >= maxline - 1)
	{
	  maxline *= GROW;
	  line = (char*) realloc(line, sizeof(char) * maxline);
	}
      line[i] = (char)c;
    }

  return (c == EOF && i == 0) ? NULL : line;
}
Ejemplo n.º 10
0
Archivo: csv.c Proyecto: faisal-w/mlelr
/* sample input: "LU",86.25,"11/4/1998","2:19PM",+4.0625 */
char *csvgetline(FILE *fin, char delim, int compress)
{
	int i, c;
	char *newl, *news;

	if (line == NULL) {			/* allocate on first call */
		maxline = maxfield = 1;
		line = (char *) malloc(maxline);
		sline = (char *) malloc(maxline);
		field = (char **) malloc(maxfield*sizeof(field[0]));
		if (line == NULL || sline == NULL || field == NULL) {
			reset();
			return NULL;		/* out of memory */
		}
	}
	for (i=0; (c=getc(fin))!=EOF && !endofline(fin,c); i++) {
		if (i >= maxline-1) {	/* grow line */
			maxline *= 2;		/* double current size */
			newl = (char *) realloc(line, maxline);
			if (newl == NULL) {
				reset();
				return NULL;
			}
			line = newl;
			news = (char *) realloc(sline, maxline);
			if (news == NULL) {
				reset();
				return NULL;
			}
			sline = news;


		}
		line[i] = c;
	}
	line[i] = '\0';
	if (split(delim, compress) == NOMEM) {
		reset();
		return NULL;			/* out of memory */
	}
	return (c == EOF && i == 0) ? NULL : line;
}
Ejemplo n.º 11
0
int combine(char**out, int start, char**_res)
{	
	int end=endofline(out, start);
	char*res=(*_res);
	if(start==end)
	{
		return(FALSE);
	}
	res=malloc(256);	
	ZERO(res, 256);
	sprintf(res, "%s", out[start]);
	start++;
	while(start!=end)
	{
		sprintf(res, "%s %s", res, out[start]);
		start++;
	}
	(*_res)=res;
	return(TRUE);
}
Ejemplo n.º 12
0
void
edit()
{
	extern	bool_t	need_redraw;
	int	c;
	register char	*p, *q;

	Prenum = 0;

	/* position the display and the cursor at the top of the file. */
	*Topchar = *Filemem;
	*Curschar = *Filemem;
	Cursrow = Curscol = 0;

	do_mlines();		/* check for mode lines before starting */

	updatescreen();

	for ( ;; ) {

	/* Figure out where the cursor is based on Curschar. */
	cursupdate();

	if (need_redraw && !anyinput()) {
		updatescreen();
		need_redraw = FALSE;
	}

	if (!anyinput())
		windgoto(Cursrow,Curscol);


	c = vgetc();

	if (State == NORMAL) {

		/* We're in the normal (non-insert) mode. */

		/* Pick up any leading digits and compute 'Prenum' */
		if ( (Prenum>0 && isdigit(c)) || (isdigit(c) && c!='0') ){
			Prenum = Prenum*10 + (c-'0');
			continue;
		}
		/* execute the command */
		normal(c);
		Prenum = 0;

	} else {

		/*
		 * Insert or Replace mode.
		 */
		switch (c) {

		case ESC:	/* an escape ends input mode */

			/*
			 * If we just did an auto-indent, truncate the
			 * line, and put the cursor back.
			 */
			if (did_ai) {
				Curschar->linep->s[0] = NUL;
				Curschar->index = 0;
				did_ai = FALSE;
			}

			set_want_col = TRUE;

			/* Don't end up on a '\n' if you can help it. */
			if (gchar(Curschar) == NUL && Curschar->index != 0)
				dec(Curschar);

			/*
			 * The cursor should end up on the last inserted
			 * character. This is an attempt to match the real
			 * 'vi', but it may not be quite right yet.
			 */
			if (Curschar->index != 0 && !endofline(Curschar))
				dec(Curschar);

			State = NORMAL;
			msg("");

			/* construct the Redo buffer */
			p=Redobuff;
			q=Insbuff;
			while ( q < Insptr )
				*p++ = *q++;
			*p++ = ESC;
			*p = NUL;
			updatescreen();
			break;

		case CTRL('D'):
			/*
			 * Control-D is treated as a backspace in insert
			 * mode to make auto-indent easier. This isn't
			 * completely compatible with vi, but it's a lot
			 * easier than doing it exactly right, and the
			 * difference isn't very noticeable.
			 */
		case BS:
			/* can't backup past starting point */
			if (Curschar->linep == Insstart->linep &&
			    Curschar->index <= Insstart->index) {
				beep();
				break;
			}

			/* can't backup to a previous line */
			if (Curschar->linep != Insstart->linep &&
			    Curschar->index <= 0) {
				beep();
				break;
			}

			did_ai = FALSE;
			dec(Curschar);
			if (State == INSERT)
				delchar(TRUE);
			/*
			 * It's a little strange to put backspaces into
			 * the redo buffer, but it makes auto-indent a
			 * lot easier to deal with.
			 */
			*Insptr++ = BS;
			Ninsert++;
			cursupdate();
			updateline();
			break;

		case CR:
		case NL:
			if (State == REPLACE)		/* DMT added, 12/89 */
				delchar(FALSE);
			*Insptr++ = NL;
			Ninsert++;
			opencmd(FORWARD, TRUE);		/* open a new line */
			break;

		default:
			did_ai = FALSE;
			insertchar(c);
			break;
		}
	}
	}
}
Ejemplo n.º 13
0
Archivo: edit.c Proyecto: kindy/vim0
void
edit()
{
        int c;
        char *p, *q;

        Prenum = 0;

        /* position the display and the cursor at the top of the file. */
        *Topchar = *Filemem;
        *Curschar = *Filemem;
        Cursrow = Curscol = 0;

        for ( ;; ) {

        /* Figure out where the cursor is based on Curschar. */
        cursupdate();

        windgoto(Cursrow,Curscol);

        c = vgetc();

        if (State == NORMAL) {

                /* We're in the normal (non-insert) mode. */

                /* Pick up any leading digits and compute 'Prenum' */
                if ( (Prenum>0 && isdigit(c)) || (isdigit(c) && c!='0') ){
                        Prenum = Prenum*10 + (c-'0');
                        continue;
                }
                /* execute the command */
                normal(c);
                Prenum = 0;

        } else {

                switch (c) {        /* We're in insert mode */

                case ESC:        /* an escape ends input mode */

                        set_want_col = TRUE;

                        /* Don't end up on a '\n' if you can help it. */
                        if (gchar(Curschar) == NUL && Curschar->index != 0)
                                dec(Curschar);

                        /*
                         * The cursor should end up on the last inserted
                         * character. This is an attempt to match the real
                         * 'vi', but it may not be quite right yet.
                         */
                        if (Curschar->index != 0 && !endofline(Curschar))
                                dec(Curschar);

                        State = NORMAL;
                        msg("");
                        *Uncurschar = *Insstart;
                        Undelchars = Ninsert;
                        /* Undobuff[0] = '\0'; */
                        /* construct the Redo buffer */
                        p=Redobuff;
                        q=Insbuff;
                        while ( q < Insptr )
                                *p++ = *q++;
                        *p++ = ESC;
                        *p = NUL;
                        updatescreen();
                        break;

                case CTRL('D'):
                        /*
                         * Control-D is treated as a backspace in insert
                         * mode to make auto-indent easier. This isn't
                         * completely compatible with vi, but it's a lot
                         * easier than doing it exactly right, and the
                         * difference isn't very noticeable.
                         */
                case BS:
                        /* can't backup past starting point */
                        if (Curschar->linep == Insstart->linep &&
                            Curschar->index <= Insstart->index) {
                                beep();
                                break;
                        }

                        /* can't backup to a previous line */
                        if (Curschar->linep != Insstart->linep &&
                            Curschar->index <= 0) {
                                beep();
                                break;
                        }

                        did_ai = FALSE;
                        dec(Curschar);
                        delchar(TRUE);
                        Insptr--;
                        Ninsert--;
                        cursupdate();
                        updateline();
                        break;

                case CR:
                case NL:
                        *Insptr++ = NL;
                        Ninsert++;
                        opencmd(FORWARD, TRUE);                /* open a new line */
                        cursupdate();
                        updatescreen();
                        break;

                default:
                        did_ai = FALSE;
                        insertchar(c);
                        break;
                }
        }
        }
}
Ejemplo n.º 14
0
/*
 * optimize out scrolls (line breaks, and newlines)
 * arg. chooses between looking for inserts or deletes
 */
static int scrolls(int inserts)
{				/* returns true if it does something */
	struct video *vpv;	/* virtual screen image */
	struct video *vpp;	/* physical screen image */
	int i, j, k;
	int rows, cols;
	int first, match, count, target, end;
	int longmatch, longcount;
	int from, to;

	if (!term.t_scroll)	/* no way to scroll */
		return FALSE;

	rows = term.t_nrow;
	cols = term.t_ncol;

	first = -1;
	for (i = 0; i < rows; i++) {	/* find first wrong line */
		if (!texttest(i, i)) {
			first = i;
			break;
		}
	}

	if (first < 0)
		return FALSE;	/* no text changes */

	vpv = vscreen[first];
	vpp = pscreen[first];

	if (inserts) {
		/* determine types of potential scrolls */
		end = endofline(vpv->v_text, cols);
		if (end == 0)
			target = first;	/* newlines */
		else if (memcmp(vpp->v_text, vpv->v_text, 4*end) == 0)
			target = first + 1;	/* broken line newlines */
		else
			target = first;
	} else {
		target = first + 1;
	}

	/* find the matching shifted area */
	match = -1;
	longmatch = -1;
	longcount = 0;
	from = target;
	for (i = from + 1; i < rows - longcount /* P.K. */ ; i++) {
		if (inserts ? texttest(i, from) : texttest(from, i)) {
			match = i;
			count = 1;
			for (j = match + 1, k = from + 1;
			     j < rows && k < rows; j++, k++) {
				if (inserts ? texttest(j, k) :
				    texttest(k, j))
					count++;
				else
					break;
			}
			if (longcount < count) {
				longcount = count;
				longmatch = match;
			}
		}
	}
	match = longmatch;
	count = longcount;

	if (!inserts) {
		/* full kill case? */
		if (match > 0 && texttest(first, match - 1)) {
			target--;
			match--;
			count++;
		}
	}

	/* do the scroll */
	if (match > 0 && count > 2) {	/* got a scroll */
		/* move the count lines starting at target to match */
		if (inserts) {
			from = target;
			to = match;
		} else {
			from = match;
			to = target;
		}
		if (2 * count < abs(from - to))
			return FALSE;
		scrscroll(from, to, count);
		for (i = 0; i < count; i++) {
			vpp = pscreen[to + i];
			vpv = vscreen[to + i];
			memcpy(vpp->v_text, vpv->v_text, 4*cols);
			vpp->v_flag = vpv->v_flag;	/* XXX */
			if (vpp->v_flag & VFREV) {
				vpp->v_flag &= ~VFREV;
				vpp->v_flag |= ~VFREQ;
			}
#if	MEMMAP
			vscreen[to + i]->v_flag &= ~VFCHG;
#endif
		}
		if (inserts) {
			from = target;
			to = match;
		} else {
			from = target + count;
			to = match + count;
		}
#if	MEMMAP == 0
		for (i = from; i < to; i++) {
			unicode_t *txt;
			txt = pscreen[i]->v_text;
			for (j = 0; j < term.t_ncol; ++j)
				txt[j] = ' ';
			vscreen[i]->v_flag |= VFCHG;
		}
#endif
		return TRUE;
	}
	return FALSE;
}
Ejemplo n.º 15
0
/*
** Name: WTSUpLoad() -
**
** Description:
**	read the following format.
**		1) --
**		2) boundary if there is a new parameter.
**		3) "Content-Disposition: form-data"
**		4) name and filename (if it is a file) of the parameter + '\r\n'
**		5) if it is a file, the mime type (Content-Type) '\r\n'
**		6) '\r\n'
**		7) the value + '\r\n'
**
** Inputs:
**
** Outputs:
**
** Returns:
**	GSTATUS	:	GSTAT_OK
**
** Exceptions:
**	None
**
** Side Effects:
**	None
**
** History:
**	8-Sep-98 (marol01)
**          fix issue with the upload feature. The format expected by the
**          function is incorrect when the system try to read a file with
**          a supported mime type.
*/
GSTATUS
WTSUpLoad(
    char*       data,
    i4          *datalength,
    WTS_SESSION *session)
{
    GSTATUS     err = GSTAT_OK;
    i4          linelen;
    i4          count = 0;
    i4          eventnum;
    char*       p = data;
    char*       work;
    PWTS_UPLOAD load = session->load;

    load->fragment &= ~FRAGMENT_INCOMPLETE;

    /*
    ** Terminate processing loop if incomplete message is encountered.
    ** This will be recalled once more data has been read.
    */
    while ((err == GSTAT_OK) && (*datalength > 0) &&
           ((load->fragment & FRAGMENT_INCOMPLETE) == 0))
    {
        count = 0;
        linelen = endofline (load, p, *datalength);
        if ((load->fragment != 0) && (load->state != US_FWRITE))
        {
            if ((load->fragment & FRAGMENT_MASK) == FRAGMENT_HEAD)
            {
                load->fraglen = linelen;
                err = GAlloc(&load->buf, load->fraglen, FALSE);
                MECOPY_VAR_MACRO(p, linelen, load->buf);
                work = NULL;
                p+=linelen;
                *datalength-=linelen;
            }
            else if ((load->fragment & FRAGMENT_MASK) == FRAGMENT_TAIL)
            {
                err = GAlloc(&load->buf, load->fraglen + linelen, TRUE);
                MECOPY_VAR_MACRO(p, linelen, load->buf + load->fraglen);
                work = load->buf;
                p+=linelen;
                *datalength-=linelen;
                linelen = load->fraglen + linelen;
            }
        }
        else
        {
            work = p;
        }
        if (work != NULL)
        {
            if (linelen != 0)
            {
                eventnum = getstrevent(load, work, linelen, *datalength,
                    &count);
                err = handler[load->state][eventnum](session, work, linelen,
                    &count);
                if (err == GSTAT_OK)
                {
                    if ((load->fragment & FRAGMENT_MASK) != FRAGMENT_TAIL)
                    {
                        /*
                        ** Processed whole line skip '\r\n'
                        */
                        count += ((count==linelen) &&
                            (*datalength>linelen+2)) ? 2 : 0;
                        *datalength -= count;
                        p+=count;
                    }
                    else
                    {
                        load->fragment = 0;
                    }
                }
                else
                {
                    break;
                }
            }
            else
            {
                if (load->state == US_FPARM)
                {
                    load->state = US_FWRITE;
                }
                count+=2;
                *datalength -= count;
                p+=count;
            }
        }
    }
    return(err);
}