Skip to content

Commit 7977aab

Browse files
committed
Fix some bad practices and compilation warnings
1 parent 470bf4e commit 7977aab

7 files changed

Lines changed: 31 additions & 23 deletions

File tree

examples/desktop.cmake

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ message(STATUS "ccap: GLFW available: ${GLFW_AVAILABLE}")
3838

3939
add_library(common_utils STATIC ${DESKTOP_EXAMPLES_DIR}/utils/helper.cpp)
4040
target_include_directories(common_utils PRIVATE ${DESKTOP_EXAMPLES_DIR}/utils)
41+
target_compile_definitions(common_utils PUBLIC _CRT_SECURE_NO_WARNINGS=1)
4142
target_link_libraries(common_utils PUBLIC ccap)
4243

4344
file(GLOB EXAMPLE_SOURCE ${DESKTOP_EXAMPLES_DIR}/*.cpp ${DESKTOP_EXAMPLES_DIR}/*.c)
@@ -52,7 +53,6 @@ foreach(EXAMPLE ${EXAMPLE_SOURCE})
5253
endif()
5354

5455
add_executable(${EXAMPLE_NAME} ${EXAMPLE})
55-
target_compile_definitions(${EXAMPLE_NAME} PRIVATE _CRT_SECURE_NO_WARNINGS=1)
5656
target_link_libraries(${EXAMPLE_NAME} PRIVATE common_utils)
5757

5858
if(APPLE)

examples/desktop/2-capture_grab_c.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,11 @@
99
#include "ccap_utils_c.h"
1010
#include "utils/helper.h"
1111

12+
#include <ctype.h>
1213
#include <stdio.h>
1314
#include <stdlib.h>
1415
#include <string.h>
1516

16-
#include <ctype.h>
17-
1817
int main(int argc, char** argv) {
1918
printf("ccap C Interface Capture Grab Example\n");
2019
printf("Version: %s\n\n", ccap_get_version());
@@ -105,7 +104,7 @@ int main(int argc, char** argv) {
105104
// Save frame to directory
106105
char outputPath[2048];
107106
int result = ccap_dump_frame_to_directory(frame, captureDir, outputPath, sizeof(outputPath));
108-
if (result > 0) {
107+
if (result >= 0) {
109108
printf("VideoFrame saved to: %s\n", outputPath);
110109
} else {
111110
fprintf(stderr, "Failed to save frame!\n");

examples/desktop/3-capture_callback_c.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ bool frame_callback(const CcapVideoFrame* frame, void* userData) {
4444
// Save frame to directory
4545
char outputPath[2048];
4646
int result = ccap_dump_frame_to_directory(frame, context->captureDir, outputPath, sizeof(outputPath));
47-
if (result > 0) {
47+
if (result >= 0) {
4848
printf("VideoFrame saved to: %s\n", outputPath);
4949
context->framesSaved++;
5050
} else {

examples/desktop/utils/helper.cpp

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,26 +14,33 @@ extern "C" {
1414

1515
// Create directory if not exists
1616
void createDirectory(const char* path) {
17-
try {
18-
std::filesystem::create_directories(path);
19-
} catch (const std::exception& e) {
20-
fprintf(stderr, "Failed to create directory %s: %s\n", path, e.what());
17+
std::error_code ec;
18+
std::filesystem::create_directories(path, ec);
19+
if (ec) {
20+
fprintf(stderr, "Failed to create directory %s: %s\n", path, ec.message().c_str());
2121
}
2222
}
2323

2424
// Get current working directory (portable)
25-
int getCurrentWorkingDirectory(char* buffer, int size) {
26-
try {
27-
auto cwd = std::filesystem::current_path().string();
28-
if (cwd.size() >= size) {
29-
return -1; // Buffer too small
30-
}
31-
strcpy(buffer, cwd.c_str());
32-
return 0;
33-
} catch (const std::exception& e) {
34-
fprintf(stderr, "Failed to get current working directory: %s\n", e.what());
25+
int getCurrentWorkingDirectory(char* buffer, size_t size) {
26+
if (!buffer || size == 0) {
27+
return -1;
28+
}
29+
30+
std::error_code ec;
31+
auto cwd = std::filesystem::current_path(ec);
32+
if (ec) {
33+
fprintf(stderr, "Failed to get current working directory: %s\n", ec.message().c_str());
3534
return -1;
3635
}
36+
37+
std::string cwd_str = cwd.string();
38+
if (cwd_str.size() >= size) {
39+
return -1; // Buffer too small
40+
}
41+
strncpy(buffer, cwd_str.c_str(), size - 1);
42+
buffer[size - 1] = '\0';
43+
return 0;
3744
}
3845

3946
int selectCamera(CcapProvider* provider) {

examples/desktop/utils/helper.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
*/
88
#pragma once
99

10+
#include <stddef.h> // for size_t
11+
1012
#ifdef __cplusplus
1113
extern "C" {
1214
#endif
@@ -21,7 +23,7 @@ int selectCamera(CcapProvider* provider);
2123
void createDirectory(const char* path);
2224
// Get current working directory (portable)
2325
// Returns 0 on success, -1 on failure
24-
int getCurrentWorkingDirectory(char* buffer, int size);
26+
int getCurrentWorkingDirectory(char* buffer, size_t size);
2527

2628
#ifdef __cplusplus
2729
} // extern "C"

src/ccap_c.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ bool ccap_provider_find_device_names_list(CcapProvider* provider, CcapDeviceName
103103

104104
// Initialize structure
105105
memset(deviceList, 0, sizeof(CcapDeviceNamesList));
106-
106+
107107
deviceList->deviceCount = devices.size();
108108
if (deviceList->deviceCount > CCAP_MAX_DEVICES) {
109109
deviceList->deviceCount = CCAP_MAX_DEVICES;

src/ccap_imp_linux.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,12 +99,12 @@ class ProviderV4L2 : public ProviderImp {
9999
bool m_isStreaming = false;
100100

101101
// V4L2 device capabilities
102-
struct v4l2_capability m_caps{};
102+
struct v4l2_capability m_caps {};
103103
std::vector<V4L2Format> m_supportedFormats;
104104
std::vector<DeviceInfo::Resolution> m_supportedResolutions;
105105

106106
// Current format
107-
struct v4l2_format m_currentFormat{};
107+
struct v4l2_format m_currentFormat {};
108108

109109
// Buffer management
110110
std::vector<V4L2Buffer> m_buffers;

0 commit comments

Comments
 (0)