import json
import re


def ConvertDictToCStructStr(d):
  """Converts a dict to a string represent a C struct initialization."""
  # Convert JSON string to C struct string.
  json_str = json.dumps(d, indent=2, separators=(',', ' = '))
  c_struct_str = re.sub(r'"(\w+)" = ', r'.\1 = ', json_str)
  # The spaces in front of the brackets are necessary to avoid
  # converting the brackets in a string describing an array, e.g.
  # motor_connections[0] --> motor_connections{0}.
  c_struct_str = c_struct_str.replace(' [', ' {').replace(' ]', ' }')
  # Makes cpplint.py complain less.
  c_struct_str = re.sub(re.compile(r'\n\s*{', re.MULTILINE), ' {', c_struct_str)
  return c_struct_str