public virtual void SearchAndExtractSubString(MHObjectRef success, MHSequence <MHParameter> args, MHEngine engine) { if (args.Size == 5) { // Find a substring within a string and return an index to the position // and the prefix to the substring. MHOctetString str = new MHOctetString(); MHOctetString searchString = new MHOctetString(); GetString(args.GetAt(0), str, engine); int nStart = GetInt(args.GetAt(1), engine); if (nStart < 1) { nStart = 1; } GetString(args.GetAt(2), searchString, engine); // Strings are indexed from one. int nPos; for (nPos = nStart - 1; nPos <= str.Size - searchString.Size; nPos++) { int i; for (i = 0; i < searchString.Size; i++) { if (searchString.GetAt(i) != str.GetAt(i + nPos)) { break; // Doesn't match } } if (i == searchString.Size) { break; // Found a match. } } // Set the results. MHParameter pResString = args.GetAt(3); MHParameter pResInt = args.GetAt(4); SetSuccessFlag(success, true, engine); // Set this first. if (nPos <= str.Size - searchString.Size) { // Found // Set the index to the position AFTER the string, counting from 1. engine.FindObject(pResInt.GetReference()).SetVariableValue(new MHUnion(nPos + 1 + searchString.Size)); // Return the sequence from nStart - 1 of length nPos - nStart + 1 MHOctetString resultString = new MHOctetString(str, nStart - 1, nPos - nStart + 1); engine.FindObject(pResString.GetReference()).SetVariableValue(new MHUnion(resultString)); } else { // Not found. Set the result string to empty and the result index to -1 engine.FindObject(pResInt.GetReference()).SetVariableValue(new MHUnion(-1)); engine.FindObject(pResString.GetReference()).SetVariableValue(new MHUnion(new MHOctetString(""))); } } else { SetSuccessFlag(success, false, engine); } }
public virtual void SearchSubString(MHObjectRef success, MHSequence <MHParameter> args, MHEngine engine) { if (args.Size == 4) { // Find a substring within a string and return an index to the position. MHOctetString str = new MHOctetString(); MHOctetString searchString = new MHOctetString(); GetString(args.GetAt(0), str, engine); int nStart = GetInt(args.GetAt(1), engine); if (nStart < 1) { nStart = 1; } GetString(args.GetAt(2), searchString, engine); // Strings are indexed from one. int nPos; for (nPos = nStart - 1; nPos <= str.Size - searchString.Size; nPos++) { int i; for (i = 0; i < searchString.Size; i++) { if (searchString.GetAt(i) != str.GetAt(i + nPos)) { break; } } if (i == searchString.Size) { break; // Found a match. } } // Set the result. MHParameter pResInt = args.GetAt(3); SetSuccessFlag(success, true, engine); // Set this first. if (nPos <= str.Size - searchString.Size) { // Found // Set the index to the position of the string, counting from 1. engine.FindObject(pResInt.GetReference()).SetVariableValue(new MHUnion(nPos + 1)); } else { // Not found. Set the result index to -1 engine.FindObject(pResInt.GetReference()).SetVariableValue(new MHUnion(-1)); } } else { SetSuccessFlag(success, false, engine); } }
public virtual void Random(MHObjectRef success, MHSequence <MHParameter> args, MHEngine engine) { if (args.Size == 2) { int nLimit = GetInt(args.GetAt(0), engine); MHParameter pResInt = args.GetAt(1); Random randomGenerator = new Random(); engine.FindObject((pResInt.GetReference())).SetVariableValue(new MHUnion(randomGenerator.Next(nLimit) + 1)); SetSuccessFlag(success, true, engine); } else { SetSuccessFlag(success, false, engine); } }
public virtual void GetStringLength(MHObjectRef success, MHSequence <MHParameter> args, MHEngine engine) { if (args.Size == 2) { // Find a substring within a string and return an index to the position. MHOctetString str = new MHOctetString(); GetString(args.GetAt(0), str, engine); MHParameter pResInt = args.GetAt(1); SetSuccessFlag(success, true, engine); engine.FindObject(pResInt.GetReference()).SetVariableValue(new MHUnion(str.Size)); } else { SetSuccessFlag(success, false, engine); } }
public virtual void SI_GetServiceIndex(MHObjectRef success, MHSequence <MHParameter> args, MHEngine engine) { // Returns an index indicating the service if (args.Size == 2) { MHOctetString str = new MHOctetString(); GetString(args.GetAt(0), str, engine); MHParameter pResInt = args.GetAt(1); // The format of the service is dvb://netID.[transPortID].serviceID // where the IDs are in hex. // or rec://svc/lcn/N where N is the "logical channel number" i.e. the Freeview channel. int nResult = engine.GetContext().GetChannelIndex(str.ToString()); engine.FindObject(pResInt.GetReference()).SetVariableValue(new MHUnion(nResult)); Logging.Log(Logging.MHLogDetail, "Get service index for " + str.Printable() + " - result " + nResult); SetSuccessFlag(success, true, engine); } else { SetSuccessFlag(success, false, engine); } }
public virtual void GetSubString(MHObjectRef success, MHSequence <MHParameter> args, MHEngine engine) { if (args.Size == 4) { // Extract a sub-string from a string. MHOctetString str = new MHOctetString(); GetString(args.GetAt(0), str, engine); int nBeginExtract = GetInt(args.GetAt(1), engine); int nEndExtract = GetInt(args.GetAt(2), engine); if (nBeginExtract < 1) { nBeginExtract = 1; } if (nBeginExtract > str.Size) { nBeginExtract = str.Size; } if (nEndExtract < 1) { nEndExtract = 1; } if (nEndExtract > str.Size) { nEndExtract = str.Size; } MHParameter pResString = args.GetAt(3); // Returns beginExtract to endExtract inclusive. engine.FindObject(pResString.GetReference()).SetVariableValue( new MHUnion(new MHOctetString(str, nBeginExtract - 1, nEndExtract - nBeginExtract + 1))); SetSuccessFlag(success, true, engine); } else { SetSuccessFlag(success, false, engine); } }
public virtual void FormatDate(MHObjectRef success, MHSequence <MHParameter> args, MHEngine engine) { if (args.Size == 4) { // This is a bit like strftime but not quite. MHOctetString format = new MHOctetString(); GetString(args.GetAt(0), format, engine); int date = GetInt(args.GetAt(1), engine); // As produced in GCD int time = GetInt(args.GetAt(2), engine); string result = ""; DateTime dt = new DateTime(1858, 11, 17); dt = dt.AddDays(date).AddSeconds(time); for (int i = 0; i < format.Size; i++) { char ch = format.GetAt(i); string part; // Largest text is 4 chars for a year + null terminator if (ch == '%') { i++; if (i == format.Size) { break; } ch = format.GetAt(i); switch (ch) { case 'Y': part = dt.ToString("yyyy"); break; case 'y': part = dt.ToString("yy"); break; case 'X': part = dt.ToString("MM"); break; case 'x': part = dt.ToString("%M"); break; case 'D': part = dt.ToString("dd"); break; case 'd': part = dt.ToString("%d"); break; case 'H': part = dt.ToString("HH"); break; case 'h': part = dt.ToString("%H"); break; case 'I': part = dt.ToString("hh"); break; case 'i': part = dt.ToString("%h"); break; case 'M': part = dt.ToString("mm"); break; case 'm': part = dt.ToString("%m"); break; case 'S': part = dt.ToString("ss"); break; case 's': part = dt.ToString("%s"); break; // TODO: These really should be localised. case 'A': part = dt.ToString("tt"); break; case 'a': part = dt.ToString("tt").ToLower(); break; default: part = ""; break; } result += part; } else { result += ch; } } MHOctetString theResult = new MHOctetString(result); MHParameter pResString = args.GetAt(3); engine.FindObject(pResString.GetReference()).SetVariableValue(new MHUnion(theResult)); SetSuccessFlag(success, true, engine); } else { SetSuccessFlag(success, false, engine); } }