DOORS DXL Buffer

What is DOORS DXL Buffer?

Buffers are a speed and memory-efficient way of manipulating text within DXL applications. It is the programmer’s responsibility to delete the buffers with delete as soon as they are no longer needed in a DXL script. Buffers is a custom data type and is not inherited from DXL’s C origin, therefore, the type name Buffer starts with an upper-case letter.

Create DXL Buffer

Here is the syntax of creating Buffer:  Buffer create([int initSize])

While creating a buffer there is no intrinsic limit on its size.  when a buffer becomes full it extends itself, if memory permits. The argument initSize specifies the initial size of the buffer. If no initial size argument is passed, this function creates a buffer that uses a default initial size of 255.

Here is a sample program to create Buffer in DXL –

// www.TheCloudStrap.Com

/************************************************************************
* $FILENAME: buff1.dxl
* $DESCRIPTION: Buffer example program.
*
* NOTICE: Copyright www.TheCloudStrap.Com. All rights reserved.
* Software comes without any warranties and guarantees, is provided 
* as is and is not supported. Use this software at your own risk. 
* Authors resume no liabilities.
* 
* Contact: admin {at} TheCloudStrap.com
************************************************************************/

// create buffer
Buffer buff1 = create()
Buffer buff2 = create()
Buffer buff3 = create()

//assign values to buffer variable
buff1 = "The"
buff2 = "CloudStrap"
buff3 = ".Com"

// print buffer variable
print(buff1"\n")
print(buff2"\n")
print(buff3"\n")

// delete buffers - free memory
delete(buff1)
delete(buff2)
delete(buff3)

We have created three different Buffers with the default size in the above example program. Here is the output –

DXL BUFFER Output

Delete Buffer

DXL does not provide a garbage collection mechanism. Therefore, It is very important for programmers to delete the dynamically allocated memory as soon as it is no longer required. The delete() function can be used to free the allocated memory.

Convert Buffer To String

There are a few functions that can only receive a string as an argument. Therefore, there is a built-in way to convert Buffer to string. Buffer can be converted to string in DXL in two different ways – using stringOf() function and using tempStringOf() function. Here is a sample example code to convert Buffer to String in DXL –

// www.TheCloudStrap.Com

/************************************************************************
* $FILENAME: buff_2.dxl
* $DESCRIPTION: Buffer example program - Convert buffer to string.
*               1. stringOf()
*               2. tempStringOf()
*
* NOTICE: Copyright www.TheCloudStrap.Com. All rights reserved.
* Software comes without any warranties and guarantees, is provided 
* as is and is not supported. Use this software at your own risk. 
* Authors resume no liabilities.
* 
* Contact: admin {at} TheCloudStrap.com
************************************************************************/

// create buffer
Buffer buff1 = create

buff1 = "Hello, welcome to DOORS dxl tutorial."

string s = stringOf(buff1)
print(s "\n")

// tempStringOf() is an undocumented function to covert a buffer to string. 
print(tempStringOf(buff1))

// delete buffers - free memory
delete(buff1)

Here is the output –

DXL BUFFER

Buffer Append Operation

Here is an example of an append operation –

// www.TheCloudStrap.Com

/************************************************************************
* $FILENAME: buff_3.dxl
* $DESCRIPTION: Buffer example program - Append operator (+=).
*
* NOTICE: Copyright www.TheCloudStrap.Com. All rights reserved.
* Software comes without any warranties and guarantees, is provided 
* as is and is not supported. Use this software at your own risk. 
* Authors resume no liabilities.
* 
* Contact: admin {at} TheCloudStrap.com
************************************************************************/

// create buffer
Buffer buff1 = create
Buffer buff2 = create

string s = "TheCloudStrap"
char c = '.'

buff2 = "Com"

buff1 = "www."	 // append string	
buff1 += s       // append string
buff1 += c	 // append char
buff1 += buff2	 // append buffer

// print buffer variable
print(tempStringOf(buff1))

