#author("2021-12-05T16:45:41+09:00","default:Real2Virtual202111","Real2Virtual202111")
#author("2024-07-01T11:50:06+09:00","default:TESLA202407","TESLA202407")
[[Real2Virtual202111]]

#code(c){{
#include "StringQueue.h"
//#include <string.h>

//void initForStringQueue(struct stringQueue *this);
/*

              0                       QMax-1
              +--------------------------+
   squeueMem0 |                          |
              +--------------------------+

              0                       QMax-1
              +--------------------------+
   squeueMem1 |                          |
              +--------------------------+


    squeue[0] -> squeueMem0 or squeueMem1
    squeue[1] -> squeueMem0 or squeueMem1



 */

StringQueue::StringQueue(void){
    int i;
    for(i=0;i<sqMax;i++){
   squeueMem[i][0]='\0';
//   squeueMem1[0]='\0';
   squeue[i]=(char *)NULL;
//   squeue[1]=(char *)NULL;
   faceIDs[i]=-1;
//   faceIDs[1]=-1;
   status[i]=0;
//   status[1]=0;
   squeueLength[i]=0;
//   squeueLength[1]=0;
    }
}

int StringQueue::putSQueue( int fid, char *x, int lx){
    int i;
    int q=getQueue(fid);
    if(q<0) return 0;
    int st=status[q];
    if(st==2) return 0;
    if(st==0) status[q]=1;
    int lq=squeueLength[q];
    if(lx+lq>=QMax) {
        clearQueue(q);
        return 0;
    }
    char *s=squeue[q];
    for(i=0;i<=lx;i++){
        s[lq+i]=x[i];
    }
    squeueLength[q]=lq+lx;
    if(x[lx]=='\0'){
        status[q]=2;
    }
    return 1;
}

int StringQueue::getQueue( int fid){
    int q=-1;
    /*
    if(faceIDs[0]==fid){
        q=0;
    }
    else
    if(faceIDs[1]==fid){
        q=1;
    }
    else
    if(status[0]==0){
        q=0;
        faceIDs[q]=fid;
    }
    else
    if(status[1]==0){
        q=1;
        faceIDs[q]=fid;
    }
    if(q<0) return -1;

    if(status[q]==2){
        return -1;
    }
*/
  for(int i=0;i<sqMax;i++){
    for(int i=0;i<sqMax;i++){
      if(faceIDs[i]==fid){
              q=i;
        }
    }
    if(q<0){
        for(int i=0;i<sqMax;i++){
               if(status[i]==0){
                     q=i;
                      faceIDs[q]=fid;
                 }
        }
    }
    if(q<0)  return -1;
    if(status[q]==2){
         return -1;
    }
    
    if(status[q]==0){
        for(int i=0;i<sqMax; i++){
        if( squeueMem[i][0] == '\0'){
            squeue[q]=squeueMem[i];
            squeueLength[q]=0;
            status[q]=1;                
            if( squeueMem[i][0] == '\0'){
                squeue[q]=squeueMem[i];
                squeueLength[q]=0;
                status[q]=1;                
                return q;
          }
            }
        }
        return -1;
    }
    else
    if(status[q]==1){
        return q;
    }
    return -1;
}

void StringQueue::clearQueue( int q){
    status[q]=0;
//    int fid=faceIDs[q];
    char *s=squeue[q];
    squeue[q]=(char *)NULL;
    s[0]='\0';
    faceIDs[q]=-1;
}

int StringQueue::getReadyQ(void){
    for(int i=0;i<sqMax; i++){
      if(status[i]==2) return i;
    }
    return -1;
}
/*
   return: face ID (fid) of the received command.
           -1 if there is no command in the queue.
   x: received command in the queue
 */
int StringQueue::getSQueue( char *x){
    int q=getReadyQ();
    if(q<0) return -1;
//    int st=status[q];
    int fid=faceIDs[q];
//    strcpy(x,squeue[q]);
    xstrcpy(x,squeue[q]);
    clearQueue(q);
    return fid;
}

}}
----
#counter

トップ   編集 差分 バックアップ 添付 複製 名前変更 リロード   新規 一覧 単語検索 最終更新   ヘルプ   最終更新のRSS