/// <summary> /// Returns a pointer to the first occurrence of str2 in str1, or a null pointer if str2 is not part of str1. /// The matching process does not include the terminating null-characters, but it stops there. /// </summary> /// <param name="str1">The string to look within</param> /// <param name="str2">The string to search for</param> /// <returns>A pointer to the first occurrence in str1 of the entire sequence of characters specified in str2, or a null pointer if the sequence is not present in str1.</returns> public static Pointer <byte> strstr(Pointer <byte> str1, Pointer <byte> str2) { uint len1 = strlen(str1); uint len2 = strlen(str2); if (len1 < len2) { return(PointerHelpers.NULL <byte>()); } // Iterate through all potential starting points of the substring for (int c = 0; c <= len1 - len2; c++) { int matchLen = 0; for (matchLen = 0; matchLen < len2; matchLen++) { if (str1[c + matchLen] != str2[matchLen]) { break; } } if (matchLen == len2) { return(str1.Point(c)); } } return(PointerHelpers.NULL <byte>()); }
/// <summary> /// Reads characters from stream and stores them as a C string into str until (num-1) characters have been read or either a newline or the end-of-file is reached, whichever happens first. /// A newline character makes fgets stop reading, but it is considered a valid character by the function and included in the string copied to str. /// A terminating null character is automatically appended after the characters copied to str. /// Notice that fgets is quite different from gets: not only fgets accepts a stream argument, but also allows to specify the maximum size of str and includes in the string any ending newline character. /// </summary> /// <param name="str">Pointer to an array of chars where the string read is copied.</param> /// <param name="num">Maximum number of characters to be copied into str (including the terminating null-character).</param> /// <returns>On success, the function returns str. /// If the end-of-file is encountered while attempting to read a character, the eof indicator is set(feof). If this happens before any characters could be read, the pointer returned is a null pointer (and the contents of str remain unchanged). /// If a read error occurs, the error indicator (ferror) is set and a null pointer is also returned (but the contents pointed by str may have changed).</returns> public Pointer <byte> fgets(Pointer <byte> str, int num) { // FIXME NOT TESTED int charsRead = 0; while (charsRead < num - 1) { int c = fgetc(); if (c == EOF) { if (charsRead == 0) { return(PointerHelpers.NULL <byte>()); } str[charsRead] = 0; return(str); } str[charsRead++] = (byte)c; if (c == '\n') { str[charsRead] = 0; return(str); } } str[charsRead] = 0; return(str); }
public static Pointer <byte> strrchr(Pointer <byte> str, byte character) { for (int c = (int)(strlen(str) - 1); c >= 0; c--) { if (str[c] == character) { return(str.Point(c)); } } return(PointerHelpers.NULL <byte>()); }
/// <summary> /// A sequence of calls to this function split str into tokens, which are sequences of contiguous characters /// separated by any of the characters that are part of delimiters. /// /// On a first call, the function expects a C string as argument for str, whose first character is used as the /// starting location to scan for tokens. In subsequent calls, the function expects a null pointer and uses the /// position right after the end of the last token as the new starting location for scanning. /// /// To determine the beginning and the end of a token, the function first scans from the starting location for /// the first character not contained in delimiters(which becomes the beginning of the token). And then scans /// starting from this beginning of the token for the first character contained in delimiters, which becomes the end of the token. /// The scan also stops if the terminating null character is found. /// /// This end of the token is automatically replaced by a null-character, and the beginning of the token is returned by the function. /// Once the terminating null character of str is found in a call to strtok, all subsequent calls to this /// function (with a null pointer as the first argument) return a null pointer. /// /// The point where the last token was found is kept internally by the function to be used on the next call /// (particular library implementations are not required to avoid data races). /// </summary> /// <param name="str">C string to truncate. /// Notice that this string is modified by being broken into smaller strings(tokens). /// Alternativelly, a null pointer may be specified, in which case the function /// continues scanning where a previous successful call to the function ended.</param> /// <param name="delimiters"></param> /// <returns>C string containing the delimiter characters. /// These can be different from one call to another.</returns> public static Pointer <byte> strtok(Pointer <byte> str, Pointer <byte> delimiters) { if (str.IsNull) { str = _last_strtok; if (str.IsNull) { // No more tokens to find return(PointerHelpers.NULL <byte>()); } } // Find the start position int start; int end; for (start = 0; IsInSet(str[start], delimiters) && str[start] != 0; start++) { } // Reached end of string while looking for start if (str[start] == 0) { _last_strtok = PointerHelpers.NULL <byte>(); } // Now look for the end of the token for (end = start + 1; !IsInSet(str[end], delimiters) && str[end] != 0; end++) { } // Reached end of string if (str[end] == 0) { _last_strtok = PointerHelpers.NULL <byte>(); } else { // Insert null token at the end str[end] = 0; // Set state for next search _last_strtok = str.Point(end + 1); } return(str.Point(start)); }
public Pointer <T> Realloc(int new_size_in_elements) { if (new_size_in_elements == 0) { Free(); return(PointerHelpers.NULL <T>()); } else if (_memory == null) { return(new Pointer <T>(new_size_in_elements)); } else { _memory.Realloc(new_size_in_elements); return(this); } }
/// <summary> /// Returns a pointer to the first occurrence of character in the C string str. /// The terminating null-character is considered part of the C string. Therefore, it can also be located in order to retrieve a pointer to the end of a string. /// </summary> /// <param name="str">A C string</param> /// <param name="character">Character to be located</param> /// <returns>A pointer to the first occurrence of character in str. ///If the character is not found, the function returns a null pointer.</returns> public static Pointer <byte> strchr(Pointer <byte> str, byte character) { uint c = 0; while (true) { if (str[c] == character) { return(str.Point(c)); } if (str[c] == 0) { return(PointerHelpers.NULL <byte>()); } c++; } }