// delete buffers - free memory
delete(buff1)
delete(buff2)

Here is the output –

DXL BUFFER Output

Buffer Concatenation

DXL allows us to concatenate two different Buffers. The space character can be used to concatenate two buffers in DXL. Here is an example –

// www.TheCloudStrap.Com

/************************************************************************
* $FILENAME: buff_4.dxl
* $DESCRIPTION: Buffer example program - concatenation operator.
*
* NOTICE: Copyright www.TheCloudStrap.Com. All rights reserved.
* Software comes without any warranties and guarantees, is provided 
* as is and is not supported. Use this software at your own risk. 
* Authors resume no liabilities.
* 
* Contact: admin {at} TheCloudStrap.com
************************************************************************/

// create buffer
Buffer buff1 = create
buff1 = "www.TheCloudStrap"

// Buffer concatenation - space character act as a concatenation operator

print buff1 ".Com"

// delete buffers - free memory
delete(buff1)

Here is the output –

DXL BUFFER Output

Buffer Comparison

Here is a sample program to demonstrate Buffer comparison –

// www.TheCloudStrap.Com

/************************************************************************
* $FILENAME: buff_5.dxl
* $DESCRIPTION: Buffer example program - buffer comparison.
*
* NOTICE: Copyright www.TheCloudStrap.Com. All rights reserved.
* Software comes without any warranties and guarantees, is provided 
* as is and is not supported. Use this software at your own risk. 
* Authors resume no liabilities.
* 
* Contact: admin {at} TheCloudStrap.com
************************************************************************/

// create buffer
Buffer buff1 = create
Buffer buff2 = create
Buffer buff3 = create

buff1 = "dxl"
buff2 = "doors"
buff3 = "dxl"

// Buffer comparison
bool b1 = false
b1 = (buff1 == buff2)
print "Is '"buff1"' is equal to '"buff2 "'?  -> " b1 "\n"

b1 = (buff1 == buff3)
print "Is '"buff1"' is equal to '"buff3 "'?  -> " b1 "\n"

// delete buffers - free memory
delete(buff1)
delete(buff2)
delete(buff3)

The program displays ‘true’ if both the buffers are the same and displays ‘false’ if they are different. Here is the output –

DXL BUFFER Output

Extract Sub-String from Buffer

The index notation, [ ], is used to extract a substring from Buffer. “Buffer buff [range]” returns the substring specified by the given range. The range must be specified in the form of int:int. If the range continues to the end of the Buffer, the second index can be omitted. The following example shows how to extract a sub-string from Buffer –

// www.TheCloudStrap.Com

/************************************************************************
* $FILENAME: buff_6.dxl
* $DESCRIPTION: Buffer example program - extract substring.
*
* NOTICE: Copyright www.TheCloudStrap.Com. All rights reserved.
* Software comes without any warranties and guarantees, is provided 
* as is and is not supported. Use this software at your own risk. 
* Authors resume no liabilities.
* 
* Contact: admin {at} TheCloudStrap.com
************************************************************************/

// create buffer
Buffer buff1 = create

buff1 = "Hello, welcome to DOORS dxl tutorial."

// Extract substring from Buffer
string s1 = buff1[0:4]
print(s1 "\n")

s1 = buff1[7:15]
print(s1)

// delete buffers - free memory
delete(buff1)

Here is the output –

DXL BUFFER Output

Get the Length of the Buffer

There are get() and set() functions to retrieve and set the length of the Buffer. The following example shows how to get the length of the Buffer –

// www.TheCloudStrap.Com

/************************************************************************
* $FILENAME: buff_7.dxl
* $DESCRIPTION: Buffer example program - length of buffer
*
* NOTICE: Copyright www.TheCloudStrap.Com. All rights reserved.
* Software comes without any warranties and guarantees, is provided 
* as is and is not supported. Use this software at your own risk. 
* Authors resume no liabilities.
* 
* Contact: admin {at} TheCloudStrap.com
************************************************************************/

// create buffer
Buffer buff1 = create

