0

I am trying to write my own operating system. I have followed the tutorials on the OSDev Wiki, and I am now working on writing a console mode, with commands. I need to be able to split a char* into a char**, without all the library functionality (hence freestanding). I have tried iterating through until I meet my delimiter etc, but however I do it, I just get garbage stuck on the end of my first result. What am I doing wrong? This is what I have so far:

static char** splitStr (char* string, char delim) {

    char returner[VGA_WIDTH][255];
    int loc = 0;
    int innerLoc = 0;
    for (int i = 0; string[i] != 0x00; i++) {
        char c = string[i];
        if (c != delim) {
            returner[loc][innerLoc] = c;
            innerLoc++;
        } else {
            print ("a string was ");
            println (returner[loc]);
            innerLoc = 0;
            loc++;
        }
    }
    print ("the first string was ");
    println (returner[0]);
    return (char**)returner;
}

I am asking a question about how to write a specific function in C++ freestanding mode.

Ron
  • 14,674
  • 4
  • 34
  • 47
JkyCosten
  • 23
  • 10

1 Answers1

1
void split(const char* str, const char d, char** into)
{
    if(str != NULL && into != NULL)
    {
        int n = 0;
        int c = 0;
        for(int i = 0; str[c] != '\0'; i++,c++)
        {
            into[n][i] = str[c];
            if(str[c] == d)
            {
                into[n][i] = '\0';
                i = -1;
                ++n;
            }
        }
    }
}

I'm allocating using calloc to get rid of garbage characters.

EDIT: You should allocate the pointers inside the char** before writing to them.

void allocarr(char** pointers, int bytes, int slots)
{
    int i = 0;
    while(i <= slots)
    {
        pointers[i] = (char*)calloc(1, bytes);
        ++i;
    }
}

...

char** sa = (char**)malloc(50*sizeof(char*));
allocarr(sa, 512, 50);
split("Hello;World;", ';', sa);
puts(sa[0]);
Beyondo
  • 2,952
  • 1
  • 17
  • 42
  • Thank you so much for the super-quick replies. I will test this code momentarily. – JkyCosten Mar 15 '18 at 10:14
  • @JavaxCosten You're welcome, this is a C function, not C++ but it works for both, Operating systems are usually written in C by the way. – Beyondo Mar 15 '18 at 10:20
  • I know, but I find C++ much pleasanter (nicer syntax, more object orientated). – JkyCosten Mar 15 '18 at 11:02
  • Just test compiled, I'm getting the following errors:`'calloc' was not declared in this scope` and `'malloc' was not declared in this scope` – JkyCosten Mar 15 '18 at 17:55
  • *freestanding* C++ doesn't provide this functions in ``, since you're building a kernel, therefore you **should've** done that layer already (implemented them) before getting into implement string functions, otherwise, the code works perfectly in whatever C or C++ compiler, you're still able to use `char []` instead of `char*` anytime and replace the above code with it so you don't need to allocate memory at **run-time** and cast `calloc`/`malloc`, also see [this answer](https://stackoverflow.com/a/7007514/8524922) if you've got a different problem. – Beyondo Mar 15 '18 at 19:47
  • 1
    Can I use the `new` operator? – JkyCosten Mar 16 '18 at 07:29
  • Good question, but even the `new`/`delete` operators depends on `malloc`/`free`, see [OSDev Wiki](https://wiki.osdev.org/C%2B%2B#The_Operators_.27new.27_and_.27delete.27) for more information about how to implement memory management functions and for sure operators. – Beyondo Mar 16 '18 at 12:59
  • Okay thanks. I’m gonna mark this as solved, because the essential problem is solved. Thanks for all the help. – JkyCosten Mar 16 '18 at 14:47