135375f98Sopenharmony_ci/* ==========================================
235375f98Sopenharmony_ci *  Unity Project - A Test Framework for C
335375f98Sopenharmony_ci *  Copyright (c) 2007 Mike Karlesky, Mark VanderVoord, Greg Williams
435375f98Sopenharmony_ci *  [Released under MIT License. Please refer to license.txt for details]
535375f98Sopenharmony_ci * ========================================== */
635375f98Sopenharmony_ci
735375f98Sopenharmony_ci#include "unity.h"
835375f98Sopenharmony_ci#include "unity_output_Spy.h"
935375f98Sopenharmony_ci
1035375f98Sopenharmony_ci#include <stdio.h>
1135375f98Sopenharmony_ci#include <string.h>
1235375f98Sopenharmony_ci#include <stdlib.h>
1335375f98Sopenharmony_ci
1435375f98Sopenharmony_cistatic int size;
1535375f98Sopenharmony_cistatic int count;
1635375f98Sopenharmony_cistatic char* buffer;
1735375f98Sopenharmony_cistatic int spy_enable;
1835375f98Sopenharmony_ci
1935375f98Sopenharmony_civoid UnityOutputCharSpy_Create(int s)
2035375f98Sopenharmony_ci{
2135375f98Sopenharmony_ci    size = (s > 0) ? s : 0;
2235375f98Sopenharmony_ci    count = 0;
2335375f98Sopenharmony_ci    spy_enable = 0;
2435375f98Sopenharmony_ci    buffer = malloc((size_t)size);
2535375f98Sopenharmony_ci    TEST_ASSERT_NOT_NULL_MESSAGE(buffer, "Internal malloc failed in Spy Create():" __FILE__);
2635375f98Sopenharmony_ci    memset(buffer, 0, (size_t)size);
2735375f98Sopenharmony_ci}
2835375f98Sopenharmony_ci
2935375f98Sopenharmony_civoid UnityOutputCharSpy_Destroy(void)
3035375f98Sopenharmony_ci{
3135375f98Sopenharmony_ci    size = 0;
3235375f98Sopenharmony_ci    free(buffer);
3335375f98Sopenharmony_ci}
3435375f98Sopenharmony_ci
3535375f98Sopenharmony_civoid UnityOutputCharSpy_OutputChar(int c)
3635375f98Sopenharmony_ci{
3735375f98Sopenharmony_ci    if (spy_enable)
3835375f98Sopenharmony_ci    {
3935375f98Sopenharmony_ci        if (count < (size-1))
4035375f98Sopenharmony_ci            buffer[count++] = (char)c;
4135375f98Sopenharmony_ci    }
4235375f98Sopenharmony_ci    else
4335375f98Sopenharmony_ci    {
4435375f98Sopenharmony_ci        putchar(c);
4535375f98Sopenharmony_ci    }
4635375f98Sopenharmony_ci}
4735375f98Sopenharmony_ci
4835375f98Sopenharmony_ciconst char * UnityOutputCharSpy_Get(void)
4935375f98Sopenharmony_ci{
5035375f98Sopenharmony_ci    return buffer;
5135375f98Sopenharmony_ci}
5235375f98Sopenharmony_ci
5335375f98Sopenharmony_civoid UnityOutputCharSpy_Enable(int enable)
5435375f98Sopenharmony_ci{
5535375f98Sopenharmony_ci    spy_enable = enable;
5635375f98Sopenharmony_ci}
57