Changeset bd2e33a in git


Ignore:
Timestamp:
27/01/14 10:26:59 (6 years ago)
Author:
Olly Betts <olly@…>
Branches:
line_contents, master, stereo, travis-osx
Children:
e6312c0
Parents:
ad3017d
git-author:
Olly Betts <olly@…> (27/01/14 10:26:46)
git-committer:
Olly Betts <olly@…> (27/01/14 10:26:59)
Message:

src/: Make cad3d remap control characters and spaces in station
names when generating PLT output in the same way aven does.

Files:
5 edited

Legend:

Unmodified
Added
Removed
  • ChangeLog

    rad3017d rbd2e33a  
     1Mon Jan 27 10:25:02 GMT 2014  Olly Betts <olly@survex.com>
     2
     3        * src/: Make cad3d remap control characters and spaces in station
     4          names when generating PLT output in the same way aven does.
     5
    16Mon Jan 27 07:43:27 GMT 2014  Olly Betts <olly@survex.com>
    27
  • src/Makefile.am

    rad3017d rbd2e33a  
    9797 $(COMMONSRC)
    9898
    99 cad3d_SOURCES = cad3d.c useful.c img_hosted.c hash.c \
     99cad3d_SOURCES = cad3d.c useful.c img_hosted.c hash.c str.c \
    100100 $(COMMONSRC)
    101101
  • src/cad3d.c

    rad3017d rbd2e33a  
    4242#include "img_hosted.h"
    4343#include "message.h"
     44#include "str.h"
    4445#include "useful.h"
    4546
     
    345346
    346347static void
    347 set_name(const img_point *p, const char *s)
     348set_name_(const img_point *p, const char *s, int copy)
    348349{
    349350   int hash;
     
    362363         /* already got name for these coordinates */
    363364         /* FIXME: what about multiple names for the same station? */
     365         if (!copy)
     366            osfree((char*)s);
    364367         return;
    365368      }
     
    367370
    368371   pt = osnew(point);
    369    pt->label = osstrdup(s);
     372   if (copy)
     373      s = osstrdup(s);
     374   pt->label = s;
    370375   pt->p = *p;
    371376   pt->next = htab[hash];
    372377   htab[hash] = pt;
    373 
    374    return;
     378}
     379
     380static void
     381set_name(const img_point *p, const char *s)
     382{
     383   set_name_(p, s, 0);
     384}
     385
     386static void
     387set_name_copy(const img_point *p, const char *s)
     388{
     389   set_name_(p, s, 1);
    375390}
    376391
     
    466481   fputs(s, fh);
    467482   fputs("</text>\n", fh);
    468    set_name(p, s);
     483   set_name_copy(p, s);
    469484}
    470485
     
    541556plt_label(const img_point *p, const char *s, bool fSurface)
    542557{
     558   const char * q;
     559   char * escaped = NULL;
     560   int elen;
    543561   (void)fSurface; /* unused */
    544    /* FIXME: also ctrl characters - ought to remap them, not give up */
    545    if (strchr(s, ' ')) {
    546       fprintf(stderr, "PLT format can't cope with spaces in station names\n");
    547       exit(1);
    548    }
    549    set_name(p, s);
     562   /* PLT format can't handle spaces or control characters, so escape them
     563    * like in URLs (an arbitrary choice of escaping, but at least a familiar
     564    * one and % isn't likely to occur in station names).
     565    */
     566   for (q = s; *q; ++q) {
     567      unsigned char ch = *q;
     568      if (ch <= ' ' || ch == '%') {
     569         s_cat_len(&escaped, &elen, s, q - s);
     570         s_catchar(&escaped, &elen, '%');
     571         s_catchar(&escaped, &elen, "0123456789abcdef"[ch >> 4]);
     572         s_catchar(&escaped, &elen, "0123456789abcdef"[ch & 0x0f]);
     573         s = q + 1;
     574      }
     575   }
     576   if (escaped) {
     577      s_cat_len(&escaped, &elen, s, q - s);
     578      set_name(p, escaped);
     579      return;
     580   }
     581   set_name_copy(p, s);
    550582}
    551583
  • src/str.c

    rad3017d rbd2e33a  
    11/* append a string */
    2 /* Copyright (c) Olly Betts 1999
     2/* Copyright (c) Olly Betts 1999, 2014
    33 *
    44 * This program is free software; you can redistribute it and/or modify
     
    2626s_cat(char **pstr, int *plen, char *s)
    2727{
    28    int new_len = strlen(s) + 1; /* extra 1 for nul */
     28   s_cat_len(pstr, plen, s, strlen(s));
     29}
     30
     31/* append a string with length */
     32void
     33s_cat_len(char **pstr, int *plen, char *s, int s_len)
     34{
     35   int new_len = s_len + 1; /* extra 1 for nul */
    2936   int len = 0;
    3037
     
    3946   }
    4047
    41    strcpy(*pstr + len, s);
     48   memcpy(*pstr + len, s, s_len);
     49   (*pstr + len)[s_len] = '\0';
    4250}
    4351
  • src/str.h

    rad3017d rbd2e33a  
    11/* append a string */
    2 /* Copyright (c) Olly Betts 1999, 2001, 2012
     2/* Copyright (c) Olly Betts 1999, 2001, 2012, 2014
    33 *
    44 * This program is free software; you can redistribute it and/or modify
     
    2121void s_cat(char **pstr, int *plen, char *s);
    2222
     23void s_cat_len(char **pstr, int *plen, char *s, int s_len);
     24
    2325/* append a character */
    2426void s_catchar(char **pstr, int *plen, char /*ch*/);
Note: See TracChangeset for help on using the changeset viewer.