Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 97 lines (82 sloc) 1.98 kb
7514500 @simsong Created repository on github
authored
1 /*
2 * utils.cpp:
3 *
4 * Some handy utilities for working with AFF
5 *
6 * Distributed under the Berkeley 4-part license
7 */
8
9
10 #include "affconfig.h"
11 #include "afflib.h"
12 #include "afflib_i.h"
13 #include "utils.h"
14 #ifdef HAVE_ERR_H
15 #include "err.h"
16 #endif
17
18 #include <string>
19 #ifdef HAVE_CSTRING
20 #include <cstring>
21 #endif
22
23 #ifdef HAVE_READLINE_READLINE_H
24 #include <readline/readline.h>
25 #endif
26
27 namespace aff {
28
29 bool ends_with(const char *buf,const char *with)
30 {
31 if(buf && with){
32 size_t buflen = strlen(buf);
33 size_t withlen = strlen(with);
34 if(buflen>withlen && strcmp(buf+buflen-withlen,with)==0) return 1;
35 }
36 return 0;
37 }
38
39 #ifdef HAVE_STL
40 /** Given argc and argv, return a string with the command line */
41 std::string command_line(int argc,char **argv) {
42 std::string command = "";
43 for(int i=0;i<argc;i++){
44 if(i>0) command += " ";
45 command += argv[i];
46 }
47 return command;
48 }
49 bool ends_with(const std::string &buf,const std::string &with)
50 {
51 return ends_with(buf.c_str(),with.c_str());
52 }
53
54
55 /* Given an AFFILE, return a seglist.
56 * Returns -1 if failure, 0 if success.
57 */
58 int seglist::get_seglist(AFFILE *af)
59 {
60 if(af_rewind_seg(af)) return -1;
61 char name_[AF_MAX_NAME_LEN];
62 size_t len_=0;
63 uint32_t arg_=0;
64 while(af_get_next_seg(af,name_,sizeof(name_),&arg_,0,&len_)==0){
65 // We shouldn't have 0-len segment names, but we do in some files.
66 // Don't copy these segments.
67 if(strlen(name_)>0){
68 seginfo si(name_,len_,arg_);
69 push_back(si);
70 }
71 }
72 return 0;
73 }
74
75 bool seglist::has_signed_segments()
76 {
77 for(seglist::const_iterator seg = begin(); seg!=end() ;seg++){
78 if(ends_with(seg->name.c_str(),AF_SIG256_SUFFIX)){
79 return true;
80 }
81 }
82 return false;
83 }
84
85
86 bool seglist::contains(std::string segname)
87 {
88 for(std::vector<seginfo>::const_iterator i = begin(); i!=end(); i++){
89 if(i->name == segname) return true;
90 }
91 return false;
92 }
93
94 #endif
95 }
96
Something went wrong with that request. Please try again.