Esempio n. 1
0
gssize
_web_socket_util_parse_status_line (const gchar *data,
                                    gsize length,
                                    guint *status,
                                    gchar **reason)
{
  const gchar *at;
  const gchar *end;
  gsize n;
  guint64 num;
  gchar *ep;

  /*
   * Here we parse a line like:
   *
   * HTTP/1.1 101 Switching protocols
   */

  at = data;
  end = memchr (at, '\n', length);
  if (end == NULL)
    return 0; /* need more data */

  n = parse_version (at, (end - at));
  if (n == 0 || at[n] != ' ')
    return -1;
  at += n;

  /* Extra spaces */
  at = strskip (at, ' ', end);

  /* First check for space after status */
  if (memchr (at, ' ', (end - at)) == NULL)
    return -1;

  /* This will stop at above space */
  num =  g_ascii_strtoull (at, &ep, 10);
  if (num == 0 || num > G_MAXUINT || *ep != ' ')
    return -1;

  at = strskip (ep, ' ', end);

  if (reason)
    {
      *reason = g_strndup (at, (end - at));
      g_strstrip (*reason);
    }
  if (status)
    *status = (guint)num;
  return (end - data) + 1;
}
bool
AsciiProcessor::emp_cmd( const char * buf,
                         BuilderBase * build,
                         const char * & next )
{
    int fref;

    if ( ! str2val( buf, fref, next ) )
    {
        P_ERROR( "EMP no frame reference" );
        return CMD_ERR;
    }

    buf = next;

    if ( ! strskip( buf, DELIMITER_CHAR, next ) )
    {
        P_ERROR( "wrong delimiter char" );
        return CMD_ERR;
    }

    build->set_cmd_empty_frame( fref );

    P_MSG( "build->set_cmd_empty_frame(" << fref << ");" );

    return CMD_OK;
}
bool
AsciiProcessor::visible_cmd( const char * buf,
                             BuilderBase * build,
                             const char * & next,
                             int visible )
{
    int fref;

    if ( ! str2val( buf, fref, next ) )
    {
        P_ERROR( "no frame reference" );
        return CMD_ERR;
    }

    buf = next;

    if ( ! strskip( buf, DELIMITER_CHAR, next ) )
    {
        P_ERROR( "wrong delimiter char" );
        return CMD_ERR;
    }

    P_MSG( "build->set_cmd_set_frame_visible(" << fref << ");" );

    if ( ! build->set_cmd_set_frame_visible( fref, visible ) )
    {
        P_ERROR( "wrong frame reference" );
    }

    return CMD_OK;
}
bool
AsciiProcessor::status_line_cmd( const char * buf,
                                 BuilderBase * build,
                                 const char * & next )
{
    int res = get_string( buf, characters, next );

    if ( res < 0 )
    {
        P_ERROR( "SL wrong text specification, retval= " << res );
        return CMD_ERR;
    }

    buf = next;

    if ( ! strskip( buf, DELIMITER_CHAR, next ) )
    {
        P_ERROR( "wrong delimiter char" );
        return CMD_ERR;
    }

    P_MSG( "build->set_cmd_set_status(characters);" );

    build->set_cmd_set_status_line( characters.cur_size, characters.tab );

    return CMD_OK;
}
int
AsciiProcessor::get_string( const char * buf,
                            int & offset,
                            char const * & next )
{
    if ( ! strskip( buf, "\"", next ) )
    {
        return -1;
    }

    offset = next - buf;

    buf = next;

    int k = 0;

    while ( *buf !=  '\"' && *buf != '\0' )
    {
        ++buf;
        ++k;
    }

    if ( *buf != '\"' )
    {
        return -2;
    }

    ++buf;

    next = buf;

    return k;
}
bool
AsciiProcessor::visual_area_cmd( const char * buf,
                                 BuilderBase * build,
                                 const char* & next )
{
    Area2d a;
    double d1;
    int res = get_max5_tuple( buf, 4,
                              a.center.x, a.center.y,
                              a.size_x, a.size_y,
                              d1,
                              next );

    if ( res != 4 )
    {
        P_ERROR( "SETAREA wrong area specification, retval= " << res );
        return CMD_ERR;
    }

    buf = next;

    if ( ! strskip( buf, DELIMITER_CHAR, next ) )
    {
        P_ERROR( "wrong delimiter char" );
        return CMD_ERR;
    }

    P_MSG( "build->set_cmd_set_view_area(area[(" << a.center.x << "," << a.center.y << ")," << a.size_x << "," << a.size_y << "]" << ");" );

    build->set_cmd_set_view_area( a );

    return CMD_OK;
}
bool
AsciiProcessor::ins_frame( const char * buf,
                           int fref,
                           BuilderBase * build,
                           const char * & next )
{
    int id = 0, lay = 0, vis = 1;
    Point2d pos;
    Angle ang;

    if ( ! get_frame_attr( buf, id, lay, vis, next ) )
    {
        return CMD_ERR;
    }

    int res = get_frame( next, pos, ang, next );

    if ( res < -1 )
    {
        return CMD_ERR;
    }

    if ( ! strskip( next, DELIMITER_CHAR, next ) )
    {
        return CMD_ERR;
    }

    build->set_cmd_insert_frame( fref, id, pos, ang, lay );

    return CMD_OK;
}
bool
AsciiProcessor::background_color_cmd( const char * buf,
                                      BuilderBase * build,
                                      const char* & next )
{
    RGBcolor col;

    if ( ! get_col( buf, col, next ) )
    {
        P_ERROR( "wrong color [" << buf << "]" );
        return false;
    }

    buf = next;

    if ( ! strskip( buf, DELIMITER_CHAR, next ) )
    {
        P_ERROR( "wrong delimiter char" );
        return CMD_ERR;
    }

    build->set_cmd_set_background_color( col );

    return CMD_OK;
}
bool
AsciiProcessor::del_cmd( const char * buf,
                         BuilderBase * build,
                         const char* & next )
{
    int fref;

    if ( ! str2val( buf, fref, next ) )
    {
        P_ERROR( "DEL no frame or object reference" );
        return CMD_ERR;
    }

    buf = next;

    int oref;

    if ( str2val( buf, oref, next ) )
    {
        buf = next;

        if ( ! strskip( buf, DELIMITER_CHAR, next ) )
        {
            P_ERROR( "wrong delimiter char" );
            return CMD_ERR;
        }

        build->set_cmd_remove_object( fref, oref );

        P_MSG( "build->set_cmd_remove_object(fref,oref);" );
        return CMD_OK;
    }

    if ( ! strskip( buf, DELIMITER_CHAR, next ) )
    {
        P_ERROR( "wrong delimiter char" );
        return CMD_ERR;
    }

    build->set_cmd_remove_frame( fref );

    P_MSG( "build->set_cmd_remove_frame(fref);" );

    return CMD_OK;
}
Esempio n. 10
0
File: tparse.c Progetto: LLNL/lmt
void
parse_utils (void)
{
    int i;
    const char *s[] = {
        "foo;bar;baz",
        "foo;bar;baz;",
    };

    for (i = 0; i < sizeof (s) / sizeof (s[0]); i++) {
        const char *p;

        p = strskip(s[i], 0, ';');
        msg ("strskip 0: %s", p == s[i] ? "OK" : "FAIL");
        p = strskip(s[i], 1, ';');
        msg ("strskip 1: %s", p == s[i]+4 ? "OK" : "FAIL");
        p = strskip(s[i], 2, ';');
        msg ("strskip 2: %s", p == s[i]+8 ? "OK" : "FAIL");
        p = strskip(s[i], 3, ';');
        msg ("strskip 3: %s", p == s[i]+strlen (s[i]) ? "OK" : "FAIL");
        p = strskip(s[i], 4, ';');
        msg ("strskip 4: %s", p ==  NULL ? "OK" : "FAIL");
    }
    for (i = 0; i < sizeof (s) / sizeof (s[0]); i++) {
        const char *q = s[i];
        char *p;

        while ((p = strskipcpy (&q, 1, ';'))) {
            msg ("strskipcpy 1: '%s'", p);
            free (p);
        }
        if (strlen (q) > 0)
            msg ("strskipcpy failed to consume entire string");
    }
    {
        char *p = xstrdup ("fubar");

        strappendfield (&p, "smurf", ';');
        msg ("strappendfield: %s", p);
        free (p);
    }
    
}
Esempio n. 11
0
File: mdt.c Progetto: LaHaine/lmt
int
lmt_mdt_decode_v1_mdtinfo (const char *s, char **mdtnamep,
                           uint64_t *inodes_freep, uint64_t *inodes_totalp,
                           uint64_t *kbytes_freep, uint64_t *kbytes_totalp,
                           List *mdopsp)
{
    int retval = -1;
    char *mdtname = xmalloc (strlen(s) + 1);
    char *cpy = NULL;
    uint64_t kbytes_free, kbytes_total;
    uint64_t inodes_free, inodes_total;
    List mdops = list_create ((ListDelF)free);
    int i = 0;

    if (sscanf (s, "%[^;];%"PRIu64";%"PRIu64";%"PRIu64";%"PRIu64";",
                mdtname, &inodes_free, &inodes_total,
                &kbytes_free, &kbytes_total) != 5) {
        if (lmt_conf_get_proto_debug ())
            msg ("lmt_mdt_v1: parse error: mdtinfo");
        goto done;
    }
    if (!(s = strskip (s, 5, ';'))) {
        if (lmt_conf_get_proto_debug ())
            msg ("lmt_mdt_v1: parse error: skipping mdtinfo");
        goto done;
    }
    while ((cpy = strskipcpy (&s, 3, ';'))) {
        if (i >= optablen_mdt_v1) {
            if (lmt_conf_get_proto_debug ())
                msg ("lmt_mdt_v1: parse error: too many mdops");
            free (cpy);
            goto done;
        }
        strappendfield (&cpy, optab_mdt_v1[i++], ';');
        list_append (mdops, cpy);
    }
    if (strlen (s) > 0) {
        if (lmt_conf_get_proto_debug ())
            msg ("lmt_mdt_v1: parse error: mdtinfo: string not exhausted");
        goto done;
    }
    *mdtnamep = mdtname;
    *inodes_freep = inodes_free;
    *inodes_totalp = inodes_total;
    *kbytes_freep = kbytes_free;
    *kbytes_totalp = kbytes_total;
    *mdopsp = mdops;
    retval = 0;
done:
    if (retval < 0) {
        free (mdtname);
        list_destroy (mdops);
    }
    return retval;
}
bool
AsciiProcessor::mov_cmd( const char * buf,
                         BuilderBase * build,
                         const char* & next )
{
    int fref;
    Point2d pos;
    Angle ang;

    if ( ! str2val( buf, fref, next ) )
    {
        P_ERROR( "MOV no frame reference specified" );
        return CMD_ERR;
    }

    buf = next;

    int res = get_frame( buf, pos, ang, next );

    if ( res < 1 )
    {
        return CMD_ERR;
    }

    if ( ! strskip( next, DELIMITER_CHAR, next ) )
    {
        return CMD_ERR;
    }

    switch ( res )
    {

    case 3:
        build->set_cmd_set_frame_pos_ang( fref, pos, ang );
        P_MSG( "build->set_cmd_set_frame_pos_ang(fref,(" << pos.x << "," << pos.y << ")," << ang.get_value() << ");" );
        break;

    case 2:
        build->set_cmd_set_frame_pos( fref, pos );
        P_MSG( "build->set_cmd_set_frame_pos(fref,pos)" );
        break;

    case 1:
        build->set_cmd_set_frame_ang( fref, ang );
        P_MSG( "build->set_cmd_set_frame_ang(fref,ang);" );
        break;
    }

    return CMD_OK;
}
Esempio n. 13
0
File: util.c Progetto: KnightKu/lmt
/* Copy a group of n sep-delimited fields
 * Don't return trailing delimiter, if any, in copy.
 */
