(root)/
libredwg-0.13/
test/
xmlsuite/
common.c
       1  // This file contains the helper function which are used in the XML Suite
       2  // to prepare the result
       3  
       4  xmlChar *spointprepare (double x, double y, double z);
       5  xmlChar *doubletochar (double x);
       6  xmlChar *inttochar (int x);
       7  xmlChar *spointprepare2 (double x, double y);
       8  xmlChar *doubletohex (double handle);
       9  
      10  /*
      11   * This functions contverts 3D Point to the following format
      12   *
      13   *  Format: (x y z)
      14   *
      15   *  To emit this in the XML file
      16   *
      17   *  @param double x The X coordinate
      18   *  @param double y The Y coordinate
      19   *  @param double z The Z coordinate
      20   *
      21   *  @return xmlChar* Converted string in the specified pattern
      22   */
      23  xmlChar *
      24  spointprepare (double x, double y, double z)
      25  {
      26    xmlChar *result;
      27    int n;
      28  
      29    // Allocating memory for the string
      30    result = malloc (100 * sizeof (xmlChar));
      31    n = sprintf ((char *)result, "(%.4f %.4f %.4f)", x, y, z);
      32  
      33    // Check if it was transferred properly
      34    if (n < 0)
      35      {
      36        strcpy ((char *)result, ""); /* return empty string on error */
      37      }
      38  
      39    return result;
      40  }
      41  
      42  /*
      43   * This functions converts double format in char to be emitted in the XML
      44   * @param double x The double digis to be converted in string
      45   *
      46   * @return char* Return the converted double in string. Return empty string if
      47   * error
      48   */
      49  xmlChar *
      50  doubletochar (double x)
      51  {
      52    xmlChar *result;
      53    int n;
      54  
      55    /* Allocate memory for the string */
      56    result = malloc (50 * sizeof (xmlChar));
      57    n = sprintf ((char *)result, "%.4f", x);
      58  
      59    if (n < 0)
      60      {
      61        strcpy ((char *)result, ""); /* Return empty string on error */
      62      }
      63  
      64    return result;
      65  }
      66  
      67  /*
      68   * This functions converts int format in char to be emitted in the XML
      69   *
      70   * @return char* Return the converted double in string. Return empty string if
      71   * error
      72   */
      73  xmlChar *
      74  inttochar (int x)
      75  {
      76    xmlChar *result;
      77    int n;
      78  
      79    /* Allocate memory for the string */
      80    result = malloc (16 * sizeof (xmlChar));
      81    n = sprintf ((char *)result, "%d", x);
      82    if (n < 0)
      83      {
      84        strcpy ((char *)result, ""); /* Return empty string on error */
      85      }
      86  
      87    return result;
      88  }
      89  
      90  /*
      91   * This function converts 2D point in the following format so as to be used
      92   * in the XML
      93   *
      94   * Format: (x y)
      95   *
      96   * @param double x The x coordinate of the point
      97   * @param double y The y coordinate of the point
      98   *
      99   * @return xmlChar* Returns the string in the given format. Return empty string
     100   * on error
     101   */
     102  xmlChar *
     103  spointprepare2 (double x, double y)
     104  {
     105    xmlChar *result;
     106    int n;
     107  
     108    /* allocate memory for the string */
     109    result = malloc (50 * sizeof (xmlChar));
     110    n = sprintf ((char *)result, "(%.4f %.4f)", x, y);
     111  
     112    // Check if it was transferred properly
     113    if (n < 0)
     114      {
     115        strcpy ((char *)result, ""); /* return empty string on error */
     116      }
     117  
     118    return result;
     119  }
     120  
     121  /*
     122   * This function converts double in hex to be used in the XML
     123   * @param double handle The handle value which is to be converted in Hex
     124   *
     125   * @return char* The converted hex in string
     126   */
     127  xmlChar *
     128  doubletohex (double handle)
     129  {
     130    xmlChar *result;
     131    int n;
     132  
     133    /* allocate memory for the string */
     134    result = malloc (20 * sizeof (xmlChar));
     135    n = sprintf ((char *)result, "%03X", (unsigned int)handle);
     136  
     137    // Check if it was transferred properly
     138  
     139    if (n < 0)
     140      {
     141        strcpy ((char *)result, ""); /* return empty string on error */
     142      }
     143  
     144    return result;
     145  }