示例#1
0
/* ****************************************************************************
*
* string2coords - 
*/
bool string2coords(std::string s, double& latitude, double& longitude)
{
  char* initial = strdup(s.c_str());
  char* cP      = initial;
  char* comma;
  char* number1;
  char* number2;

  cP = wsStrip(cP);

  comma = strchr(cP, ',');
  if (comma == NULL)
  {
    free(initial);
    return false;
  }
  *comma = 0;
  ++comma;

  number1 = cP;
  number2 = comma;

  number1 = wsStrip(number1);
  number2 = wsStrip(number2);

  std::string err;
  double oldLatitude = latitude;
  double oldLongitude = longitude;
  latitude = atoF(number1, err);
  if (err.length() > 0) {
     latitude = oldLatitude; 
     free(initial);
     return false;
  }
  else {
     longitude = atoF(number2, err);
     if (err.length() > 0) {
         /* Rollback latitude */
         latitude = oldLatitude;
         longitude = oldLongitude;
         free(initial);
         return false;
     }
  }

  free(initial);
  return true;
}
示例#2
0
文件: atof.c 项目: prakhar4/c_stuff
int main()
{
	//int atoI(char []);		//prototypes required if written above all func. 
	//double atoF(char []);
	char a[100];
	
	printf("\nEnter the a string with float values ");
	scanf("%[^\n]s",a);

	printf("\nthe final doulbe is %f \n",atoF(a));
	
	printf("\nthe integer equivalent is %d\n",atoI(a));
	return 0;
	
}
示例#3
0
/* ****************************************************************************
*
* string2coords - 
*/
bool string2coords(const std::string& s, double& latitude, double& longitude)
{
  char*  initial = strdup(s.c_str());
  char*  cP      = initial;
  char*  comma;
  char*  number1;
  char*  number2;
  bool   ret = true;

  cP    = wsStrip(cP);

  comma = strchr(cP, ',');
  if (comma == NULL)
  {
    free(initial);
    return false;
  }
  *comma = 0;
  ++comma;

  number1 = cP;
  number2 = comma;

  number1 = wsStrip(number1);
  number2 = wsStrip(number2);

  std::string  err;
  double       oldLatitude  = latitude;
  double       oldLongitude = longitude;

  latitude                  = atoF(number1, &err);

  if (err.length() > 0)
  {
    latitude = oldLatitude;
    LM_W(("Bad Input (bad latitude value in coordinate string '%s')", initial));
    ret = false;
  }
  else
  {
    longitude = atoF(number2, &err);

    if (err.length() > 0)
    {
      /* Rollback latitude */
      latitude = oldLatitude;
      longitude = oldLongitude;
      LM_W(("Bad Input (bad longitude value in coordinate string '%s')", initial));
      ret = false;
    }
  }

  if ((latitude > 90) || (latitude < -90))
  {
    LM_W(("Bad Input (bad value for latitude '%s')", initial));
    ret = false;
  }
  else if ((longitude > 180) || (longitude < -180))
  {
    LM_W(("Bad Input (bad value for longitude '%s')", initial));
    ret = false;
  }

  free(initial);
  return ret;
}
示例#4
0
/* ****************************************************************************
*
* atoF - 
*/
TEST(string, atoF)
{
  std::string  e;
  double       d;

  d = atoF("", &e);
  EXPECT_EQ(0.0, d);
  EXPECT_EQ("empty string", e);
  
  d = atoF(" ", &e);
  EXPECT_EQ(0.0, d);
  EXPECT_EQ("invalid characters in string to convert", e);

  d = atoF("34", &e);
  EXPECT_EQ(34, d);
  EXPECT_EQ("", e);

  d = atoF("-34.0", &e);
  EXPECT_EQ(-34.0, d);
  EXPECT_EQ("", e);

  d = atoF("+34.1", &e);
  EXPECT_EQ(34.1, d);
  EXPECT_EQ("", e);

  d = atoF(".34", &e);
  EXPECT_EQ(.34, d);
  EXPECT_EQ("", e);

  d = atoF("--4", &e);
  EXPECT_EQ(0.0, d);
  EXPECT_EQ("non-digit after unary minus/plus", e);

  d = atoF("+-4", &e);
  EXPECT_EQ(0.0, d);
  EXPECT_EQ("non-digit after unary minus/plus", e);

  d = atoF("-.4", &e);
  EXPECT_EQ(-0.4, d);
  EXPECT_EQ("", e);

  d = atoF(".34.0", &e);
  EXPECT_EQ(0.0, d);
  EXPECT_EQ("more than one dot", e);

  d = atoF("34.", &e);
  EXPECT_EQ(0.0, d);
  EXPECT_EQ("last character in a double cannot be a dot", e);

  d = atoF("x34", &e);
  EXPECT_EQ(0.0, d);
  EXPECT_EQ("invalid characters in string to convert", e);

  d = atoF("--224", &e);
  EXPECT_EQ(0.0, d);
  EXPECT_EQ("non-digit after unary minus/plus", e);

  d = atoF("-.224", &e);
  EXPECT_EQ(-0.224, d);
  EXPECT_EQ("", e);

  d = atoF("2-24", &e);
  EXPECT_EQ(0.0, d);
  EXPECT_EQ("invalid characters in string to convert", e);

  d = atoF("224-", &e);
  EXPECT_EQ(0.0, d);
  EXPECT_EQ("invalid characters in string to convert", e);

  d = atoF("224.-", &e);
  EXPECT_EQ(0.0, d);
  EXPECT_EQ("invalid characters in string to convert", e);

  d = atoF("224.", &e);
  EXPECT_EQ(0.0, d);
  EXPECT_EQ("last character in a double cannot be a dot", e);

  d = atoF("2.2.4", &e);
  EXPECT_EQ(0.0, d);
  EXPECT_EQ("more than one dot", e);

  d = atoF("2 24", &e);
  EXPECT_EQ(0.0, d);
  EXPECT_EQ("invalid characters in string to convert", e);
}