<commit_before>#include <stdlib.h>

#include "ftdi_driver.h"
#include "ftdi_constants.h"

using namespace v8;
using namespace node;

/**********************************
 * Local typedefs
 **********************************/
struct DeviceListBaton
{
    Persistent<Value> callback;
    FT_DEVICE_LIST_INFO_NODE *devInfo;
    DWORD listLength;
    FT_STATUS status;
    int vid;
    int pid;

    DeviceListBaton()
    {
        devInfo = NULL;
    }
};


/**********************************
 * Local Helper Functions protoypes
 **********************************/
bool DeviceMatchesFilterCriteria(FT_DEVICE_LIST_INFO_NODE *devInfo, int filterVid, int filterPid);



/**********************************
 * Local Variables
 **********************************/
uv_mutex_t libraryMutex;
uv_mutex_t vidPidMutex;



/**********************************
 * Functions
 **********************************/
void InitializeList(Handle<Object> target) 
{
    Persistent<FunctionTemplate> constructor_template;
    Local<FunctionTemplate> t = FunctionTemplate::New();
    constructor_template = Persistent<FunctionTemplate>::New(t);
    constructor_template->SetClassName(String::NewSymbol("FtdiDriver"));
    constructor_template->InstanceTemplate()->SetInternalFieldCount(1);

    NODE_SET_METHOD(constructor_template, "findAll", FindAll);
    target->Set(String::NewSymbol("FtdiDriver"), constructor_template->GetFunction());

    uv_mutex_init(&vidPidMutex);
}

void FindAllAsync(uv_work_t* req)
{
    DeviceListBaton* listBaton = static_cast<DeviceListBaton*>(req->data);

    FT_STATUS ftStatus;
    DWORD numDevs = 0;

    // Lock Readout
    uv_mutex_lock(&vidPidMutex);

#ifndef WIN32
    if(listBaton->vid != 0 && listBaton->pid != 0)
    {
        uv_mutex_lock(&libraryMutex);  
        ftStatus = FT_SetVIDPID(listBaton->vid, listBaton->pid);
        uv_mutex_unlock(&libraryMutex);  
    }
#endif
    // create the device information list
    uv_mutex_lock(&libraryMutex);  
    ftStatus = FT_CreateDeviceInfoList(&numDevs);
    uv_mutex_unlock(&libraryMutex);
    if (ftStatus == FT_OK) 
    {
        if (numDevs > 0) 
        {
            // allocate storage for list based on numDevs
            listBaton->devInfo =  (FT_DEVICE_LIST_INFO_NODE*) malloc(sizeof(FT_DEVICE_LIST_INFO_NODE) * numDevs); 
            
            // get the device information list
            uv_mutex_lock(&libraryMutex);
            ftStatus = FT_GetDeviceInfoList(listBaton->devInfo, &numDevs); 
            uv_mutex_unlock(&libraryMutex);
        }
    }
    uv_mutex_unlock(&vidPidMutex);  

    listBaton->listLength = numDevs;              
    listBaton->status = ftStatus;
}

