~ubuntu-branches/ubuntu/trusty/schroot/trusty

« back to all changes in this revision

Viewing changes to sbuild/sbuild-basic-keyfile.h

  • Committer: Bazaar Package Importer
  • Author(s): Roger Leigh
  • Date: 2009-11-07 10:46:54 UTC
  • mfrom: (1.1.20 upstream) (2.1.6 experimental)
  • Revision ID: james.westby@ubuntu.com-20091107104654-kk1fp1icv8dzfyfi
* New upstream development release.
* schroot:
  - Return success when ending a session and the operation
    succeeded (Closes: #554907).

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* Copyright © 2005-2008  Roger Leigh <rleigh@debian.org>
 
1
/* Copyright © 2005-2009  Roger Leigh <rleigh@debian.org>
2
2
 *
3
3
 * schroot is free software: you can redistribute it and/or modify it
4
4
 * under the terms of the GNU General Public License as published by
48
48
    typedef keyfile_base::error error;
49
49
 
50
50
    /// The constructor.
51
 
    basic_keyfile_parser ()
 
51
    basic_keyfile_parser ():
 
52
      group(),
 
53
      group_set(false),
 
54
      key(),
 
55
      key_set(false),
 
56
      value(),
 
57
      value_set(false),
 
58
      comment(),
 
59
      comment_set(false),
 
60
      line_number(0)
52
61
    {
53
62
    }
54
63
 
123
132
  /**
124
133
   * Configuration file parser.  This class loads an INI-style
125
134
   * configuration file from a file or stream.  The format is
126
 
   * documented in schroot.conf(5).  It is an independent
127
 
   * reimplementation of the Glib GKeyFile class, which it replaces.
 
135
   * documented in schroot.conf(5).
128
136
   */
129
137
  template <typename K, typename P = basic_keyfile_parser<K> >
130
138
  class basic_keyfile : public keyfile_base
145
153
    /// Line number.
146
154
    typedef typename K::size_type size_type;
147
155
 
 
156
    /// Vector of groups
 
157
    typedef std::vector<group_name_type> group_list;
 
158
 
 
159
    /// Vector of values
 
160
    typedef std::vector<value_type> value_list;
 
161
 
148
162
  private:
149
163
    /// Parse type.
150
164
    typedef P parse_type;
162
176
    /// Map between group name and group-items-comment tuple.
163
177
    typedef std::map<group_name_type,group_type> group_map_type;
164
178
 
 
179
    /// Vector of keys
 
180
    typedef std::vector<key_type> key_list;
 
181
 
165
182
  public:
166
183
    /// The constructor.
167
184
    basic_keyfile ();
189
206
     * @returns a list of groups in the basic_keyfile.  If no groups exist,
190
207
     * the list will be empty.
191
208
     */
192
 
    string_list
 
209
    group_list
193
210
    get_groups () const;
194
211
 
195
212
    /**
199
216
     * @returns a list of keys in a group.  If no keys exist in the
200
217
     * group, or the group does not exist, the list will be empty.
201
218
     */
202
 
    string_list
 
219
    key_list
203
220
    get_keys (group_name_type const& group) const;
204
221
 
205
222
    /**
212
229
     */
213
230
    void
214
231
    check_keys (group_name_type const& group,
215
 
                string_list const&     keys) const;
 
232
                key_list const&        keys) const;
216
233
 
217
234
    /**
218
235
     * Check if a group exists.
232
249
     */
233
250
    bool
234
251
    has_key (group_name_type const& group,
235
 
             key_type const&         key) const;
 
252
             key_type const&        key) const;
236
253
 
237
254
    /**
238
255
     * Set a group.  The group will be created (and the comment set)
242
259
     * @param comment the comment to set.
243
260
     */
244
261
    void
245
 
    set_group (group_name_type const&   group,
246
 
               comment_type const& comment);
 
262
    set_group (group_name_type const& group,
 
263
               comment_type const&    comment);
247
264
 
248
265
    /**
249
266
     * Set a group.  The group will be created (and the comment set)
276
293
     */
277
294
    comment_type
278
295
    get_comment (group_name_type const& group,
279
 
                 key_type const& key) const;
 
296
                 key_type const&        key) const;
280
297
 
281
298
    /**
282
299
     * Get a group line number.
296
313
     */