char *
strskipcpy (const char **sp, int n, char sep)
{
    char *res = NULL;
    const char *s = *sp;
    const char *p = strskip (s, n, sep);
    int len = p ? (p - s) : 0;

    if (len > 0)
        res = xstrndup (s, s[len - 1] == sep ? len - 1 : len);
    if (res)
        *sp += len;
    return res;
}
int
AsciiProcessor::get_lines( const char * buf,
                           Multi< Line2d > & lines,
                           char const * & next )
{
    int res;
    int k = 0;

    while ( 1 )
    {
        if ( k >= lines.max_size )
        {
            lines.set_cur_size( k ); //do not loose entries
            lines.set_max_size_and_preserve_cur_size( k + k + 1 );
        }

        res = get_line( buf, lines.tab[k], next );

        if ( res == -1 )
        {
            break;
        }

        if ( res != 4 )
        {
            return -1;
        }

        buf = next;

        ++k;
    };

    if ( ! strskip( buf, DELIMITER_CHAR, next ) )
    {
        P_ERROR( "wrong delimiter char" );
        return -1;
    }

    lines.set_cur_size( k );

    return k;
}
int
AsciiProcessor::get_circlearcs( const char * buf,
                                Multi< CircleArc2d > & circles,
                                char const* & next )
{
    int res;
    int k = 0;

    while ( 1 )
    {
        if ( k >= circles.max_size )
        {
            circles.set_cur_size( k ); //do not loose entries
            circles.set_max_size_and_preserve_cur_size( k + k + 1 );
        }

        res = get_circlearc( buf, circles.tab[k], next );

        if ( res == -1 )
        {
            break;
        }

        if ( res != 5 && res != 3 )
        {
            return -1;
        }

        buf = next;

        ++k;
    };

    if ( ! strskip( buf, DELIMITER_CHAR, next ) )
    {
        P_ERROR( "wrong delimiter char" );
        return -1;
    }

    circles.set_cur_size( k );

    return k;
}
int
AsciiProcessor::get_points( const char * buf,
                            Multi< Point2d > & points,
                            char const* & next )
{
    int res;
    int k = 0;

    while ( 1 )
    {
        if ( k >= points.max_size )
        {
            points.set_cur_size( k ); //do not loose the points
            points.set_max_size_and_preserve_cur_size( k + k + 1 );
        }

        res = get_point( buf, points.tab[k], next );

        if ( res == -1 )
        {
            break;
        }

        if ( res != 2 )
        {
            return -1;
        }

        buf = next;

        k++;
    };

    if ( ! strskip( buf, DELIMITER_CHAR, next ) )
    {
        P_ERROR( "wrong delimiter char" );
        return -1;
    }

    points.set_cur_size( k );

    return k;
}
Esempio n. 17
0
File: mdt.c Progetto: LaHaine/lmt
int
lmt_mdt_decode_v1 (const char *s, char **mdsnamep, float *pct_cpup,
                   float *pct_memp, List *mdtinfop)
{
    const int mdtfields = 5 + 3 * optablen_mdt_v1;
    int retval = -1;
    char *mdsname = xmalloc (strlen(s) + 1);
    char *cpy = NULL;
    float pct_mem, pct_cpu;
    List mdtinfo = list_create ((ListDelF)free);

    if (sscanf (s, "%*f;%[^;];%f;%f;", mdsname, &pct_cpu, &pct_mem) != 3) {
        if (lmt_conf_get_proto_debug ())
            msg ("lmt_mdt_v1: parse error: mdsinfo");
        goto done;
    }
    if (!(s = strskip (s, 4, ';'))) {
        if (lmt_conf_get_proto_debug ())
            msg ("lmt_mdt_v1: parse error: skipping mdsinfo");
        goto done;
    }
    mdtinfo = list_create ((ListDelF)free);
    while ((cpy = strskipcpy (&s, mdtfields, ';')))
        list_append (mdtinfo, cpy);
    if (strlen (s) > 0) {
        if (lmt_conf_get_proto_debug ())
            msg ("lmt_mdt_v1: parse error: string not exhausted");
        goto done;
    }
    *mdsnamep = mdsname;
    *pct_cpup = pct_cpu;
    *pct_memp = pct_mem;
    *mdtinfop = mdtinfo;
    retval = 0;
done:
    if (retval < 0) {
        free (mdsname);
        list_destroy (mdtinfo);
    }
    return retval;
}
int
AsciiProcessor::get_string( const char * buf,
                            Multi< char > & str,
                            char const * & next )
{
    if ( ! strskip( buf, "\"", next ) )
    {
        return -1;
    }

    buf = next;

    int k = 0;

    while ( *buf !=  '\"' && *buf != '\0' )
    {
        if ( k >= str.max_size )
        {
            str.set_cur_size( k ); //do not loose entries
            str.set_max_size_and_preserve_cur_size( k + k + 1 );
        }

        str.tab[k] = *buf;

        ++buf;
        ++k;
    }

    if ( *buf != '\"' )
    {
        return -2;
    }

    ++buf;

    next = buf;

    str.set_cur_size( k );

    return k;
}
Esempio n. 19
0
File: ost.c Progetto: haasken/lmt
int
lmt_ost_decode_v2 (const char *s, char **ossnamep, float *pct_cpup,
                   float *pct_memp, List *ostinfop)
{
    int retval = -1;
    char *ossname =  xmalloc (strlen(s) + 1);
    char *cpy = NULL;
    float pct_mem, pct_cpu;
    List ostinfo = list_create ((ListDelF)free);

    if (sscanf (s, "%*f;%[^;];%f;%f;", ossname, &pct_cpu, &pct_mem) != 3) {
        if (lmt_conf_get_proto_debug ())
            msg ("lmt_ost_v2: parse error: oss component");
        goto done;
    }
    if (!(s = strskip (s, 4, ';'))) {
        if (lmt_conf_get_proto_debug ())
            msg ("lmt_ost_v2: parse error: skipping oss component");
        goto done;
    }
    while ((cpy = strskipcpy (&s, 15, ';')))
        list_append (ostinfo, cpy);
    if (strlen (s) > 0) {
        if (lmt_conf_get_proto_debug ())
            msg ("lmt_ost_v2: parse error: string not exhausted");
        goto done;
    }
    *ossnamep = ossname;
    *pct_cpup = pct_cpu;
    *pct_memp = pct_mem;
    *ostinfop = ostinfo;
    retval = 0;
done:
    if (retval < 0) {
        free (ossname);
        list_destroy (ostinfo);
    }
    return retval;
}
/*!

  \retval 0 a empty tuple was received "()"
  \retval 1 a one tuple "( <num> )"
  \retval 2,3,4,5 analogous with 1
  \retval -1: the was no tuple at all, i.e. the string doesn't begin with a '('
  \retval -2: some failure occured
*/
int
AsciiProcessor::get_max5_tuple( const char * buf,
                                int num,
                                double & d0,
                                double & d1,
                                double & d2,
                                double & d3,
                                double & d4,
                                char const * & next )
{
    if ( ! strskip( buf, '(', next ) )
    {
        return -1;
    }

    buf = next;

    if ( ! str2val( buf, d0, next ) )
    {
        if ( strskip( buf, ')', next ) )
        {
            return 0;
        }

        return -2;
    }

    if ( num < 1 ) return -2;

    buf = next;

    if ( ! strskip( buf, ',', next ) )
    {
        if ( strskip( buf, ')', next ) )
        {
            return 1;
        }

        return -2;
    }

    if ( num < 2 ) return -2;

    buf = next;

    if ( ! str2val( buf, d1, next ) )
    {
        return -2;
    }

    buf = next;

    if ( !strskip( buf, ',', next ) )
    {
        if ( strskip( buf, ')', next ) )
        {
            return 2;
        }

        return -2;
    }

    if ( num < 3 ) return -2;

    buf = next;

    if ( !str2val( buf, d2, next ) )
    {
        return -2;
    }

    buf = next;

    if ( ! strskip( buf, ',', next ) )
    {
        if ( strskip( buf, ')', next ) )
        {
            return 3;
        }

        return -2;
    }

    if ( num < 4 ) return -2;

    buf = next;

    if ( ! str2val( buf, d3, next ) )
    {
        return -2;
    }

    buf = next;

    if ( ! strskip( buf, ',', next ) )
    {
        if ( strskip( buf, ')', next ) )
        {
            return 4;
        }

        return -2;
    }

    if ( num < 5 ) return -2;

    buf = next;

    if ( ! str2val( buf, d4, next ) )
    {
        return -2;
    }

    buf = next;

    if ( ! strskip( buf, ')', next ) )
    {
        return -2;
    }

    return 5;
}
Esempio n. 21
0
File: mdt.c Progetto: LaHaine/lmt
int lmt_mds_decode_v2 (const char *s, char **mdsnamep, char **mdtnamep,
                       float *pct_cpup, float *pct_memp,
                       uint64_t *inodes_freep, uint64_t *inodes_totalp,
                       uint64_t *kbytes_freep, uint64_t *kbytes_totalp,
                       List *mdopsp)
{
    char *mdsname = xmalloc (strlen(s) + 1);
    char *mdtname = xmalloc (strlen(s) + 1);
    List mdops = list_create ((ListDelF)free);
    int i = 0, retval = -1;
    char *cpy = NULL;
    float pct_mem, pct_cpu;
    uint64_t kbytes_free, kbytes_total;
    uint64_t inodes_free, inodes_total;

    if (sscanf (s, "%*f;%[^;];%[^;];%f;%f;%"
                PRIu64";%"PRIu64";%"PRIu64";%"PRIu64";", mdsname, mdtname,
                &pct_cpu, &pct_mem, &inodes_free, &inodes_total,
                &kbytes_free, &kbytes_total) != 8) {
        if (lmt_conf_get_proto_debug ())
            msg ("lmt_mds_v2: parse error: mds component");
        goto done;
    }
    if (!(s = strskip (s, 9, ';'))) {
        if (lmt_conf_get_proto_debug ())
            msg ("lmt_mds_v2: parse error: skipping mds component");
        goto done;
    }
    while ((cpy = strskipcpy (&s, 3, ';'))) {
        if (i >= optablen_mds_v2) {
            if (lmt_conf_get_proto_debug ())
                msg ("lmt_mds_v2: parse error: too many mdops");
            free (cpy);
            goto done;
        }
        strappendfield (&cpy, optab_mds_v2[i++], ';');
        if (!list_append (mdops, cpy)) {
            free (cpy);
            goto done;
        }
    }
    if (strlen (s) > 0) {
        msg ("lmt_mds_v2: parse error: string not exhausted");
        goto done;
    }
    *mdsnamep = mdsname;
    *mdtnamep = mdtname;
    *pct_cpup = pct_cpu;
    *pct_memp = pct_mem;
    *inodes_freep = inodes_free;
    *inodes_totalp = inodes_total;
    *kbytes_freep = kbytes_free;
    *kbytes_totalp = kbytes_total;
    *mdopsp = mdops;
    retval = 0;
done:
    if (retval < 0) {
        free (mdsname);
        free (mdtname);
        list_destroy (mdops);
    }
    return retval;
}
bool
AsciiProcessor:: get_frame_attr( const char * buf,
                                 int & id,
                                 int & lay,
                                 int & vis,
                                 char const * & next )
{
    next = buf;
    bool got_attr[4] = { false, false, false, false };

    while ( next )
    {
        if ( strskip( buf, "id", next ) )
        {
            if ( got_attr[0] )
            {
                return false;
            }

            got_attr[0] = true;

            buf = next;

            if ( ! strskip( buf, "=", next ) )
            {
                return false;
            }

            buf = next;

            if ( ! str2val( buf, id, next ) )
            {
                return false;
            }

            buf = next;
        }
        else if ( strskip( buf, "lay", next ) )
        {
            if ( got_attr[1] )
            {
                return false;
            }

            got_attr[1] = true;

            buf = next;

            if ( ! strskip( buf, "=", next ) )
            {
                return false;
            }

            buf = next;

            if ( ! str2val( buf, lay, next ) )
            {
                return false;
            }

            buf = next;
        }
        else if ( strskip( buf, "vis", next ) )
        {
            bool b;

            if ( got_attr[1] )
            {
                return false;
            }

            got_attr[1] = true;

            buf = next;

            if ( ! strskip( buf, "=", next ) )
            {
                return false;
            }

            buf = next;

            if ( ! str2val( buf, b, next ) )
            {
                return false;
            }

            vis = b;

            buf = next;
        }
        else
        {
            next = buf;
            return true;
        }
    }

    next = buf;

    return true;
}
Esempio n. 23
0
/**
 * web_socket_util_parse_req_line:
 * @data: (array length=length): the input data
 * @length: length of data
 * @method: (out): location to place HTTP method, or %NULL
 * @resource: (out): location to place HTTP resource path, or %NULL
 *
 * Parse an HTTP request line.
 *
 * The number of bytes parsed will be returned if parsing succeeds, including
 * the new line at the end of the request line. A negative value will be
 * returned if parsing fails.
 *
 * If the HTTP request line was truncated (ie: not all of it was present
 * within @length) then zero will be returned.
 *
 * The @method and @resource should point to string pointers. The values
 * returned should be freed by the caller using g_free().
 *
 * Return value: zero if truncated, negative if fails, or number of
 *               characters parsed
 */
