<commit_before>/*  Crazy attempt to write the disorderBook backend in C.
    The data layout stolen from DanielVF.

    We store all data in memory so that the user can retrieve it later.
    As such, there are very few free() calls.


    PROTOCOL:

    We don't handle user input directly. The frontend is responsible for
    sending us commands as single lines. Only the ORDER command is tricky:

    ORDER <account> <account_id> <qty> <price> <dir:1|2> <orderType:1|2|3|4>

    e.g.

    ORDER CES134127  5            100   5000    1         3

    The frontend must give each account a unique, low, non-negative integer as
    an id (RAM is allocated based on these, so keep them as low as possible).

    Numbers for direction and orderType are defined below.

    Other commands:

    QUOTE
    ORDERBOOK
    CANCEL <id>
    STATUS <id>
    STATUSALL <account_id>

    __SCORES__
    __DEBUG_MEMORY__
    __ACC_FROM_ID__ <id>

    This last is not a direct response to a user query, but can be used by the
    frontend for authentication purposes (i.e. is the user entitled to cancel
    this order?)

    */

#include <assert.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

// On Windows, need this so we can use _setmode so we don't send \r\n
#if defined(_WIN32)
    #include <fcntl.h>
#endif

#define BUY 1       // Don't change these now, they are also used in the frontend
#define SELL 2

#define LIMIT 1     // Don't change these now, they are also used in the frontend
#define MARKET 2
#define FOK 3
#define IOC 4

#define MAXSTRING 2048
#define SMALLSTRING 64
#define MAXTOKENS 64                // Well-behaved frontend will never send this many

#define MAXORDERS 2000000000        // Not going all the way to MAX_INT, because various numbers might go above this
#define MAXACCOUNTS 5000

#define TOO_MANY_ORDERS 1
#define SILLY_VALUE 2
#define TOO_HIGH_ACCOUNT 3


#define EXECUTION_TEMPLATE_1 "{\
  \"ok\": true,\
  \"account\": \"%s\",\
  \"venue\": \"%s\",\
  \"symbol\": \"%s\",\
  \"order\": "

#define EXECUTION_TEMPLATE_2 ",\
  \"standingId\": %d,\
  \"incomingId\": %d,\
  \"price\": %d,\
  \"filled\": %d,\
  \"filledAt\": \"%s\",\
  \"standingComplete\": %s,\
  \"incomingComplete\": %s\
}"


typedef struct Fill_struct {
    int price;
    int qty;
    char * ts;
} FILL;

typedef struct FillNode_struct {
    struct Fill_struct * fill;
    struct FillNode_struct * prev;
    struct FillNode_struct * next;
} FILLNODE;

typedef struct Account_struct {<commit_msg>rm whitespace<commit_after>/*  Crazy attempt to write the disorderBook backend in C.
    The data layout stolen from DanielVF.

    We store all data in memory so that the user can retrieve it later.
    As such, there are very few free() calls.


    PROTOCOL:

    We don't handle user input directly. The frontend is responsible for
    sending us commands as single lines. Only the ORDER command is tricky:

    ORDER <account> <account_id> <qty> <price> <dir:1|2> <orderType:1|2|3|4>

    e.g.

    ORDER CES134127  5            100   5000    1         3

    The frontend must give each account a unique, low, non-negative integer as
    an id (RAM is allocated based on these, so keep them as low as possible).

    Numbers for direction and orderType are defined below.

    Other commands:

    QUOTE
    ORDERBOOK
    CANCEL <id>
    STATUS <id>
    STATUSALL <account_id>

    __SCORES__
    __DEBUG_MEMORY__
    __ACC_FROM_ID__ <id>

    This last is not a direct response to a user query, but can be used by the
    frontend for authentication purposes (i.e. is the user entitled to cancel
    this order?)

    */

#include <assert.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

// On Windows, need this so we can use _setmode so we don't send \r\n
#if defined(_WIN32)
    #include <fcntl.h>
#endif

#define BUY 1       // Don't change these now, they are also used in the frontend
#define SELL 2

#define LIMIT 1     // Don't change these now, they are also used in the frontend
#define MARKET 2
#define FOK 3
#define IOC 4

#define MAXSTRING 2048
#define SMALLSTRING 64
#define MAXTOKENS 64                // Well-behaved frontend will never send this many

#define MAXORDERS 2000000000        // Not going all the way to MAX_INT, because various numbers might go above this
#define MAXACCOUNTS 5000

#define TOO_MANY_ORDERS 1
#define SILLY_VALUE 2
#define TOO_HIGH_ACCOUNT 3


#define EXECUTION_TEMPLATE_1 "{\
\"ok\": true,\
\"account\": \"%s\",\
\"venue\": \"%s\",\
\"symbol\": \"%s\",\
\"order\": "

#define EXECUTION_TEMPLATE_2 ",\
\"standingId\": %d,\
\"incomingId\": %d,\
\"price\": %d,\
\"filled\": %d,\
\"filledAt\": \"%s\",\
\"standingComplete\": %s,\
\"incomingComplete\": %s\
}"


typedef struct Fill_struct {
    int price;
    int qty;
    char * ts;
} FILL;

typedef struct FillNode_struct {
    struct Fill_struct * fill;
    struct FillNode_struct * prev;
    struct FillNode_struct * next;
} FILLNODE;

typedef struct Account_struct {
{'fraction_non_alphanumeric': 0.10382945124358468, 'fraction_numerical': 0.02250296091590999, 'mean_word_length': 2.897692307692308, 'pattern_counts': {'":': 24, '<': 37, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, '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': '7098688', 'n_tokens_mistral': 1785, 'n_tokens_neox': 1620, 'n_words': 651}