Assists music production by grouping standalone programs into sessions. Community version of "Non Session Manager".
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

234 lines
5.6KB

  1. /*******************************************************************************/
  2. /* Copyright (C) 2008 Jonathan Moore Liles */
  3. /* */
  4. /* This program is free software; you can redistribute it and/or modify it */
  5. /* under the terms of the GNU General Public License as published by the */
  6. /* Free Software Foundation; either version 2 of the License, or (at your */
  7. /* option) any later version. */
  8. /* */
  9. /* This program is distributed in the hope that it will be useful, but WITHOUT */
  10. /* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or */
  11. /* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for */
  12. /* more details. */
  13. /* */
  14. /* You should have received a copy of the GNU General Public License along */
  15. /* with This program; see the file COPYING. If not,write to the Free Software */
  16. /* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
  17. /*******************************************************************************/
  18. #include "Fl_Menu_Settings.H"
  19. #include <string.h>
  20. /* code to dump and restore (portions of) an Fl_Menu_ */
  21. void
  22. Fl_Menu_Settings::remove_ampersands ( char *str, int n )
  23. {
  24. char *d = str;
  25. char *s = str;
  26. while ( n-- )
  27. {
  28. if ( *s == '&' )
  29. {
  30. ++s;
  31. continue;
  32. }
  33. *(d++) = *(s++);
  34. }
  35. *d = '\0';
  36. }
  37. void
  38. Fl_Menu_Settings::indent ( FILE *fp, int n )
  39. {
  40. while ( n-- )
  41. fprintf( fp, "\t" );
  42. }
  43. int
  44. Fl_Menu_Settings::item_pathname_x ( char *path, int n, Fl_Menu_Item *item )
  45. {
  46. Fl_Menu_::item_pathname( path, n, item );
  47. remove_ampersands( path, n );
  48. }
  49. /** dump options from submenu /menu/ of menubar /bar/ to file /fp/ */
  50. Fl_Menu_Item *
  51. Fl_Menu_Settings::dump ( Fl_Menu_ *bar, Fl_Menu_Item *menu, FILE *fp, int depth )
  52. {
  53. static char path[256];
  54. Fl_Menu_Item *m = menu;
  55. for ( ; m->text; ++m )
  56. {
  57. bool is_radio = false;
  58. if ( m->flags & FL_SUBMENU )
  59. {
  60. strcpy( path, m->text );
  61. remove_ampersands( path, sizeof( path ) - 1 );
  62. indent( fp, depth );
  63. fprintf( fp, "%s\n", path );
  64. /* recurse */
  65. m = dump( bar, ++m, fp, depth + 1 );
  66. continue;
  67. }
  68. if ( m->flags & FL_MENU_RADIO )
  69. is_radio = true;
  70. // bar->item_pathname( path, sizeof( path ) - 1, m );
  71. item_pathname_x( path, sizeof( path ) - 1, m );
  72. if ( m->flags & FL_MENU_TOGGLE || m->flags & FL_MENU_RADIO )
  73. {
  74. if ( ! is_radio )
  75. {
  76. indent( fp, depth );
  77. fprintf( fp, "%s\n", rindex( path, '/' ) + 1 );
  78. indent( fp, depth + 1 );
  79. fprintf( fp, "%s\n", m->flags & FL_MENU_VALUE ? "true" : "false" );
  80. }
  81. else if ( m->flags & FL_MENU_VALUE )
  82. {
  83. indent( fp, depth );
  84. *rindex( path, '/' ) = '\0';
  85. fprintf( fp, "%s\n", rindex( path, '/' ) + 1 );
  86. indent( fp, depth + 1 );
  87. fprintf( fp, "%s\n", path + strlen( path ) + 1 );
  88. }
  89. }
  90. }
  91. return m;
  92. }
  93. /** dump menu to file /name/ starting at /item. */
  94. int
  95. Fl_Menu_Settings::dump ( Fl_Menu_Item *item, const char *name )
  96. {
  97. FILE *fp = fopen( name, "w" );
  98. if ( ! fp )
  99. return false;
  100. dump( this, item, fp, 0 );
  101. fclose( fp );
  102. return true;
  103. }
  104. static void
  105. path_push ( char *path, const char *s )
  106. {
  107. strcat( path, s );
  108. strcat( path, "/" );
  109. }
  110. static void
  111. path_pop ( char *path )
  112. {
  113. char *s;
  114. int l = strlen( path );
  115. if ( path[ l - 1 ] == '/' )
  116. path[ l - 1 ] = '\0';
  117. s = rindex( path, '/' );
  118. s = s ? s : path;
  119. *(s + 1) = '\0';
  120. }
  121. void
  122. Fl_Menu_Settings::load ( Fl_Menu_ *bar, Fl_Menu_Item *item, FILE *fp, int depth, char *path, int pmax )
  123. {
  124. char line[256];
  125. /* FIXME: overflow */
  126. while ( ! feof( fp ) )
  127. {
  128. *line = '\0';
  129. fgets( line, sizeof( line ), fp );
  130. if ( *line == '#' )
  131. continue;
  132. line[ strlen( line ) - 1 ] = '\0';
  133. int ld = strspn( line, "\t" );
  134. if ( ld > depth )
  135. {
  136. path_push( path, line + ld );
  137. ++depth;
  138. // load( bar, item, fp, depth + 1, path, pmax );
  139. /* */;
  140. }
  141. else if ( ld < depth )
  142. {
  143. /* we should know the path and the value now */
  144. // path_pop( path );
  145. *rindex( path, '/' ) = '\0';
  146. *rindex( path, '/' ) = '\0';
  147. printf( "%s = %s\n", path, path + strlen( path ) + 1 );
  148. while ( ld < depth )
  149. {
  150. path_pop( path );
  151. depth--;
  152. }
  153. path_push( path, line + ld );
  154. /* FIXME: still need to process the current line */
  155. }
  156. else /* d == depth */
  157. {
  158. /* doesn't apply? */
  159. }
  160. }
  161. }
  162. /** load settings from file /name/ into menu starting at /item */
  163. int
  164. Fl_Menu_Settings::load ( Fl_Menu_Item *item, const char *name )
  165. {
  166. FILE *fp = fopen( name, "r" );
  167. if ( ! fp )
  168. return false;
  169. char path[256];
  170. path[0] = '\0';
  171. load( this, item, fp, 0, path, sizeof( path ) );
  172. fclose( fp );
  173. }