ホームページ >バックエンド開発 >C++ >chatGPT - C プログラミング Linux Windows クロスプラットフォーム - コード レビュー リクエスト

chatGPT - C プログラミング Linux Windows クロスプラットフォーム - コード レビュー リクエスト

Barbara Streisand
Barbara Streisandオリジナル
2024-11-09 20:35:03980ブラウズ

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

C でプログラミングするために chatGPT プロンプトを使用することに手を出してきました。

増分プロンプトを使用すると、chatGPT 3.5 に期待以上のことを実行させることができます。

コードの品質について疑問に思っているので、プロンプトとコードの両方に関するフィードバックを楽しみにしています。

機能の目的

  • パスが有効であることを確認し、パスのコンポーネントを解析します
  • MacOS Linux Windows のクロスプラットフォーム互換性

実装

スレッドセーフコード
静的パス
より大きなパス文字列に対する動的パス割り当て

Linux/MacO の標準的な方法は動的に割り当てること (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

コードは、目的のスタイルに合わせて一度に 1 つの機能を整形します

modify example 
replace 60 by equate

一部のプロンプトは繰り返されずに固定されたままでした。 「no WSL cygwin」は、Windows OS 固有のコードの入力を求めるために初期に追加されましたが、その後削除されました。

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 中国語 Web サイトの他の関連記事を参照してください。

声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。