samilliken/openDCIM — pull request #274 by AndHam89 (2013-10-05 13:35:10+00:00)
Title: Function introduced to parse generator strings and generate strings e.g....

... for ports numbers of patch panels and switches.

These are the functions which could be used in /device_templates.php to add a port naming scheme for switches and patch panels. The device specification would need to be extended by a portNameGenPattern which could be the string of the port name generator and optionally the array representation encoded as json data stream.

By default if a device template of type patch panel or switch has a defined port naming scheme this would be instantiated to name the ports. Additionally, in /devices.php a "Generate" button and a port naming scheme pattern field could be used. By pressing the "Generate" button the port names would be assigned according to the naming scheme.

Parse exception would need to be provided as feedback to the user.

Below is a list of patterns and the names generated for 8 iterations.

```PHP
$patterns = array(
'abc(1)def',
'xld(1;2)/dada_(a)',
'abc\(1\)(0;2)',
'bla\\\\_x(1;7)',
'xld(1;2)/dada_(a)',
'R(A)/C(1)',
'R\((A)\)/C\((1)\)',
'P(100;1)_(X)',
'IF(0;1)_(XX)',
'IF(0;2)_T_(xx)',
'3/(1)',
'Fa0/(1)',
'astr(1',
'star(77;',
'bla()',
);

foreach ($patterns as $pat) {
echo 'Pattern specification \'', $pat, '\'', PHP_EOL;
list($result, $msg, $idx) = parseGeneratorString($pat);
if (! $result) {
echo 'Exception: ', $msg, ' ', $idx, PHP_EOL, PHP_EOL;
continue;
}
$patternList = generatePatterns($result, 8);
print_r($patternList);
echo PHP_EOL, PHP_EOL;
}
```

Output is
```
Pattern specification 'abc(1)def'
Array
(
[0] => abc1def
[1] => abc2def
[2] => abc3def
[3] => abc4def
[4] => abc5def
[5] => abc6def
[6] => abc7def
[7] => abc8def
)

Pattern specification 'xld(1;2)/dada_(a)'
Array
(
[0] => xld1/dada_a
[1] => xld3/dada_b
[2] => xld5/dada_c
[3] => xld7/dada_d
[4] => xld9/dada_e
[5] => xld11/dada_f
[6] => xld13/dada_g
[7] => xld15/dada_h
)

Pattern specification 'abc\(1\)(0;2)'
Array
(
[0] => abc(1)0
[1] => abc(1)2
[2] => abc(1)4
[3] => abc(1)6
[4] => abc(1)8
[5] => abc(1)10
[6] => abc(1)12
[7] => abc(1)14
)

Pattern specification 'bla\\_x(1;7)'
Array
(
[0] => bla\_x1
[1] => bla\_x8
[2] => bla\_x15
[3] => bla\_x22
[4] => bla\_x29
[5] => bla\_x36
[6] => bla\_x43
[7] => bla\_x50
)

Pattern specification 'xld(1;2)/dada_(a)'
Array
(
[0] => xld1/dada_a
[1] => xld3/dada_b
[2] => xld5/dada_c
[3] => xld7/dada_d
[4] => xld9/dada_e
[5] => xld11/dada_f
[6] => xld13/dada_g
[7] => xld15/dada_h
)

Pattern specification 'R(A)/C(1)'
Array
(
[0] => RA/C1
[1] => RB/C2
[2] => RC/C3
[3] => RD/C4
[4] => RE/C5
[5] => RF/C6
[6] => RG/C7
[7] => RH/C8
)

Pattern specification 'R\((A)\)/C\((1)\)'
Array
(
[0] => R(A)/C(1)
[1] => R(B)/C(2)
[2] => R(C)/C(3)
[3] => R(D)/C(4)
[4] => R(E)/C(5)
[5] => R(F)/C(6)
[6] => R(G)/C(7)
[7] => R(H)/C(8)
)

Pattern specification 'P(100;1)_(X)'
Array
(
[0] => P100_X
[1] => P101_Y
[2] => P102_Z
[3] => P103_AA
[4] => P104_AB
[5] => P105_AC
[6] => P106_AD
[7] => P107_AE
)

Pattern specification 'IF(0;1)_(XX)'
Array
(
[0] => IF0_XX
[1] => IF1_XY
[2] => IF2_XZ
[3] => IF3_YA
[4] => IF4_YB
[5] => IF5_YC
[6] => IF6_YD
[7] => IF7_YE
)

Pattern specification 'IF(0;2)_T_(xx)'
Array
(
[0] => IF0_T_xx
[1] => IF2_T_xy
[2] => IF4_T_xz
[3] => IF6_T_ya
[4] => IF8_T_yb
[5] => IF10_T_yc
[6] => IF12_T_yd
[7] => IF14_T_ye
)

Pattern specification '3/(1)'
Array
(
[0] => 3/1
[1] => 3/2
[2] => 3/3
[3] => 3/4
[4] => 3/5
[5] => 3/6
[6] => 3/7
[7] => 3/8
)

Pattern specification 'Fa0/(1)'
Array
(
[0] => Fa0/1
[1] => Fa0/2
[2] => Fa0/3
[3] => Fa0/4
[4] => Fa0/5
[5] => Fa0/6
[6] => Fa0/7
[7] => Fa0/8
)

Pattern specification 'astr(1'
Error: Parse pattern return error - 'Incomplete pattern specification, missing stop character [';',')']' 6
Exception: Incomplete pattern specification, missing stop character [';',')'] 6

Pattern specification 'star(77;'
Error: Parse pattern return error - 'Incomplete increment specification' 8
Exception: Incomplete increment specification 8

Pattern specification 'bla()'
Error: Parse pattern return error - 'No start value detected.' 4
Exception: No start value detected. 4
```