@@ -547,12 +547,15 @@ struct GMT_DATASET * gmt_DCW_operation (struct GMT_CTRL *GMT, struct GMT_DCW_SEL
547547}
548548
549549unsigned int gmt_DCW_list (struct GMT_CTRL * GMT , struct GMT_DCW_SELECT * F ) {
550- /* List available countries [and optionally states]; then make program exit */
551- unsigned int list_mode , i , j , k , kk , GMT_DCW_COUNTRIES = 0 , GMT_DCW_STATES = 0 , GMT_DCW_N_COUNTRIES_WITH_STATES = 0 , n_bodies [3 ] = {0 , 0 , 0 };
550+ /* Create dataset of available countries [and optionally states]; write to stdout, then make calling program exit */
551+ unsigned int list_mode , i , j , k , kk , row , GMT_DCW_COUNTRIES = 0 , GMT_DCW_STATES = 0 , GMT_DCW_N_COUNTRIES_WITH_STATES = 0 , n_bodies [3 ] = {0 , 0 , 0 };
552552 bool search = false;
553+ char string [GMT_LEN128 ] = {"" };
553554 struct GMT_DCW_COUNTRY * GMT_DCW_country = NULL ;
554555 struct GMT_DCW_STATE * GMT_DCW_state = NULL ;
555556 struct GMT_DCW_COUNTRY_STATE * GMT_DCW_country_with_state = NULL ;
557+ struct GMT_DATASET * D = NULL ;
558+ struct GMT_DATASEGMENT * S = NULL ;
556559
557560 list_mode = F -> mode ;
558561 if ((list_mode & GMT_DCW_LIST ) == 0 ) return 0 ;
@@ -565,24 +568,56 @@ unsigned int gmt_DCW_list (struct GMT_CTRL *GMT, struct GMT_DCW_SELECT *F) {
565568 if (!F -> item [k ]-> codes || F -> item [k ]-> codes [0 ] == '\0' ) continue ;
566569 search = true; /* Gave some codes */
567570 }
568- for (i = k = 0 ; i < GMT_DCW_COUNTRIES ; i ++ ) {
569- if (search ) { /* Listed continent(s) */
570- bool found = false;
571- for (kk = 0 ; kk < F -> n_items ; kk ++ ) {
572- if (F -> item [kk ]-> codes [0 ] == '=' && strstr (F -> item [kk ]-> codes , GMT_DCW_country [i ].continent ))
573- found = true;
571+
572+ for (int step = 0 ; step < 2 ; step ++ ) { /* First time through, D == NULL */
573+ for (i = k = row = 0 ; i < GMT_DCW_COUNTRIES ; i ++ ) {
574+ if (search ) { /* Listed continent(s) */
575+ bool found = false;
576+ for (kk = 0 ; kk < F -> n_items ; kk ++ ) {
577+ if (F -> item [kk ]-> codes [0 ] == '=' && strstr (F -> item [kk ]-> codes , GMT_DCW_country [i ].continent ))
578+ found = true;
579+ }
580+ if (!found ) continue ;
581+ }
582+ if (F -> n_items == 0 && (i == 0 || strcmp (GMT_DCW_country [i ].continent , GMT_DCW_country [i - 1 ].continent )) ) {
583+ if (S ) {
584+ sprintf (string , "%s [%s]" , GMT_DCW_continents [k ++ ], GMT_DCW_country [i ].continent );
585+ S -> text [row ] = strdup (string );
586+ }
587+ row ++ ;
588+ }
589+ if (S ) {
590+ sprintf (string , "%s\t%s" , GMT_DCW_country [i ].code , GMT_DCW_country [i ].name );
591+ S -> text [row ] = strdup (string );
592+ }
593+ row ++ ;
594+ if ((list_mode & 2 ) && gmtdcw_country_has_states (GMT_DCW_country [i ].code , GMT_DCW_country_with_state , GMT_DCW_N_COUNTRIES_WITH_STATES )) {
595+ for (j = 0 ; j < GMT_DCW_STATES ; j ++ ) {
596+ if (!strcmp (GMT_DCW_country [i ].code , GMT_DCW_state [j ].country )) {
597+ if (S ) {
598+ sprintf (string , "%s.%s\t%s" , GMT_DCW_country [i ].code , GMT_DCW_state [j ].code , GMT_DCW_state [j ].name );
599+ S -> text [row ] = strdup (string );
600+ }
601+ row ++ ;
602+ }
603+ }
574604 }
575- if (!found ) continue ;
576605 }
577- if (F -> n_items == 0 && (i == 0 || strcmp (GMT_DCW_country [i ].continent , GMT_DCW_country [i - 1 ].continent )) )
578- printf ("%s [%s]:\n" , GMT_DCW_continents [k ++ ], GMT_DCW_country [i ].continent );
579- printf (" %s\t%s\n" , GMT_DCW_country [i ].code , GMT_DCW_country [i ].name );
580- if ((list_mode & 2 ) && gmtdcw_country_has_states (GMT_DCW_country [i ].code , GMT_DCW_country_with_state , GMT_DCW_N_COUNTRIES_WITH_STATES )) {
581- for (j = 0 ; j < GMT_DCW_STATES ; j ++ ) {
582- if (!strcmp (GMT_DCW_country [i ].code , GMT_DCW_state [j ].country )) printf ("\t\t%s.%s\t%s\n" , GMT_DCW_country [i ].code , GMT_DCW_state [j ].code , GMT_DCW_state [j ].name );
606+ if (step == 0 ) { /* Tally up and allocate the dataset, then go back and populate it */
607+ uint64_t dim [4 ] = {1 , 1 , row , 0 };
608+ if ((D = GMT_Create_Data (GMT -> parent , GMT_IS_DATASET , GMT_IS_TEXT , GMT_WITH_STRINGS , dim , NULL , NULL , 0 , 0 , NULL )) == NULL ) {
609+ GMT_Report (GMT -> parent , GMT_MSG_ERROR , "Unable to make data set for country listing!\n" );
610+ return GMT_RUNTIME_ERROR ;
583611 }
612+ S = D -> table [0 ]-> segment [0 ]; /* The one and only segment in this table */
584613 }
585614 }
615+
616+ if (GMT_Write_Data (GMT -> parent , GMT_IS_DATASET , GMT_IS_FILE , GMT_IS_TEXT , 0 , NULL , NULL , D ) != GMT_NOERROR ) {
617+ GMT_Report (GMT -> parent , GMT_MSG_ERROR , "Unable to write data set for country listing to stdout!\n" );
618+ return GMT_RUNTIME_ERROR ;
619+ }
620+
586621 gmt_M_free (GMT , GMT_DCW_country );
587622 gmt_M_free (GMT , GMT_DCW_state );
588623 gmt_M_free (GMT , GMT_DCW_country_with_state );
0 commit comments