297
314
    size_type
298
315
    get_line (group_name_type const& group,
299
 
              key_type const& key) const;
 
316
              key_type const&        key) const;
300
317
 
301
318
    /**
302
319
     * Get a key value.
311
328
    template <typename T>
312
329
    bool
313
330
    get_value (group_name_type const& group,
314
 
               key_type const& key,
315
 
               T&                 value) const
 
331
               key_type const&        key,
 
332
               T&                     value) const
316
333
    {
317
334
      log_debug(DEBUG_INFO) << "Getting keyfile group=" << group
318
335
                            << ", key=" << key << std::endl;
360
377
    template <typename T>
361
378
    bool
362
379
    get_value (group_name_type const& group,
363
 
               key_type const& key,
364
 
               priority           priority,
365
 
               T&                 value) const
 
380
               key_type const&        key,
 
381
               priority               priority,
 
382
               T&                     value) const
366
383
    {
367
384
      bool status = get_value(group, key, value);
368
385
      check_priority(group, key, priority, status);
381
398
    bool
382
399
    get_locale_string (group_name_type const& group,
383
400
                       key_type const&        key,
384
 
                       std::string&           value) const;
 
401
                       value_type&            value) const;
385
402
 
386
403
    /**
387
404
     * Get a localised key string value.  If the value does not exist,
396
413
     */
397
414
    bool
398
415
    get_locale_string (group_name_type const& group,
399
 
                       key_type const& key,
400
 
                       priority           priority,
401
 
                       std::string&       value) const;
 
416
                       key_type const&        key,
 
417
                       priority               priority,
 
418
                       value_type&            value) const;
402
419
 
403
420
    /**
404
421
     * Get a localised key string value for a specific locale.
412
429
     */
413
430
    bool
414
431
    get_locale_string (group_name_type const& group,
415
 
                       key_type const& key,
416
 
                       std::string const& locale,
417
 
                       std::string&       value) const;
 
432
                       key_type const&        key,
 
433
                       std::string const&     locale,
 
434
                       value_type&            value) const;
418
435
 
419
436
    /**
420
437
     * Get a localised key string value for a specific locale.  If the
431
448
     */
432
449
    bool
433
450
    get_locale_string (group_name_type const& group,
434
 
                       key_type const& key,
435
 
                       std::string const& locale,
436
 
                       priority           priority,
437
 
                       std::string&       value) const;
 
451
                       key_type const&        key,
 
452
                       std::string const&     locale,
 
453
                       priority               priority,
 
454
                       value_type&            value) const;
438
455
 