gsize
web_socket_util_parse_req_line (const gchar *data,
                                gsize length,
                                gchar **method,
                                gchar **resource)
{
  const gchar *end;
  const gchar *method_end;
  const gchar *path_beg;
  const gchar *path_end;
  const gchar *version;
  const gchar *last;
  gsize n;

  /*
   * Here we parse a line like:
   *
   * GET /path/to/file HTTP/1.1
   */

  g_return_val_if_fail (data != NULL || length == 0, -1);

  if (length == 0)
    return 0;

  end = memchr (data, '\n', length);
  if (end == NULL)
    return 0; /* need more data */

  if (data[0] == ' ')
    return -1;

  method_end = memchr (data, ' ', (end - data));
  if (method_end == NULL)
    return -1;

  path_beg = strskip (method_end + 1, ' ', end);
  path_end = memchr (path_beg, ' ', (end - path_beg));
  if (path_end == NULL)
    return -1;

  version = strskip (path_end + 1, ' ', end);

  /* Returns number of characters consumed */
  n = parse_version (version, (end - version));
  if (n == 0)
    return -1;

  last = version + n;
  while (last != end)
    {
      /* Acceptable trailing characters */
      if (!strchr ("\r ", last[0]))
        return -1;
      last++;
    }

  if (method)
    *method = g_strndup (data, (method_end - data));
  if (resource)
    *resource = g_strndup (path_beg, (path_end - path_beg));
  return (end - data) + 1;
}
bool
AsciiProcessor::ins_string_grid( const char * buf,
                                 int fref,
                                 BuilderBase * build,
                                 const char * & next )
{
    int id = 0, lay = 0, fil = 0;
    RGBcolor col( 0, 0, 0 );

    if ( ! get_obj_attr( buf, id, lay, col, fil, next ) )
    {
        return CMD_ERR;
    }

    while ( true )
    {
        double p_x, p_y, v1_x, v1_y, v2_x, v2_y, d3, d4, d5;
        int num_v1 = 1, num_v2 = 1;

        if ( ! strskip( next, '(', next ) )
        {
            return CMD_ERR;
        }

        if ( get_max5_tuple( next, 2, p_x, p_y, d3, d4, d5, next ) != 2 )
        {
            return CMD_ERR;
        }

        if ( get_max5_tuple( next, 2, v1_x, v1_y, d3, d4, d5, next ) != 2 )
        {
            return CMD_ERR;
        }

        if ( strskip( next, ':', next ) )
        {
            if ( ! str2val( next, num_v1, next ) )
            {
                return CMD_ERR;
            }
        }

        if ( get_max5_tuple( next, 2, v2_x, v2_y, d3, d4, d5, next ) != 2 )
        {
            return CMD_ERR;
        }

        if ( strskip( next, ':', next ) )
        {
            if ( ! str2val( next, num_v2, next ) )
            {
                return CMD_ERR;
            }
        }

        const double  d1 = 1.0 / double( num_v1 );

        const double  d2 = 1.0 / double( num_v2 );

        bool early_stop = false;

        for ( int i = 0; i < num_v2; ++i )
        {
            Point2d point;
            point.x = p_x + i * v2_x * d2 ;
            point.y = p_y + i * v2_y * d2 ;

            for ( int j = 0; j < num_v1; j++ )
            {
                int offset;

                if ( strskip( next, ')', next ) )
                {
                    early_stop = true;
                    break;
                }

                buf = next;

                int res = get_string( buf, offset, next );

                if ( res < 0 )
                {
                    return CMD_ERR;
                }

                VisualObject2d * string2d = new VisualString2d( id, lay, col, point, res, buf + offset );

                build->set_cmd_insert_visobject( fref, string2d );

                point.x += v1_x * d1;
                point.y += v1_y * d1;
            }

            if ( early_stop )
            {
                break;
            }
        }

        if ( ! early_stop )
        {
            if ( ! strskip( next, ')', next ) )
            {
                return CMD_ERR;
            }
        }

        if ( strskip( next, DELIMITER_CHAR, next ) )
        {
            break;
        }
    }

    return CMD_OK;
}
bool
AsciiProcessor::ins_grid( const char * buf,
                          int fref,
                          BuilderBase * build,
                          const char * & next )
{
    int id = 0, lay = 0, fil = 0;
    RGBcolor col( 0, 0, 0 );

    if ( ! get_obj_attr( buf, id, lay, col, fil, next ) )
    {
        return CMD_ERR;
    }

    int idx = 0;

    while ( true ) //accept more then one grid!
    {
        double p_x, p_y, v1_x, v1_y, v2_x, v2_y, d3, d4, d5;
        int num_v1 = 1, num_v2 = 1;

        if ( ! strskip( next, '(', next ) )
        {
            return CMD_ERR;
        }

        if ( get_max5_tuple( next, 2, p_x, p_y, d3, d4, d5, next ) != 2 )
        {
            return CMD_ERR;
        }

        if ( get_max5_tuple( next, 2, v1_x, v1_y, d3, d4, d5, next ) != 2 )
        {
            return CMD_ERR;
        }

        if ( strskip( next, ':', next ) )
        {
            if ( ! str2val( next, num_v1, next ) )
            {
                return CMD_ERR;
            }
        }

        if ( get_max5_tuple( next, 2, v2_x, v2_y, d3, d4, d5, next ) != 2 )
        {
            return CMD_ERR;
        }

        if ( strskip( next, ':', next ) )
        {
            if ( ! str2val( next, num_v2, next ) )
            {
                return CMD_ERR;
            }
        }

        if ( ! strskip( next, ')', next ) )
        {
            return CMD_ERR;
        }

        lines.set_cur_size( idx );

        lines.set_max_size_and_preserve_cur_size( idx + num_v1 + num_v2 + 2 );

        const double  d1 = 1.0 / double( num_v1 );

        const double  d2 = 1.0 / double( num_v2 );

        for ( int i = 0; i < num_v2 + 1; ++i )
        {
            Line2d & l = lines.tab[idx];
            l.p1.x = p_x + i * v2_x * d2 ;
            l.p1.y = p_y + i * v2_y * d2 ;
            l.p2.x = l.p1.x + v1_x;
            l.p2.y = l.p1.y + v1_y;
            idx++;
        }

        for ( int i = 0; i < num_v1 + 1; ++i )
        {
            Line2d & l = lines.tab[idx];
            l.p1.x = p_x + i * v1_x * d1 ;
            l.p1.y = p_y + i * v1_y * d1 ;
            l.p2.x = l.p1.x + v2_x;
            l.p2.y = l.p1.y + v2_y;
            idx++;
        }

        if ( strskip( next, DELIMITER_CHAR, next ) )
        {
            break;
        }
    }

    build->set_cmd_insert_lines( fref, id, idx, lines.tab, lay, col );

    return CMD_OK;
}
bool
AsciiProcessor::ins_string( const char * buf,
                            int fref,
                            BuilderBase * build,
                            const char * & next )
{
    int id = 0, lay = 0, fil = 0;
    RGBcolor col( 0, 0, 0 );

    Point2d point;

    if ( ! get_obj_attr( buf, id, lay, col, fil, next ) )
    {
        return CMD_ERR;
    }

    while ( true ) //accept more then one string!
    {
        if ( ! strskip( next, '(', next ) )
        {
            return CMD_ERR;
        }

        if ( ! str2val( next, point.x, next ) )
        {
            return CMD_ERR;
        }

        if ( ! strskip( next, ',', next ) )
        {
            return CMD_ERR;
        }

        if ( ! str2val( next, point.y, next ) )
        {
            return CMD_ERR;
        }

        if ( ! strskip( next, ',', next ) )
        {
            return CMD_ERR;
        }

        buf = next;

        int offset;

        int res = get_string( buf, offset, next );

        if ( res < 0 )
        {
            return CMD_ERR;
        }

        if ( ! strskip( next, ')', next ) )
        {
            return CMD_ERR;
        }

        VisualObject2d * string2d = new VisualString2d( id, lay, col, point, res, buf + offset );

        build->set_cmd_insert_visobject( fref, string2d );

        if ( strskip( next, DELIMITER_CHAR, next ) )
        {
            break;
        }
    }

    return CMD_OK;
}
bool
AsciiProcessor:: get_obj_attr( const char * buf,
                               int & id,
                               int & lay,
                               RGBcolor & col,
                               int & fil,
                               char const * & next )
{
    next = buf;
    bool got_attr[4] = { false, false, false, false };

    while ( next )
    {
        if ( strskip( buf, "id", next ) )
        {
            if ( got_attr[0] )
            {
                return false;
            }

            got_attr[0] = true;

            buf = next;

            if ( ! strskip( buf, "=", next ) )
            {
                return false;
            }

            buf = next;

            if ( ! str2val( buf, id, next ) )
            {
                return false;
            }

            buf = next;
        }
        else if ( strskip( buf, "lay", next ) )
        {
            if ( got_attr[1] )
            {
                return false;
            }

            got_attr[1] = true;

            buf = next;

            if ( ! strskip( buf, "=", next ) )
            {
                return false;
            }

            buf = next;

            if ( ! str2val( buf, lay, next ) )
            {
                return false;
            }

            buf = next;
        }
        else if ( strskip( buf, "col", next ) )
        {
            if ( got_attr[2] )
            {
                return false;
            }

            got_attr[2] = true;

            buf = next;

            if ( ! strskip( buf, "=", next ) )
            {
                return false;
            }

#if 0
            buf = next;

            if ( ! strskip( buf, "#", next ) )
            {
                return false;
            }

#endif
            buf = next;

            if ( ! get_col( buf, col, next ) )
            {
                P_ERROR( "wrong color [" << buf << "]" );
                return false;
            }

            buf = next;

            //if ( !strconv(buf,col,next) )
            //return false;
            buf = next;
        }
        else if ( strskip( buf, "fil", next ) )
        {
            if ( got_attr[3] )
            {
                return false;
            }

            got_attr[3] = true;

            buf = next;

            if ( ! strskip( buf, "=", next ) )
            {
                return false;
            }

            buf = next;

            bool b;

            if ( ! str2val( buf, b, next ) )
            {
                return false;
            }

            fil = b;

            buf = next;
        }
        else
        {
            next = buf;
            return true;
        }
    }

    next = buf;

    return true;
}
Esempio n. 28
0
/*
 * Recursive file perusal.
 *
 * Return FALSE on "?", otherwise TRUE.
 *
 * This function could be made much more efficient with the use of "seek"
 * functionality, especially when moving backwards through a file, or
 * forwards through a file by less than a page at a time.  XXX XXX XXX
 */
