首页 > 后端开发 > C++ > 正文

chatGPT - C 编程 Linux Windows 跨平台 - 代码审查请求

Barbara Streisand
发布: 2024-11-09 20:35:03
原创
812 人浏览过

chatGPT - C programming Linux Windows cross-platform - code review request

我一直在尝试使用 chatGPT 提示在 C 中进行编程。

使用增量提示可以让 chatGPT 3.5 做比预期更多的事情。

我想知道代码质量,并且希望获得有关提示和代码的反馈。

功能目标

  • 验证路径是否有效并解析路径组件
  • MacOS Linux Windows 跨平台兼容性

执行

线程安全代码
静态路径
较大路径字符串的动态路径分配

Linux / MacOs 的标准做法是动态分配(4095 / 1024 图表路径限制),而 Windows(260 路径限制)是静态分配。

迅速的

我逐步构建查询,否则 chatGPT 往往会忘记以前的请求更改。

最后,这看起来像是某种原始形式的元编码,可以强制 LLM 生成所需的代码。

_query
    LLM perplexity.ai

Modify code example
  Portable Path Parsing and Validation Code in C
    use thread safe function
      use function pointer
        strtok_s() for Windows
        strtok_r() for linux
allocate INITIAL_BUFFER_SIZE 60 char static path_copy
  declare pointer pfinal_path_copy initialize to path_copy
      when necessary
          dynamically allocate final_path_copy
          pfinal_path_copy = final_path_copy
use single strcpy(pfinal_path_copy, path) function
登录后复制

代码一次成形一个功能以匹配所需的风格

modify example 
replace 60 by equate
登录后复制

一些提示仍然粘着而没有重复。早期添加了“no WSL cygwin”以提示输入 Windows 操作系统特定代码,然后被删除。

write code example
 parse path in portable way
    linux windows MacOS
        no WSL cygwin
登录后复制

代码

生成的代码会在第一次编译并运行。


#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#ifdef _WIN32
#include <io.h>  // For _access() on Windows
#include <windows.h> // For Windows API
#else
#include <unistd.h>  // For access() on Unix-like systems (Linux, macOS)
#include <sys/stat.h>
#endif

// Define a constant for the initial buffer size
#define INITIAL_BUFFER_SIZE 60

// Function to check if a path is valid
int path_validate(const char *path) {
#ifdef _WIN32
    return _access(path, 0) == 0; // 0 means the file exists
#else
    struct stat buffer;
    return (stat(path, &buffer) == 0); // Returns 1 if valid, 0 if invalid
#endif
}

// Type definition for tokenization function pointer
typedef char* (*tokenizer_func)(char*, const char*, char**);

// Function to parse the path into components
void parse_path(const char *path) {
    char path_copy[INITIAL_BUFFER_SIZE]; // Static buffer for initial use
    char *final_path_copy = NULL; // Pointer for dynamic allocation
    char *pfinal_path_copy = path_copy; // Pointer to point to either path_copy or final_path_copy

    // Check if the input path length exceeds INITIAL_BUFFER_SIZE
    if (strlen(path) + 1 > INITIAL_BUFFER_SIZE) {
        final_path_copy = malloc(strlen(path) + 1); // Dynamically allocate memory
        if (final_path_copy == NULL) {
            fprintf(stderr, "Memory allocation failed.\n");
            exit(EXIT_FAILURE);
        }
        pfinal_path_copy = final_path_copy; // Point to dynamically allocated memory
    }

    // Use a single strcpy function to copy the path into the appropriate buffer
    strcpy(pfinal_path_copy, path); // Copy full path into the selected buffer

    // Determine the separator based on the platform and assign the tokenizer function
    const char *separator;
    tokenizer_func tokenizer;
    char *saveptr;

#ifdef _WIN32
    separator = "\";
    tokenizer = strtok_s; // Use strtok_s for Windows
#else
    separator = "/";
    tokenizer = strtok_r; // Use strtok_r for Linux
#endif

    // Tokenize using the assigned tokenizer function
    char *token = tokenizer(pfinal_path_copy, separator, &saveptr);

    while (token != NULL) {
        printf("Path component: %s\n", token);
        token = tokenizer(NULL, separator, &saveptr); // Continue tokenizing
    }

    free(final_path_copy); // Free dynamically allocated memory after use (if allocated)
}

int main(int argc, char *argv[]) {
    if (argc < 2) {
        fprintf(stderr, "Usage: %s <path>\n", argv[0]);
        return EXIT_FAILURE;
    }

    const char *path = argv[1];

    // Validate the path using the new function name
    if (path_validate(path)) {
        printf("The path '%s' is valid.\n", path);
        printf("Parsing components:\n");
        parse_path(path);
    } else {
        printf("The path '%s' is invalid.\n", path);
    }

    return EXIT_SUCCESS;
}

## Unit test

Was created but not included here

## Performance

The entire exercise took 2:30 hour.
I have not tested the code on Windows or MacOS.
I'd like some feedback on code quality.
登录后复制

以上是chatGPT - C 编程 Linux Windows 跨平台 - 代码审查请求的详细内容。更多信息请关注PHP中文网其他相关文章!

来源:dev.to
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
作者最新文章
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板