/// <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 <Pointer <T> > InitTwoDimensionalArrayPointer <T>(int x, int y) { Pointer <Pointer <T> > returnVal = PointerHelpers.Malloc <Pointer <T> >(x); for (int c = 0; c < x; c++) { returnVal[c] = PointerHelpers.Malloc <T>(y); } return(returnVal); }
public DowncastingMemoryBlockAccess(MemoryBlock <T> array) { _array = array; if (!PointerHelpers.IsPrimitiveIntegerType(typeof(T))) { throw new ArgumentException("DowncastingMemoryBlockAccess can only cast to primitive integer types"); } _elemSize = PointerHelpers.GetElementSize(typeof(T)); }
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>()); }
public UpcastingMemoryBlockAccess(MemoryBlock <byte> array, int absoluteByteOffset = 0) { _array = array; _byteOffset = absoluteByteOffset; if (!PointerHelpers.IsPrimitiveIntegerType(typeof(T))) { throw new ArgumentException("UpcastingMemoryBlockAccess can only cast to primitive integer types"); } _elemSize = PointerHelpers.GetElementSize(typeof(T)); }
/// <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++; } }
public Pointer(IMemoryBlockAccess <T> memoryAccess, int absoluteOffsetElements) { _memory = memoryAccess; _elementsOffset = absoluteOffsetElements; _elementSize = PointerHelpers.GetElementSize(typeof(T)); }
public Pointer(T[] buffer, int absoluteOffsetElements) { _memory = new BasicMemoryBlockAccess <T>(new MemoryBlock <T>(buffer)); _elementsOffset = absoluteOffsetElements; _elementSize = PointerHelpers.GetElementSize(typeof(T)); }
public Pointer <E> ReinterpretCast <E>() where E : struct { Type currentType = typeof(T); Type targetType = typeof(E); if (!PointerHelpers.IsPrimitiveIntegerType(currentType)) { throw new InvalidOperationException("Cannot cast a pointer from a non-primitive type"); } if (!PointerHelpers.IsPrimitiveIntegerType(targetType)) { throw new InvalidOperationException("Cannot cast a pointer to a non-primitive type"); } if (currentType == targetType) { return((Pointer <E>)(object) this); } if (!(this._memory is BasicMemoryBlockAccess <T>)) { throw new InvalidOperationException("Cannot cast a pointer that has already been cast from another type"); } // Upcasting - accessing a byte array as a sequence of wide integers if (currentType == typeof(byte) && (targetType == typeof(sbyte) || targetType == typeof(short) || targetType == typeof(ushort) || targetType == typeof(int) || targetType == typeof(uint) || targetType == typeof(long) || targetType == typeof(ulong) || targetType == typeof(float) || targetType == typeof(double))) { MemoryBlock <byte> block = (this._memory as BasicMemoryBlockAccess <byte>).Block; int targetElemSize = PointerHelpers.GetElementSize(targetType); return(new Pointer <E>(new UpcastingMemoryBlockAccess <E>(block, _elementsOffset % targetElemSize), _elementsOffset / targetElemSize)); } // Downcasting - accessing an integer array as a sequence of bytes if (targetType == typeof(byte) && (currentType == typeof(sbyte) || currentType == typeof(short) || currentType == typeof(ushort) || currentType == typeof(int) || currentType == typeof(uint) || currentType == typeof(long) || currentType == typeof(ulong) || currentType == typeof(float) || currentType == typeof(double))) { throw new NotImplementedException("Cannot cast a pointer in this way"); //MemoryBlock<T> block = (this._memory as BasicMemoryBlockAccess<T>).Block; //int sourceElemSize = PointerHelpers.GetElementSize(currentType); //return (Pointer<E>)(object)(new Pointer<byte>(new DowncastingMemoryBlockAccess<T>(block), _elementsOffset * sourceElemSize)); } throw new InvalidOperationException("Cannot cast a pointer from " + currentType.ToString() + " to " + targetType.ToString()); }
private int _elementSize; // Size in bytes of each array element (set to 1 for non-primitive types) #endregion #region Constructors public Pointer(int capacity) { _memory = new BasicMemoryBlockAccess <T>(new MemoryBlock <T>(capacity)); _elementsOffset = 0; _elementSize = PointerHelpers.GetElementSize(typeof(T)); }
public static void Main(string[] args) { string rootDir = "C:\\Code\\Durandal"; string modelDir = rootDir + "\\Data\\sphinx\\en-us-semi"; string dictFile = rootDir + "\\Data\\sphinx\\cmudict_SPHINX_40.txt"; trigger_adapter trigger = psphinx_trigger.trigger_create(modelDir, dictFile, true); Pointer <byte> configuration1 = cstring.ToCString("ACTIVATE/3.16227766016838e-13/\nEXECUTE COURSE/3.16227766016838e-13/\n"); psphinx_trigger.trigger_reconfigure(trigger, configuration1); // Read input file 1 byte[] file_bytes = System.IO.File.ReadAllBytes(rootDir + "\\Extensions\\Pocketsphinx\\Test1.raw"); int samples = file_bytes.Length / 2; short[] file = new short[samples]; Pointer <short> input_file_ptr = new Pointer <short>(new UpcastingMemoryBlockAccess <short>(new MemoryBlock <byte>(file_bytes)), 0); input_file_ptr.MemCopyTo(file, 0, samples); input_file_ptr = new Pointer <short>(file); // Send it to the trigger in chunks Pointer <byte> hyp = PointerHelpers.Malloc <byte>(512); Pointer <byte> lasthyp = PointerHelpers.Malloc <byte>(512); psphinx_trigger.trigger_start_processing(trigger); for (int cursor = 0; cursor < (samples - 159); cursor += 160) { psphinx_trigger.trigger_process_samples(trigger, input_file_ptr + cursor, 160); psphinx_trigger.trigger_get_last_hyp(trigger, hyp); if (cstring.strlen(hyp) != 0 && cstring.strcmp(hyp, lasthyp) != 0) { Console.Write("Got trigger {0} at sample number {1}\n", cstring.FromCString(hyp), cursor); cstring.strncpy(lasthyp, hyp, 512); } } psphinx_trigger.trigger_stop_processing(trigger); Console.Write("\n\nON TO TEST #2\n\n\n"); Pointer <byte> configuration2 = cstring.ToCString("COMPUTER/3.16227766016838e-13/\n"); psphinx_trigger.trigger_reconfigure(trigger, configuration2); // Read input file 2 file_bytes = System.IO.File.ReadAllBytes(rootDir + "\\Extensions\\Pocketsphinx\\Test2.raw"); samples = file_bytes.Length / 2; file = new short[samples]; input_file_ptr = new Pointer <short>(new UpcastingMemoryBlockAccess <short>(new MemoryBlock <byte>(file_bytes)), 0); input_file_ptr.MemCopyTo(file, 0, samples); input_file_ptr = new Pointer <short>(file); // Send it to the trigger in chunks hyp = PointerHelpers.Malloc <byte>(512); lasthyp = PointerHelpers.Malloc <byte>(512); psphinx_trigger.trigger_start_processing(trigger); for (int cursor = 0; cursor < (samples - 159); cursor += 160) { psphinx_trigger.trigger_process_samples(trigger, input_file_ptr + cursor, 160); psphinx_trigger.trigger_get_last_hyp(trigger, hyp); if (cstring.strlen(hyp) != 0 && cstring.strcmp(hyp, lasthyp) != 0) { Console.Write("Got trigger {0} at sample number {1}\n", cstring.FromCString(hyp), cursor); cstring.strncpy(lasthyp, hyp, 512); } } psphinx_trigger.trigger_stop_processing(trigger); psphinx_trigger.trigger_free(trigger); }