@@ -1622,7 +1622,7 @@ def virtualfile_from_stringio(self, stringio: io.StringIO):
1622
1622
1623
1623
- ``"#"`` indicates a comment line.
1624
1624
- ``">"`` indicates a segment header.
1625
- - The object only contains one table and one segment .
1625
+ - The object only contains one table.
1626
1626
1627
1627
Parameters
1628
1628
----------
@@ -1655,19 +1655,26 @@ def virtualfile_from_stringio(self, stringio: io.StringIO):
1655
1655
2 S 0.1i c 0.15i p300/12 0.25p 0.3i My circle
1656
1656
"""
1657
1657
# Parse the strings in the io.StringIO object.
1658
- header = None
1659
- string_arrays = []
1658
+ segments = []
1659
+ current_segment = { "header" : "" , "data" : []}
1660
1660
for line in stringio .getvalue ().splitlines ():
1661
1661
if line .startswith ("#" ): # Skip comments
1662
1662
continue
1663
1663
if line .startswith (">" ): # Segment header
1664
- if header is not None : # Only one segment is allowed now.
1665
- raise GMTInvalidInput ("Only one segment is allowed." )
1666
- header = line
1667
- continue
1668
- string_arrays .append (line )
1669
- # Only one table and one segment. No numeric data, so n_columns is 0.
1670
- n_tables , n_segments , n_rows , n_columns = 1 , 1 , len (string_arrays ), 0
1664
+ if current_segment ["data" ]: # If we have data, start a new segment
1665
+ segments .append (current_segment )
1666
+ current_segment = {"header" : "" , "data" : []}
1667
+ current_segment ["header" ] = line .strip (">" ).strip ()
1668
+ else :
1669
+ current_segment ["data" ].append (line )
1670
+ if current_segment ["data" ]: # Add the last segment if it has data
1671
+ segments .append (current_segment )
1672
+
1673
+ # One table with one or more segments. No numeric data, so n_columns is 0.
1674
+ n_tables = 1
1675
+ n_segments = len (segments )
1676
+ n_rows = sum (len (segment ["data" ]) for segment in segments )
1677
+ n_columns = 0
1671
1678
1672
1679
family , geometry = "GMT_IS_DATASET" , "GMT_IS_TEXT"
1673
1680
dataset = self .create_data (
@@ -1677,18 +1684,20 @@ def virtualfile_from_stringio(self, stringio: io.StringIO):
1677
1684
dim = [n_tables , n_segments , n_rows , n_columns ],
1678
1685
)
1679
1686
dataset = ctp .cast (dataset , ctp .POINTER (_GMT_DATASET ))
1680
- # Assign the strings to the segment
1681
- seg = dataset .contents .table [0 ].contents .segment [0 ].contents
1682
- if header is not None :
1683
- seg .header = header .encode ()
1684
- seg .text = strings_to_ctypes_array (string_arrays )
1687
+ table = dataset .contents .table [0 ].contents
1688
+ for i , segment in enumerate (segments ):
1689
+ seg = table .segment [i ].contents
1690
+ if segment ["header" ] != "" :
1691
+ seg .header = segment ["header" ].encode ()
1692
+ seg .text = strings_to_ctypes_array (segment ["data" ])
1685
1693
1686
1694
with self .open_virtualfile (family , geometry , "GMT_IN" , dataset ) as vfile :
1687
1695
try :
1688
1696
yield vfile
1689
1697
finally :
1690
1698
# Must set the text to None to avoid double freeing the memory
1691
- seg .text = None
1699
+ for i in range (n_segments ):
1700
+ table .segment [i ].contents .text = None
1692
1701
1693
1702
def virtualfile_in ( # noqa: PLR0912
1694
1703
self ,
0 commit comments