本文整理汇总了C#中StringParser.MoveAndExtractNext方法的典型用法代码示例。如果您正苦于以下问题:C# StringParser.MoveAndExtractNext方法的具体用法?C# StringParser.MoveAndExtractNext怎么用?C# StringParser.MoveAndExtractNext使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类StringParser
的用法示例。
在下文中一共展示了StringParser.MoveAndExtractNext方法的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ParseGatewayAddressesFromRouteFile
// /proc/net/route contains some information about gateway addresses,
// and separates the information about by each interface.
internal static List<GatewayIPAddressInformation> ParseGatewayAddressesFromRouteFile(string filePath, string interfaceName)
{
if (!File.Exists(filePath))
{
throw ExceptionHelper.CreateForInformationUnavailable();
}
List<GatewayIPAddressInformation> collection = new List<GatewayIPAddressInformation>();
// Columns are as follows (first-line header):
// Iface Destination Gateway Flags RefCnt Use Metric Mask MTU Window IRTT
string[] fileLines = File.ReadAllLines(filePath);
foreach (string line in fileLines)
{
if (line.StartsWith(interfaceName))
{
StringParser parser = new StringParser(line, '\t', skipEmpty: true);
parser.MoveNext();
parser.MoveNextOrFail();
string gatewayIPHex = parser.MoveAndExtractNext();
long addressValue = Convert.ToInt64(gatewayIPHex, 16);
IPAddress address = new IPAddress(addressValue);
collection.Add(new SimpleGatewayIPAddressInformation(address));
}
}
return collection;
}
示例2: TestParserWithNoEndingSeparatorWithSkipEmpty
public void TestParserWithNoEndingSeparatorWithSkipEmpty()
{
string buffer = ",,,Str|ing,,123";
char separator = ',';
StringParser sp = new StringParser(buffer, separator, true);
Assert.Equal("Str|ing", sp.MoveAndExtractNext());
Assert.Equal(123, sp.ParseNextInt32());
}
示例3: TestParserWithNoEndingSeparatorWithNoSkipEmpty
public void TestParserWithNoEndingSeparatorWithNoSkipEmpty()
{
string buffer = ",,Str|ing,,123";
char separator = ',';
StringParser sp = new StringParser(buffer, separator, false);
sp.MoveNextOrFail();
sp.MoveNextOrFail();
Assert.Equal("Str|ing", sp.MoveAndExtractNext());
Assert.Throws<InvalidDataException>(() => sp.ParseNextInt32());
Assert.Equal(123, sp.ParseNextInt32());
}
示例4: TestParserMoveWithSkipEmpty
public void TestParserMoveWithSkipEmpty()
{
string buffer = "My,, ,Te,,st,Str|ing,,123,,";
char separator = ',';
StringParser sp = new StringParser(buffer, separator, true);
sp.MoveNextOrFail();
Assert.Equal("My", sp.ExtractCurrent());
Assert.Equal(' ', sp.ParseNextChar());
sp.MoveNextOrFail();
Assert.Equal("Te", sp.ExtractCurrent());
Assert.Equal("st", sp.MoveAndExtractNext());
sp.MoveNextOrFail();
Assert.Equal("Str|ing", sp.ExtractCurrent());
Assert.Equal(123, sp.ParseNextInt32());
Assert.Throws<InvalidDataException>(() => sp.MoveNextOrFail());
sp.MoveNext();
Assert.Equal("", sp.ExtractCurrent());
}
示例5: CreateIPv4Statistics
/// <summary>
/// Constructs an IPGlobalStatistics object from the snmp4 stats file.
/// </summary>
public static LinuxIPGlobalStatistics CreateIPv4Statistics()
{
LinuxIPGlobalStatistics stats = new LinuxIPGlobalStatistics();
string fileContents = File.ReadAllText(NetworkFiles.SnmpV4StatsFile);
int firstIpHeader = fileContents.IndexOf("Ip:");
int secondIpHeader = fileContents.IndexOf("Ip:", firstIpHeader + 1);
int endOfSecondLine = fileContents.IndexOf(Environment.NewLine, secondIpHeader);
string ipData = fileContents.Substring(secondIpHeader, endOfSecondLine - secondIpHeader);
StringParser parser = new StringParser(ipData, ' ');
// NOTE: Need to verify that this order is consistent. Otherwise, we need to parse the first-line header
// to determine the order of information contained in the file.
parser.MoveNextOrFail(); // Skip Ip:
// According to RFC 1213, "1" indicates "acting as a gateway". "2" indicates "NOT acting as a gateway".
stats._forwarding = parser.MoveAndExtractNext() == "1";
stats._defaultTtl = parser.ParseNextInt32();
stats._inReceives = parser.ParseNextInt32();
stats._inHeaderErrors = parser.ParseNextInt32();
stats._inAddressErrors = parser.ParseNextInt32();
stats._forwardedDatagrams = parser.ParseNextInt32();
stats._inUnknownProtocols = parser.ParseNextInt32();
stats._inDiscards = parser.ParseNextInt32();
stats._inDelivers = parser.ParseNextInt32();
stats._outRequests = parser.ParseNextInt32();
stats._outDiscards = parser.ParseNextInt32();
stats._outNoRoutes = parser.ParseNextInt32();
stats._reassemblyTimeout = parser.ParseNextInt32();
stats._reassemblyRequireds = parser.ParseNextInt32();
stats._reassemblyOKs = parser.ParseNextInt32();
stats._reassemblyFails = parser.ParseNextInt32();
stats._fragmentOKs = parser.ParseNextInt32();
stats._fragmentFails = parser.ParseNextInt32();
stats._fragmentCreates = parser.ParseNextInt32();
string routeFile = ReadProcConfigFile(NetworkFiles.Ipv4RouteFile);
stats._numRoutes = CountOccurences(Environment.NewLine, routeFile) - 1; // File includes one-line header
stats._numInterfaces = GetNumInterfaces();
stats._numIPAddresses = GetNumIPAddresses();
return stats;
}
示例6: ParseRemoteConnectionInformation
// Common parsing logic for the remote connection information
private static void ParseRemoteConnectionInformation(string line, out IPAddress remoteIPAddress, out int remotePort)
{
StringParser parser = new StringParser(line, ' ', true);
parser.MoveNextOrFail(); // skip Index
parser.MoveNextOrFail(); // skip local_address
string remoteAddressAndPort = parser.MoveAndExtractNext();
int indexOfColon = remoteAddressAndPort.IndexOf(':');
if (indexOfColon == -1)
{
throw new InvalidOperationException("Parsing error. No colon in " + remoteAddressAndPort);
}
string remoteAddressString = remoteAddressAndPort.Substring(0, indexOfColon);
remoteIPAddress = ParseHexIPAddress(remoteAddressString);
string portString = remoteAddressAndPort.Substring(indexOfColon + 1, remoteAddressAndPort.Length - (indexOfColon + 1));
if (!int.TryParse(portString, NumberStyles.HexNumber, CultureInfo.InvariantCulture, out remotePort))
{
throw new InvalidOperationException("Couldn't parse remote port number " + portString);
}
}
示例7: ParseTcpConnectionInformationFromLine
// Parsing logic for local and remote addresses and ports, as well as socket state.
internal static TcpConnectionInformation ParseTcpConnectionInformationFromLine(string line)
{
StringParser parser = new StringParser(line, ' ', true);
parser.MoveNextOrFail(); // skip Index
string localAddressAndPort = parser.MoveAndExtractNext(); // local_address
IPAddress localAddress;
int localPort;
ParseAddressAndPort(localAddressAndPort, out localAddress, out localPort);
string remoteAddressAndPort = parser.MoveAndExtractNext(); // rem_address
IPAddress remoteAddress;
int remotePort;
ParseAddressAndPort(remoteAddressAndPort, out remoteAddress, out remotePort);
string socketStateHex = parser.MoveAndExtractNext();
int state;
if (!int.TryParse(socketStateHex, NumberStyles.HexNumber, CultureInfo.InvariantCulture, out state))
{
throw new InvalidOperationException("Invalid state value: " + socketStateHex);
}
TcpState tcpState = MapTcpState((Interop.LinuxTcpState)state);
IPEndPoint localEndPoint = new IPEndPoint(localAddress, localPort);
IPEndPoint remoteEndPoint = new IPEndPoint(remoteAddress, remotePort);
return new SimpleTcpConnectionInformation(localEndPoint, remoteEndPoint, tcpState);
}
示例8: ParseLocalConnectionInformation
// Common parsing logic for the local connection information.
private static IPEndPoint ParseLocalConnectionInformation(string line)
{
StringParser parser = new StringParser(line, ' ', true);
parser.MoveNextOrFail(); // skip Index
string localAddressAndPort = parser.MoveAndExtractNext();
int indexOfColon = localAddressAndPort.IndexOf(':');
if (indexOfColon == -1)
{
throw ExceptionHelper.CreateForParseFailure();
}
string remoteAddressString = localAddressAndPort.Substring(0, indexOfColon);
IPAddress localIPAddress = ParseHexIPAddress(remoteAddressString);
string portString = localAddressAndPort.Substring(indexOfColon + 1, localAddressAndPort.Length - (indexOfColon + 1));
int localPort;
if (!int.TryParse(portString, NumberStyles.HexNumber, CultureInfo.InvariantCulture, out localPort))
{
throw ExceptionHelper.CreateForParseFailure();
}
return new IPEndPoint(localIPAddress, localPort);
}
示例9: ParseTcpConnectionInformationFromLine
// Parsing logic for local and remote addresses and ports, as well as socket state.
internal static TcpConnectionInformation ParseTcpConnectionInformationFromLine(string line)
{
StringParser parser = new StringParser(line, ' ', skipEmpty: true);
parser.MoveNextOrFail(); // skip Index
string localAddressAndPort = parser.MoveAndExtractNext(); // local_address
IPEndPoint localEndPoint = ParseAddressAndPort(localAddressAndPort);
string remoteAddressAndPort = parser.MoveAndExtractNext(); // rem_address
IPEndPoint remoteEndPoint = ParseAddressAndPort(remoteAddressAndPort);
string socketStateHex = parser.MoveAndExtractNext();
int nativeTcpState;
if (!int.TryParse(socketStateHex, NumberStyles.HexNumber, CultureInfo.InvariantCulture, out nativeTcpState))
{
throw ExceptionHelper.CreateForParseFailure();
}
TcpState tcpState = MapTcpState(nativeTcpState);
return new SimpleTcpConnectionInformation(localEndPoint, remoteEndPoint, tcpState);
}
示例10: ParseIPv4GlobalStatisticsFromSnmpFile
public static IPGlobalStatisticsTable ParseIPv4GlobalStatisticsFromSnmpFile(string filePath)
{
string fileContents = File.ReadAllText(filePath);
int firstIpHeader = fileContents.IndexOf("Ip:", StringComparison.Ordinal);
int secondIpHeader = fileContents.IndexOf("Ip:", firstIpHeader + 1, StringComparison.Ordinal);
int endOfSecondLine = fileContents.IndexOf(Environment.NewLine, secondIpHeader, StringComparison.Ordinal);
string ipData = fileContents.Substring(secondIpHeader, endOfSecondLine - secondIpHeader);
StringParser parser = new StringParser(ipData, ' ');
parser.MoveNextOrFail(); // Skip Ip:
// According to RFC 1213, "1" indicates "acting as a gateway". "2" indicates "NOT acting as a gateway".
return new IPGlobalStatisticsTable()
{
Forwarding = parser.MoveAndExtractNext() == "1",
DefaultTtl = parser.ParseNextInt32(),
InReceives = parser.ParseNextInt32(),
InHeaderErrors = parser.ParseNextInt32(),
InAddressErrors = parser.ParseNextInt32(),
ForwardedDatagrams = parser.ParseNextInt32(),
InUnknownProtocols = parser.ParseNextInt32(),
InDiscards = parser.ParseNextInt32(),
InDelivers = parser.ParseNextInt32(),
OutRequests = parser.ParseNextInt32(),
OutDiscards = parser.ParseNextInt32(),
OutNoRoutes = parser.ParseNextInt32(),
ReassemblyTimeout = parser.ParseNextInt32(),
ReassemblyRequireds = parser.ParseNextInt32(),
ReassemblyOKs = parser.ParseNextInt32(),
ReassemblyFails = parser.ParseNextInt32(),
FragmentOKs = parser.ParseNextInt32(),
FragmentFails = parser.ParseNextInt32(),
FragmentCreates = parser.ParseNextInt32(),
};
}
示例11: TestExtractingStringFromParentheses
public void TestExtractingStringFromParentheses()
{
string buffer = "This(is a unicode \u0020)something,(89),(haha123blah)After brace,";
char separator = ',';
StringParser sp = new StringParser(buffer, separator);
Assert.Throws<InvalidDataException>(() => sp.MoveAndExtractNextInOuterParens());
Assert.Equal("This(is a unicode \u0020)something", sp.ExtractCurrent());
Assert.Equal("89),(haha123blah", sp.MoveAndExtractNextInOuterParens());
Assert.Equal("(89),(haha123blah)", sp.ExtractCurrent());
Assert.Equal("fter brace", sp.MoveAndExtractNext());
Assert.Equal("", sp.MoveAndExtractNext());
}
示例12: TestUnicodeSeparator
public void TestUnicodeSeparator()
{
string buffer = "\u0020some\u0020123";
char separator = '\u0020';
StringParser sp = new StringParser(buffer, separator);
sp.MoveNextOrFail();
Assert.Equal("some", sp.MoveAndExtractNext());
Assert.Equal((uint)123, sp.ParseNextUInt32());
}
示例13: GetGatewayAddresses
// /proc/net/route contains some information about gateway addresses,
// and seperates the information about by each interface.
public GatewayIPAddressInformationCollection GetGatewayAddresses()
{
GatewayIPAddressInformationCollection collection = new GatewayIPAddressInformationCollection();
// Columns are as follows (first-line header):
// Iface Destination Gateway Flags RefCnt Use Metric Mask MTU Window IRTT
string[] fileLines = File.ReadAllLines(NetworkFiles.Ipv4RouteFile);
foreach (string line in fileLines)
{
if (line.StartsWith(_linuxNetworkInterface.Name))
{
StringParser parser = new StringParser(line, '\t', skipEmpty: true);
parser.MoveNext();
parser.MoveNextOrFail();
string gatewayIPHex = parser.MoveAndExtractNext();
long addressValue = Convert.ToInt64(gatewayIPHex, 16);
IPAddress address = new IPAddress(addressValue);
collection.InternalAdd(new SimpleGatewayIPAddressInformation(address));
}
}
return collection;
}
示例14: TestExtractCurrentToEnd
public void TestExtractCurrentToEnd()
{
string buffer = "This has a /path/to my favorite file/with a space";
char separator = ' ';
StringParser sp = new StringParser(buffer, separator);
Assert.Equal("This", sp.MoveAndExtractNext());
Assert.Equal("has", sp.MoveAndExtractNext());
Assert.Equal("a", sp.MoveAndExtractNext());
Assert.True(sp.MoveNext());
Assert.Equal("/path/to my favorite file/with a space", sp.ExtractCurrentToEnd());
}