439
456
    /**
440
457
     * Get a key value as a list.
451
468
    template <typename C>
452
469
    bool
453
470
    get_list_value (group_name_type const& group,
454
 
                    key_type const& key,
455
 
                    C&                 container) const
 
471
                    key_type const&        key,
 
472
                    C&                     container) const
456
473
    {
457
 
      std::string item_value;
 
474
      value_type item_value;
458
475
      if (get_value(group, key, item_value))
459
476
        {
460
 
          string_list items = split_string(item_value,
461
 
                                           std::string(1, this->separator));
462
 
          for (string_list::const_iterator pos = items.begin();
 
477
          value_list items = split_string(item_value,
 
478
                                          this->separator);
 
479
          for (typename value_list::const_iterator pos = items.begin();
463
480
               pos != items.end();
464
481
               ++pos
465
482
               )
510
527
    template <typename C>
511
528
    bool
512
529
    get_list_value (group_name_type const& group,
513
 
                    key_type const& key,
514
 
                    priority           priority,
515
 
                    C&                 container) const
 
530
                    key_type const&        key,
 
531
                    priority               priority,
 
532
                    C&                     container) const
516
533
    {
517
534
      bool status = get_list_value(group, key, container);
518
535
      check_priority(group, key, priority, status);
530
547
    template <typename T>
531
548
    void
532
549
    set_value (group_name_type const& group,
533
 
               key_type const& key,
534
 
               T const&           value)
 
550
               key_type const&        key,
 
551
               T const&               value)
535
552
    {
536
553
      set_value(group, key, value, comment_type());
537
554
    }
548
565
    template <typename T>
549
566
    void
550
567
    set_value (group_name_type const& group,
551
 
               key_type const& key,
552
 
               T const&           value,
553
 
               comment_type const& comment)
 
568
               key_type const&        key,
 
569
               T const&               value,
 
570
               comment_type const&    comment)
554
571
    {
555
572
      set_value(group, key, value, comment, 0);
556
573
    }
568
585
    template <typename T>
569
586
    void
570
587
    set_value (group_name_type const& group,
571
 
               key_type const& key,
572
 
               T const&           value,
573
 
               comment_type const& comment,
574
 
               size_type       line)
 
588
               key_type const&        key,
 
589
               T const&               value,
 
590
               comment_type const&    comment,
 
591
               size_type              line)
575
592
    {
576
593
      std::ostringstream os;
577
594
      os.imbue(std::locale::classic());
588
605
        items.erase(pos);
589
606
      items.insert
590
607
        (typename item_map_type::value_type(key,
591
 
                                            item_type(key, os.str(), comment, line)));
 
608
                                            item_type(key, os.str(),
 
609
                                                      comment, line)));
592
610
    }
593
611
 
594
612
    /**
603
621
    template <typename I>
604
622
    void
605
623
    set_list_value (group_name_type const& group,
606
 
                    key_type const& key,
607
 
                    I                  begin,
608
 
                    I                  end)
 
624
                    key_type const&        key,
 
625
                    I                      begin,
 
626
                    I                      end)
609
627
    {
610
628
      set_list_value(group, key, begin, end, comment_type());
611
629
    }
623
641
    template <typename I>
624
642
    void
625
643
    set_list_value (group_name_type const& group,
626
 
                    key_type const& key,
627
 
                    I                  begin,
628
 
                    I                  end,
629
 
                    comment_type const& comment)
 
644
                    key_type const&        key,
 
645
                    I                      begin,
 
646
                    I                      end,
 
647
                    comment_type const&    comment)
630
648
    {
631
649
      set_list_value (group, key, begin, end, comment, 0);
632
650
    }
645
663
    template <typename I>
646
664
    void
647
665
    set_list_value (group_name_type const& group,
648
 
                    key_type const& key,
649
 
                    I                  begin,
650
 
                    I                  end,
651
 
                    comment_type const& comment,
652
 
                    size_type       line)
 
666
                    key_type const&        key,
 
667
                    I                      begin,
 
668
                    I                      end,
 
669
                    comment_type const&    comment,
 
670
                    size_type              line)
653
671
    {
654
 
      std::string strval;
 
672
      value_type strval;
655
673
 
656
674
      for (I pos = begin; pos != end; ++ pos)
657
675
        {
685
703
     */
686
704
    void
687
705
    remove_key (group_name_type const& group,
688
 
                key_type const& key);
 
706
                key_type const&        key);
689
707
 
690
708
    /**
691
709
     * Add a basic_keyfile to the basic_keyfile.
719
737
    friend
720
738
    std::basic_istream<charT,traits>&
721
739
    operator >> (std::basic_istream<charT,traits>& stream,
722
 
                 basic_keyfile&                          kf)
 
740
                 basic_keyfile&                    kf)
723
741
    {
724
742
      basic_keyfile tmp;
725
743
      parse_type state;
769
787
    friend
770
788
    std::basic_ostream<charT,traits>&
771
789
    operator << (std::basic_ostream<charT,traits>& stream,
772
 
                 basic_keyfile const&                    kf)
 
790
                 basic_keyfile const&              kf)
773
791
    {
774
792
      size_type group_count = 0;
775
793
 
837
855
     */
838
856
    const item_type *
839
857
    find_item (group_name_type const& group,
840
 
               key_type const& key) const;
 
858
               key_type const&        key) const;
841
859
 
842
860
    /**
843
861
     * Find a key by it's group and name.
848
866
     */
849
867
    item_type *
850
868
    find_item (group_name_type const& group,
851
 
               key_type const& key);
 
869
               key_type const&        key);
852
870
 
853
871
    /**
854
872
     * Check if a key is missing or present when not permitted.
860
878
     */
861
879
    void
862
880
    check_priority (group_name_type const& group,
863
 
                    key_type const& key,
864
 
                    priority           priority,
865
 
                    bool               valid) const;
 
881
                    key_type const&        key,
 
882
                    priority               priority,
 
883
                    bool                   valid) const;
866
884
 
