all repos — xrxs @ cd612551fd99f00a071ded1080bbab0ec9f023a8

experimental networked application/game server with 9p

cart.c (raw)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include <u.h>
#include <libc.h>
#include "util.h"
#include "user.h"
#include "cart.h"

Cart* create_cart(char* name) {
  char path[64] = {0};
  char file[64] = {0};
  Cart* cart;
  char* cart_data;

  scat(path, "carts/");
  scat(path, name);
  scpy(path, file, 64);
  ccat(file, '/');
  scat(file, name);
  scat(file, ".rom");

  cart_data = read_bytes(file);
  if (cart_data != nil) {
    cart = (Cart*)malloc(sizeof(Cart));
    scpy(name, cart->name, 32);
    cart->rom = cart_data;

    scpy(path, file, 64);
    scat(file, "/data/sprites");
    cart->sprite_data = read_bytes(file);
    scpy(path, file, 64);
    scat(file, "/data/text");
    cart->txt_data = read_bytes(file);
    scpy(path, file, 64);
    scat(file, "/data/audio");
    cart->audio_data = read_bytes(file);

    return cart;
  }
  return nil;
}

Cart* find_cart(UserInfo* table, char* name) {
  UserInfo* u = table;
  int i = 0;
  for (i = 0; i < 64; i++) {
    if (u->name != 0 && u->cart != nil && scmp(u->cart->name, name))
      return u->cart;
    u++;
  }
  return nil;
}

uint count_carts(UserInfo* table, char* name) {
  UserInfo* u = table;
  uint i, j;
  j = 0;
  for (i = 0; i < 64; i++) {
    if (u->name != 0 && u->cart != nil && scmp(u->cart->name, name))
      j++;
  }
  return j;
}

void destroy_cart(Cart* self) {
  if (self != nil) {
    free(self->rom);
    if (self->sprite_data != nil)
      free(self->sprite_data);
    if (self->audio_data != nil)
      free(self->audio_data);
    if (self->txt_data != nil)
      free(self->txt_data);

    free(self);
    self = nil;
  }
}