implementing strings

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
stonedzealot

implementing strings

Post by stonedzealot »

I'll be brief as possible. I've gotten to the stage in the OS where I want to get some human input (through the keyboard) and I'd like to be able to use some strings, but of course strings are non-existent in C w/o libraries. Just wondering if I have to write all sorts of code handling them or if an array of chars will work. Basically: What's the deal with getting strings to work?
gtsphere

Re:implementing strings

Post by gtsphere »

char *string;

thats a basic string that will allow input as follows

string = "whatever";

or you can assign directly from your keyboard handler.

As for all of the string functions, I do believe that most are platform-independent, IE: strcmp(); strlen();. So you might luck out with certain ones.

Hope this helps
-GT
Schol-R-LEA

Re:implementing strings

Post by Schol-R-LEA »

wangpeng wrote: I've gotten to the stage in the OS where I want to get some human input (through the keyboard) and I'd like to be able to use some strings, but of course strings are non-existent in C w/o libraries. Just wondering if I have to write all sorts of code handling them or if an array of chars will work. Basically: What's the deal with getting strings to work?
You were pretty close there already; a C string (as opposed to a Pascal string, or a C++ String, or a x86 assembly string - more on this later) is just a zero-delimited buffer - that is, an array of chars large enough to hold the expected data (hopefully; string buffer overruns are one of the most common bugs in C programming), and in which the end of the actual data is marked by a \00. For example, if you have the following code:

Code: Select all

char string[8], *p;

p = &string;
strcpy(string, "Hello");
then string would hold:
[tt] 0 1 2 3 4 5 6 7[/tt]
[tt]---------------------------------------------------[/tt]
[tt]| 'H' | 'e' | 'l' | 'l' | 'o' | \00 | ?? | ?? |[/tt]
[tt]--------------------------------------------------- [/tt]

Where the double question marks ("??") stand in for undefined values.

I am assuming you already knew that, however. If not, then the answer to your question should be fairly clear now.

As for using them, yes, you'll need to write at least some of the routines to manipulate them yourself; fortunately, most of them can be written in a portable manner - indeed, many are frequently implemented as macros. You can probably just recompile the GNU string library code with only a few changes, though for efficiency's sake you may want to rewrite some of them in assembly, if you think you can out-tweak gcc's optimizer. In any case, most of the standard string tools are fairly easy to implement, and those which aren't are almost certainly portable. For example, a simple implementation of strcmp() might go like this:

Code: Select all

/* Warning: this is just an example; the code shown here */ 
/* hasn't been tested. Use at your own risk */

int strncmp(const char* str1, const char* str2, size_t count)
{
   int i, j;

   for(i = 0,  j = (int) count; 
        str1[i] == str2[i] && NULL != str1[i]  && NULL != str2[i] &&  i  < j;
        i++);

   /* The function is supposed to use a 'lexicographical compare', but this is an acceptable approximation of it */
       
   return ((int) toupper(str1[i]) - (int) toupper(str2[i]));
}
While hardly the best example code, it does show how it can be fairly easily done, if necessary.

I might add that the answer the question you meant to ask, rather than the one you did ask, is: you will have to write the keyboard driver yourself, although you may be able to borrow one from another OS and modify it to suit yours (the "we'll make it fit" school of OS development - though I must admit that it does generally work better for software than for automobile mufflers :-) ) How you will move character data from your keyboard buffer into you applications will depend on your design.

HTH. Comments and corrections welcome.
stonedzealot

Re:implementing strings

Post by stonedzealot »

Aha! Thank you very much


--One thing. With the standard char *string, can you then say:

string = string + "a" ;

or would you have to increment the pointer and then
set that to "a" ala:

*string++;
string = "a"

this is assuming, of course that you don't know the length of the string...

Also, in response to the above "the question I meant to ask" was the question I asked. I understand how you thought otherwise, but that's my bad grammar...I already have a driver running for the kboard... ::)
User avatar
Pype.Clicker
Member
Member
Posts: 5964
Joined: Wed Oct 18, 2006 2:31 am
Location: In a galaxy, far, far away
Contact:

Re:implementing strings

Post by Pype.Clicker »

neither string = string + "a" nor string++; string="a" will work. In C, strings are pointer to arrays of characters. period. No magic operator will help you concatenating / searching / etc. in them: you just have access to usual pointer arithmetics.

So, for instance if you set string="hello"; after you do string++, string will now display "ello", simply because you advanced the pointer by one character.

some library function may help you doing strings manipulations, like strcat(S2,S1) will do S2 <= S2 . S1, provided that S2 is large enough to hold length_of_S2 + length_of_S1 + 1 characters.
stonedzealot

Re:implementing strings

Post by stonedzealot »

I see, so you'd make a function concat that would just fill string3 with contents of strings 1 & 2.

Therefore: a pascal string = string + "a";
would look more like: concat(string, "a", string);
and of course, the length of string would be heightened

I think I understand fully now. Thanks to all three of you for helping me out.
User avatar
Pype.Clicker
Member
Member
Posts: 5964
Joined: Wed Oct 18, 2006 2:31 am
Location: In a galaxy, far, far away
Contact:

Re:implementing strings

Post by Pype.Clicker »

Something like that.

However, it is usually preferable to avoid copying from and to the same address, so your "concat" would be hard to write correctly (because you specified string as both your s1 string and tgt string.

so "tgt = s1 + s2" will rather be implemented by "strcpy(tgt,s1) ; strcat(tgt,s2);"
stonedzealot

Re:implementing strings

Post by stonedzealot »

I was just reading through the rest of the posts, to make sure I had eeked out all of the info that was included in them and came across:
So, for instance if you set string="hello"; after you do string++, string will now display "ello", simply because you advanced the pointer by one character.
Well, I have to disagree. If the pointer is defined as an array of chars, (char *string) and you set the pointer to
equal "hello" (*string = "hello") then string (not the whole pointer) would equal "h" right off the bat, because it can only hold one char at a time. If you incremented the pointer, it would move to the next character, and thus string would equal "e". Am I wrong?
Slasher

Re:implementing strings

Post by Slasher »

That is the character at *string, not the string!
strings end with '\0' therefore C will continue to associate all chars from *string till *string+N until it finds a '\0' at the Nth position. Read the C books carefully!
stonedzealot

Re:implementing strings

Post by stonedzealot »

I understand, that's why you end the string with the terminator (\0). All that I know is that I've gotten past the problem with the description in the above post of mine, and everything's hunky-dory. (can't believe I just said that)
Post Reply