Drupal feed importer

Recently, I have a chance to use drupal commerce with feed importer. Feed importer is very good because I can easily transfer data from MySQL to the website.

However, sometime the import process crash and I am stuck with importing page with xx% status and nothing can be done via UI.

To fix this, I have to manually edit the status of the import. Basically, all I have to do is to

> update feeds_source set state = ‘b:0;’;
update feeds_source set fetcher_result=’b:0;’;

Check this .

Drupal feed importer

Recently, I have a chance to use drupal commerce with feed importer. Feed importer is very good because I can easily transfer data from MySQL to the website.

However, sometime the import process crash and I am stuck with importing page with xx% status and nothing can be done via UI.

To fix this, I have to manually edit the status of the import. Basically, all I have to do is to

> update feeds_source set state = ‘b:0;’;
update feeds_source set fetcher_result=’b:0;’;

Check this .

07-huffmanTree.c

#include <stdio.h>
#include <stdlib.h>
 
//-------------------------------------------------------
struct SHuffmanNode;
typedef struct SHuffmanNode *HuffmanNode;
 
typedef HuffmanNode DType;
int cmp(DType d1, DType d2);
char *toString(char *buf, DType d);
//-------------------------------------------------------
struct SBHeap {
    DType *data;
    int length;
    int size;
};
typedef struct SBHeap *BHeap;
//---------------------------------------------
BHeap newBHeap(int length) {
    BHeap h = (BHeap) malloc(sizeof(struct SBHeap));
    h->data = (DType *) malloc(length * sizeof(DType));
    h->length = length;
    h->size = 0;
    return h;
}
void freeBHeap(BHeap h) {
    free(h->data);
    free(h);
}
//---------------------------------------------
int sizeOfBHeap(BHeap h) {
    return h->size;
}
int isEmptyBHeap(BHeap h) {
    return h->size == 0;
}
int isFullBHeap(BHeap h) {
    return h->size == h->length;
}
//---------------------------------------------
void printBHeap(BHeap h) {
    printf("heap:[ ");
    int i;
    char buf[100];
    for (i=0; i<h->size; i++) {
        printf("%s ", toString(buf, h->data[i]));
    }
    printf("]\n");
}
//---------------------------------------------
void addBHeap(BHeap h, DType x) {
    if (isFullBHeap(h)) {
        printf("add : heap is full\n");
        exit(1);
    } else {
        int k = h->size;
        while( k > 0 ) {
            int p = (k-1)/2;
            if (cmp(h->data[p],x) <= 0) break;  // min heap
            h->data[k] = h->data[p];
            k = p;
        }
        h->data[k] = x;
        h->size++;
    }
}
void _heapify(BHeap h, int k) {
    int c;
    while ( (c = 2*k+1) < h->size) {
        if (c+1 < h->size && cmp(h->data[c+1], h->data[ c ]) < 0) c++;
 
        if (cmp(h->data[ c ],h->data[k]) >= 0) break;
        DType t = h->data[k];
        h->data[k] = h->data[ c ];
        h->data[ c ] = t;
        k = c;
    }
}
DType removeMinBHeap(BHeap h) {  // min heap
    if (isEmptyBHeap(h)) {
        printf("heap is empty !\n");
        exit(1);
    } else {
        DType min = h->data[0];
        h->data[0] = h->data[h->size-1];
        _heapify(h, 0);
        h->size--;
        return min;
    }
}
//---------------------------------------------
struct SHuffmanNode  {
    char    c;
    int     freq;
    HuffmanNode left;
    HuffmanNode right;
};
//-------------------------------------------------------
int cmp(HuffmanNode n1, HuffmanNode n2) {
    if (n1->freq == n2->freq) return 0;
    return (n1->freq < n2->freq) ? -1 : 1;
}
char *toString(char *buf, HuffmanNode n) {
    sprintf(buf, "[%c:%d]", n->c, n->freq);
    return buf;
}
//---------------------------------------------
HuffmanNode newHuffmanNode(char c, int f, HuffmanNode left, HuffmanNode right) {
    HuffmanNode n = (HuffmanNode) malloc(sizeof(struct SHuffmanNode));
    n->c = c;
    n->freq = f;
    n->left = left;
    n->right = right;
    return n;
}
void freeHuffmanNode(HuffmanNode n) {
    free(n);
}
HuffmanNode buildHuffmanTree(char c[], int f[], int n) {
    BHeap h = newBHeap(n);
    int i;
    for (i = 0; i < n; i++) {
        addBHeap(h, newHuffmanNode(c[i], f[i], NULL, NULL));
    }
    for (i = 0; i < n - 1; i++) {
        HuffmanNode n1 = removeMinBHeap(h);
        HuffmanNode n2 = removeMinBHeap(h);
        addBHeap(h, newHuffmanNode('*', n1->freq + n2->freq, n2, n1));
    }
    return removeMinBHeap(h);
}
 
