Browse code

core: xavp - helper function to shift xavps to left with rotation

Daniel-Constantin Mierla authored on 10/05/2022 17:35:02
Showing 2 changed files
... ...
@@ -519,6 +519,105 @@ int xavp_count(str *name, sr_xavp_t **start)
519 519
 	return n;
520 520
 }
521 521
 
522
+/**
523
+ * Left shift xavps
524
+ */
525
+int xavp_lshift(str *name, sr_xavp_t **head, int idx)
526
+{
527
+	sr_xavp_t *avp;
528
+	sr_xavp_t *lhead = NULL;
529
+	sr_xavp_t *lhead_last = NULL;
530
+	sr_xavp_t *ltail = NULL;
531
+	sr_xavp_t *ltail_last = NULL;
532
+	sr_xavp_t *crt=0;
533
+	sr_xavp_t *prv=0;
534
+	unsigned int id;
535
+	int n=0;
536
+	int xcnt;
537
+
538
+	if(name==NULL || name->s==NULL || name->len<=0) {
539
+		return 0;
540
+	}
541
+
542
+	if(idx==0) {
543
+		return 1;
544
+	}
545
+	xcnt = xavp_count(name, head);
546
+	if(xcnt <= 0) {
547
+		return -2;
548
+	}
549
+	while(idx < 0) {
550
+		idx = xcnt + idx;
551
+	}
552
+	if(idx==0) {
553
+		return 1;
554
+	}
555
+	idx = idx % xcnt;
556
+	if(idx==0) {
557
+		return 1;
558
+	}
559
+
560
+	id = get_hash1_raw(name->s, name->len);
561
+	if(head!=NULL)
562
+		avp = *head;
563
+	else
564
+		avp = *_xavp_list_crt;
565
+	while(avp)
566
+	{
567
+		crt = avp;
568
+		avp=avp->next;
569
+		if(crt->id==id && crt->name.len==name->len
570
+				&& strncmp(crt->name.s, name->s, name->len)==0)
571
+		{
572
+			if(prv!=NULL)
573
+				prv->next=crt->next;
574
+			else if(head!=NULL)
575
+				*head = crt->next;
576
+			else
577
+				*_xavp_list_crt = crt->next;
578
+			crt->next = NULL;
579
+			if(n < idx) {
580
+				if(ltail==NULL) {
581
+					ltail = crt;
582
+				}
583
+				if(ltail_last!=NULL) {
584
+					ltail_last->next = crt;
585
+				}
586
+				ltail_last = crt;
587
+			} else {
588
+				if(lhead==NULL) {
589
+					lhead = crt;
590
+				}
591
+				if(lhead_last!=NULL) {
592
+					lhead_last->next = crt;
593
+				}
594
+				lhead_last = crt;
595
+			}
596
+			n++;
597
+		} else {
598
+			prv = crt;
599
+		}
600
+	}
601
+
602
+	if(lhead_last) {
603
+		lhead_last->next = ltail;
604
+	}
605
+
606
+	if(head!=NULL) {
607
+		if(ltail_last) {
608
+			ltail_last->next = *head;
609
+		}
610
+		*head = lhead;
611
+	} else {
612
+		if(ltail_last) {
613
+			ltail_last->next = *_xavp_list_crt;
614
+		}
615
+		*_xavp_list_crt = lhead;
616
+	}
617
+
618
+	return 0;
619
+}
620
+
522 621
 void xavp_destroy_list_unsafe(sr_xavp_t **head)
523 622
 {
524 623
 	sr_xavp_t *avp, *foo;
... ...
@@ -98,6 +98,7 @@ struct str_list *xavp_get_list_key_names(sr_xavp_t *xavp);
98 98
 
99 99
 int xavp_insert(sr_xavp_t *xavp, int idx, sr_xavp_t **list);
100 100
 sr_xavp_t *xavp_extract(str *name, sr_xavp_t **list);
101
+int xavp_lshift(str *name, sr_xavp_t **head, int idx);
101 102
 
102 103
 void xavp_print_list(sr_xavp_t **head);
103 104