buff1 = "Hello, welcome to DOORS dxl tutorial."

// length of the buffer
int i = length(buff1)
print ("Buffer = "buff1"\n")
print("Length of the buffer = "i"")

// delete buffers - free memory
delete(buff1)

Here is the output –

DXL BUFFER Output

Set the Length of the Buffer

Here is the example to set the length of the Buffer –

// www.TheCloudStrap.Com

/************************************************************************
* $FILENAME: buff_8.dxl
* $DESCRIPTION: Buffer example program - set length of buffer.
*
* NOTICE: Copyright www.TheCloudStrap.Com. All rights reserved.
* Software comes without any warranties and guarantees, is provided 
* as is and is not supported. Use this software at your own risk. 
* Authors resume no liabilities.
* 
* Contact: admin {at} TheCloudStrap.com
************************************************************************/

// create buffer
Buffer buff1 = create

buff1 = "Hello, welcome to DOORS dxl tutorial."
print("Actual Buffer  =  " buff1 "\n")

// Get length of the buffer
int i = length(buff1)

// Set length of the buffer - Trim last char of the buffer
length(buff1, (i-5))

print("Trimmed Buffer = " buff1 "\n")

// delete buffers - free memory
delete(buff1)

Here is the output –

DXL BUFFER Output

Replace a character in Buffer

The following program demonstrates how to replace a character in Buffer –

// www.TheCloudStrap.Com

/************************************************************************
* $FILENAME: buff_9.dxl
* $DESCRIPTION: Buffer example program - replace a char in buffer.
*
* NOTICE: Copyright www.TheCloudStrap.Com. All rights reserved.
* Software comes without any warranties and guarantees, is provided 
* as is and is not supported. Use this software at your own risk. 
* Authors resume no liabilities.
* 
* Contact: admin {at} TheCloudStrap.com
************************************************************************/

// create buffer
Buffer buff1 = create

buff1 = "Hello, welcome to DOORS dxl tutorial."
print(buff1 "\n")
print "Replacing 6th character with '-'\n"

// Replace/Set a char in the buffer
if(buff1[5] == ',') 
	set(buff1, 5, '-')

print(buff1 "")

// delete buffers - free memory
delete(buff1)

Here is the output –

DXL BUFFER Output

Replace all occurrences of a char in Buffer

Here is an example –

// www.TheCloudStrap.Com

/************************************************************************
* $FILENAME: buff_10.dxl
* $DESCRIPTION: Buffer example program - replace all the occurrences of 
*               a char in the buffer.
*
* NOTICE: Copyright www.TheCloudStrap.Com. All rights reserved.
* Software comes without any warranties and guarantees, is provided 
* as is and is not supported. Use this software at your own risk. 
* Authors resume no liabilities.
* 
* Contact: admin {at} TheCloudStrap.com
************************************************************************/

// Replace/Set all the occurrences of ',' in the buffer
Buffer ReplaceCharInBuffer(Buffer bIn) {
	int i = 0
	
	for(i=0;i<length(bIn);i++)
	{
		if(bIn[i] == ',') 
			set(bIn, i, '-')	
	}
	return bIn
}

//------------------------------------------------------
//						 Main Code
//------------------------------------------------------
// create buffer
Buffer buff1 = create

buff1 = "Hello, welcome, to DOORS dxl tutorial."
print(buff1 "\n")

print "Replacing all occurrences of ',' with '-' \n"

buff1 = ReplaceCharInBuffer(buff1)
print(buff1 "")

// delete buffers - free memory
delete(buff1)

Here is the output –

DXL BUFFER Output

Set Empty Buffer

You can set the Buffer empty with the built-in function – setempty(). The setempty() function empties the buffer but does not claim any space. Here is an example –

// www.TheCloudStrap.Com

