Skip to content

Commit

Permalink
Add Utils::splitHost()
Browse files Browse the repository at this point in the history
The contents of this function was duplicated in two places in client
code, so moving it into StdFuncs where it can be shared.
  • Loading branch information
Colin Ward authored and hitman-codehq committed May 9, 2024
1 parent 82f7133 commit f26d598
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 0 deletions.
58 changes: 58 additions & 0 deletions Utils.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@

#include "StdFuncs.h"
#include "Lex.h"
#include "OS4Support.h"
#include "StdWindow.h"

Expand Down Expand Up @@ -3080,6 +3081,63 @@ TInt Utils::setProtection(const char *a_pccFileName, TUint a_uiAttributes)
return(RetVal);
}

/**
* Split a host into its hostname and port components.
* This function parses a string in the format hostname:port and extracts the two components into a string
* and an unsigned short variable respectively. The port is optional and if not present, a default value will
* be used. It is considered an error for the hostname to not be present.
*
* @date Friday 10-May-2024 6:03 am, Code HQ Tokyo Tsukuda
* @param a_pccHost The host to parse
* @param a_roServer The string into which to place the extracted hostname
* @param a_rusPort The unsigned short into which to place the extracted port
* @param a_usDefaultPort The port to be used if no port is specified
* @return KErrNone if successful
* @return KErrNotFound if the hostname was not present
*/

int Utils::splitHost(const char *a_pccHost, std::string &a_roServer, unsigned short &a_rusPort, unsigned short a_usDefaultPort)
{
int serverLength, portLength, port, retVal;
TLex lex(a_pccHost, static_cast<int>(strlen(a_pccHost)));

retVal = KErrNone;

/* Extract the server name and port number from the host passed in and, if a port was specified, */
/* convert it and check its validity */
lex.SetWhitespace(":");

const char *serverString = lex.NextToken(&serverLength);
const char *portString = lex.NextToken(&portLength);

if (serverString != nullptr)
{
a_roServer = std::string(serverString, serverLength);

/* The port string is optional so only parse if it is there, and use the default value if it is missing */
/* or invalid */
a_rusPort = a_usDefaultPort;

if (portString)
{
if (Utils::StringToInt(portString, &port) == KErrNone)
{
a_rusPort = static_cast<unsigned short>(port);
}
else
{
Utils::info("Utils::splitURL() => Specified port \"%s\" is invalid, using default port %d", portString, a_rusPort);
}
}
}
else
{
retVal = KErrNotFound;
}

return retVal;
}

/**
* Strips white space from the start and end of the line.
* This function will strip spaces and tabs from the start and end of the line, as well as the
Expand Down
2 changes: 2 additions & 0 deletions Utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ class Utils

static TInt setProtection(const char *a_pccFileName, TUint a_uiAttributes);

static int splitHost(const char *a_pccHost, std::string &a_roServer, unsigned short &a_rusPort, unsigned short a_usDefaultPort = 80);

static char *StripDags(char *a_pcLine, TInt *a_piLength);

static TInt StringToInt(const char *a_pccString, TInt *a_piResult);
Expand Down

0 comments on commit f26d598

Please sign in to comment.