<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Neo</title>
    <description>The latest articles on DEV Community by Neo (@onearb).</description>
    <link>https://dev.to/onearb</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F2383483%2F2d5d7b2f-d22c-44fe-8abf-3b457e5d0c04.png</url>
      <title>DEV Community: Neo</title>
      <link>https://dev.to/onearb</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/onearb"/>
    <language>en</language>
    <item>
      <title>chatGPT - C programming Linux Windows cross-platform - code review request</title>
      <dc:creator>Neo</dc:creator>
      <pubDate>Fri, 08 Nov 2024 12:32:09 +0000</pubDate>
      <link>https://dev.to/onearb/chatgpt-c-programming-linux-windows-cross-platform-code-review-request-1db2</link>
      <guid>https://dev.to/onearb/chatgpt-c-programming-linux-windows-cross-platform-code-review-request-1db2</guid>
      <description>&lt;p&gt;I have been meddling with using chatGPT prompts to program in C.&lt;/p&gt;

&lt;p&gt;Using incremental prompts it is possible to get chatGPT 3.5 to do a lot more than expected.&lt;/p&gt;

&lt;p&gt;I wonder about code quality and would enjoy feedback both on prompts and code.&lt;/p&gt;

&lt;h2&gt;
  
  
  Function objective
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;verify a path is valid and parse the path components&lt;/li&gt;
&lt;li&gt;MacOS Linux Windows cross-platform compatibility&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Implementation
&lt;/h2&gt;

&lt;p&gt;thread safe code&lt;br&gt;
static path&lt;br&gt;
dynamic path allocation for larger path string&lt;/p&gt;

&lt;p&gt;Standard practice for Linux / MacOs is to dynamically allocate (4095 / 1024 chart path limit), whereas Windows (260 path limit) is to statically allocate.&lt;/p&gt;
&lt;h2&gt;
  
  
  Prompt
&lt;/h2&gt;

&lt;p&gt;I incrementally build up the query, otherwise chatGPT tends to unlearn previous request changes.&lt;/p&gt;

&lt;p&gt;In the end this looks like some crude form of meta coding where it is possible to coerce the LLM to generate the desired code.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;_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
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Code is shaped one feature at a time to match the desired style&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;modify example 
replace 60 by equate
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Some prompts remained sticky without being repeated. "no WSL cygwin" was added early on to prompt for Windows OS specific code, then dropped.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;write code example
 parse path in portable way
    linux windows MacOS
        no WSL cygwin
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Code
&lt;/h2&gt;

&lt;p&gt;Resulting code compiles and runs on the first go.&lt;/p&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

#include &amp;lt;stdio.h&amp;gt;
#include &amp;lt;stdlib.h&amp;gt;
#include &amp;lt;string.h&amp;gt;

#ifdef _WIN32
#include &amp;lt;io.h&amp;gt;  // For _access() on Windows
#include &amp;lt;windows.h&amp;gt; // For Windows API
#else
#include &amp;lt;unistd.h&amp;gt;  // For access() on Unix-like systems (Linux, macOS)
#include &amp;lt;sys/stat.h&amp;gt;
#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, &amp;amp;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 &amp;gt; 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, &amp;amp;saveptr);

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

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

int main(int argc, char *argv[]) {
    if (argc &amp;lt; 2) {
        fprintf(stderr, "Usage: %s &amp;lt;path&amp;gt;\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.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

</description>
      <category>chatgpt</category>
      <category>c</category>
      <category>promptengineering</category>
    </item>
  </channel>
</rss>