867
885
    /**
868
886
     * Print a comment to a stream.  The comment will have hash ('#')
877
895
     */
878
896
    static void
879
897
    print_comment (comment_type const& comment,
880
 
                   std::ostream&      stream);
 
898
                   std::ostream&       stream);
881
899
 
882
900
    /// The top-level groups.
883
901
    group_map_type groups;
884
902
    /// The separator used as a list item delimiter.
885
 
    char           separator;
 
903
    value_type     separator;
886
904
 
887
905
  public:
888
906
    /**
899
917
     */
900
918
    template<class C, typename T>
901
919
    static void
902
 
    set_object_value (C const&            object,
903
 
                      T             (C::* method)() const,
904
 
                      basic_keyfile&            basic_keyfile,
905
 
                      group_name_type const&  group,
906
 
                      key_type const&  key)
 
920
    set_object_value (C const&               object,
 
921
                      T                (C::* method)() const,
 
922
                      basic_keyfile&         basic_keyfile,
 
923
                      group_name_type const& group,
 
924
                      key_type const&        key)
907
925
    {
908
926
      try
909
927
        {
910
 
          basic_keyfile.set_value(group, key, (object.*method)());
 
928
          if (method)
 
929
            basic_keyfile.set_value(group, key, (object.*method)());
911
930
        }
912
931
      catch (std::runtime_error const& e)
913
932
        {
929
948
     */
930
949
    template<class C, typename T>
931
950
    static void
932
 
    set_object_value (C const&            object,
933
 
                      T const&      (C::* method)() const,
934
 
                      basic_keyfile&            basic_keyfile,
935
 
                      group_name_type const&  group,
936
 
                      key_type const&  key)
 
951
    set_object_value (C const&               object,
 
952
                      T const&         (C::* method)() const,
 
953
                      basic_keyfile&         basic_keyfile,
 
954
                      group_name_type const& group,
 
955
                      key_type const&        key)
937
956
    {
938
957
      try
939
958
        {
940
 
          basic_keyfile.set_value(group, key, (object.*method)());
 
959
          if (method)
 
960
            basic_keyfile.set_value(group, key, (object.*method)());
941
961
        }
942
962
      catch (std::runtime_error const& e)
943
963
        {
960
980
     */
961
981
    template<class C, typename T>
962
982
    static void
963
 
    set_object_list_value (C const&            object,
964
 
                           T             (C::* method)() const,
965
 
                           basic_keyfile&            basic_keyfile,
966
 
                           group_name_type const&  group,
967
 
                           key_type const&  key)
 
983
    set_object_list_value (C const&               object,
 
984
                           T                (C::* method)() const,
 
985
                           basic_keyfile&         basic_keyfile,
 
986
                           group_name_type const& group,
 
987
                           key_type const&        key)
968
988
    {
969
989
      try
970
990
        {
971
 
          basic_keyfile.set_list_value(group, key,
972
 
                                 (object.*method)().begin(),
973
 
                                 (object.*method)().end());
 
991
          if (method)
 
992
            basic_keyfile.set_list_value(group, key,
 
993
                                         (object.*method)().begin(),
 
994
                                         (object.*method)().end());
974
995
        }
975
996
      catch (std::runtime_error const& e)
976
997
        {
994
1015
     */
995
1016
    template<class C, typename T>
996
1017
    static void
997
 
    set_object_list_value (C const&            object,
998
 
                           T const&      (C::* method)() const,
999
 
                           basic_keyfile&            basic_keyfile,
1000
 
                           group_name_type const&  group,
1001
 
                           key_type const&  key)
 
1018
    set_object_list_value (C const&               object,
 
1019
                           T const&         (C::* method)() const,
 
1020
                           basic_keyfile&         basic_keyfile,
 
1021
                           group_name_type const& group,
 
1022
                           key_type const&        key)
1002
1023
    {
1003
1024
      try
1004
1025
        {
1005
 
          basic_keyfile.set_list_value(group, key,
1006
 
                                 (object.*method)().begin(),
1007
 
                                 (object.*method)().end());
 
1026
          if (method)
 
1027
            basic_keyfile.set_list_value(group, key,
 
1028
                                         (object.*method)().begin(),
 
1029
                                         (object.*method)().end());
1008
1030
        }
1009
1031
      catch (std::runtime_error const& e)
1010
1032
        {
1028
1050
     */
1029
1051
    template<class C, typename T>
1030
1052
    static void
1031
 
    get_object_value (C&                  object,
1032
 
                      void          (C::* method)(T param),
1033
 
                      basic_keyfile const&      basic_keyfile,
 
1053
    get_object_value (C&                      object,
 
1054
                      void              (C::* method)(T param),
 
1055
                      basic_keyfile const&    basic_keyfile,
1034
1056
                      group_name_type const&  group,
1035
 
                      key_type const&  key,
1036
 
                      basic_keyfile::priority   priority)
 
1057
                      key_type const&         key,
 
1058
                      basic_keyfile::priority priority)
1037
1059
    {
1038
1060
      try
1039
1061
        {
1040
1062
          T value;
1041
 
          if (basic_keyfile.get_value(group, key, priority, value))
 
1063
          if (basic_keyfile.get_value(group, key, priority, value)
 
1064
              && method)
1042
1065
            (object.*method)(value);
1043
1066
        }
1044
1067
      catch (std::runtime_error const& e)
1067
1090
     */
1068
1091
    template<class C, typename T>
1069
1092
    static void
1070
 
    get_object_value (C&                  object,
1071
 
                      void          (C::* method)(T const& param),
1072
 
                      basic_keyfile const&      basic_keyfile,
 
1093
    get_object_value (C&                      object,
 
1094
                      void              (C::* method)(T const& param),
 
1095
                      basic_keyfile const&    basic_keyfile,
1073
1096
                      group_name_type const&  group,
1074
 
                      key_type const&  key,
1075
 
                      basic_keyfile::priority   priority)
 
1097
                      key_type const&         key,
 
1098
                      basic_keyfile::priority priority)
1076
1099
    {
1077
1100
      try
1078
1101
        {
1079
1102
          T value;
1080
 
          if (basic_keyfile.get_value(group, key, priority, value))
 
1103
          if (basic_keyfile.get_value(group, key, priority, value)
 
1104
              && method)
1081
1105
            (object.*method)(value);
1082
1106
        }
1083
1107
      catch (std::runtime_error const& e)
1106
1130
     */
1107
1131
    template<class C, typename T>
1108
1132
    static void
1109
 
    get_object_list_value (C&                  object,
1110
 
                           void          (C::* method)(T param),
1111
 
                           basic_keyfile const&      basic_keyfile,
 
1133
    get_object_list_value (C&                      object,
 
1134
                           void              (C::* method)(T param),
 
1135
                           basic_keyfile const&    basic_keyfile,
1112
1136
                           group_name_type const&  group,
1113
 
                           key_type const&  key,
1114
 
                           basic_keyfile::priority   priority)
 
1137
                           key_type const&         key,
 
1138
                           basic_keyfile::priority priority)
1115
1139
    {
1116
1140
      try
1117
1141
        {
1118
1142
          T value;
1119
 
          if (basic_keyfile.get_list_value(group, key, priority, value))
 
1143
          if (basic_keyfile.get_list_value(group, key, priority, value)
 
1144
              && method)
1120
1145
            (object.*method)(value);
1121
1146
        }
1122
1147
      catch (std::runtime_error const& e)
1148
1173
     */
1149
1174
    template<class C, typename T>
1150
1175
    static void
1151
 
    get_object_list_value (C&                  object,
1152
 
                           void          (C::* method)(T const& param),
1153
 
                           basic_keyfile const&      basic_keyfile,
 
1176
    get_object_list_value (C&                      object,
 
1177
                           void              (C::* method)(T const& param),
 
1178
                           basic_keyfile const&    basic_keyfile,
1154
1179
                           group_name_type const&  group,
1155
 
                           key_type const&  key,
1156
 
                           basic_keyfile::priority   priority)
 
1180
                           key_type const&         key,
 
1181
                           basic_keyfile::priority priority)
1157
1182
    {
1158
1183
      try
1159
1184
        {
1160
1185
          T value;
1161
 
          if (basic_keyfile.get_list_value(group, key, priority, value))
 
1186
          if (basic_keyfile.get_list_value(group, key, priority, value)
 
1187
              && method)
1162
1188
            (object.*method)(value);
1163
1189
        }
1164
1190
      catch (std::runtime_error const& e)