void printCodesR(HuffmanNode r, char codes[], int k) {
    if (r->left == NULL && r->right == NULL) {
        codes[k] = '\0';
        printf("%c : %s\n", r->c, codes);
    } else {
        codes[k] = '0'; printCodesR(r->left, codes, k+1);
        codes[k] = '1'; printCodesR(r->right, codes, k+1);
    }
}
void printCodes(HuffmanNode r) {
    char codes[40];
    printCodesR(r, codes, 0);
}
//---------------------------------------------------
void visit(HuffmanNode r) {
    char buf[100];
    printf("%s ", toString(buf, r));
}
void inorder(HuffmanNode r) {
    if (r == NULL) return;
    inorder(r->left);
    visit(r);
    inorder(r->right);
}
void preorder(HuffmanNode r) {
    if (r == NULL) return;
    visit(r);
    preorder(r->left);
    preorder(r->right);
}
void postorder(HuffmanNode r) {
    if (r == NULL) return;
    postorder(r->left);
    postorder(r->right);
    visit(r);
}
//-----------------------------------------------------
int main(int argc, char *argv[]) {
    int n = 13;
//    char c[] = {'1', '2', '3', '4', '5', '6', '7',
//                '8', '9', 'A', 'B', 'C', 'D'};
//    int  f[] = {24, 16, 15, 11, 8, 4, 4, 4, 4, 4, 2, 2, 2};
    char c[] = {'A', 'B', 'C', 'D', 'E', 'F'};
    int f[] = {40, 21, 15, 14, 8, 2};
    n = 6;
    HuffmanNode r = buildHuffmanTree(c, f, n);
    printf("inorder   = "); inorder(r); printf("\n");
    printf("preorder  = "); preorder(r); printf("\n");
    printf("postorder = "); postorder(r); printf("\n");
    printCodes(r);
    return 0;
}

05-linkedlist.c

#include <stdio.h>
#include <stdlib.h>
 
//-------------------------------------------------------
typedef float DType;
int cmp(DType d1, DType d2);
char *toString(char *buf, DType d);
//-------------------------------------------------------
int cmp(float d1, float d2) {
    if (d1 == d2) return 0;
    return (d1 < d2) ? -1 : 1;
}
char *toString(char *buf, float d) {
    sprintf(buf, "%6.2f", d);
    return buf;
}
//-------------------------------------------------------
struct SLinkedNode;
typedef struct SLinkedNode *LinkedNode;
 
struct SLinkedNode  {
    DType data;
    LinkedNode next;
};
LinkedNode newLinkedNode(DType d, LinkedNode next) {
    LinkedNode n = malloc(sizeof(struct SLinkedNode));
    n->data = d;
    n->next = next;
    return n;
}
//----------------------------------------------------------
// Singly-linked list with header node
//----------------------------------------------------------
struct SLinkedList;
typedef struct SLinkedList *LinkedList;
 
