/*****  名前ジェネレータ namegen    *****/
/*****  ptrbuf.c                    *****/



#include <stdio.h>
#include <stdlib.h>
#include "namegen.h"



/*  ptr格納バッファ ptrbuf を一個確保し、       */
/*  旧ptrbufのnext_pにそれへのポインタを格納    */
/*  ret:確保した ptrbuf への ptr                */
Ptrbuf *get_ptrbuf(Ptrbuf  *ptrbuf_p_old        /* 旧ptrbufへのポインタ     */
                 ){

    Ptrbuf          *ptrbuf_p;

    ptrbuf_p = (Ptrbuf*)malloc(PTRBUF_SIZE);

    if (ptrbuf_p == NULL){
        error(ERR_OUT_OF_MEMORY,NULL);
    }
    if (ptrbuf_p_old != NULL){
        ptrbuf_p_old->next_p = ptrbuf_p;
    }
    return ptrbuf_p;
}



/*  ret: ptrbuf_pの新しい値（変更なければ前の値）                           */
/*       そうでなければ             FALSE                                   */
Ptrbuf *set_ptr(Ptrbuf   *ptrbuf_p, /* カレントptrbufへのポインタ           */
                int      *ix,       /* セットする配列要素のixへのポインタ   */
                char     *word      /* セットするワードへのポインタ         */
           ){
    ptrbuf_p->ptr[*ix] = word;
    (*ix)++;
    if (*ix == PTRBUF_MAX){
        ptrbuf_p = get_ptrbuf(ptrbuf_p);
        *ix = 0;
    }
    return (ptrbuf_p);
}


