bool BinaryReader::CBinaryReader::open(const FilePath& path) { if (m_opened) { close(); } if (FileSystem::IsResource(path)) { HMODULE hModule = ::GetModuleHandleW(nullptr); const std::wstring pathW = path.toWstr(); if (HRSRC hrs = ::FindResourceW(hModule, &pathW[1], L"FILE")) { m_pResource = ::LockResource(::LoadResource(hModule, hrs)); m_size = ::SizeofResource(hModule, hrs); LOG_DEBUG(U"📤 BinaryReader: Opened resource \"{0}\" size: {1}"_fmt(path, FMTBYTES(m_size))); m_opened = true; m_fullPath = path; return true; } else { LOG_FAIL(U"❌ BinaryReader: Failed to open resource \"{0}\""_fmt(path)); return false; } } else { m_handle = ::CreateFileW(path.toWstr().c_str(), GENERIC_READ, (FILE_SHARE_READ | FILE_SHARE_WRITE), nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr); m_opened = (m_handle != INVALID_HANDLE_VALUE); if (!m_opened) { LOG_FAIL(U"❌ BinaryReader: Failed to open file \"{0}\""_fmt(path)); return false; } m_fullPath = FileSystem::FullPath(path); LARGE_INTEGER size; ::GetFileSizeEx(m_handle, &size); m_size = size.QuadPart; LOG_DEBUG(U"📤 BinaryReader: Opened file \"{0}\" size: {1}"_fmt(m_fullPath, FMTBYTES(m_size))); return true; } }
VertexShader_GL(const String& source) { const std::string sourceUTF8 = source.toUTF8(); const char* pSource = sourceUTF8.c_str(); m_vsProgram = ::glCreateShaderProgramv(GL_VERTEX_SHADER, 1, &pSource); GLint status = GL_FALSE; ::glGetProgramiv(m_vsProgram, GL_LINK_STATUS, &status); GLint logLen = 0; ::glGetProgramiv(m_vsProgram, GL_INFO_LOG_LENGTH, &logLen); if (logLen > 4) { std::string log(logLen + 1, '\0'); ::glGetProgramInfoLog(m_vsProgram, logLen, &logLen, &log[0]); LOG_FAIL(L"❌ Vertex shader compilation failed: {0}"_fmt(CharacterSet::Widen(log))); } if (status == GL_FALSE) { ::glDeleteProgram(m_vsProgram); m_vsProgram = 0; } m_initialized = m_vsProgram != 0; }
int64 BinaryReader::CBinaryReader::lookahead(void* const buffer, const int64 pos, const int64 size) { assert(buffer != nullptr || size == 0); if (m_pResource) { const int64 readSize = Clamp(size, 0LL, m_size - pos); ::memcpy(buffer, static_cast<const Byte*>(m_pResource) + pos, static_cast<size_t>(readSize)); return readSize; } else { const auto current = getPos(); DWORD readBytes; setPos(pos); if (!::ReadFile(m_handle, buffer, static_cast<DWORD>(size), &readBytes, nullptr)) { LOG_FAIL(U"❌ BinaryReader: Failed ::ReadFile() \"{0}\""_fmt(m_fullPath)); return 0; } setPos(current); return readBytes; } }
int main( ) { if(F(5) == 141) LOG_PASS(); else LOG_FAIL(); return 0; }
bool get_property_and_output_log(GDBusProxy *_proxy, const gchar *_property_name, const gchar *_format, void *_dest) { bool ret = get_property(_proxy, _property_name, _format, _dest); if (ret == false) { LOG_FAIL(U"❌ PowerStatus: Failed to get UPower device property ({})."_fmt(_property_name)); } return ret; }
int main() { char* str_palin[] = { "abcba", "qwertytrewq", "zxcvbvcxz" }; char* str_nonpalin[] = { "asdfghj", "poqwert" }; for(int i=0; i<(sizeof(str_palin)/sizeof(str_palin[0])); i++) { if(!is_palindrome(str_palin[i])) { LOG_FAIL(); } } for(int i=0; i<(sizeof(str_nonpalin)/sizeof(str_nonpalin[0])); i++) { if(is_palindrome(str_nonpalin[i])) { LOG_FAIL(); } } LOG_PASS(); return 0; }
PixelShader_GL(const String& source) { const std::string sourceUTF8 = source.toUTF8(); const char* pSource = sourceUTF8.c_str(); m_psProgram = ::glCreateShaderProgramv(GL_FRAGMENT_SHADER, 1, &pSource); GLint status = GL_FALSE; ::glGetProgramiv(m_psProgram, GL_LINK_STATUS, &status); GLint logLen = 0; ::glGetProgramiv(m_psProgram, GL_INFO_LOG_LENGTH, &logLen); if (logLen > 4) { std::string log(logLen + 1, '\0'); ::glGetProgramInfoLog(m_psProgram, logLen, &logLen, &log[0]); LOG_FAIL(U"❌ Pixel shader compilation failed: {0}"_fmt(Unicode::Widen(log))); } if (status == GL_FALSE) { ::glDeleteProgram(m_psProgram); m_psProgram = 0; } if (m_psProgram) { const int32 t = ::glGetUniformLocation(m_psProgram, "Tex0"); if (t != -1) { m_textureIndex = t; } } m_initialized = m_psProgram != 0; }
void GetPowerStatus_Linux(PowerStatus& result) { std::vector<std::string> devicePaths; GDBusProxy *proxy; GDBusConnection *conn; GError *error = nullptr; char *s; guint32 u; guint64 x; gboolean b; gdouble d; GVariant *variant; GVariantIter *iter; conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, nullptr, &error); if (conn == nullptr) { LOG_FAIL(U"❌ PowerStatus: Failed to get d-bus connection."); return; } proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE, nullptr, NAME_UPower, OBJECT_UPower, INTERFACE_UPower, nullptr, &error); if (proxy == nullptr) { LOG_FAIL(U"❌ PowerStatus: Failed to get d-bus proxy."); return; } variant = g_dbus_proxy_get_cached_property(proxy, "DaemonVersion"); if (variant == nullptr) { LOG_FAIL(U"❌ PowerStatus: Failed to get UPower properties."); return; } g_variant_get(variant, "s", &s); g_variant_unref(variant); LOG_INFO(U"ℹ️ PowerStatus: UPower daemon version {}"_fmt(s)); variant = g_dbus_proxy_call_sync(proxy, "EnumerateDevices", nullptr, G_DBUS_CALL_FLAGS_NONE, -1, nullptr, &error); if (variant == nullptr) { LOG_FAIL(U"❌ PowerStatus: Failed to enumerate power supply devices."); return; } g_variant_get(variant, "(ao)", &iter); while (g_variant_iter_loop(iter, "o", &s)) { devicePaths.push_back(s); } g_variant_iter_free(iter); g_variant_unref(variant); g_object_unref(proxy); result.battery = BatteryStatus::NoBattery; for (const auto &dev : devicePaths) { proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE, nullptr, NAME_UPower, dev.c_str(), INTERFACE_UPower_Device, nullptr, &error); if (!get_property_and_output_log(proxy, "Type", "u", &u)) continue; if (u == 1) { //line power if (get_property_and_output_log(proxy, "Online", "b", &b)) { result.ac = b ? ACLineStatus::Online : ACLineStatus::Offline; } } else if (u == 2) { //battery if (get_property_and_output_log(proxy, "Percentage", "d", &d)) { result.batteryLifePercent = static_cast<int32>(d); result.battery = d <= 5 ? BatteryStatus::Critical : d <= 33 ? BatteryStatus::Low : d <= 66 ? BatteryStatus::Middle : BatteryStatus::High; } if (get_property_and_output_log(proxy, "State", "u", &u)) { if (u == 1) { //charging result.charging = true; if (get_property_and_output_log(proxy, "TimeToFull", "x", &x)) { if (x != 0) result.batteryTimeToFullChargeSec = static_cast<int32>(x); } } else if (u == 2) { //discharging if (get_property_and_output_log(proxy, "TimeToEmpty", "x", &x)) { if (x != 0) result.batteryLifeTimeSec = static_cast<int32>(x); } } } } } g_object_unref(conn); }
int main() { int i; /* generic index */ int N; /* number of points in FFT */ double (*x)[2]; /* pointer to time-domain samples */ double (*X)[2]; /* pointer to frequency-domain samples */ double (*Xexp)[2]; /* pointer expected frequency-domain samples */ double epsilon; /* tolerance factor */ /* Initialize parameters */ N=2; /* Check that N = 2^n for some integer n >= 1. */ if(N >= 2) { i = N; while(i==2*(i/2)) i = i/2; /* While i is even, factor out a 2. */ } /* For N >=2, we now have N = 2^n iff i = 1. */ if(N < 2 || i != 1) { printf(", which does not equal 2^n for an integer n >= 1."); exit(EXIT_FAILURE); } /* Allocate time- and frequency-domain memory. */ x = malloc(2 * N * sizeof(double)); X = malloc(2 * N * sizeof(double)); Xexp = malloc(2 * N * sizeof(double)); /* Initialize time domain samples */ x[0][0] = 1.2 ; x[0][1] = 3.4; x[1][0] = 5.6 ; x[1][1] = 0.4; /* Initialize freq domain expected samples */ Xexp[0][0] = 6.8 ; Xexp[0][1] = 3.8 ; Xexp[1][0] = -4.4 ; Xexp[1][1] = 3.0 ; epsilon = 0.1; /* Calculate FFT. */ fft(N, x, X); /* check results */ for(i=0; i<N; i++){ if(!(FLOAT_EQ(X[i][0], Xexp[i][0], epsilon) && FLOAT_EQ(X[i][1], Xexp[i][1], epsilon))){ LOG_FAIL(); } } /* Print time-domain samples and resulting frequency-domain samples. */ #if 0 printf("\nx(n):"); for(i=0; i<N; i++) printf("\n n=%d: %12f %12f", i, x[i][0], x[i][1]); printf("\nX(k):"); for(i=0; i<N; i++) printf("\n k=%d: %12f %12f", i, X[i][0], X[i][1]); printf("\n"); #endif /* Free memory. */ free(x); free(X); LOG_PASS(); }
static bool ExitInputError(int32 lineNumber) { LOG_FAIL(U"Wrong input format at line {}"_fmt(lineNumber)); return false; }
bool Problem::CProblem::load(const FilePath& path) { int max_index, inst_max_index, i; size_t elements, j; FILE *fp = fopen(path.narrow().c_str(), "r"); char *endptr; char *idx, *val, *label; if (fp == NULL) { LOG_FAIL(U"Could not open `{}`"_fmt(path)); return false; } m_problem.l = 0; elements = 0; m_max_line_len = 1024; m_line = (char*)::malloc(m_max_line_len); while (readline(fp) != NULL) { char *p = strtok(m_line, " \t"); // label // features while (1) { p = strtok(NULL, " \t"); if (p == NULL || *p == '\n') // check '\n' as ' ' may be after the last feature break; ++elements; } ++elements; ++m_problem.l; } rewind(fp); m_problem.y = new double[m_problem.l]; m_problem.x = new svm_node*[m_problem.l]; m_nodes.resize(elements); max_index = 0; j = 0; for (i = 0; i<m_problem.l; i++) { inst_max_index = -1; // strtol gives 0 if wrong format, and precomputed kernel has <index> start from 0 readline(fp); m_problem.x[i] = &m_nodes[j]; label = strtok(m_line, " \t\n"); if (label == NULL) // empty line { return detail::ExitInputError(i + 1); } m_problem.y[i] = strtod(label, &endptr); if (endptr == label || *endptr != '\0') { return detail::ExitInputError(i + 1); } while (1) { idx = strtok(NULL, ":"); val = strtok(NULL, " \t"); if (val == NULL) break; errno = 0; m_nodes[j].index = (int)strtol(idx, &endptr, 10); if (endptr == idx || errno != 0 || *endptr != '\0' || m_nodes[j].index <= inst_max_index) { return detail::ExitInputError(i + 1); } else inst_max_index = m_nodes[j].index; errno = 0; m_nodes[j].value = strtod(val, &endptr); if (endptr == val || errno != 0 || (*endptr != '\0' && !isspace(*endptr))) { return detail::ExitInputError(i + 1); } ++j; } if (inst_max_index > max_index) max_index = inst_max_index; m_nodes[j++].index = -1; } fclose(fp); m_maxIndex = max_index; m_hasData = true; return true; }
explicit CTobii(const FilePath&) { LOG_FAIL(L"❌ Tobii is not supported on macOS"); }