static gdouble read_real_offset(GHashTable *hash, const gchar *key) { gchar *value; gdouble off = 0.0; if ((value = g_hash_table_lookup(hash, key))) { off = g_ascii_strtod(value, NULL); /* Catch NaNs */ if (!(off == off)) { g_warning("%s is NaN, fixing to 0.0", key); off = 0.0; } } return off; }
/** * @private * * Convert a string representation of a numeric value to a double. The * conversion is strict and will fail if the complete string does not represent * a valid double. The function sets errno according to the details of the * failure. This version ignores the locale. * * @param str The string representation to convert. * @param ret Pointer to double where the result of the conversion will be stored. * * @retval SR_OK Conversion successful. * @retval SR_ERR Failure. */ SR_PRIV int sr_atod_ascii(const char *str, double *ret) { double tmp; char *endptr = NULL; errno = 0; tmp = g_ascii_strtod(str, &endptr); if (!endptr || *endptr || errno) { if (!errno) errno = EINVAL; return SR_ERR; } *ret = tmp; return SR_OK; }
static gdouble read_real_size(GHashTable *hash, const gchar *key) { gchar *value; gdouble dim = 1.0; if ((value = g_hash_table_lookup(hash, key))) { dim = g_ascii_strtod(value, NULL); /* Use negated positive conditions to catch NaNs */ if (!((dim = fabs(dim)) > 0)) { g_warning("%s is 0.0 or NaN, fixing to 1.0", key); dim = 1.0; } } return dim; }
/* <frequency>: Frequency values are used with aural properties. The normative definition of frequency values can be found in [CSS2-AURAL]. A frequency value is a <number> immediately followed by a frequency unit identifier. Frequency unit identifiers are: * Hz: Hertz * kHz: kilo Hertz Frequency values may not be negative. */ double rsvg_css_parse_frequency (const char *str) { double f_hz; char *end_ptr; f_hz = g_ascii_strtod (str, &end_ptr); /* todo: error condition - figure out how to best represent it */ if ((f_hz == -HUGE_VAL || f_hz == HUGE_VAL) && (ERANGE == errno)) return 0.0; if (end_ptr && !strcmp (end_ptr, "kHz")) return f_hz * 1000.; return f_hz; }
static int _ping(gchar *dest, gchar *to) { gdouble timeout = g_ascii_strtod(to, NULL); GByteArray *encoded = message_marshall_gba_and_clean ( metautils_message_create_named("REQ_PING")); gint64 start = oio_ext_monotonic_time(); GError *err = gridd_client_exec(dest, timeout, encoded); gint64 end = oio_ext_monotonic_time(); if (err) { g_print("KO (%d) %s\n", err->code, err->message); g_clear_error(&err); return 1; } else { g_print("OK %lfs\n", (end - start) / (gdouble)G_TIME_SPAN_SECOND); return 0; } }
double LoadHandler::getAttribDouble(const char * name) { XOJ_CHECK_TYPE(LoadHandler); const char * attrib = getAttrib(name); if (attrib == NULL) { error(_("Attribute \"%s\" could not be parsed as double, the value is NULL"), name); return 0; } char * ptr = NULL; double val = g_ascii_strtod(attrib, &ptr); if (ptr == attrib) { error(_("Attribute \"%s\" could not be parsed as double, the value is \"%s\""), name, attrib); } return val; }
void test_string (char *number, double res) { gdouble d; char *locales[] = {"sv_SE", "en_US", "fa_IR", "C"}; int l; char *end; for (l = 0; l < G_N_ELEMENTS (locales); l++) { setlocale (LC_NUMERIC, locales[l]); d = g_ascii_strtod (number, &end); if (d != res) g_print ("g_ascii_strtod for locale %s failed\n", locales[l]); if (*end != 0) g_print ("g_ascii_strtod for locale %s endptr was wrong\n", locales[l]); } }
static void x3p_text(G_GNUC_UNUSED GMarkupParseContext *context, const gchar *text, G_GNUC_UNUSED gsize text_len, gpointer user_data, G_GNUC_UNUSED GError **error) { X3PFile *x3pfile = (X3PFile*)user_data; const gchar *semicolon; gchar *path = x3pfile->path->str; gchar *value; if (!strlen(text)) return; /* Data represented directly in XML. */ if (gwy_strequal(path, "/ISO5436_2/Record3/DataList/Datum")) { if (x3pfile->datapos >= x3pfile->ndata) { g_set_error(error, GWY_MODULE_FILE_ERROR, GWY_MODULE_FILE_ERROR_DATA, _("Too many DataList items for given " "matrix dimensions.")); return; } /* There can be a ;-separated list. And it can contain dummy values * for the unused y-axis for profiles (poor design). Since we do not * care about PCL, we only ever want the last value of the list, * whatever it is. */ if ((semicolon = strlenrchr(text, ';', text_len))) text = semicolon + 1; x3pfile->values[x3pfile->datapos] = x3pfile->dz*g_ascii_strtod(text, NULL) + x3pfile->zoff; x3pfile->valid[x3pfile->datapos] = TRUE; gwy_debug("valid Datum %g", x3pfile->values[x3pfile->datapos]); x3pfile->datapos++; x3pfile->seen_datum = TRUE; return; } value = g_strdup(text); g_strstrip(value); g_hash_table_replace(x3pfile->hash, g_strdup(path), value); }
/* reads a textsize entity from the dxf file */ static void read_entity_textsize_dxf(FILE *filedxf, DxfData *data, DiagramData *dia) { if(read_dxf_codes(filedxf, data) == FALSE) return; switch(data->code) { case 40: text_scale = g_ascii_strtod(data->value, NULL); if (text_scale > 0.0) text_scale = 1.0 / text_scale; /*printf( "Text Size: %f\n", text_scale );*/ break; default: break; } }
/** * b_parse_double: * @str: the string to parse * @value: location to store the double value * * Parse a double (floating-point) value from a string. * * Return value: %TRUE if the string could be parsed, %FALSE otherwise **/ gboolean b_parse_double (const gchar *str, gdouble *value) { gchar *end; gdouble d; g_return_val_if_fail (str != NULL, FALSE); d = g_ascii_strtod (str, &end); if (end && *end) return FALSE; if (value) *value = d; return TRUE; }
double S52_atof(CCHAR *str) // safe atof() { if (NULL == str) { PRINTF("WARNING: NULL string\n"); g_assert(0); return 0; } if (0 == strlen(str)) { PRINTF("WARNING: zero length string\n"); g_assert(0); return 0; } //return g_strtod(str, NULL); //return atof(str); return g_ascii_strtod(str, NULL); }
static gboolean polygon_get_value(gchar const **p, gdouble *v) { while (**p != '\0' && (**p == ',' || **p == '\x20' || **p == '\x9' || **p == '\xD' || **p == '\xA')) { (*p)++; } if (**p == '\0') { return false; } gchar *e = NULL; *v = g_ascii_strtod(*p, &e); if (e == *p) { return false; } *p = e; return true; }
void _gpx_parser_text(GMarkupParseContext *context, const gchar *text, gsize text_len, gpointer user_data, GError **error) { dt_gpx_t *gpx = (dt_gpx_t *)user_data; if(!gpx->current_track_point) return; if(gpx->current_parser_element == GPX_PARSER_ELEMENT_TIME) { if(!g_time_val_from_iso8601(text, &gpx->current_track_point->time)) { gpx->invalid_track_point = TRUE; fprintf(stderr, "broken gpx file, failed to pars is8601 time '%s' for trackpoint\n", text); } } else if(gpx->current_parser_element == GPX_PARSER_ELEMENT_ELE) gpx->current_track_point->elevation = g_ascii_strtod(text, NULL); }
static inline void read_temperature (GWeatherInfo *info, xmlNodePtr node) { xmlChar *unit; xmlChar *val; double celsius; unit = xmlGetProp (node, XC("unit")); if (unit == NULL || strcmp ((char*)unit, "celsius")) return; val = xmlGetProp (node, XC("value")); if (val == NULL) return; celsius = g_ascii_strtod ((char*) val, NULL); info->priv->temp = TEMP_C_TO_F (celsius); }
static gboolean parse_scale(gchar **p, const gchar *name, double *value, GwySIUnit **units, GError **error) { gint power10; gchar *vp, *line; line = gwy_str_next_line(p); if (!line) { err_MISSING_FIELD(error, name); return FALSE; } vp = strchr(line, ':'); if (!vp) { g_set_error(error, GWY_MODULE_FILE_ERROR, GWY_MODULE_FILE_ERROR_DATA, _("Missing colon in header line.")); return FALSE; } *vp = '\0'; vp++; gwy_debug("<%s> = <%s>", name, vp); if (!gwy_strequal(line, name)) { err_MISSING_FIELD(error, name); return FALSE; } *value = g_ascii_strtod(vp, &vp); *units = gwy_si_unit_new_parse(vp, &power10); *value *= pow10(power10); if (!*value) { g_warning("%s is 0.0, fixing to 1.0", name); *value = 1.0; } return TRUE; }
static gboolean field_extractor(GError **e, gpointer u, gint code, MESSAGE r) { (void) code; if (!r) { GSETERROR(e, "invalid parameter"); return FALSE; } gchar **fields = metautils_message_get_field_names (r); if (fields) { for (gchar **field=fields; *field ;field++) { gchar *str_val = NULL; gdouble val; if (!g_str_has_prefix(*field, MSGFIELD_STATPREFIX)) continue; if (!(str_val = metautils_message_extract_string_copy(r, *field))) continue; if (strchr(str_val, '.')) val = g_ascii_strtod (str_val, NULL); else { gint64 i64 = g_ascii_strtoll(str_val, NULL, 10); val = i64; } g_free(str_val); if (errno==ERANGE) { WARN("wrong stat for '%s' : overflow/underflow", *field); continue; } g_hash_table_insert(*((GHashTable**)u), g_strdup((*field) + strlen(MSGFIELD_STATPREFIX)), g_memdup(&val, sizeof(val))); } } g_strfreev(fields); return TRUE; }
/** * im_read_dmask: * @filename: read matrix from this file * * Reads a matrix from a file. * * Matrix files have a simple format that's supposed to be easy to create with * a text editor or a spreadsheet. * * The first line has four numbers for width, height, scale and * offset (scale and offset may be omitted, in which case they default to 1.0 * and 0.0). Scale must be non-zero. Width and height must be positive * integers. The numbers are separated by any mixture of spaces, commas, * tabs and quotation marks ("). The scale and offset fields may be * floating-point, and must use '.' * as a decimal separator. * * Subsequent lines each hold one line of matrix data, with numbers again * separated by any mixture of spaces, commas, * tabs and quotation marks ("). The numbers may be floating-point, and must * use '.' * as a decimal separator. * * Extra characters at the ends of lines or at the end of the file are * ignored. * * See also: im_read_imask(), im_gauss_dmask(). * * Returns: the loaded mask on success, or NULL on error. */ DOUBLEMASK * im_read_dmask( const char *filename ) { FILE *fp; double sc, off; int xs, ys; DOUBLEMASK *out; int x, y, i; char buf[MAX_LINE]; if( !(fp = im__file_open_read( filename, NULL, TRUE )) ) return( NULL ); if( read_header( fp, &xs, &ys, &sc, &off ) ) { fclose( fp ); return( NULL ); } if( !(out = im_create_dmask( filename, xs, ys )) ) { fclose( fp ); return( NULL ); } out->scale = sc; out->offset = off; for( i = 0, y = 0; y < ys; y++ ) { char *p; if( get_line( fp, buf ) ) { im_free_dmask( out ); fclose( fp ); return( NULL ); } for( p = buf, x = 0; p && x < xs; x++, i++, p = im_break_token( p, " \t,\";" ) ) out->coeff[i] = g_ascii_strtod( p, NULL ); } fclose( fp ); return( out ); }
static inline void read_pressure (GWeatherInfo *info, xmlNodePtr node) { xmlChar *unit; xmlChar *val; double hpa; /* hPa == mbar */ unit = xmlGetProp (node, XC("unit")); if (unit == NULL || strcmp ((char*)unit, "hPa")) return; val = xmlGetProp (node, XC("value")); if (val == NULL) return; hpa = g_ascii_strtod ((char*) val, NULL); info->priv->pressure = PRESSURE_MBAR_TO_INCH (hpa); }
/* reads a scale entity from the dxf file */ static void read_entity_scale_dxf(FILE *filedxf, DxfData *data, DiagramData *dia) { if(read_dxf_codes(filedxf, data) == FALSE) return; switch(data->code) { case 40: coord_scale = g_ascii_strtod(data->value, NULL); if (coord_scale > 0.0) coord_scale = 1.0 / coord_scale; g_message("Scale: %f", coord_scale ); break; default: break; } }
static inline void read_humidity (GWeatherInfo *info, xmlNodePtr node) { xmlChar *unit; xmlChar *val; double percent; unit = xmlGetProp (node, XC("unit")); if (unit == NULL || strcmp ((char*)unit, "%")) return; val = xmlGetProp (node, XC("value")); if (val == NULL) return; percent = g_ascii_strtod ((char*) val, NULL); info->priv->humidity = percent; info->priv->hasHumidity = TRUE; }
static char* find_path_by_uuid(const char* uuid) { g_assert(uuid != NULL); if (g_str_has_prefix (uuid, "disk")) { return installer_get_disk_path (uuid); } else if (g_str_has_prefix (uuid, "part")) { return installer_get_partition_path (uuid); } else if (g_str_has_prefix (uuid, "free")) { char** sets = g_strsplit(uuid, ":", 3); g_assert(g_strv_length(sets) == 3); char* path = find_partition_path_by_sector_and_disk_path(sets[1], g_ascii_strtod(sets[2], NULL)); g_debug("----------------------try find free partition's path: %s:%d = %s\n", sets[1], (int)g_ascii_strtod(sets[2], NULL), path); g_strfreev(sets); return path; } else { return g_strdup(uuid); } }
static gboolean handle_statoverride_line (const char *line, void *data, GError **error) { GHashTable *files = data; const char *spc; guint mode_add; spc = strchr (line, ' '); if (spc == NULL) { return glnx_throw (error, "Malformed statoverride file (no space found)"); } mode_add = (guint32)(gint32)g_ascii_strtod (line, NULL); g_hash_table_insert (files, g_strdup (spc + 1), GUINT_TO_POINTER((gint32)mode_add)); return TRUE; }
double sp_svg_read_percentage(char const *str, double def) { if (str == NULL) { return def; } char *u; double v = g_ascii_strtod(str, &u); while (isspace(*u)) { if (*u == '\0') { return v; } u++; } if (*u == '%') { v /= 100.0; } return v; }
/* scroll a bar in a given direction */ void scroll(GtkAdjustment* bar, gchar *amount_str) { gchar *end; gdouble max_value; gdouble page_size = gtk_adjustment_get_page_size(bar); gdouble value = gtk_adjustment_get_value(bar); gdouble amount = g_ascii_strtod(amount_str, &end); if (*end == '%') value += page_size * amount * 0.01; else value += amount; max_value = gtk_adjustment_get_upper(bar) - page_size; if (value > max_value) value = max_value; /* don't scroll past the end of the page */ gtk_adjustment_set_value (bar, value); }
static void arv_gc_register_node_set_value_from_string (ArvGcFeatureNode *node, const char *string, GError **error) { ArvGcRegisterNode *gc_register_node = ARV_GC_REGISTER_NODE (node); switch (gc_register_node->value_type) { case G_TYPE_INT64: arv_gc_integer_set_value (ARV_GC_INTEGER (node), g_ascii_strtoll (string, NULL, 0), error); break; case G_TYPE_DOUBLE: arv_gc_float_set_value (ARV_GC_FLOAT (node), g_ascii_strtod (string, NULL), error); break; case G_TYPE_STRING: arv_gc_string_set_value (ARV_GC_STRING (node), string, error); break; default: break; } arv_warning_genicam ("[GcRegisterNode::set_value_from_string] Invalid value type"); }
/* <angle>: An angle value is a <number> optionally followed immediately with an angle unit identifier. Angle unit identifiers are: * deg: degrees * grad: grads * rad: radians For properties defined in [CSS2], an angle unit identifier must be provided. For SVG-specific attributes and properties, the angle unit identifier is optional. If not provided, the angle value is assumed to be in degrees. */ double rsvg_css_parse_angle (const char *str) { double degrees; char *end_ptr; degrees = g_ascii_strtod (str, &end_ptr); /* todo: error condition - figure out how to best represent it */ if ((degrees == -HUGE_VAL || degrees == HUGE_VAL) && (ERANGE == errno)) return 0.0; if (end_ptr) { if (!strcmp (end_ptr, "rad")) return degrees * 180. / G_PI; else if (!strcmp (end_ptr, "grad")) return degrees * 360. / 400.; } return degrees; }
void gnomegadu_gadugadu_connect (GnomeGaduProtocolStatus status) { gchar *profile = NULL; gchar *uin = NULL; gchar *password = NULL; struct gg_login_params p; if (gnomegadu_gadugadu_session) { g_printerr ("gnomegadu_gadugadu_connect(): trying connect while session is not disconnected\n"); return; } gnomegadu_userlist_cleanup_model_status (); //just in case profile = gnomegadu_conf_get_profile (); g_print("Connect profile: %s\n",profile); uin = gnomegadu_conf_get_account_uin (profile); password = gnomegadu_conf_get_account_password (profile); if (!password || !uin) return; memset (&p, 0, sizeof (p)); p.uin = g_ascii_strtod (uin, NULL); p.password = g_strdup (password); p.async = 1; p.status = gnomegadu_gadugadu_to_protocol_status (status,FALSE); gnomegadu_gadugadu_session = gg_login (&p); gnomegadu_source_chan = g_io_channel_unix_new (gnomegadu_gadugadu_session->fd); gnomegadu_watch_protocol = g_io_add_watch (gnomegadu_source_chan, G_IO_IN | G_IO_ERR | G_IO_HUP, gnomegadu_protocol_loop, NULL); g_free (profile); g_free (uin); g_free (password); }
/** * The function called by the parser when an opening tag is read. * The state variable is changed in this function and * sometimes memory allocated for the information that's going to be read. * @see The GLib manual (Simple XML parser). */ void xml_lg_commentary_read_start_element (GMarkupParseContext *context, const gchar *element_name, const gchar **attribute_names, const gchar **attribute_values, gpointer user_data, GError **error) { #ifdef DEBUG printf("xml_lg_commentary_read_start_element\n"); #endif gint atidx = 0; if(strcmp(element_name, TAG_EVENT) == 0) state = STATE_EVENT; else if(strcmp(element_name, TAG_EVENT_NAME) == 0) state = STATE_EVENT_NAME; else if(strcmp(element_name, TAG_EVENT_COMMENTARY) == 0) { state = STATE_EVENT_COMMENTARY; condition = NULL; priority = 1; while(attribute_names[atidx] != NULL) { if(strcmp(attribute_names[atidx], ATT_NAME_CONDITION) == 0) condition = g_strdup(attribute_values[atidx]); else if(strcmp(attribute_names[atidx], ATT_NAME_PRIORITY) == 0) priority = (gint)g_ascii_strtod(attribute_values[atidx], NULL); atidx++; } } else if(strcmp(element_name, TAG_LG_COMMENTARY) != 0) debug_print_message("xml_lg_commentary_read_start_element: unknown tag: %s; I'm in state %d\n", element_name, state); }
static gint unisoku_sscanf(const gchar *str, const gchar *format, ...) { va_list ap; gchar *endptr; gint *pi; gdouble *pd; gint count = 0; va_start(ap, format); while (*format) { switch (*format++) { case 'i': pi = va_arg(ap, gint*); g_assert(pi); *pi = strtol(str, &endptr, 10); break; case 'd': pd = va_arg(ap, gdouble*); g_assert(pd); *pd = g_ascii_strtod(str, &endptr); break; default: g_return_val_if_reached(0); break; } if ((gchar*)str == endptr) break; count++; str = endptr; } va_end(ap); return count; }
int S52_atoi(CCHAR *str) // safe atoi() // use for parsing the PLib and S57 attribute { if (NULL == str) { PRINTF("WARNING: NULL string\n"); g_assert(0); return 0; } if (0 == strlen(str)) { PRINTF("WARNING: zero length string\n"); g_assert(0); return 0; } // the to (int) might not be such a great idea! (no rounding) //return (int)S52_atof(str); //return (int)g_strtod(str, NULL); return (int)g_ascii_strtod(str, NULL); //return atoi(str); }