예제 #1
0
파일: NNumber.cpp 프로젝트: x414e54/nano
//============================================================================
//		NNumber::NNumber : Constructor.
//----------------------------------------------------------------------------
NNumber::NNumber(const NString &theValue)
{


	// Initialise ourselves
	SetUInt8(0);

	if (!theValue.IsEmpty())
		SetValue(theValue);
}
logical pc0_ImportProtokoll :: WriteMessages (NString &nstring, logical clear_opt )
{
  PropertyHandle  *errors = GPH("errors");
  if ( clear_opt )
    *errors = "";

  if ( !nstring.IsEmpty() )
    *errors += (char *)nstring;

  return(NO);
}
예제 #3
0
파일: NTask.cpp 프로젝트: x414e54/nano
//============================================================================
//		NTask::WriteInput : Write to the input stream.
//----------------------------------------------------------------------------
void NTask::WriteInput(const NString &theValue)
{


	// Validate our parameters
	NN_ASSERT(!theValue.IsEmpty());



	// Write to the task
	NTargetSystem::TaskWrite(mTask, theValue);
}
예제 #4
0
//============================================================================
//		NPreferences::RemoveKey : Remove a key.
//----------------------------------------------------------------------------
void NPreferences::RemoveKey(const NString &theKey)
{


	// Validate our parameters
	NN_ASSERT(!theKey.IsEmpty());



	// Remove the key
	NTargetPreferences::RemoveKey(theKey);
}
예제 #5
0
//============================================================================
//		NPreferences::SetValue : Set a value.
//----------------------------------------------------------------------------
void NPreferences::SetValue(const NString &theKey, const NVariant &theValue)
{


	// Validate our parameters
	NN_ASSERT(!theKey.IsEmpty());
	


	// Set the value
	NTargetPreferences::SetValue(theKey, theValue);
	BroadcastMessage(kMsgNPreferenceValueChanged, &theKey);
}
예제 #6
0
파일: NTask.cpp 프로젝트: x414e54/nano
//============================================================================
//		NTask::SetCommand : Set the command.
//----------------------------------------------------------------------------
void NTask::SetCommand(const NString &theCmd)
{


	// Validate our parameters and state
	NN_ASSERT(!theCmd.IsEmpty());
	NN_ASSERT(!IsRunning());



	// Set the command
	mCommand = theCmd;
}
예제 #7
0
//============================================================================
//		NPreferences::HasKey : Does a key exist?
//----------------------------------------------------------------------------
bool NPreferences::HasKey(const NString &theKey, bool checkDefaults) const
{	bool	hasKey;



	// Validate our parameters
	NN_ASSERT(!theKey.IsEmpty());



	// Check the key
	hasKey = NTargetPreferences::HasKey(theKey);
	if (!hasKey && checkDefaults)
		hasKey = NPropertyStore::HasKey(theKey, true);
	
	return(hasKey);
}
예제 #8
0
//============================================================================
//		NPreferences::GetValue : Get a value.
//----------------------------------------------------------------------------
NVariant NPreferences::GetValue(const NString &theKey) const
{	NVariant		theValue;



	// Validate our parameters
	NN_ASSERT(!theKey.IsEmpty());
	


	// Get the value
	theValue = NTargetPreferences::GetValue(theKey);
	if (!theValue.IsValid())
		theValue = NPropertyStore::GetValue(theKey);
	
	return(theValue);
}
예제 #9
0
//============================================================================
//		NDataEncoder::Hex_Decode : Decode from hex.
//----------------------------------------------------------------------------
NData NDataEncoder::Hex_Decode(const NString &theValue)
{	NIndex			n, theSize;
	NData			theResult;
	uint8_t			*dataPtr;
	const char		*textPtr;
	unsigned int	byteVal;



	// Validate our parameters
	NN_ASSERT(theValue.IsEmpty() || (theValue.GetSize() % 2) == 0);



	// Get the state we need
	theSize = theValue.GetSize() / 2;
	dataPtr = theResult.AppendData(theSize);
	textPtr = theValue.GetUTF8();

	if (theSize == 0 || dataPtr == NULL || textPtr == NULL)
		return(theResult);



	// Convert the string
	//
	// Visual Studio does not support 'hh' correctly, so we need to read byte
	// values into a temporary variable:
	//
	// https://connect.microsoft.com/VisualStudio/feedback/details/416843/sscanf-cannot-not-handle-hhd-format
	for (n = 0; n < theSize; n++)
		{
		sscanf(textPtr, "%2x", &byteVal);
		NN_ASSERT((byteVal & 0xFFFFFF00) == 0);

		dataPtr[n] = (uint8_t) byteVal;
		textPtr   += 2;
		}
	
	return(theResult);
}
예제 #10
0
//============================================================================
//		NCommandLine::GetFlagFile : Get a file flag value.
//----------------------------------------------------------------------------
NFile NCommandLine::GetFlagFile(const NString &theArgument) const
{	NFile		theResult;
	NString		theValue;



	// Get the value
	//
	// Relative paths are relative to the current working directory.
	theValue = GetFlagString(theArgument);
	
	if (!theValue.IsEmpty())
		{
		if (!theValue.StartsWith("/"))
			theValue = NFileUtilities::GetCWD().GetPath() + "/" + theValue;

		theResult = NFile(theValue);
		}

	return(theResult);
}
예제 #11
0
//============================================================================
//		NFileUtilities::GetDirectory : Get a standard directory.
//----------------------------------------------------------------------------
NFile NFileUtilities::GetDirectory(NDirectoryLocation theLocation, const NString &fileName, NDirectoryDomain theDomain, bool canCreate)
{	NFile		theFile;



	// Get the directory
	theFile = NTargetFile::GetDirectory(theDomain, theLocation);
	theFile.ResolveTarget();

	if (!fileName.IsEmpty() && theFile.IsValid())
		{
		theFile = theFile.GetChild(fileName);
		theFile.ResolveTarget();
		}



	// Create it if necessary
	if (!theFile.Exists() && canCreate)
		theFile.CreateDirectory();

	return(theFile);
}
예제 #12
0
//============================================================================
//		NFileUtilities::GetUniqueFile : Get a uniquely-named file.
//----------------------------------------------------------------------------
NFile NFileUtilities::GetUniqueFile(const NFile &theDirectory, const NString &fileName)
{	NString		nameChild, nameFile, nameExt;
	NRange		extBreak;
	NFile		theFile;
	NIndex		n;



	// Validate our parameters
	NN_ASSERT(theDirectory.IsDirectory());



	// Get the state we need
	if (fileName.IsEmpty())
		{
		nameFile = "Temp";
		nameExt  = ".tmp";
		}
	else
		{
		extBreak = fileName.Find(".", kNStringBackwards);
		if (extBreak.GetSize() == 1)
			{
			nameFile = fileName.GetLeft(  extBreak.GetLocation());
			nameExt  = fileName.GetString(extBreak.GetLocation());
			}
		else
			{
			nameFile = fileName;
			nameExt  = "";
			}
		}



	// Generate a unique name
	n = 0;
	
	while (n < 10000)
		{
		// Build the name
		if (n == 0)
			nameChild = nameFile;
		else
			nameChild.Format("%@ %ld", nameFile, n);

		if (!nameExt.IsEmpty())
			nameChild += nameExt;



		// Check for the file
		theFile = theDirectory.GetChild(nameChild);
		if (!theFile.Exists())
			return(theFile);

		n++;
		}



	// Handle failure
	NN_LOG("Unable to create a unique name");
	theFile.Clear();
	
	return(theFile);
}
예제 #13
0
//============================================================================
//      NTargetNetwork::SocketOpen : Open a socket.
//----------------------------------------------------------------------------
NSocketRef NTargetNetwork::SocketOpen(NSocket *nanoSocket, const NString &theHost, uint16_t thePort)
{	struct addrinfo		*theAddress, *addrList;
	int					sysErr, tmpSocket;
	NSocketRef			theSocket;
	const char			*hostName;
	struct addrinfo		addrInfo;
	bool				isListen;
	NString				portNum;



	// Get the state we need
	isListen  = theHost.IsEmpty();
	theAddress = NULL;

	hostName = isListen ? NULL : theHost.GetUTF8();
	portNum.Format("%d",thePort);



	// Create the address
	memset(&addrInfo, 0, sizeof(struct addrinfo));

	addrInfo.ai_family   = AF_UNSPEC;						// Allow IPv4 or IPv6
	addrInfo.ai_socktype = SOCK_STREAM;						// Always TCP
	addrInfo.ai_flags    = isListen ? AI_PASSIVE : 0;

	sysErr = getaddrinfo(hostName, portNum.GetUTF8(), &addrInfo, &addrList);
	NN_ASSERT_NOERR(sysErr);

	if (sysErr != 0)
		return(NULL);



	// Create the socket
	//
	// As we may have multiple addresses, we loop until we find one that we can connect/bind.
	for (theAddress = addrList; theAddress != NULL; theAddress = theAddress->ai_next)
		{
		tmpSocket = socket(theAddress->ai_family, theAddress->ai_socktype, theAddress->ai_protocol);
		if (tmpSocket == kSocketHandleInvalid)
			continue;

		if (isListen)
			{
			if (bind(tmpSocket, theAddress->ai_addr, theAddress->ai_addrlen) == 0)
				break;
			}
		else
			{
			if (connect(tmpSocket, theAddress->ai_addr, theAddress->ai_addrlen) == 0)
				break;
			}

		close(tmpSocket);
		}

	freeaddrinfo(addrList);

	NN_ASSERT(theAddress != NULL);
	if (theAddress == NULL)
		return(NULL);



	// Setup signal handler for closed sockets
	//
	// TO DO
		/*
		sa.sa_handler = sigchld_handler;
		sigemptyset(&sa.sa_mask);

		sa.sa_flags = SA_RESTART;
		NN_ASSERT(sigaction(SIGCHLD, &sa, NULL) != -1);
	*/

	// TODO Create thread for listening/accepting connections



	// Create the socket info
	theSocket = new NSocketInfo;

	theSocket->nanoSocket   = nanoSocket;
	theSocket->nativeSocket = tmpSocket;

	return(theSocket);
}