struct SLinkedList  {
    int size;
    LinkedNode header;
};
LinkedList newLinkedList() {
    LinkedList list = malloc(sizeof(struct SLinkedList));
    list->size = 0;
    list->header = newLinkedNode(0, NULL);
    return list;
}
void freeLinkedList(LinkedList list) {
    LinkedNode q, p = list->header;
    while (p != NULL) {
        q = p->next;
        free(p);
        p = q;
    }
    free(list);
}
//---------------------------------------------------------
int sizeOfLinkedList(LinkedList list) {
    return list->size;
}
int isEmptyLinkedList(LinkedList list) {
    return list->size == 0;
}
void printLinkedList(LinkedList list) {
    printf("LinkedList[ ");
    LinkedNode p = list->header->next;
    char buf[100];
    while( p != NULL) {
        printf("%s ", toString(buf, p->data));
        p = p->next;
    }
    printf("]\n");
}
//---------------------------------------------
LinkedNode _nodeOf(LinkedList list, int pos) { // if pos = -1 return header
    int i;
    LinkedNode p = list->header;
    for (i=-1; i<pos; i++) {
        p = p->next;
    }
    return p;
}
DType getLinkedList(LinkedList list, int pos) {
    if (pos < 0 || pos >= list->size) {
        printf("getLinkedList : out of range\n");
        exit(1);
    }
    LinkedNode p = _nodeOf(list, pos);
    return p->data;
}
LinkedNode _addAfterLinkedNode(LinkedList list, LinkedNode p, DType x) {
    p->next = newLinkedNode(x, p->next);
    list->size++;
    return p->next;
}
void addLinkedList(LinkedList list, DType x, int pos) {
    if (pos < 0 || pos > list->size) {
        printf("addLinkedList : out of range\n");
        exit(1);
    }
    LinkedNode p = _nodeOf(list, pos-1);
    _addAfterLinkedNode(list, p, x);
}
void addFirstLinkedList(LinkedList list, DType x) {
    addLinkedList(list, x, 0);
}
void addLastLinkedList(LinkedList list, DType x) {
    addLinkedList(list, x, list->size);
}
int indexOfLinkedList(LinkedList list, DType x) {  // -1 = notfound
    LinkedNode p = list->header->next;
    int i;
    for (i=0; i<list->size; i++) {
        if (cmp(p->data, x) == 0) return i;
        p = p->next;
    }
    return -1;
}
DType removeLinkedList(LinkedList list, int pos) {
    if (pos < 0 || pos >= list->size) {
        printf("removeLinkedList : out of range\n");
        exit(1);
    }
    LinkedNode p = _nodeOf(list, pos-1);
    LinkedNode n = p->next;
    DType x = n->data;
    p->next = n->next;
    free(n);
    list->size--;
    return x;
}
/*-----------------------------------------
LinkedNode reverse(LinkedNode p) {
    if (p == NULL) return NULL;
    if (p->next == NULL) return p;
    LinkedNode q = p->next;
    LinkedNode r = reverse(p->next);
    q->next = p;
    p->next = NULL;
    return r;
}
void reverseLinkedList(LinkedList list) {
    list->header->next = reverse(list->header->next);
}
-------------------------------------------
*/
void reverseLinkedList(LinkedList list) {
    LinkedNode p1 = NULL;
    LinkedNode p2 = list->header->next;
    LinkedNode p3;
    while(p2 != NULL) {
        p3 = p2->next;
        p2->next = p1;
        p1 = p2;
        p2 = p3;
    }
    list->header->next = p1;
}
int equalLinkedList(LinkedList list1, LinkedList list2) { // 0 = not equal, 1 = equal
    if (list1->size != list2->size) return 0;
    LinkedNode p1 = list1->header->next;
    LinkedNode p2 = list2->header->next;
    while (p1 != NULL && p2 != NULL) {
        if (cmp(p1->data,p2->data)!=0) return 0;
        p1 = p1->next;
        p2 = p2->next;
    }
    return p1 == NULL && p2 == NULL;
}
LinkedList createLinkedList(DType data[], int n) {
    LinkedList out = newLinkedList();
    int i;
    for (i=n-1; i>=0; i--) {
        addFirstLinkedList(out, data[i]);
    }
    return out;
}
//----------------------------------------------
int main(int argc, char *argv[]) {
    LinkedList q = newLinkedList();
    addLastLinkedList(q, 1);
    printLinkedList(q);
    addLastLinkedList(q, 2);
    printLinkedList(q);
    addLinkedList(q, 3, 0);
    printLinkedList(q);
    addFirstLinkedList(q, 9);
    printLinkedList(q);
    removeLinkedList(q, 1);
    printLinkedList(q);
    addLinkedList(q, 13, 1);
    printLinkedList(q);
    addLinkedList(q, 22, 3);
    removeLinkedList(q, 2);
    printLinkedList(q);
    reverseLinkedList(q);
    printLinkedList(q);
    //------------------------
    float data[] = {2,22,13,9};
    LinkedList copy = createLinkedList(data, 4);
    printf("copy + equals = %d\n", equalLinkedList(copy, q));
    int i, n;
    int correct = 1;
    for (i=0, n=sizeOfLinkedList(q); correct && i<n; i++) {
        int x = getLinkedList(q, i);
        if (indexOfLinkedList(q, x)!=i) correct = 0;
    }
    if (indexOfLinkedList(q, -999) != -1) correct = 0;
    printf("indexOf = %d\n", correct);
    return 0;
}