void FindAllFinished(uv_work_t* req)
{
    DeviceListBaton* listBaton = static_cast<DeviceListBaton*>(req->data);

    //printf("Num DevFound: %d, status: %d\r\n", listBaton->listLength, listBaton->status);

    Handle<Value> argv[2];

    if(listBaton->status == FT_OK)
    {
        // Determine the length of the resulting list 
        int resultListLength = 0;
        for (DWORD i = 0; i < listBaton->listLength; i++) 
        {
            if(DeviceMatchesFilterCriteria(&listBaton->devInfo[i], listBaton->vid, listBaton->pid))
            {
                resultListLength++;
            }<commit_msg>Fallback for empty fields in find function<commit_after>#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#include "ftdi_driver.h"
#include "ftdi_constants.h"

using namespace v8;
using namespace node;

/**********************************
 * Local typedefs
 **********************************/
struct DeviceListBaton
{
    Persistent<Value> callback;
    FT_DEVICE_LIST_INFO_NODE *devInfo;
    DWORD listLength;
    FT_STATUS status;
    int vid;
    int pid;

    DeviceListBaton()
    {
        devInfo = NULL;
    }
};


/**********************************
 * Local Helper Functions protoypes
 **********************************/
bool DeviceMatchesFilterCriteria(FT_DEVICE_LIST_INFO_NODE *devInfo, int filterVid, int filterPid);



/**********************************
 * Local Variables
 **********************************/
uv_mutex_t libraryMutex;
uv_mutex_t vidPidMutex;



/**********************************
 * Functions
 **********************************/
void InitializeList(Handle<Object> target) 
{
    Persistent<FunctionTemplate> constructor_template;
    Local<FunctionTemplate> t = FunctionTemplate::New();
    constructor_template = Persistent<FunctionTemplate>::New(t);
    constructor_template->SetClassName(String::NewSymbol("FtdiDriver"));
    constructor_template->InstanceTemplate()->SetInternalFieldCount(1);

    NODE_SET_METHOD(constructor_template, "findAll", FindAll);
    target->Set(String::NewSymbol("FtdiDriver"), constructor_template->GetFunction());

    uv_mutex_init(&vidPidMutex);
}

void FindAllAsync(uv_work_t* req)
{
    DeviceListBaton* listBaton = static_cast<DeviceListBaton*>(req->data);

    FT_STATUS ftStatus;
    DWORD numDevs = 0;

    // Lock Readout
    uv_mutex_lock(&vidPidMutex);

#ifndef WIN32
    if(listBaton->vid != 0 && listBaton->pid != 0)
    {
        uv_mutex_lock(&libraryMutex);  
        ftStatus = FT_SetVIDPID(listBaton->vid, listBaton->pid);
        uv_mutex_unlock(&libraryMutex);  
    }
#endif
    // create the device information list
    uv_mutex_lock(&libraryMutex);  
    ftStatus = FT_CreateDeviceInfoList(&numDevs);
    uv_mutex_unlock(&libraryMutex);
    if (ftStatus == FT_OK) 
    {
        if (numDevs > 0) 
        {
            // allocate storage for list based on numDevs
            listBaton->devInfo =  (FT_DEVICE_LIST_INFO_NODE*) malloc(sizeof(FT_DEVICE_LIST_INFO_NODE) * numDevs); 
            memset(listBaton->devInfo, 0, sizeof(FT_DEVICE_LIST_INFO_NODE) * numDevs);
            
            // get the device information list
            uv_mutex_lock(&libraryMutex);
            ftStatus = FT_GetDeviceInfoList(listBaton->devInfo, &numDevs); 
            uv_mutex_unlock(&libraryMutex);

            for(DWORD i = 0; i < numDevs; i++)
            {
                if(strlen(listBaton->devInfo[i].SerialNumber) == 0)
                {
                    FT_ListDevices((PVOID)i, listBaton->devInfo[i].SerialNumber, FT_LIST_BY_INDEX | FT_OPEN_BY_SERIAL_NUMBER);
                }
                if(strlen(listBaton->devInfo[i].Description) == 0)
                {
                    FT_ListDevices((PVOID)i, listBaton->devInfo[i].Description, FT_LIST_BY_INDEX | FT_OPEN_BY_DESCRIPTION);
                }
                if(listBaton->devInfo[i].LocId == 0)
                {
                    FT_ListDevices((PVOID)i, &listBaton->devInfo[i].LocId, FT_LIST_BY_INDEX | FT_OPEN_BY_LOCATION);
                }
            }
        }
    }
    uv_mutex_unlock(&vidPidMutex);  

    listBaton->listLength = numDevs;              
    listBaton->status = ftStatus;
}

void FindAllFinished(uv_work_t* req)
{
    DeviceListBaton* listBaton = static_cast<DeviceListBaton*>(req->data);

    //printf("Num DevFound: %d, status: %d\r\n", listBaton->listLength, listBaton->status);

    Handle<Value> argv[2];

    if(listBaton->status == FT_OK)
    {
        // Determine the length of the resulting list 
        int resultListLength = 0;
        for (DWORD i = 0; i < listBaton->listLength; i++) 
        {
            if(DeviceMatchesFilterCriteria(&listBaton->devInfo[i], listBaton->vid, listBaton->pid))
            {
                resultListLength++;
            }
{'fraction_non_alphanumeric': 0.17709452201933404, 'fraction_numerical': 0.0036251342642320086, 'mean_word_length': 3.213235294117647, 'pattern_counts': {'":': 0, '<': 26, '<?xml version=': 0, '>': 76, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2180163', 'n_tokens_mistral': 2334, 'n_tokens_neox': 2242, 'n_words': 382}