字符串转换器2013.08.23

// tranformer.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <cassert>
#include <malloc.h>
#include <string.h>
#include "trans_app.h"

#define INT_EQUAL(x, y) do {\
    if (x != y)\
    {\
        printf("%d is not equal to %d.\n", x, y);\
    }\
} while(0)

typedef struct
{
    TransView tran_view;
    const char **trans;
    int trans_num;
    
    TransList selTrans;    
} MockView;


void mockview_init_trans_view(TransView *trans_view, const char **trans, int num)
{
    MockView *mock_view = (MockView *)trans_view;
    mock_view->trans = trans;
    mock_view->trans_num = num;
}

void mockview_refresh_trans_view(struct tranView *trans_view, TransList selTrans)
{
    MockView *mock_view = (MockView *)trans_view;
    mock_view->selTrans = selTrans;
}

MockView * newMockView()
{
    MockView *mock_view = (MockView *)malloc(sizeof(MockView));

    if (NULL != mock_view)
    {
        memset(mock_view, 0, sizeof(MockView));
        mock_view->tran_view.init_trans_view = mockview_init_trans_view;
        mock_view->tran_view.refresh_trans_view = mockview_refresh_trans_view;
    }

    return mock_view;
}

int mockview_getAllTransLen(MockView *mock_view)
{
    return mock_view->trans_num;
}

int mockview_getAllSelTransLen(MockView *mock_view)
{
    return mock_view->selTrans.num;
}

void test_init()
{

    MockView *mock_view = NULL;
    TransApp *tans_app = NULL;
    
    mock_view = newMockView();
    tans_app = newTransApp(&mock_view->tran_view, TRANS, 3 );
    INT_EQUAL(3, mockview_getAllTransLen(mock_view));
}

void test_add()
{
    MockView *mock_view = NULL;
    TransApp *tans_app = NULL;
    
    mock_view = newMockView();
    tans_app = newTransApp(&mock_view->tran_view, TRANS, 3 );
    trans_addTransFormer(tans_app, "Upper");

    INT_EQUAL(1, mockview_getAllSelTransLen(mock_view));
}

int _tmain(int argc, _TCHAR* argv[])
{
    test_init();
    test_add();
	return 0;
}


 

#ifndef _TRANS_APP_H_
#define _TRANS_APP_H_

#include <stdio.h>

const char *TRANS[];

#define MAX_TRANS 3

typedef struct
{
    const char *trans[MAX_TRANS];
    int num;
} TransList;

typedef struct tranView
{
    void (* init_trans_view)(struct tranView *trans_view, const char **trans, int num);
    void (* refresh_trans_view)(struct tranView *trans_view, TransList);
} TransView;

typedef struct
{
    TransView *trans_view;

    const char **trans;
    int num;
    
    TransList selTrans;

} TransApp;

TransApp *newTransApp(TransView *trans_view, const char **trans, int num);
void trans_addTransFormer(TransApp *tans_app, const char *trans);
#endif


trans_app.cpp

#include "stdafx.h"
#include <stdio.h>
#include <malloc.h>
#include <string.h>
#include "trans_app.h"

const char *TRANS[] = {"Upper", "Lower", "TrimPrefix"};
TransApp *newTransApp(TransView *trans_view, const char **trans, int num)
{
    TransApp *trans_app = (TransApp *)malloc(sizeof(TransApp));
    if (NULL != trans_app)
    {
        memset(trans_app, 0, sizeof(TransApp));
        trans_app->trans_view = trans_view;
        trans_app->selTrans.num = 0;
        trans_view->init_trans_view(trans_view, trans, num);
    }
    return trans_app;
}


void trans_addTransFormer(TransApp *trans_app, const char *trans)
{
    trans_app->selTrans.trans[trans_app->selTrans.num++] = trans;
    trans_app->trans_view->refresh_trans_view(trans_app->trans_view, trans_app->selTrans);
}


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值