wi_terminal_t * wi_terminal_init_with_type(wi_terminal_t *terminal, wi_string_t *type) { char *env; int err, co, li; err = tgetent(NULL, wi_string_cstring(type)); if(err <= 0) { if(err == 0) wi_error_set_lib_error(WI_ERROR_TERMCAP_NOSUCHENTRY); else wi_error_set_lib_error(WI_ERROR_TERMCAP_TERMINFONOTFOUND); wi_release(terminal); return NULL; } env = getenv("COLUMNS"); co = env ? strtol(env, NULL, 10) : tgetnum("co"); terminal->co = (co <= 0) ? 80 : co; env = getenv("LINES"); li = env ? strtol(env, NULL, 10) : tgetnum("li"); terminal->li = (li <= 0) ? 24 : li; terminal->ce = (char *) tgetstr("ce", NULL); terminal->cl = (char *) tgetstr("cl", NULL); terminal->cm = (char *) tgetstr("cm", NULL); terminal->cs = (char *) tgetstr("cs", NULL); terminal->scroll = wi_make_range(0, terminal->li); terminal->buffers = wi_array_init_with_capacity(wi_array_alloc(), 10); return terminal; }
void wi_test_indexset_creation(void) { wi_indexset_t *indexset; indexset = wi_indexset(); WI_TEST_ASSERT_NOT_NULL(indexset, ""); WI_TEST_ASSERT_EQUALS(wi_indexset_count(indexset), 0U, ""); WI_TEST_ASSERT_EQUALS(wi_indexset_first_index(indexset), 0U, ""); WI_TEST_ASSERT_EQUALS(wi_indexset_last_index(indexset), 0U, ""); indexset = wi_indexset_with_index(1); WI_TEST_ASSERT_NOT_NULL(indexset, ""); WI_TEST_ASSERT_EQUALS(wi_indexset_count(indexset), 1U, ""); WI_TEST_ASSERT_TRUE(wi_indexset_contains_index(indexset, 1), ""); WI_TEST_ASSERT_EQUALS(wi_indexset_first_index(indexset), 1U, ""); WI_TEST_ASSERT_EQUALS(wi_indexset_last_index(indexset), 1U, ""); indexset = wi_indexset_with_indexes_in_range(wi_make_range(1, 3)); WI_TEST_ASSERT_NOT_NULL(indexset, ""); WI_TEST_ASSERT_EQUALS(wi_indexset_count(indexset), 3U, ""); WI_TEST_ASSERT_TRUE(wi_indexset_contains_index(indexset, 1), ""); WI_TEST_ASSERT_TRUE(wi_indexset_contains_index(indexset, 2), ""); WI_TEST_ASSERT_TRUE(wi_indexset_contains_index(indexset, 3), ""); WI_TEST_ASSERT_EQUALS(wi_indexset_first_index(indexset), 1U, ""); WI_TEST_ASSERT_EQUALS(wi_indexset_last_index(indexset), 3U, ""); }
void wr_draw_transfers(wi_boolean_t force) { static wi_time_interval_t update; wi_enumerator_t *enumerator; wi_mutable_string_t *string; wi_string_t *status; wr_transfer_t *transfer; wi_time_interval_t interval; wi_uinteger_t i = 0; interval = wi_time_interval(); if(!force && interval - update < 1.0) return; update = interval; wi_terminal_set_scroll(wr_terminal, wi_make_range(1 + wi_array_count(wr_transfers), wi_terminal_size(wr_terminal).height - 4)); enumerator = wi_array_data_enumerator(wr_transfers); while((transfer = wi_enumerator_next_data(enumerator))) { wi_terminal_move(wr_terminal, wi_make_point(0, i + 1)); wi_terminal_clear_line(wr_terminal); if(transfer->state == WR_TRANSFER_RUNNING && interval - transfer->start_time > 0.0) { transfer->speed = ((double) transfer->total_transferred - transfer->total_offset) / (interval - transfer->start_time); status = wi_string_with_format(WI_STR("%@/%@, %@/s"), wr_files_string_for_size(transfer->total_transferred), wr_files_string_for_size(transfer->total_size), wr_files_string_for_size(transfer->speed)); } else if(transfer->state == WR_TRANSFER_QUEUED) { status = wi_string_with_format(WI_STR("queued at %u"), transfer->queue); } else { status = wi_string_with_cstring("waiting"); } string = wi_mutable_string_with_format(WI_STR("%u %3.0f%% %@"), transfer->tid, transfer->total_size > 0 ? 100 * ((double) transfer->total_transferred / (double) transfer->total_size) : 0, transfer->name); wi_terminal_adjust_string_to_fit_width(wr_terminal, string); wi_mutable_string_delete_characters_from_index(string, wi_string_length(string) - wi_string_length(status)); wi_mutable_string_append_string(string, status); wi_terminal_printf(wr_terminal, WI_STR("%@"), string); i++; } wr_terminal_reset_location(); }
wi_date_t * wi_date_init_with_iso8601_string(wi_date_t *date, wi_string_t *string) { if(wi_string_length(string) < _WI_DATE_ISO8601_STRING_SIZE) { wi_release(date); return NULL; } #ifdef HAVE_GLIBC string = wi_string_by_deleting_characters_in_range(string, wi_make_range(19, 6)); return wi_date_init_with_string(date, string, WI_STR("%Y-%m-%dT%H:%M:%S")); #else string = wi_string_by_deleting_characters_in_range(string, wi_make_range(22, 1)); return wi_date_init_with_string(date, string, WI_STR("%Y-%m-%dT%H:%M:%S%z")); #endif }
static wi_process_t * _wi_process_init_with_argv(wi_process_t *process, int argc, const char **argv) { wi_array_t *array; wi_string_t *string; struct utsname name; #if defined(HAVE_NXGETLOCALARCHINFO) const NXArchInfo *archinfo; cpu_type_t cputype; size_t cputypesize; #elif defined(HAVE_SYSINFO) && defined(SI_ARCHITECTURE) char buffer[SYS_NMLN]; #endif array = wi_array_init_with_argv(wi_array_alloc(), argc, argv); string = wi_array_first_data(array); if(string) { process->path = wi_retain(string); process->name = wi_retain(wi_string_last_path_component(process->path)); } else { process->path = wi_retain(WI_STR("unknown")); process->name = wi_retain(process->path); } if(wi_array_count(array) <= 1) process->arguments = wi_array_init(wi_array_alloc()); else process->arguments = wi_retain(wi_array_subarray_with_range(array, wi_make_range(1, wi_array_count(array) - 1))); wi_release(array); uname(&name); process->os_name = wi_string_init_with_cstring(wi_string_alloc(), name.sysname); process->os_release = wi_string_init_with_cstring(wi_string_alloc(), name.release); #if defined(HAVE_NXGETLOCALARCHINFO) cputypesize = sizeof(cputype); if(sysctlbyname("sysctl.proc_cputype", &cputype, &cputypesize, NULL, 0) < 0) cputype = NXGetLocalArchInfo()->cputype; archinfo = NXGetArchInfoFromCpuType(cputype, CPU_SUBTYPE_MULTIPLE); if(archinfo) process->arch = wi_string_init_with_cstring(wi_string_alloc(), archinfo->name); #elif defined(HAVE_SYSINFO) && defined(SI_ARCHITECTURE) if(sysinfo(SI_ARCHITECTURE, buffer, sizeof(buffer)) >= 0) process->arch = wi_string_init_with_cstring(wi_string_alloc(), buffer); #endif if(!process->arch) process->arch = wi_string_init_with_cstring(wi_string_alloc(), name.machine); return process; }
void wi_test_indexset_indexes(void) { wi_indexset_t *indexset; indexset = wi_indexset_with_indexes_in_range(wi_make_range(1, 3)); WI_TEST_ASSERT_NOT_NULL(indexset, ""); WI_TEST_ASSERT_EQUALS(wi_indexset_count(indexset), 3U, ""); WI_TEST_ASSERT_TRUE(wi_indexset_contains_index(indexset, 1), ""); WI_TEST_ASSERT_TRUE(wi_indexset_contains_index(indexset, 2), ""); WI_TEST_ASSERT_TRUE(wi_indexset_contains_index(indexset, 3), ""); WI_TEST_ASSERT_FALSE(wi_indexset_contains_index(indexset, 4), ""); WI_TEST_ASSERT_TRUE(wi_indexset_contains_indexes(indexset, wi_indexset_with_index(1)), ""); WI_TEST_ASSERT_FALSE(wi_indexset_contains_indexes(indexset, wi_indexset_with_index(4)), ""); WI_TEST_ASSERT_TRUE(wi_indexset_contains_indexes_in_range(indexset, wi_make_range(2, 1)), ""); WI_TEST_ASSERT_FALSE(wi_indexset_contains_indexes_in_range(indexset, wi_make_range(4, 1)), ""); WI_TEST_ASSERT_EQUALS(wi_indexset_first_index(indexset), 1U, ""); WI_TEST_ASSERT_EQUALS(wi_indexset_last_index(indexset), 3U, ""); }
wi_string_t * wi_socket_read_to_string(wi_socket_t *socket, wi_time_interval_t timeout, wi_string_t *separator) { wi_string_t *string, *substring; wi_uinteger_t index; index = wi_string_index_of_string(socket->buffer, separator, 0); if(index != WI_NOT_FOUND) { substring = wi_string_substring_to_index(socket->buffer, index + wi_string_length(separator)); wi_mutable_string_delete_characters_in_range(socket->buffer, wi_make_range(0, wi_string_length(substring))); return substring; } while((string = wi_socket_read_string(socket, timeout))) { if(wi_string_length(string) == 0) return string; wi_mutable_string_append_string(socket->buffer, string); index = wi_string_index_of_string(socket->buffer, separator, 0); if(index == WI_NOT_FOUND) { if(wi_string_length(socket->buffer) > _WI_SOCKET_BUFFER_MAX_SIZE) { wi_error_set_libwired_error_with_format(WI_ERROR_SOCKET_OVERFLOW, WI_STR("Buffer is %u bytes"), wi_string_length(socket->buffer)); wi_mutable_string_set_string(socket->buffer, WI_STR("")); return NULL; } } else { substring = wi_string_substring_to_index(socket->buffer, index + wi_string_length(separator)); wi_mutable_string_delete_characters_in_range(socket->buffer, wi_make_range(0, wi_string_length(substring))); return substring; } } return NULL; }
wi_string_t * wi_socket_read_to_string(wi_socket_t *socket, wi_time_interval_t timeout, wi_string_t *separator) { wi_string_t *string, *substring; wi_uinteger_t index; index = wi_string_index_of_string(socket->buffer, separator, 0); if(index != WI_NOT_FOUND) { substring = wi_string_substring_to_index(socket->buffer, index + wi_string_length(separator)); wi_string_delete_characters_in_range(socket->buffer, wi_make_range(0, wi_string_length(substring))); return substring; } while((string = wi_socket_read(socket, timeout, WI_SOCKET_BUFFER_SIZE))) { if(wi_string_length(string) == 0) return string; wi_string_append_string(socket->buffer, string); index = wi_string_index_of_string(socket->buffer, separator, 0); if(index == WI_NOT_FOUND) { if(wi_string_length(socket->buffer) > _WI_SOCKET_BUFFER_MAX_SIZE) { substring = wi_string_substring_to_index(socket->buffer, _WI_SOCKET_BUFFER_MAX_SIZE); wi_string_delete_characters_in_range(socket->buffer, wi_make_range(0, wi_string_length(substring))); return substring; } } else { substring = wi_string_substring_to_index(socket->buffer, index + wi_string_length(separator)); wi_string_delete_characters_in_range(socket->buffer, wi_make_range(0, wi_string_length(substring))); return substring; } } return NULL; }
void wr_draw_transfers(wi_boolean_t force) { static wi_time_interval_t update; wi_list_node_t *node; wi_string_t *string, *status; wr_transfer_t *transfer; wi_time_interval_t interval; unsigned int i = 0; interval = wi_time_interval(); if(!force && interval - update < 1.0) return; update = interval; wi_terminal_set_scroll(wr_terminal, wi_make_range(1 + wi_list_count(wr_transfers), wi_terminal_size(wr_terminal).height - 3)); WI_LIST_FOREACH(wr_transfers, node, transfer) { wi_terminal_move(wr_terminal, wi_make_point(0, i + 1)); wi_terminal_clear_line(wr_terminal); if(transfer->state == WR_TRANSFER_RUNNING && interval - transfer->start_time > 0.0) { transfer->speed = ((double) transfer->transferred - transfer->offset) / (interval - transfer->start_time); status = wi_string_with_format(WI_STR("%@/%@, %@/s"), wr_files_string_for_size(transfer->transferred), wr_files_string_for_size(transfer->size), wr_files_string_for_size(transfer->speed)); } else if(transfer->state == WR_TRANSFER_QUEUED) { status = wi_string_with_format(WI_STR("queued at %u"), transfer->queue); } else { status = wi_string_with_cstring("waiting"); } string = wi_string_with_format(WI_STR("%u %3.0f%% %@"), transfer->tid, transfer->size > 0 ? 100 * ((double) transfer->transferred / (double) transfer->size) : 0, transfer->name); wi_terminal_adjust_string_to_fit_width(wr_terminal, string); wi_string_delete_characters_from_index(string, wi_string_length(string) - wi_string_length(status)); wi_string_append_string(string, status); wi_terminal_printf(wr_terminal, WI_STR("%@"), string); i++; }
wi_date_t * wi_date_init_with_rfc3339_string(wi_date_t *date, wi_string_t *string) { wi_mutable_string_t *fullstring; wi_string_t *timezone; if(wi_string_length(string) >= 19) { fullstring = wi_autorelease(wi_mutable_copy(string)); timezone = wi_string_substring_from_index(fullstring, 19); if(wi_is_equal(timezone, WI_STR("Z"))) { wi_mutable_string_delete_characters_in_range(fullstring, wi_make_range(19, 1)); return wi_date_init_with_string(date, fullstring, WI_STR("%Y-%m-%dT%H:%M:%S")); } else if(wi_string_length(timezone) == 6) { wi_mutable_string_delete_characters_in_range(fullstring, wi_make_range(22, 1)); return wi_date_init_with_string(date, fullstring, WI_STR("%Y-%m-%dT%H:%M:%S%z")); } } wi_release(date); return NULL; }
void wi_test_indexset_enumeration_with_range(void) { wi_indexset_t *indexset; wi_enumerator_t *enumerator; wi_uinteger_t i, index; indexset = wi_indexset_with_indexes_in_range(wi_make_range(0, 1000)); enumerator = wi_indexset_index_enumerator(indexset); i = 0; while(wi_enumerator_get_next_data(enumerator, (void **) &index)) { WI_TEST_ASSERT_EQUALS(index, i, ""); i++; } }
static wi_boolean_t wd_users_get_human_readable_version(wi_string_t *version, wi_string_t **application_name, wi_string_t **application_version, wi_string_t **os_name, wi_string_t **os_version, wi_string_t **arch) { wi_range_t range; wi_uinteger_t index; range = wi_make_range(0, wi_string_length(version)); index = wi_string_index_of_string_in_range(version, WI_STR("/"), 0, range); if(index == WI_NOT_FOUND || index == range.length - 1) return false; *application_name = wi_string_substring_with_range(version, wi_make_range(range.location, index - range.location)); range.location = index + 1; range.length -= index + 1; index = wi_string_index_of_string_in_range(version, WI_STR(" ("), 0, range); if(index == WI_NOT_FOUND || index == range.length - 2) return false; *application_version = wi_string_substring_with_range(version, wi_make_range(range.location, index - range.location)); range.location = index + 2; range.length -= index + 2; index = wi_string_index_of_string_in_range(version, WI_STR("; "), 0, range); if(index == WI_NOT_FOUND || index == range.length - 2) return false; *os_name = wi_string_substring_with_range(version, wi_make_range(range.location, index - range.location)); range.location = index + 2; range.length -= index + 2; index = wi_string_index_of_string_in_range(version, WI_STR("; "), 0, range); if(index == WI_NOT_FOUND || index == range.length - 2) return false; *os_version = wi_string_substring_with_range(version, wi_make_range(range.location, index - range.location)); range.location = index + 2; range.length -= index + 2; index = wi_string_index_of_string_in_range(version, WI_STR(")"), 0, range); if(index == WI_NOT_FOUND || index == range.length - 1) return false; *arch = wi_string_substring_with_range(version, wi_make_range(range.location, index - range.location)); return true; }
static wi_process_t * _wi_process_init_with_argv(wi_process_t *process, int argc, const char **argv) { wi_array_t *array; wi_string_t *string; struct utsname name; #ifdef HAVE_MACH_O_ARCH_H const NXArchInfo *arch_info; #endif array = wi_array_init_with_argv(wi_array_alloc(), argc, argv); string = wi_array_first_data(array); if(string) { process->path = wi_retain(string); process->name = wi_retain(wi_string_last_path_component(process->path)); } else { process->path = wi_retain(WI_STR("unknown")); process->name = wi_retain(process->path); } if(wi_array_count(array) <= 1) process->arguments = wi_array_init(wi_array_alloc()); else process->arguments = wi_retain(wi_array_subarray_with_range(array, wi_make_range(1, wi_array_count(array) - 1))); wi_release(array); uname(&name); process->os_name = wi_string_init_with_cstring(wi_string_alloc(), name.sysname); process->os_release = wi_string_init_with_cstring(wi_string_alloc(), name.release); #ifdef HAVE_MACH_O_ARCH_H arch_info = NXGetArchInfoFromCpuType(NXGetLocalArchInfo()->cputype, CPU_SUBTYPE_MULTIPLE); process->arch = wi_string_init_with_cstring(wi_string_alloc(), arch_info->name); #else process->arch = wi_string_init_with_cstring(wi_string_alloc(), name.machine); #endif return process; }
void wi_test_indexset_runtime_functions(void) { wi_indexset_t *indexset1; wi_mutable_indexset_t *indexset2; indexset1 = wi_indexset_with_indexes_in_range(wi_make_range(1, 3)); indexset2 = wi_autorelease(wi_mutable_copy(indexset1)); WI_TEST_ASSERT_EQUAL_INSTANCES(indexset1, indexset2, ""); WI_TEST_ASSERT_EQUALS(wi_hash(indexset1), wi_hash(indexset2), ""); WI_TEST_ASSERT_EQUALS(wi_runtime_id(indexset1), wi_indexset_runtime_id(), ""); WI_TEST_ASSERT_EQUALS(wi_runtime_id(indexset2), wi_indexset_runtime_id(), ""); WI_TEST_ASSERT_TRUE(wi_runtime_options(indexset1) & WI_RUNTIME_OPTION_IMMUTABLE, ""); WI_TEST_ASSERT_TRUE(wi_runtime_options(indexset2) & WI_RUNTIME_OPTION_MUTABLE, ""); wi_mutable_indexset_remove_index(indexset2, 1); wi_mutable_indexset_add_index(indexset2, 4); WI_TEST_ASSERT_NOT_EQUAL_INSTANCES(indexset1, indexset2, ""); WI_TEST_ASSERT_NOT_EQUALS(wi_string_index_of_string(wi_description(indexset2), WI_STR("4"), 0), WI_NOT_FOUND, ""); }
void wi_test_indexset_mutation(void) { wi_mutable_indexset_t *indexset1, *indexset2; indexset1 = wi_mutable_indexset(); WI_TEST_ASSERT_EQUALS(wi_indexset_count(indexset1), 0U, ""); WI_TEST_ASSERT_FALSE(wi_indexset_contains_index(indexset1, 1), ""); WI_TEST_ASSERT_FALSE(wi_indexset_contains_index(indexset1, 2), ""); WI_TEST_ASSERT_FALSE(wi_indexset_contains_index(indexset1, 3), ""); WI_TEST_ASSERT_EQUALS(wi_indexset_first_index(indexset1), 0U, ""); WI_TEST_ASSERT_EQUALS(wi_indexset_last_index(indexset1), 0U, ""); wi_mutable_indexset_add_index(indexset1, 1); wi_mutable_indexset_add_indexes(indexset1, wi_indexset_with_index(2)); wi_mutable_indexset_add_indexes_in_range(indexset1, wi_make_range(3, 1)); WI_TEST_ASSERT_EQUALS(wi_indexset_count(indexset1), 3U, ""); WI_TEST_ASSERT_TRUE(wi_indexset_contains_index(indexset1, 1), ""); WI_TEST_ASSERT_TRUE(wi_indexset_contains_index(indexset1, 2), ""); WI_TEST_ASSERT_TRUE(wi_indexset_contains_index(indexset1, 3), ""); WI_TEST_ASSERT_EQUALS(wi_indexset_first_index(indexset1), 1U, ""); WI_TEST_ASSERT_EQUALS(wi_indexset_last_index(indexset1), 3U, ""); wi_mutable_indexset_remove_index(indexset1, 1); wi_mutable_indexset_remove_indexes(indexset1, wi_indexset_with_index(2)); wi_mutable_indexset_remove_indexes_in_range(indexset1, wi_make_range(3, 1)); WI_TEST_ASSERT_EQUALS(wi_indexset_count(indexset1), 0U, ""); WI_TEST_ASSERT_FALSE(wi_indexset_contains_index(indexset1, 1), ""); WI_TEST_ASSERT_FALSE(wi_indexset_contains_index(indexset1, 2), ""); WI_TEST_ASSERT_FALSE(wi_indexset_contains_index(indexset1, 3), ""); WI_TEST_ASSERT_EQUALS(wi_indexset_first_index(indexset1), 0U, ""); WI_TEST_ASSERT_EQUALS(wi_indexset_last_index(indexset1), 0U, ""); wi_mutable_indexset_add_indexes_in_range(indexset1, wi_make_range(1, 3)); indexset2 = wi_autorelease(wi_mutable_copy(indexset1)); wi_mutable_indexset_add_index(indexset2, 5); wi_mutable_indexset_add_index(indexset2, 4); WI_TEST_ASSERT_EQUALS(wi_indexset_count(indexset2), 5U, ""); WI_TEST_ASSERT_TRUE(wi_indexset_contains_index(indexset2, 1), ""); WI_TEST_ASSERT_TRUE(wi_indexset_contains_index(indexset2, 2), ""); WI_TEST_ASSERT_TRUE(wi_indexset_contains_index(indexset2, 3), ""); WI_TEST_ASSERT_TRUE(wi_indexset_contains_index(indexset2, 4), ""); WI_TEST_ASSERT_TRUE(wi_indexset_contains_index(indexset2, 5), ""); WI_TEST_ASSERT_EQUALS(wi_indexset_first_index(indexset2), 1U, ""); WI_TEST_ASSERT_EQUALS(wi_indexset_last_index(indexset2), 5U, ""); wi_mutable_indexset_set_indexes(indexset1, indexset2); WI_TEST_ASSERT_EQUAL_INSTANCES(indexset1, indexset2, ""); wi_mutable_indexset_remove_all_indexes(indexset1); WI_TEST_ASSERT_EQUALS(wi_indexset_count(indexset1), 0U, ""); WI_TEST_ASSERT_FALSE(wi_indexset_contains_index(indexset1, 1), ""); WI_TEST_ASSERT_FALSE(wi_indexset_contains_index(indexset1, 2), ""); WI_TEST_ASSERT_FALSE(wi_indexset_contains_index(indexset1, 3), ""); WI_TEST_ASSERT_EQUALS(wi_indexset_first_index(indexset1), 0U, ""); WI_TEST_ASSERT_EQUALS(wi_indexset_last_index(indexset1), 0U, ""); }
wi_date_t * wi_date_init_with_string(wi_date_t *date, wi_string_t *string, wi_string_t *format) { wi_regexp_t *regexp; wi_string_t *substring; struct tm tm; time_t clock; wi_uinteger_t count, offset, hours, minutes; memset(&tm, 0, sizeof(tm)); if(!strptime(wi_string_utf8_string(string), wi_string_utf8_string(format), &tm)) { wi_release(date); return NULL; } offset = 0; if(wi_string_contains_string(format, WI_STR("%z"), WI_STRING_CASE_INSENSITIVE)) { regexp = wi_regexp_with_pattern(WI_STR("((\\+|\\-)[0-9]{4})"), 0); substring = wi_regexp_string_of_first_match_in_string(regexp, string); if(substring) { hours = wi_string_uinteger(wi_string_substring_with_range(substring, wi_make_range(1, 2))); minutes = wi_string_uinteger(wi_string_substring_with_range(substring, wi_make_range(3, 2))); offset = (hours * 3600) + (minutes * 60); if(wi_string_has_prefix(substring, WI_STR("-"))) offset = -offset; } } clock = wi_timegm(&tm) - offset; return wi_date_init_with_time(date, clock); }
void wi_terminal_close(wi_terminal_t *terminal) { wi_terminal_set_scroll(terminal, wi_make_range(0, terminal->li)); wi_terminal_move(terminal, wi_make_point(0, terminal->li - 1)); }