AVRPART to std::vector
@@ -25,6 +25,8 @@ | ||
25 | 25 | #include "avrdude.h" |
26 | 26 | #include "libavrdude.h" |
27 | 27 | |
28 | +#include <algorithm> | |
29 | + | |
28 | 30 | /*** |
29 | 31 | *** Elementary functions dealing with OPCODE structures |
30 | 32 | ***/ |
@@ -490,49 +492,64 @@ | ||
490 | 492 | |
491 | 493 | void avr_free_part(AVRPART * d) |
492 | 494 | { |
493 | -int i; | |
494 | - ldestroy_cb(d->mem, (void(*)(void *))avr_free_mem); | |
495 | - d->mem = NULL; | |
496 | - for(i=0;i<sizeof(d->op)/sizeof(d->op[0]);i++) | |
495 | + int i; | |
496 | + ldestroy_cb(d->mem, (void(*)(void *))avr_free_mem); | |
497 | + d->mem = NULL; | |
498 | + for(i=0; i < sizeof(d->op) / sizeof(d->op[0]); i++) | |
499 | + { | |
500 | + if(d->op[i] != NULL) | |
497 | 501 | { |
498 | - if (d->op[i] != NULL) | |
499 | - { | |
500 | - avr_free_opcode(d->op[i]); | |
501 | - d->op[i] = NULL; | |
502 | - } | |
502 | + avr_free_opcode(d->op[i]); | |
503 | + d->op[i] = NULL; | |
503 | 504 | } |
504 | - free(d); | |
505 | + } | |
505 | 506 | } |
506 | 507 | |
507 | -AVRPART * locate_part(LISTID parts, char * partdesc) | |
508 | +void avr_free_parts() | |
508 | 509 | { |
509 | - LNODEID ln1; | |
510 | - AVRPART * p = NULL; | |
511 | - int found; | |
510 | + for(std::vector<AVRPART>::iterator it = part_list.begin(); it != part_list.end(); ++it) | |
511 | + { | |
512 | + avr_free_part(&(*it)); | |
513 | + } | |
514 | + part_list.clear(); | |
515 | +} | |
512 | 516 | |
513 | - found = 0; | |
517 | +// New std::vector lists | |
518 | +void remove_part(std::vector<AVRPART> &parts, AVRPART *data_ptr) | |
519 | +{ | |
520 | + for( std::vector<AVRPART>::iterator it = parts.begin(); it != parts.end(); ++it ) | |
521 | + { | |
522 | + AVRPART *p = &(*it); | |
523 | + if(p == data_ptr) | |
524 | + { | |
525 | + avr_free_part(p); | |
526 | + parts.erase(it); | |
527 | + return; | |
528 | + } | |
529 | + } | |
530 | +} | |
514 | 531 | |
515 | - for (ln1=lfirst(parts); ln1 && !found; ln1=lnext(ln1)) { | |
516 | - p = static_cast<AVRPART*>(ldata(ln1)); | |
517 | - if ((strcasecmp(partdesc, p->id) == 0) || | |
518 | - (strcasecmp(partdesc, p->desc) == 0)) | |
519 | - found = 1; | |
532 | +AVRPART *locate_part(std::vector<AVRPART> &parts, char *partdesc) | |
533 | +{ | |
534 | + for(std::vector<AVRPART>::iterator it = parts.begin(); it != parts.end(); ++it) | |
535 | + { | |
536 | + AVRPART *p = &(*it); | |
537 | + if((strcasecmp(partdesc, p->id) == 0) || | |
538 | + (strcasecmp(partdesc, p->desc) == 0)) | |
539 | + { | |
540 | + return p; // found = 1; | |
541 | + } | |
520 | 542 | } |
521 | 543 | |
522 | - if (found) | |
523 | - return p; | |
524 | - | |
525 | 544 | return NULL; |
526 | 545 | } |
527 | 546 | |
528 | -AVRPART * locate_part_by_avr910_devcode(LISTID parts, int devcode) | |
547 | +AVRPART *locate_part_by_avr910_devcode(std::vector<AVRPART> &parts, int devcode) | |
529 | 548 | { |
530 | - LNODEID ln1; | |
531 | - AVRPART * p = NULL; | |
532 | - | |
533 | - for (ln1=lfirst(parts); ln1; ln1=lnext(ln1)) { | |
534 | - p = static_cast<AVRPART*>(ldata(ln1)); | |
535 | - if (p->avr910_devcode == devcode) | |
549 | + for(std::vector<AVRPART>::iterator it = parts.begin(); it != parts.end(); ++it) | |
550 | + { | |
551 | + AVRPART *p = &(*it); | |
552 | + if(p->avr910_devcode == devcode) | |
536 | 553 | return p; |
537 | 554 | } |
538 | 555 |
@@ -539,20 +556,20 @@ | ||
539 | 556 | return NULL; |
540 | 557 | } |
541 | 558 | |
542 | -AVRPART * locate_part_by_signature(LISTID parts, unsigned char * sig, | |
543 | - int sigsize) | |
559 | +AVRPART *locate_part_by_signature(std::vector<AVRPART> &parts, unsigned char * sig, int sigsize) | |
544 | 560 | { |
545 | - LNODEID ln1; | |
546 | - AVRPART * p = NULL; | |
547 | - int i; | |
548 | - | |
549 | - if (sigsize == 3) { | |
550 | - for (ln1=lfirst(parts); ln1; ln1=lnext(ln1)) { | |
551 | - p = static_cast<AVRPART*>(ldata(ln1)); | |
552 | - for (i=0; i<3; i++) | |
553 | - if (p->signature[i] != sig[i]) | |
561 | + if(sigsize == 3) | |
562 | + { | |
563 | + for(std::vector<AVRPART>::iterator it = parts.begin(); it != parts.end(); ++it) | |
564 | + { | |
565 | + AVRPART *p = &(*it); | |
566 | + int i = 0; | |
567 | + for(; i < 3; i++) | |
568 | + { | |
569 | + if(p->signature[i] != sig[i]) | |
554 | 570 | break; |
555 | - if (i == 3) | |
571 | + } | |
572 | + if(i == 3) | |
556 | 573 | return p; |
557 | 574 | } |
558 | 575 | } |
@@ -570,13 +587,11 @@ | ||
570 | 587 | * . the line number of the config file this avrpart has been defined at |
571 | 588 | * . the "cookie" passed into walk_avrparts() (opaque client data) |
572 | 589 | */ |
573 | -void walk_avrparts(LISTID avrparts, walk_avrparts_cb cb, void *cookie) | |
590 | +void walk_avrparts(const std::vector<AVRPART> &avrparts, walk_avrparts_cb cb, void *cookie) | |
574 | 591 | { |
575 | - LNODEID ln1; | |
576 | - AVRPART * p; | |
577 | - | |
578 | - for (ln1 = lfirst(avrparts); ln1; ln1 = lnext(ln1)) { | |
579 | - p = static_cast<AVRPART*>(ldata(ln1)); | |
592 | + for(std::vector<AVRPART>::const_iterator it = avrparts.begin(); it != avrparts.end(); ++it) | |
593 | + { | |
594 | + const AVRPART *p = &(*it); | |
580 | 595 | cb(p->id, p->desc, p->config_file, p->lineno, cookie); |
581 | 596 | } |
582 | 597 | } |
@@ -584,20 +599,17 @@ | ||
584 | 599 | /* |
585 | 600 | * Compare function to sort the list of programmers |
586 | 601 | */ |
587 | -static int sort_avrparts_compare(AVRPART * p1,AVRPART * p2) | |
602 | +bool sort_avrparts_compare(const AVRPART &p1, const AVRPART &p2) | |
588 | 603 | { |
589 | - if(p1 == NULL || p2 == NULL) { | |
590 | - return 0; | |
591 | - } | |
592 | - return strncasecmp(p1->desc,p2->desc,AVR_DESCLEN); | |
604 | + return strncasecmp(p1.desc, p2.desc, AVR_DESCLEN) < 0; | |
593 | 605 | } |
594 | 606 | |
595 | 607 | /* |
596 | 608 | * Sort the list of programmers given as "programmers" |
597 | 609 | */ |
598 | -void sort_avrparts(LISTID avrparts) | |
610 | +void sort_avrparts(std::vector<AVRPART> &avrparts) | |
599 | 611 | { |
600 | - lsort(avrparts,(int (*)(void*, void*)) sort_avrparts_compare); | |
612 | + std::sort(avrparts.begin(), avrparts.end(), sort_avrparts_compare); | |
601 | 613 | } |
602 | 614 | |
603 | 615 |
@@ -59,7 +59,7 @@ | ||
59 | 59 | PROGRAMMER * current_prog; |
60 | 60 | AVRPART * current_part; |
61 | 61 | AVRMEM * current_mem; |
62 | -LISTID part_list; | |
62 | +std::vector<AVRPART> part_list; | |
63 | 63 | std::vector<PROGRAMMER> programmers; |
64 | 64 | |
65 | 65 | int lineno; |
@@ -71,7 +71,7 @@ | ||
71 | 71 | |
72 | 72 | void cleanup_config(void) |
73 | 73 | { |
74 | - ldestroy_cb(part_list, (void(*)(void*))avr_free_part); | |
74 | + avr_free_parts(); | |
75 | 75 | ldestroy_cb(string_list, (void(*)(void*))free_token); |
76 | 76 | ldestroy_cb(number_list, (void(*)(void*))free_token); |
77 | 77 | } |
@@ -83,7 +83,6 @@ | ||
83 | 83 | current_prog = NULL; |
84 | 84 | current_part = NULL; |
85 | 85 | current_mem = NULL; |
86 | - part_list = lcreat(NULL, 0); | |
87 | 86 | |
88 | 87 | lineno = 1; |
89 | 88 | infile = NULL; |
@@ -384,10 +384,9 @@ | ||
384 | 384 | existing_part->config_file, existing_part->lineno); |
385 | 385 | lineno = temp; |
386 | 386 | } |
387 | - lrmv_d(part_list, existing_part); | |
388 | - avr_free_part(existing_part); | |
387 | + remove_part(part_list, existing_part); | |
389 | 388 | } |
390 | - PUSH(part_list, current_part); | |
389 | + part_list.push_back(*current_part); | |
391 | 390 | current_part = NULL; |
392 | 391 | } |
393 | 392 | ; |
@@ -46,16 +46,6 @@ | ||
46 | 46 | typedef void * LNODEID; |
47 | 47 | |
48 | 48 | |
49 | -/*---------------------------------------------------------------------- | |
50 | - several defines to access the LIST structure as as stack or a queue | |
51 | - --- use for program readability | |
52 | - ----------------------------------------------------------------------*/ | |
53 | -#define STACKID LISTID | |
54 | -#define SNODEID LNODEID | |
55 | -#define QUEUEID LISTID | |
56 | -#define QNODEID LNODEID | |
57 | - | |
58 | - | |
59 | 49 | #define PUSH(s,d) lins_n(s,d,1) /* push 'd' onto the stack */ |
60 | 50 | #define POP(s) lrmv_n(s,1) /* pop the stack */ |
61 | 51 | #define LOOKSTACK(s) lget_n(s,1) /* look at the top of the stack, |
@@ -323,9 +313,11 @@ | ||
323 | 313 | AVRPART * avr_new_part(void); |
324 | 314 | AVRPART * avr_dup_part(AVRPART * d); |
325 | 315 | void avr_free_part(AVRPART * d); |
326 | -AVRPART * locate_part(LISTID parts, char * partdesc); | |
327 | -AVRPART * locate_part_by_avr910_devcode(LISTID parts, int devcode); | |
328 | -AVRPART * locate_part_by_signature(LISTID parts, unsigned char * sig, | |
316 | +void avr_free_parts(); | |
317 | +void remove_part(std::vector<AVRPART> &parts, AVRPART *data_ptr); | |
318 | +AVRPART * locate_part(std::vector<AVRPART> &parts, char *partdesc); | |
319 | +AVRPART * locate_part_by_avr910_devcode(std::vector<AVRPART> &parts, int devcode); | |
320 | +AVRPART * locate_part_by_signature(std::vector<AVRPART> &parts, unsigned char * sig, | |
329 | 321 | int sigsize); |
330 | 322 | void avr_display(FILE * f, AVRPART * p, const char * prefix, int verbose); |
331 | 323 |
@@ -332,8 +324,8 @@ | ||
332 | 324 | typedef void (*walk_avrparts_cb)(const char *name, const char *desc, |
333 | 325 | const char *cfgname, int cfglineno, |
334 | 326 | void *cookie); |
335 | -void walk_avrparts(LISTID avrparts, walk_avrparts_cb cb, void *cookie); | |
336 | -void sort_avrparts(LISTID avrparts); | |
327 | +void walk_avrparts(const std::vector<AVRPART> &avrparts, walk_avrparts_cb cb, void *cookie); | |
328 | +void sort_avrparts(std::vector<AVRPART> &avrparts); | |
337 | 329 | //#ifdef __cplusplus |
338 | 330 | //} |
339 | 331 | //#endif |
@@ -909,7 +901,7 @@ | ||
909 | 901 | |
910 | 902 | /* formerly config.h */ |
911 | 903 | |
912 | -extern LISTID part_list; | |
904 | +extern std::vector<AVRPART> part_list; | |
913 | 905 | extern std::vector<PROGRAMMER> programmers; |
914 | 906 | extern char default_programmer[]; |
915 | 907 | extern char default_parallel[]; |
@@ -269,7 +269,7 @@ | ||
269 | 269 | } |
270 | 270 | } |
271 | 271 | |
272 | -static void list_parts(FILE * f, const char *prefix, LISTID avrparts) | |
272 | +static void list_parts(FILE *f, const char *prefix, std::vector<AVRPART> &avrparts) | |
273 | 273 | { |
274 | 274 | struct list_walk_cookie c; |
275 | 275 |
@@ -185,7 +185,21 @@ | ||
185 | 185 | return pgm; |
186 | 186 | } |
187 | 187 | |
188 | +// New std::vector lists | |
189 | +void remove_programmer(std::vector<PROGRAMMER> &programmers, PROGRAMMER *data_ptr ) | |
190 | +{ | |
191 | + for( std::vector<PROGRAMMER>::iterator it = programmers.begin(); it != programmers.end(); ++it ) | |
192 | + { | |
193 | + PROGRAMMER *p = &(*it); | |
194 | + if(p == data_ptr) | |
195 | + { | |
196 | + programmers.erase(it); | |
197 | + return; | |
198 | + } | |
199 | + } | |
200 | +} | |
188 | 201 | |
202 | + | |
189 | 203 | static void pgm_default(void) |
190 | 204 | { |
191 | 205 | avrdude_message(MSG_INFO, "%s: programmer operation not supported\n", progname); |
@@ -144,18 +144,5 @@ | ||
144 | 144 | } |
145 | 145 | } |
146 | 146 | |
147 | -// New std::vector lists | |
148 | -void remove_programmer(std::vector<PROGRAMMER> &programmers, PROGRAMMER *data_ptr ) | |
149 | -{ | |
150 | - for( std::vector<PROGRAMMER>::iterator it = programmers.begin(); it != programmers.end(); ++it ) | |
151 | - { | |
152 | - PROGRAMMER *p = &(*it); | |
153 | - if(p == data_ptr) | |
154 | - { | |
155 | - programmers.erase(it); | |
156 | - return; | |
157 | - } | |
158 | - } | |
159 | -} | |
160 | 147 | |
161 | 148 |