Branch: Tag:

2014-08-16

2014-08-16 16:14:35 by Martin Nilsson <nilsson@opera.com>

Use variant to support multiple constructors. Serialize correctly. Added more protected. Removed some 8bit strings.

26:   //! is up. Zero is the shell of the current ellipsoid.   float alt;    - //! @decl void create(float lat, float long, void|float alt) + //! @decl void create(int|float lat, int|float long, void|int|float alt)   //! @decl void create(string lat, string long) - //! @decl void create(string both) + //! @decl void create(string position) + //! @decl void create()   //!   //! Constructor for this class. If fed with strings,   //! it will perform a dwim scan on the strings. If they   //! fails to be understood, there will be an exception.   //! - void create(void|int|float|string _lat, -  void|int|float|string _long, void|float _alt) +  + protected void create()   { -  if(undefinedp(_lat)) return; -  if (stringp(_lat)) +  create(0, 0); + } +  + protected variant void create(string pos)   { -  if (undefinedp(_long)) -  { -  string tmp; -  if (sscanf(_lat,"%sN %s",tmp,_long)==2) _lat=tmp+"N"; -  else if (sscanf(_lat,"%sS %s",tmp,_long)==2) _lat=tmp+"S"; -  else if (sscanf(_lat,"%sW %s",tmp,_lat)==2) _long=tmp+"W"; -  else if (sscanf(_lat,"%sE %s",tmp,_lat)==2) _long=tmp+"N"; -  else if (sscanf(_lat,"%s %s",tmp,_long)==2) _lat=tmp; +  string tmp, lat, long; +  if (sscanf(pos, "%sN %s", tmp, long)==2) lat=tmp+"N"; +  else if (sscanf(pos, "%sS %s", tmp, long)==2) lat=tmp+"S"; +  else if (sscanf(pos, "%sW %s", tmp, lat)==2) long=tmp+"W"; +  else if (sscanf(pos, "%sE %s", tmp, lat)==2) long=tmp+"N"; +  else if (sscanf(pos, "%s %s", tmp, long)==2) lat=tmp; +  create(lat, long);   } -  _lat=dwim(_lat,"NS"); -  if (stringp(_lat)) -  error("Failed to understand latitude %O\n",lat); -  } -  if (stringp(_long)) +  + protected variant void create(string lat, string long)   { -  _long=dwim(_long,"EW"); -  if (stringp(_long)) -  error("Failed to understand longitude %O\n",long); +  create(dwim(lat,"NS"), dwim(long,"EW"));   } -  lat=(float)_lat; -  long=(float)_long; -  alt=(float)_alt; +  + protected variant void create(int|float lat, int|float long, +  void|int|float alt) + { +  this_program::lat=(float)lat; +  this_program::long=(float)long; +  this_program::alt=(float)alt;    set_ellipsoid("WGS 84");   }    - private float|string dwim(string what,string direction) + private float dwim(string what,string direction)   {    float d,m,s;    string dir=0;    int neg=0; - #define DIV "%*[ \t\r\n'`°\":.]" + #define DIV "%*[ \t\r\n'`\260\":.]"       if (sscanf(what,"-%s",what)) neg=1;   
95:    {    case -1: return sprintf("%.5g",what);    case 1: -  return sprintf("%.3f°%s",what,directions); +  return sprintf("%.3f\260%s",what,directions);    case 3: -  return sprintf("%%d'%.1f\"%s", +  return sprintf("%d\260%d'%.1f\"%s",    (int)floor(what),(int)floor(60*(what-floor(what))),    3600*(what-floor(60*what)/60),    directions);    default: -  return sprintf("%%.3f'%s", +  return sprintf("%d\260%.3f'%s",    (int)floor(what),60*(what-floor(what)),    directions);    }
567:      // --- "Technical" methods --------------    - string|array cast(string to) + protected string|array cast(string to)   {    if (to[..4]=="array")    return ({lat,long});
579:   }      //! - int __hash() + protected int __hash()   {    return (int)(lat*3600000+long*3600000);   }      //! - int `==(object pos) + protected int `==(object pos)   {    return (objectp(pos) && pos->lat==lat && pos->long==long);   }      //! - int `<(object pos) + protected int `<(object pos)   {    if (pos->lat>lat) return 1;    else if (pos->lat==lat && pos->long>long) return 1;
599:   }      //! - int `>(object pos) + protected int `>(object pos)   {    if (pos->lat<lat) return 1;    else if (pos->lat==lat && pos->long<long) return 1;
607:   }      //! - string _sprintf(int|void t) + protected string _sprintf(int|void t)   {    return t=='O' && sprintf("%O(%s, %s)", this_program,    latitude(), longitude());
623:   }      // encoder - array(float) _encode() { return ({lat,long,alt}); } - void _decode(array(float) v) { create(@v); } + array(float) _encode() + { +  return ({ lat, long, alt, polar_radius, equatorial_radius }); + } + void _decode(array(float) v) { +  create(@v[..2]); +  if( sizeof(v)==5 ) +  { +  polar_radius = v[3]; +  equatorial_radius = v[4]; +  } + }