/************************************************************************
* $FILENAME: buff_11.dxl
* $DESCRIPTION: Buffer example program - empty buffer.
*
* NOTICE: Copyright www.TheCloudStrap.Com. All rights reserved.
* Software comes without any warranties and guarantees, is provided 
* as is and is not supported. Use this software at your own risk. 
* Authors resume no liabilities.
* 
* Contact: admin {at} TheCloudStrap.com
************************************************************************/

// create buffer
Buffer buff1 = create

buff1 = "Hello, welcome, to DOORS dxl tutorial."
print(buff1 "\n")

setupper(buff1)
print(buff1 "\n")

setlower(buff1)
print(buff1 "\n")

// empty buffer - this does not deallocate the memory
print "Setting the buffer empty..\n"
setempty(buff1)
print("Buffer = " buff1 "\n")

// delete buffers - free memory
delete(buff1)

Here is the output –

DXL BUFFER Output

Search keyword in Buffer

The syntax of the keyword() function is as follows – int keyword (Buffer b, string word, int offset). The keyword() function returns the index at which the string word appears in Buffer b, starting from the character offset. If the word does not appear, the function returns -1. The following program shows how to search a keyword in Buffer –

// www.TheCloudStrap.Com

/************************************************************************
* $FILENAME: buff_12.dxl
* $DESCRIPTION: Buffer example program - search a keyword in  buffer.
*
* NOTICE: Copyright www.TheCloudStrap.Com. All rights reserved.
* Software comes without any warranties and guarantees, is provided 
* as is and is not supported. Use this software at your own risk. 
* Authors resume no liabilities.
* 
* Contact: admin {at} TheCloudStrap.com
************************************************************************/

// create buffer
Buffer buff1 = create

//------------------------------------------------------	
// Case - 1
//------------------------------------------------------	
buff1 = "Hello, welcome to DOORS dxl tutorial."
print "Case-1: Buffer is = "buff1"\n"

// Find the keyword - dxl
// int keyword(Buffer b, string word, int offset)
int index = keyword(buff1, "dxl", 0)

if(index == -1)
	print("Keyword - \"dxl\" not found!\n")
else
	print("Keyword - \"dxl\" found at position -> " index "\n")


//------------------------------------------------------
// Case - 2
//------------------------------------------------------	
buff1 = "Hello, welcome to DOORS DXL tutorial."
print "\nCase-2: Buffer is = "buff1"\n"

// Find the keyword - dxl
// int keyword(Buffer b, string word, int offset)
index = keyword(buff1, "dxl", 0)

if(index == -1)
	print("Keyword - \"dxl\" not found!\n")
else
	print("Keyword - \"dxl\" found at position -> " index "\n")

// delete buffers - free memory
delete(buff1)

Here is the output –

DXL BUFFER Output

Using regular Expression with Buffer

You can use the regular expression with Buffer. Regular expressions can be used to Buffers in the same way as strings. Here is an example –

// www.TheCloudStrap.Com

/************************************************************************
* $FILENAME: buff_13.dxl
* $DESCRIPTION: Buffer example program - apply regular expression.
*
* NOTICE: Copyright www.TheCloudStrap.Com. All rights reserved.
* Software comes without any warranties and guarantees, is provided 
* as is and is not supported. Use this software at your own risk. 
* Authors resume no liabilities.
* 
* Contact: admin {at} TheCloudStrap.com
************************************************************************/

// create buffer
Buffer buff1 = create
buff1 = "Hello, welcome to DOORS dxl tutorial."
print "Buffer is = "buff1"\n"

print "Applying Regular Expression - (regexp2 '.*,') ...\n"
Regexp re = regexp2 ".*,"

re buff1	//apply regular expression

print "Buffer is = "buff1[match 0] "\n"

// delete buffers - free memory
delete(buff1)

Here is the output –

DXL BUFFER

Conclusion

In this article, we have discussed DOORS DXL Buffer. We have explained how to declare and define a Buffer, and how to use various built-in functions, We hope this was a helpful article. However, if you have any questions about DOORS DXL Buffer, please feel free to comment below.

DOORS DXL Buffer
Scroll to top
error: Content is protected !!