bool show_file(const char *name, const char *what, int line, int mode)
{
	int i, k, n;

	struct keypress ch;

	/* Number of "real" lines passed by */
	int next = 0;

	/* Number of "real" lines in the file */
	int size;

	/* Backup value for "line" */
	int back = 0;

	/* This screen has sub-screens */
	bool menu = FALSE;

	/* Case sensitive search */
	bool case_sensitive = FALSE;

	/* Current help file */
	ang_file *fff = NULL;

	/* Find this string (if any) */
	char *find = NULL;

	/* Jump to this tag */
	const char *tag = NULL;

	/* Hold a string to find */
	char finder[80] = "";

	/* Hold a string to show */
	char shower[80] = "";

	/* Filename */
	char filename[1024];

	/* Describe this thing */
	char caption[128] = "";

	/* Path buffer */
	char path[1024];

	/* General buffer */
	char buf[1024];

	/* Lower case version of the buffer, for searching */
	char lc_buf[1024];

	/* Sub-menu information */
	char hook[26][32];

	int wid, hgt;
	
	/* TRUE if we are inside a RST block that should be skipped */
	bool skip_lines = FALSE;



	/* Wipe the hooks */
	for (i = 0; i < 26; i++) hook[i][0] = '\0';

	/* Get size */
	Term_get_size(&wid, &hgt);

	/* Copy the filename */
	my_strcpy(filename, name, sizeof(filename));

	n = strlen(filename);

	/* Extract the tag from the filename */
	for (i = 0; i < n; i++)
	{
		if (filename[i] == '#')
		{
			filename[i] = '\0';
			tag = filename + i + 1;
			break;
		}
	}

	/* Redirect the name */
	name = filename;

	/* Hack XXX XXX XXX */
	if (what)
	{
		my_strcpy(caption, what, sizeof(caption));

		my_strcpy(path, name, sizeof(path));
		fff = file_open(path, MODE_READ, FTYPE_TEXT);
	}

	/* Look in "help" */
	if (!fff)
	{
		strnfmt(caption, sizeof(caption), "Help file '%s'", name);

		path_build(path, sizeof(path), ANGBAND_DIR_HELP, name);
		fff = file_open(path, MODE_READ, FTYPE_TEXT);
	}

	/* Look in "info" */
	if (!fff)
	{
		strnfmt(caption, sizeof(caption), "Info file '%s'", name);

		path_build(path, sizeof(path), ANGBAND_DIR_INFO, name);
		fff = file_open(path, MODE_READ, FTYPE_TEXT);
	}

	/* Oops */
	if (!fff)
	{
		/* Message */
		msg("Cannot open '%s'.", name);
		event_signal(EVENT_MESSAGE_FLUSH);

		/* Oops */
		return (TRUE);
	}


	/* Pre-Parse the file */
	while (TRUE)
	{
		/* Read a line or stop */
		if (!file_getl(fff, buf, sizeof(buf))) break;

		/* Skip lines if we are inside a RST directive*/
		if(skip_lines){
			if(contains_only_spaces(buf))
				skip_lines=FALSE;
			continue;
		}

		/* Parse a very small subset of RST */
		/* TODO: should be more flexible */
		if (prefix(buf, ".. "))
		{
			/* parse ".. menu:: [x] filename.txt" (with exact spacing)*/
			if(prefix(buf+strlen(".. "), "menu:: [") && 
                           buf[strlen(".. menu:: [x")]==']')
			{
				/* This is a menu file */
				menu = TRUE;

				/* Extract the menu item */
				k = A2I(buf[strlen(".. menu:: [")]);

				/* Store the menu item (if valid) */
				if ((k >= 0) && (k < 26))
					my_strcpy(hook[k], buf + strlen(".. menu:: [x] "), sizeof(hook[0]));
			}
			/* parse ".. _some_hyperlink_target:" */
			else if (buf[strlen(".. ")] == '_')
			{
				if (tag)
				{
					/* Remove the closing '>' of the tag */
					buf[strlen(buf) - 1] = '\0';

					/* Compare with the requested tag */
					if (streq(buf + strlen(".. _"), tag))
					{
						/* Remember the tagged line */
						line = next;
					}
				}
			}

			/* Skip this and enter skip mode*/
			skip_lines = TRUE;
			continue;
		}

		/* Count the "real" lines */
		next++;
	}

	/* Save the number of "real" lines */
	size = next;


	/* Display the file */
	while (TRUE)
	{
		/* Clear screen */
		Term_clear();


		/* Restrict the visible range */
		if (line > (size - (hgt - 4))) line = size - (hgt - 4);
		if (line < 0) line = 0;

		skip_lines = FALSE;
		/* Re-open the file if needed */
		if (next > line)
		{
			/* Close it */
			file_close(fff);

			/* Hack -- Re-Open the file */
			fff = file_open(path, MODE_READ, FTYPE_TEXT);
			if (!fff) return (TRUE);

			/* File has been restarted */
			next = 0;
		}


		/* Goto the selected line */
		while (next < line)
		{
			/* Get a line */
			if (!file_getl(fff, buf, sizeof(buf))) break;

			/* Skip lines if we are inside a RST directive*/
			if(skip_lines){
				if(contains_only_spaces(buf))
					skip_lines=FALSE;
				continue;
			}

			/* Skip RST directives */
			if (prefix(buf, ".. "))
			{
				skip_lines=TRUE;
				continue;
			}

			/* Count the lines */
			next++;
		}


		/* Dump the next lines of the file */
		for (i = 0; i < hgt - 4; )
		{
			/* Hack -- track the "first" line */
			if (!i) line = next;

			/* Get a line of the file or stop */
			if (!file_getl(fff, buf, sizeof(buf))) break;

			/* Skip lines if we are inside a RST directive*/
			if(skip_lines){
				if(contains_only_spaces(buf))
					skip_lines=FALSE;
				continue;
			}

			/* Skip RST directives */
			if (prefix(buf, ".. "))
			{
				skip_lines=TRUE;
				continue;
			}

			/* skip | characters */
			strskip(buf,'|');

			/* escape backslashes */
			strescape(buf,'\\');

			/* Count the "real" lines */
			next++;

			/* Make a copy of the current line for searching */
			my_strcpy(lc_buf, buf, sizeof(lc_buf));

			/* Make the line lower case */
			if (!case_sensitive) string_lower(lc_buf);

			/* Hack -- keep searching */
			if (find && !i && !strstr(lc_buf, find)) continue;

			/* Hack -- stop searching */
			find = NULL;

			/* Dump the line */
			Term_putstr(0, i+2, -1, COLOUR_WHITE, buf);

			/* Highlight "shower" */
			if (shower[0])
			{
				const char *str = lc_buf;

				/* Display matches */
				while ((str = strstr(str, shower)) != NULL)
				{
					int len = strlen(shower);

					/* Display the match */
					Term_putstr(str-lc_buf, i+2, len, COLOUR_YELLOW,
								&buf[str-lc_buf]);

					/* Advance */
					str += len;
				}
			}

			/* Count the printed lines */
			i++;
		}

		/* Hack -- failed search */
		if (find)
		{
			bell("Search string not found!");
			line = back;
			find = NULL;
			continue;
		}


		/* Show a general "title" */
		prt(format("[%s, %s, Line %d-%d/%d]", buildid,
		           caption, line, line + hgt - 4, size), 0, 0);


		/* Prompt -- menu screen */
		if (menu)
		{
			/* Wait for it */
			prt("[Press a Letter, or ESC to exit.]", hgt - 1, 0);
		}

		/* Prompt -- small files */
		else if (size <= hgt - 4)
		{
			/* Wait for it */
			prt("[Press ESC to exit.]", hgt - 1, 0);
		}

		/* Prompt -- large files */
		else
		{
			/* Wait for it */
			prt("[Press Space to advance, or ESC to exit.]", hgt - 1, 0);
		}

		/* Get a keypress */
		ch = inkey();

		/* Exit the help */
		if (ch.code == '?') break;

		/* Toggle case sensitive on/off */
		if (ch.code == '!')
		{
			case_sensitive = !case_sensitive;
		}

		/* Try showing */
		if (ch.code == '&')
		{
			/* Get "shower" */
			prt("Show: ", hgt - 1, 0);
			(void)askfor_aux(shower, sizeof(shower), NULL);

			/* Make the "shower" lowercase */
			if (!case_sensitive) string_lower(shower);
		}

		/* Try finding */
		if (ch.code == '/')
		{
			/* Get "finder" */
			prt("Find: ", hgt - 1, 0);
			if (askfor_aux(finder, sizeof(finder), NULL))
			{
				/* Find it */
				find = finder;
				back = line;
				line = line + 1;

				/* Make the "finder" lowercase */
				if (!case_sensitive) string_lower(finder);

				/* Show it */
				my_strcpy(shower, finder, sizeof(shower));
			}
		}

		/* Go to a specific line */
		if (ch.code == '#')
		{
			char tmp[80] = "0";

			prt("Goto Line: ", hgt - 1, 0);
			if (askfor_aux(tmp, sizeof(tmp), NULL))
				line = atoi(tmp);
		}

		/* Go to a specific file */
		if (ch.code == '%')
		{
			char ftmp[80] = "help.hlp";

			prt("Goto File: ", hgt - 1, 0);
			if (askfor_aux(ftmp, sizeof(ftmp), NULL))
			{
				if (!show_file(ftmp, NULL, 0, mode))
					ch.code = ESCAPE;
			}
		}

		switch (ch.code) {
			/* up a line */
			case ARROW_UP:
			case '8': line--; break;

			/* up a page */
			case KC_PGUP:
			case '9':
			case '-': line -= (hgt - 4); break;

			/* home */
			case KC_HOME:
			case '7': line = 0; break;

			/* down a line */
			case ARROW_DOWN:
			case '2':
			case KC_ENTER: line++; break;

			/* down a page */
			case KC_PGDOWN:
			case '3':
			case ' ': line += hgt - 4; break;

			/* end */
			case KC_END:
			case '1': line = size; break;
		}

		/* Recurse on letters */
		if (menu && isalpha((unsigned char)ch.code))
		{
			/* Extract the requested menu item */
			k = A2I(ch.code);

			/* Verify the menu item */
			if ((k >= 0) && (k <= 25) && hook[k][0])
			{
				/* Recurse on that file */
				if (!show_file(hook[k], NULL, 0, mode)) ch.code = ESCAPE;
			}
		}

		/* Exit on escape */
		if (ch.code == ESCAPE) break;
	}

	/* Close the file */
	file_close(fff);

	/* Done */
	return (ch.code != '?');
}
bool
AsciiProcessor::ins_obj( const char * buf,
                         int fref,
                         BuilderBase * build,
                         const char* & next )
{
    // ==========================================================
    if ( strskip( buf, "FRAME", next ) )
    {
        buf = next;
        return ins_frame( buf, fref, build, next );
    }

    // ==========================================================
    else if ( strskip( buf, "CIRCLE", next ) )
    {
        buf = next;
        return ins_circle( buf, fref, build, next );
    }

    // ==========================================================

    else if ( strskip( buf, "LINE", next ) )
    {
        buf = next;
        return ins_line( buf, fref, build, next );
    }

    // ==========================================================

    else if ( strskip( buf, "POINT", next ) )
    {
        buf = next;
        return ins_point( buf, fref, build, next );
    }

    // ==========================================================

    else if ( strskip( buf, "POLYLINE", next ) )
    {
        buf = next;
        return ins_polyline( buf, fref, build, next );
    }

    // ==========================================================

    else if ( strskip( buf, "POLYGON", next ) )
    {
        buf = next;
        return ins_polygon( buf, fref, build, next );
    }

    // ==========================================================

    else if ( strskip( buf, "STRING", next ) )
    {
        buf = next;
        return ins_string( buf, fref, build, next );
    }

    // ==========================================================

    else if ( strskip( buf, "GRID", next ) )
    {
        buf = next;
        return ins_grid( buf, fref, build, next );
    }

    // ==========================================================

    else if ( strskip( buf, "STR_GRID", next ) )
    {
        buf = next;
        return ins_string_grid( buf, fref, build, next );
    }

    else
    {
        return CMD_ERR;
    }

    return CMD_OK;
}