static int unit_find(const char *str, bUnitDef *unit) { if (unit_find_str(str, unit->name_short)) return 1; if (unit_find_str(str, unit->name_plural)) return 1; if (unit_find_str(str, unit->name_alt)) return 1; if (unit_find_str(str, unit->name)) return 1; return 0; }
static bool unit_find(const char *str, bUnitDef *unit) { if (unit_find_str(str, unit->name_short)) return true; if (unit_find_str(str, unit->name_plural)) return true; if (unit_find_str(str, unit->name_alt)) return true; if (unit_find_str(str, unit->name)) return true; return false; }
static int unit_scale_str(char *str, int len_max, char *str_tmp, double scale_pref, bUnitDef *unit, const char *replace_str) { char *str_found; if ((len_max > 0) && (str_found = (char *)unit_find_str(str, replace_str))) { /* XXX - investigate, does not respect len_max properly */ int len, len_num, len_name, len_move, found_ofs; found_ofs = (int)(str_found - str); len = strlen(str); len_name = strlen(replace_str); len_move = (len - (found_ofs + len_name)) + 1; /* 1+ to copy the string terminator */ len_num = BLI_snprintf(str_tmp, TEMP_STR_SIZE, "*%g"SEP_STR, unit->scalar / scale_pref); /* # removed later */ if (len_num > len_max) len_num = len_max; if (found_ofs + len_num + len_move > len_max) { /* can't move the whole string, move just as much as will fit */ len_move -= (found_ofs + len_num + len_move) - len_max; } if (len_move > 0) { /* resize the last part of the string */ memmove(str_found + len_num, str_found + len_name, len_move); /* may grow or shrink the string */ } if (found_ofs + len_num > len_max) { /* not even the number will fit into the string, only copy part of it */ len_num -= (found_ofs + len_num) - len_max; } if (len_num > 0) { /* its possible none of the number could be copied in */ memcpy(str_found, str_tmp, len_num); /* without the string terminator */ } /* since the null terminator wont be moved if the stringlen_max * was not long enough to fit everything in it */ str[len_max - 1] = '\0'; return found_ofs + len_num; } return 0; }
/* 45µm --> 45um */ void bUnit_ToUnitAltName(char *str, int len_max, const char *orig_str, int system, int type) { bUnitCollection *usys = unit_get_system(system, type); bUnitDef *unit; bUnitDef *unit_def = unit_default(usys); /* find and substitute all units */ for (unit = usys->units; unit->name; unit++) { if (len_max > 0 && (unit->name_alt || unit == unit_def)) { const char *found = unit_find_str(orig_str, unit->name_short); if (found) { int offset = (int)(found - orig_str); int len_name = 0; /* copy everything before the unit */ offset = (offset < len_max ? offset : len_max); strncpy(str, orig_str, offset); str += offset; orig_str += offset + strlen(unit->name_short); len_max -= offset; /* print the alt_name */ if (unit->name_alt) len_name = BLI_strncpy_rlen(str, unit->name_alt, len_max); else len_name = 0; len_name = (len_name < len_max ? len_name : len_max); str += len_name; len_max -= len_name; } } } /* finally copy the rest of the string */ strncpy(str, orig_str, len_max); }