Tuesday, October 27, 2015

XML or .osm special character issue and solution in C


You probably have experience while parsing openstreetmap .osm file - some name [TAG-NAME] appear as

C&C - which is in clear text "C&C". So while you are coding for a .osm or XML parser, it is important to consider such special character at XML specification.

If it need to present such value at literal form, it is important to replace those characters with some other clear text format.

For example, if it need to replace the above "&" a below C program can make an idea.

 char *special_ch_replace   
 (char *s, char ser, char rep)   
 {  
  char *p1;  
  for (p1=s; *p1; p1++)   
  {  
   if (*p1 == ser)  
      {  
    *p1 = rep;  
   }  
  }  
  return s;  
 }  

Saturday, October 3, 2015

C program to generate .osm[XML] with efficient memory management


It was a requirement to generate new .osm file with selectively extracted openstreetmap's data from a big file [e.g. a country / state .osm file].

Equally the requirement extended as no use of any pre-programmed library, only simple C programming and very importantly, the program should avoid -

1. memory leaking
2. buffer overflow
3. change of pointer address
4. extra memory allocation, and
5. efficient.

End output has to be something like this -

     <node id="73900462" lat="54.3884" lon="10.38285">  
         <tag k="name" v="Lidl"/>  
         <tag k="shop" v="supermarket"/>  
         <tag k="addr:city" v="Schönberg"/>  
         <tag k="addr:street" v="Große Mühlenstraße"/>  
         <tag k="addr:housenumber" v="51"/>  
     </node>  

The output will write to a file. Where, node_id, lat, lon, tag_value/s etc. will be dynamic.