static void test_util_num_to_str() { char str[100]; // Check NaN and Inf are correctly written TASSERT2(strcmp(num_to_str(NAN, 2, str),"NaN") == 0, "Got: %s", str); TASSERT2(strcmp(num_to_str(INFINITY, 2, str),"Inf") == 0, "Got: %s", str); }
void CServer::host_stat_request(const char *id_string) { int host_id; if (sscanf(id_string, "%d", &host_id) != 1) { throw std::runtime_error(std::string("Cannot extract host id from host request: ") + id_string); } const CHostData *host = m_host_set.find_host(host_id); if (host == 0) { throw std::runtime_error(std::string("Cannot write stats about unknown host ") + num_to_str(host_id)); } long long unsigned int res_size[RES_TYPE_MAX]; const CFileSet& fs = host->fileset(); fs.res_type_stat(res_size); char buf[200]; char *ptr; ptr = buf; for (int i = 0; i < RES_TYPE_MAX; i++) { ptr += sprintf(ptr, "%llu\t", res_size[i]); } // cut trailing \t ptr--; *ptr = 0; send_string(std::string(buf)); }
void CServer::debug_request(const char *level_string) { int level; if (sscanf(level_string, "%d", &level) != 1) { throw std::runtime_error(std::string("Cannot extract debug level from request: ") + level_string); } logger->set_level(level); send_string(std::string("Debug level ") + num_to_str(logger->get_level())); }
void CServer::delete_request(const char *id_string) { int host_id; CHostData *host; if (sscanf(id_string, "%d", &host_id) != 1) { throw std::runtime_error(std::string("Cannot extract id from id request: ") + id_string); } host = m_host_set.find_host(host_id); if (host == 0) { throw std::runtime_error(std::string("Request to delete unexisting host ") + num_to_str(host_id)); } logger->log(2, "Invalidating cache"); m_cache.invalidate_host(host_id); logger->log(2, "Deleting host %d (%s): %d files", host_id, host->name(), host->fileset().count()); int deleted = host->fileset().count(); m_host_set.delete_host(host_id); send_string(num_to_str(deleted)); }
TextureDialog::TextureDialog(EditorState *pstate, Node *pnode, CubeSide pface): Dialog(pstate), node(pnode), face(pface), lb(NULL), the_image(NULL), context(NULL) { IVideoDriver *driver = state->device->getVideoDriver(); IGUIEnvironment *guienv = state->device->getGUIEnvironment(); // Window and basic items win = guienv->addWindow(rect<s32>(340, 50, 340 + 74 * 3 + 10, 50 + 74 * 3 + 10), true, narrow_to_wide(std::string(getCubeSideName(face)) + " texture").c_str()); guienv->addButton(rect<s32>(155, 30, 74*3, 55), win, ETD_GUI_ID_APPLY, L"Apply", L"Apply this texture selection to the node face"); guienv->addButton(rect<s32>(155, 60, 74*3, 85), win, ETD_GUI_ID_IMPORT, L"Import", L"Import images from files"); guienv->addButton(rect<s32>(84, 60, 150, 85), win, ETD_GUI_ID_ACTIONS, L"Actions"); // Fill out listbox lb = guienv->addListBox(rect<s32>(10, 104, 74 * 3, 74 * 3), win, 502); Media *media = &state->project->media; std::map<std::string, Media::Image*>& images = media->getList(); int count = 1; lb->addItem(L""); lb->setSelected(0); for (std::map<std::string, Media::Image*>::const_iterator it = images.begin(); it != images.end(); ++it) { if (!it->second) { continue; } if (it->second->name == "default") { lb->addItem(L""); } else { lb->addItem(narrow_to_wide(it->second->name + " [used " + num_to_str(it->second->getHolders()) + " times]").c_str()); } if (it->second == node->getTexture(face)) lb->setSelected(count); count++; } Media::Image *image = node->getTexture(face); if (image) { the_image = driver->addTexture("tmpicon.png", image->get()); } // Context menu context = guienv->addContextMenu(rect<s32>(84, 85, 150, 180), win, ETD_GUI_ID_ACTIONS_CM); context->addItem(L"Export", ETD_GUI_ID_EXPORT); context->setCloseHandling(ECMC_HIDE); context->setVisible(false); context->setEventParent(win); }
int main() { std::ifstream in("in.txt"); std::ofstream out("out.txt"); std::string as, bs; while (in >> as) { in >> bs; if (bs == "0") { out << "<error>\n\n\n"; continue; } std::vector<int> a, b; a = str_to_num(as); b = str_to_num(bs); std::pair<std::vector<int>, std::vector<int>> res = divide(a, b); out << num_to_str(res.first) << "\n" << num_to_str(res.second) << "\n\n"; } }
void CServer::load_request(const char *request) { if (strlen(request) == 0) { throw std::runtime_error("Empty load request"); } const char *p = request; p = strchr(request, ' '); if (p == 0) { throw std::runtime_error("Invalid load request"); } while (*p == ' ') p++; p = strchr(p, ' '); if (p == 0) { throw std::runtime_error("Invalid load request"); } while (*p == ' ') p++; const char *file_name = p; if (strlen(file_name) == 0) { throw std::runtime_error("Empty filename in load request"); } int host_id = 0; int estimated = 0; if (sscanf(request, "%d %d", &host_id, &estimated) != 2) { throw std::runtime_error("Host id or estimated count is invalid"); } CHostData *host = m_host_set.find_host(host_id); if (host == 0) { throw std::runtime_error("Cannot load file list for unknown host " + num_to_str(host_id)); } logger->log(2, "Invalidating cache"); m_cache.invalidate_host(host_id); logger->log(2, "Loading file for host %d from file %s", host_id, file_name); host->fileset().load_from_file(file_name); int new_count = host->fileset().count(); send_string(num_to_str(new_count)); logger->log(2, "%d new records inserted", new_count); if (new_count > 0) { logger->log(2, "Flushing cache"); m_cache.invalidate_all(); } }
/* Print thirty lines. */ static void verb_page(winid_t win) { int ix; char buf[32]; glk_set_window(win); for (ix=0; ix<30; ix++) { num_to_str(buf, ix); glk_put_string(buf); glk_put_char('\n'); } }
void CServer::host_info_request(const char *id_string) { int host_id; if (sscanf(id_string, "%d", &host_id) != 1) { throw std::runtime_error(std::string("Cannot extract host id from host request: ") + id_string); } const CHostData *host = m_host_set.find_host(host_id); if (host == 0) { throw std::runtime_error("Cannot write info about unknown host " + num_to_str(host_id)); } send_string(sprintf(*host)); }
void Project::AddNode(Node* node, bool select) { _node_count++; if (node->name == "") { node->name = "node_" + num_to_str(_node_count); } if (node->position == vector3di(0, 0, 0)) node->position = vector3di((_node_count - 1), 0, 0); node->remesh(); nodes.push_back(node); if (select) { snode = _node_count - 1; } }
/* Print every character, or rather try to. */ static void verb_chars(winid_t win) { int ix; char buf[16]; glk_set_window(win); for (ix=0; ix<256; ix++) { num_to_str(buf, ix); glk_put_string(buf); glk_put_string(": "); glk_put_char(ix); glk_put_char('\n'); } }
int vmstat_logger(struct trigger *trigp) { pid_t child; char *pid_str; char *pid_file, *log_file; struct stat *statp; int fd; pid_file = "vmstat.pid"; log_file = "vmstat.log"; if (trigp->active == 0) { statp = malloc(sizeof(struct stat *)); if (stat(pid_file, statp) == -1) return 0; pid_str = malloc(32 * sizeof(char)); fd = open(pid_file, O_RDONLY); read(fd, pid_str, 32); printf("Killing %s\n", pid_str); close(fd); unlink(pid_file); kill(strtol(pid_str, NULL, 10), 9); return 0; } if ((child = fork()) == 0) { fd = open(log_file, O_WRONLY|O_CREAT|O_TRUNC, S_IRUSR|S_IWUSR); dup2(fd, STDOUT_FILENO); execlp(trigp->command, trigp->command, trigp->command_args, NULL); perror("Execlp failed"); } else { statp = malloc(sizeof(struct stat *)); if (stat(pid_file, statp) != -1) { unlink(pid_file); free(statp); } fd = open(pid_file, O_WRONLY|O_CREAT|O_TRUNC, S_IRUSR|S_IWGRP); pid_str = num_to_str((long)child); write(fd, pid_str, strlen(pid_str)); close(fd); } return 0; }
int main(void) { setlocale(LC_CTYPE, "Rus"); //Массив с формами слова const wchar_t *str[3]; str[0] = L"лет"; str[1] = L"год"; str[2] = L"года"; int age; wprintf(L"Введите возраст (от %d до %d): ", MIN_AGE, MAX_AGE); do { age = read_int(stdin); if (age < MIN_AGE || age > MAX_AGE) wprintf(L"Неверный возраст. Возраст должен быть в диапазоне " "от %d до %d. Повторите ввод: \n", MIN_AGE, MAX_AGE); } while (age < MIN_AGE || age > MAX_AGE); wprintf(L"Мне %d %ls\n", age, num_to_str(age, str)); return 0; }
/* Print thirty lines in both windows. This gets fancy by printing to each window alternately, without setting the output stream, by using glk_put_string_stream() instead of glk_put_string(). There's no particular difference; this is just a demonstration. */ static void verb_pageboth(winid_t win) { int ix; winid_t otherwin; strid_t str, otherstr; char buf[32]; str = glk_window_get_stream(win); otherwin = print_to_otherwin(win); if (otherwin) otherstr = glk_window_get_stream(otherwin); else otherstr = NULL; for (ix=0; ix<30; ix++) { num_to_str(buf, ix); str_cat(buf, "\n"); glk_put_string_stream(str, buf); if (otherstr) glk_put_string_stream(otherstr, buf); } }
static char *arg_to_str(struct event_filter *filter, struct filter_arg *arg) { char *str; switch (arg->type) { case FILTER_ARG_BOOLEAN: str = malloc_or_die(6); if (arg->boolean.value) strcpy(str, "TRUE"); else strcpy(str, "FALSE"); return str; case FILTER_ARG_OP: return op_to_str(filter, arg); case FILTER_ARG_NUM: return num_to_str(filter, arg); case FILTER_ARG_STR: return str_to_str(filter, arg); case FILTER_ARG_VALUE: return val_to_str(filter, arg); case FILTER_ARG_FIELD: return field_to_str(filter, arg); case FILTER_ARG_EXP: return exp_to_str(filter, arg); default: /* ?? */ return NULL; } }
void CServer::dump_index_request(const char *request) { const char *p = request; p = strchr(request, ' '); if (p == 0) { throw std::runtime_error("Invalid dump request"); } while (*p == ' ') p++; const char *file_name = p; if (strlen(file_name) == 0) { throw std::runtime_error("Empty filename in dump request"); } int host_id = 0; if (sscanf(request, "%d", &host_id) != 1) { throw std::runtime_error("Invalid host id"); } const CHostData *host = m_host_set.find_host(host_id); if (host == 0) { throw std::runtime_error("Cannot dump index for unknown host " + num_to_str(host_id)); } logger->log(2, "Dumping word index for host %d to file %s", host_id, file_name); dump_index(host->fileset(), file_name); send_string("OK"); }
////////////////////////////////////////////////////////////////////// /// @fn string int_to_str(int num, unsigned int precision = 1) /// @note -- DEPRECATED. Use num_to_str() ////////////////////////////////////////////////////////////////////// string int_to_str(int num, unsigned int precision) { return num_to_str(num, precision); }
// Syntax: SEARCH num1 num2 [short|full] hostfilter query void CServer::search_request(const char *request) { int num_begin, num_end; bool is_full = true; if (strlen(request) == 0) throw std::runtime_error("Empty request"); const char *p = request; p = strchr(request, ' '); if (p == 0) { throw std::runtime_error("Invalid search request"); } while (*p == ' ') p++; p = strchr(p, ' '); if (p == 0) { throw std::runtime_error("Invalid search request"); } while (*p == ' ') p++; if (strncmp(p, "SHORT", 5) == 0) { p += 5; is_full = false; while (*p == ' ') p++; } const char *query = p; if (strlen(query) == 0) { throw std::runtime_error("Empty search query"); } if (sscanf(request, "%d %d", &num_begin, &num_end) != 2) { throw std::runtime_error("Minimal or maximal number is invalid"); } if ((num_begin < 0) || (num_end < num_begin)) { throw std::runtime_error("Minimal or maximal number is invalid"); } if (num_end - num_begin > 10000) { num_end = num_begin + 10000; } int error; CCachedQuery *cached = m_cache.do_query(p, error); if (error == CQueryCache::SyntaxError) { throw std::runtime_error("Syntax error"); } if (error == CQueryCache::NoHintFound) { send_string("FULLSCANREQUIRED"); return; } char query_info[100]; snprintf(query_info, 100, "QUERY_ID %d HINT %.4f%%", cached->id(), cached->hint_level() * 100.0); send_string(query_info); int matched = cached->count(); const std::vector<CFoundItem>& result = cached->get_result(); if (num_begin >= matched) { send_string("COUNT " + num_to_str(matched) + " 0 " + double_to_str(cached->search_time())); return; } if (num_end > matched) { num_end = matched; } int returned = num_end - num_begin; send_string("COUNT " + num_to_str(matched) + ' ' + num_to_str(returned) + ' ' + double_to_str(cached->search_time())); char record_buf[FILE_RECORD_STRING_LEN]; for (int i = num_begin; i < num_end; i++) { CRecordPtr ptr = result[i].m_record; const CFileRecord& record = *ptr; const CHostData *host = result[i].m_host; std::ostringstream buf; sprintf(record, record_buf); buf << record_buf; if (is_full) { buf << '\t'; if (record.parent().notNull()) buf << path(*record.parent()); buf << '\t' << host->wkg_id(); buf << '\t' << host->host_id(); buf << '\t' << host->proto(); buf << '\t' << host->name(); buf << '\t' << host->ip(); buf << '\t' << host->status(); } send_string(buf.str()); } // Disable exhaustive logging logger->log(2, "search time: %5.3lf sec", cached->search_time()); }
void CServer::count_request() { send_string(num_to_str(m_host_set.file_count())); }
/* convert a struct value to a string */ char * value_to_str(struct value *val, TBOOLEAN need_quotes) { static int i = 0; static char * s[4] = {NULL, NULL, NULL, NULL}; static size_t c[4] = {0, 0, 0, 0}; static const int minbufsize = 54; int j = i; i = (i + 1) % 4; if (s[j] == NULL) { s[j] = (char *) gp_alloc(minbufsize, "value_to_str"); c[j] = minbufsize; } switch (val->type) { case INTGR: sprintf(s[j], "%d", val->v.int_val); break; case CMPLX: if (isnan(val->v.cmplx_val.real)) sprintf(s[j], "NaN"); else if (val->v.cmplx_val.imag != 0.0) sprintf(s[j], "{%s, %s}", num_to_str(val->v.cmplx_val.real), num_to_str(val->v.cmplx_val.imag)); else return num_to_str(val->v.cmplx_val.real); break; case STRING: if (val->v.string_val) { if (!need_quotes) { return val->v.string_val; } else { char * cstr = conv_text(val->v.string_val); size_t reqsize = strlen(cstr) + 3; if (reqsize > c[j]) { /* Don't leave c[j[ non-zero if realloc fails */ s[j] = (char *) gp_realloc(s[j], reqsize + 20, NULL); if (s[j] != NULL) { c[j] = reqsize + 20; } else { c[j] = 0; int_error(NO_CARET, "out of memory"); } } sprintf(s[j], "\"%s\"", cstr); } } else { s[j][0] = NUL; } break; case DATABLOCK: { char **dataline = val->v.data_array; int nlines = 0; if (dataline != NULL) { while (*dataline++ != NULL) nlines++; } sprintf(s[j], "<%d line data block>", nlines); break; } default: int_error(NO_CARET, "unknown type in value_to_str()"); } return s[j]; }
////////////////////////////////////////////////////////////////////// /// @fn string doub_to_str(double num, unsigned int precision = -1) /// @note -- DEPRECATED. Use num_to_str() ////////////////////////////////////////////////////////////////////// string doub_to_str(double num, unsigned int precision) { return num_to_str(num, precision); }